From 86b4b8448be3050813224c35100e8509d6c5504e Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Thu, 8 Oct 2020 22:37:14 +0200 Subject: [PATCH] Added confirmation screen --- src/application.rs | 197 ++++++++++++++++++++++++++++++++++++++++----- src/profiles.rs | 2 +- 2 files changed, 179 insertions(+), 20 deletions(-) diff --git a/src/application.rs b/src/application.rs index 0e33725..bd70eef 100644 --- a/src/application.rs +++ b/src/application.rs @@ -9,7 +9,7 @@ use embedded_graphics::{ }; use embedded_hal::digital::v2::{InputPin, OutputPin}; -use profont::{ProFont12Point, ProFont9Point}; +use profont::{ProFont12Point, ProFont14Point, ProFont9Point}; use rtt_target::{rprintln, rtt_init_print}; use st7735_lcd::Orientation; use stm32f1xx_hal::{ @@ -139,12 +139,18 @@ pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> A impl App { pub fn run(mut self) -> ! { self = self.splash_screen(); - self = self.profile_selection(); - - loop {} + loop { + self = self.profile_selection(); + let (confirmed, app) = self.confirm_profile(); + self = app; + if !confirmed { + continue; + } + self = self.run_profile(); + } } - pub fn splash_screen(mut self) -> App { + fn splash_screen(mut self) -> App { let mut disp = st7735_lcd::ST7735::new(self.spi, self.disp_dc, self.disp_rst, true, false, 160, 128); self.disp_cs.set_low().unwrap(); @@ -166,8 +172,6 @@ impl App { self.delay.delay_ms(2000u16); - rect.draw(&mut disp).unwrap(); - let (spi, disp_dc, disp_rst) = disp.release(); self.spi = spi; self.disp_dc = disp_dc; @@ -176,7 +180,7 @@ impl App { self } - pub fn profile_selection(mut self) -> App { + fn profile_selection(mut self) -> App { let mut disp = st7735_lcd::ST7735::new(self.spi, self.disp_dc, self.disp_rst, true, false, 160, 128); self.disp_cs.set_low().unwrap(); @@ -185,6 +189,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); + + rect.draw(&mut disp).unwrap(); + let text_lager = TextStyleBuilder::new(ProFont12Point) .text_color(Rgb565::WHITE) .build(); @@ -206,23 +217,25 @@ impl App { .draw(&mut disp) .unwrap(); + let encoder_start = self.qei.count(); + let mut press_count = 0; let mut needs_redraw = true; while press_count < 5 { if needs_redraw { for i in 0..profiles::REFLOW_PROFILES.len() { - let rect = Rectangle::new( + let style = if i == self.selected_profile { + selected_box + } else { + profile_box + }; + Rectangle::new( Point::new(0, 20 + (i as i32) * 16), Point::new(159, 20 + (i as i32) * 16 + 16), - ); - - let styled_rect = if i == self.selected_profile { - rect.into_styled(selected_box) - } else { - rect.into_styled(profile_box) - }; - - styled_rect.draw(&mut disp).unwrap(); + ) + .into_styled(style) + .draw(&mut disp) + .unwrap(); Text::new( profiles::REFLOW_PROFILES[i].get_name(), @@ -236,7 +249,8 @@ impl App { } self.delay.delay_ms(10u16); - let new_selection = ((self.qei.count() as usize) / 4) % profiles::REFLOW_PROFILES.len(); + let new_selection = (((self.qei.count().wrapping_sub(encoder_start)) as usize) / 4) + % profiles::REFLOW_PROFILES.len(); if new_selection != self.selected_profile { self.selected_profile = new_selection; needs_redraw = true; @@ -261,4 +275,149 @@ impl App { self } + + fn confirm_profile(mut self) -> (bool, App) { + let mut disp = + st7735_lcd::ST7735::new(self.spi, self.disp_dc, self.disp_rst, true, false, 160, 128); + self.disp_cs.set_low().unwrap(); + + disp.init(&mut self.delay).unwrap(); + 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); + 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) + .draw(&mut disp) + .unwrap(); + + Text::new( + profiles::REFLOW_PROFILES[self.selected_profile].get_name(), + Point::new(20, 50), + ) + .into_styled(text_big) + .draw(&mut disp) + .unwrap(); + + let mut press_count = 0; + let mut needs_redraw = true; + let mut confirmed = false; + let encoder_start = self.qei.count(); + + while press_count < 5 { + if needs_redraw { + let style = if confirmed { ok_box } else { 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 }; + Text::new("Start", Point::new(12, 105)) + .into_styled(text_style) + .draw(&mut disp) + .unwrap(); + + let style = if !confirmed { cancel_box } else { 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 }; + Text::new("Cancel", Point::new(96, 105)) + .into_styled(text_style) + .draw(&mut disp) + .unwrap(); + + needs_redraw = false; + } + + self.delay.delay_ms(10u16); + let new_selection = + (((self.qei.count().wrapping_sub(encoder_start)) as usize) / 4) % 2 == 0; + if new_selection != confirmed { + needs_redraw = true; + confirmed = new_selection; + } + + if !needs_redraw && self.button.is_low().unwrap() { + press_count += 1; + } else { + press_count = 0; + } + } + + // Make sure the button has been released, before continuing to the next stage + while !self.button.is_high().unwrap() { + self.delay.delay_ms(10u16); + } + + let (spi, disp_dc, disp_rst) = disp.release(); + self.spi = spi; + self.disp_dc = disp_dc; + self.disp_rst = disp_rst; + + (confirmed, self) + } + + fn run_profile(mut self) -> App { + let mut disp = + st7735_lcd::ST7735::new(self.spi, self.disp_dc, self.disp_rst, true, false, 160, 128); + self.disp_cs.set_low().unwrap(); + + disp.init(&mut self.delay).unwrap(); + 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); + rect.draw(&mut disp).unwrap(); + + let (spi, disp_dc, disp_rst) = disp.release(); + self.spi = spi; + self.disp_dc = disp_dc; + self.disp_rst = disp_rst; + + loop {} + + self + } } diff --git a/src/profiles.rs b/src/profiles.rs index fa99523..0826456 100644 --- a/src/profiles.rs +++ b/src/profiles.rs @@ -63,7 +63,7 @@ pub const REFLOW_PROFILES: [ReflowProfile; 4] = [ ], }, ReflowProfile { - name: "To Cold", + name: "Too Cold", points: [ ProfilePoint { time: 0f32,