reflow-firmware3.0/src/application/confirm_profile.rs

135 lines
4.3 KiB
Rust

use embedded_graphics::{
drawable::Drawable, fonts::Text, pixelcolor::Rgb565, prelude::*,
primitives::rectangle::Rectangle, style::PrimitiveStyleBuilder, style::TextStyleBuilder,
};
use embedded_hal::digital::v2::{InputPin, OutputPin};
use profont::{ProFont12Point, ProFont14Point};
use st7735_lcd::Orientation;
use stm32f1xx_hal::prelude::*;
use crate::application::App;
use crate::profiles;
impl App {
pub 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)
}
}