From 1fb40ac65fe64b79d207928181a7f31add0b480f Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Thu, 24 Dec 2020 14:35:38 +0100 Subject: [PATCH] Centralised styles --- src/application/confirm_profile.rs | 65 ++++++++++------------------ src/application/mod.rs | 10 +++++ src/application/profile_selection.rs | 30 +++---------- src/application/run_profile.rs | 23 ++++------ src/application/setup.rs | 3 ++ src/application/splash.rs | 6 +-- src/application/styles.rs | 52 ++++++++++++++++++++++ 7 files changed, 105 insertions(+), 84 deletions(-) create mode 100644 src/application/styles.rs diff --git a/src/application/confirm_profile.rs b/src/application/confirm_profile.rs index 85869cd..392b957 100644 --- a/src/application/confirm_profile.rs +++ b/src/application/confirm_profile.rs @@ -4,7 +4,6 @@ use embedded_graphics::{ }; use embedded_hal::digital::v2::{InputPin, OutputPin}; -use profont::{ProFont12Point, ProFont14Point}; use st7735_lcd::Orientation; use stm32f1xx_hal::prelude::*; @@ -21,44 +20,12 @@ impl App { disp.set_orientation(&Orientation::LandscapeSwapped) .unwrap(); - let style_black = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .build(); - let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)).into_styled(style_black); + let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)) + .into_styled(self.styles.fill_black); rect.draw(&mut disp).unwrap(); - let text = TextStyleBuilder::new(ProFont12Point) - .text_color(Rgb565::WHITE) - .build(); - - let text_big = TextStyleBuilder::new(ProFont14Point) - .text_color(Rgb565::WHITE) - .build(); - - let text_big_black = TextStyleBuilder::new(ProFont14Point) - .text_color(Rgb565::BLACK) - .build(); - - let normal_box = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .stroke_color(Rgb565::WHITE) - .stroke_width(1) - .build(); - - let ok_box = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::GREEN) - .stroke_color(Rgb565::WHITE) - .stroke_width(1) - .build(); - - let cancel_box = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::RED) - .stroke_color(Rgb565::WHITE) - .stroke_width(1) - .build(); - Text::new("Confirm profile", Point::new(4, 4)) - .into_styled(text) + .into_styled(self.styles.text) .draw(&mut disp) .unwrap(); @@ -66,7 +33,7 @@ impl App { profiles::REFLOW_PROFILES[self.selected_profile].get_name(), Point::new(20, 50), ) - .into_styled(text_big) + .into_styled(self.styles.text_big) .draw(&mut disp) .unwrap(); @@ -77,25 +44,41 @@ impl App { while press_count < 5 { if needs_redraw { - let style = if confirmed { ok_box } else { normal_box }; + let style = if confirmed { + self.styles.ok_box + } else { + self.styles.normal_box + }; Rectangle::new(Point::new(4, 104), Point::new(70, 124)) .into_styled(style) .draw(&mut disp) .unwrap(); - let text_style = if confirmed { text_big_black } else { text_big }; + let text_style = if confirmed { + self.styles.text_big_black + } else { + self.styles.text_big + }; Text::new("Start", Point::new(12, 105)) .into_styled(text_style) .draw(&mut disp) .unwrap(); - let style = if !confirmed { cancel_box } else { normal_box }; + let style = if !confirmed { + self.styles.cancel_box + } else { + self.styles.normal_box + }; Rectangle::new(Point::new(90, 104), Point::new(155, 124)) .into_styled(style) .draw(&mut disp) .unwrap(); - let text_style = if !confirmed { text_big_black } else { text_big }; + let text_style = if !confirmed { + self.styles.text_big_black + } else { + self.styles.text_big + }; Text::new("Cancel", Point::new(96, 105)) .into_styled(text_style) .draw(&mut disp) diff --git a/src/application/mod.rs b/src/application/mod.rs index 057517d..4caa117 100644 --- a/src/application/mod.rs +++ b/src/application/mod.rs @@ -1,3 +1,5 @@ +use embedded_hal::digital::v2::{InputPin, OutputPin}; +use st7735_lcd::Orientation; use stm32f1xx_hal::{ delay::Delay, gpio::{gpioa, gpiob, gpioc, Alternate, Floating, Input, Output, PushPull}, @@ -11,9 +13,12 @@ mod profile_selection; mod run_profile; mod setup; mod splash; +mod styles; pub use setup::setup; +use styles::Styles; + type AppSPI = Spi< pac::SPI1, Spi1NoRemap, @@ -30,6 +35,9 @@ type AppQEI = qei::Qei< (gpiob::PB4>, gpiob::PB5>), >; +type AppDisp = + st7735_lcd::ST7735>, gpioa::PA1>>; + pub struct App { delay: Delay, board_led: gpioc::PC13>, @@ -42,6 +50,8 @@ pub struct App { button: gpiob::PB3>, selected_profile: usize, + + styles: Styles, } impl App { diff --git a/src/application/profile_selection.rs b/src/application/profile_selection.rs index ee781ce..f00e8fc 100644 --- a/src/application/profile_selection.rs +++ b/src/application/profile_selection.rs @@ -22,31 +22,13 @@ impl App { disp.set_orientation(&Orientation::LandscapeSwapped) .unwrap(); - let style_black = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .build(); - let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)).into_styled(style_black); + let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)) + .into_styled(self.styles.fill_black); rect.draw(&mut disp).unwrap(); - let text_lager = TextStyleBuilder::new(ProFont12Point) - .text_color(Rgb565::WHITE) - .build(); - - let profile_box = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .stroke_color(Rgb565::WHITE) - .stroke_width(1) - .build(); - - let selected_box = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLUE) - .stroke_color(Rgb565::WHITE) - .stroke_width(1) - .build(); - Text::new("Selected a profile", Point::new(4, 4)) - .into_styled(text_lager) + .into_styled(self.styles.text) .draw(&mut disp) .unwrap(); @@ -58,9 +40,9 @@ impl App { if needs_redraw { for i in 0..profiles::REFLOW_PROFILES.len() { let style = if i == self.selected_profile { - selected_box + self.styles.selected_box } else { - profile_box + self.styles.normal_box }; Rectangle::new( Point::new(0, 20 + (i as i32) * 16), @@ -74,7 +56,7 @@ impl App { profiles::REFLOW_PROFILES[i].get_name(), Point::new(4, 21 + (i as i32) * 16), ) - .into_styled(text_lager) + .into_styled(self.styles.text) .draw(&mut disp) .unwrap(); } diff --git a/src/application/run_profile.rs b/src/application/run_profile.rs index dcf8c9b..ec82805 100644 --- a/src/application/run_profile.rs +++ b/src/application/run_profile.rs @@ -1,7 +1,7 @@ use embedded_graphics::{ - drawable::Drawable, fonts::Text, pixelcolor::Rgb565, prelude::*, - primitives::rectangle::Rectangle, primitives::Line, style::PrimitiveStyleBuilder, - style::TextStyleBuilder, + drawable::Drawable, fonts::Text, pixelcolor::Rgb565, prelude::*, primitive_style, + primitives::rectangle::Rectangle, primitives::Line, style::PrimitiveStyle, + style::PrimitiveStyleBuilder, style::TextStyleBuilder, }; use embedded_hal::digital::v2::OutputPin; @@ -31,27 +31,20 @@ impl App { disp.set_orientation(&Orientation::LandscapeSwapped) .unwrap(); - let style_black = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .build(); - let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)).into_styled(style_black); + let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)) + .into_styled(self.styles.fill_black); rect.draw(&mut disp).unwrap(); - let style_grid = PrimitiveStyleBuilder::new() - .stroke_color(Rgb565::new(4, 8, 4)) - .stroke_width(1) - .build(); - for x in (0..160).step_by(30) { Line::new(Point::new(x, 20), Point::new(x, 127)) - .into_styled(style_grid) + .into_styled(self.styles.grid) .draw(&mut disp) .unwrap(); } for y in (0..110).step_by(10) { Line::new(Point::new(0, 127 - y), Point::new(195, 127 - y)) - .into_styled(style_grid) + .into_styled(self.styles.grid) .draw(&mut disp) .unwrap(); } @@ -60,7 +53,7 @@ impl App { let y = 148 - (profiles::REFLOW_PROFILES[self.selected_profile].get_temp(t as f32) / 2.0) as i32; - Pixel(Point::new((t / 2) as i32, y), Rgb565::new(24, 48, 24)) + Pixel(Point::new((t / 2) as i32, y), self.styles.profile_color) .draw(&mut disp) .unwrap(); } diff --git a/src/application/setup.rs b/src/application/setup.rs index 429eb00..cf13401 100644 --- a/src/application/setup.rs +++ b/src/application/setup.rs @@ -9,6 +9,7 @@ use stm32f1xx_hal::{ timer::Timer, }; +use crate::application::styles::Styles; use crate::application::App; pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> App { @@ -88,5 +89,7 @@ pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> A button: button, selected_profile: 0, + + styles: Styles::new(), } } diff --git a/src/application/splash.rs b/src/application/splash.rs index 33f3fe9..a7ce9a2 100644 --- a/src/application/splash.rs +++ b/src/application/splash.rs @@ -21,10 +21,8 @@ impl App { disp.set_orientation(&Orientation::LandscapeSwapped) .unwrap(); - let style_black = PrimitiveStyleBuilder::new() - .fill_color(Rgb565::BLACK) - .build(); - let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)).into_styled(style_black); + let rect = Rectangle::new(Point::new(0, 0), Point::new(160, 128)) + .into_styled(self.styles.fill_black); rect.draw(&mut disp).unwrap(); diff --git a/src/application/styles.rs b/src/application/styles.rs new file mode 100644 index 0000000..ed32c16 --- /dev/null +++ b/src/application/styles.rs @@ -0,0 +1,52 @@ +use embedded_graphics::{ + fonts::Text, pixelcolor::Rgb565, prelude::*, primitive_style, primitives::rectangle::Rectangle, + primitives::Line, style::PrimitiveStyle, style::TextStyle, style::TextStyleBuilder, text_style, +}; + +use profont::{ProFont12Point, ProFont14Point}; + +pub struct Styles { + pub fill_black: PrimitiveStyle, + pub text: TextStyle, + pub text_big: TextStyle, + pub text_big_black: TextStyle, + pub normal_box: PrimitiveStyle, + pub selected_box: PrimitiveStyle, + pub ok_box: PrimitiveStyle, + pub cancel_box: PrimitiveStyle, + pub grid: PrimitiveStyle, + pub profile_color: Rgb565, +} + +impl Styles { + pub fn new() -> Styles { + Styles { + fill_black: primitive_style!(fill_color = Rgb565::BLACK), + text: text_style!(font = ProFont12Point, text_color = Rgb565::WHITE), + text_big: text_style!(font = ProFont14Point, text_color = Rgb565::WHITE), + text_big_black: text_style!(font = ProFont14Point, text_color = Rgb565::BLACK), + normal_box: primitive_style!( + fill_color = Rgb565::BLACK, + stroke_color = Rgb565::WHITE, + stroke_width = 1 + ), + selected_box: primitive_style!( + fill_color = Rgb565::BLUE, + stroke_color = Rgb565::WHITE, + stroke_width = 1 + ), + ok_box: primitive_style!( + fill_color = Rgb565::GREEN, + stroke_color = Rgb565::WHITE, + stroke_width = 1 + ), + cancel_box: primitive_style!( + fill_color = Rgb565::GREEN, + stroke_color = Rgb565::WHITE, + stroke_width = 1 + ), + grid: primitive_style!(stroke_color = Rgb565::new(4, 8, 4), stroke_width = 1), + profile_color: Rgb565::new(24, 48, 24), + } + } +}