clock_generator/firmware/rust/src/screen/setup.rs
finga 343b95dc78
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
fw-rust: Refactor printing to the LCD
Pass iterators for control and display data to the printing function
instead of just printing it.
2022-04-10 18:18:37 +02:00

154 lines
5.5 KiB
Rust

use super::{Event, Home, Screens};
use crate::{eeprom, lcd::Lcd, Input, BACKLIGHT, CONTRAST};
use nb::block;
enum Selection {
Backlight,
BacklightEdit,
Contrast,
ContrastEdit,
Back,
}
pub struct Setup {
active: Selection,
backlight: u8,
contrast: u8,
}
impl Setup {
pub fn new() -> Self {
Self {
active: Selection::Backlight,
backlight: block!(eeprom::read_byte(&BACKLIGHT)).unwrap(),
contrast: block!(eeprom::read_byte(&CONTRAST)).unwrap(),
}
}
pub fn input(&mut self, input: &Input) -> Event {
self.active = match self.active {
Selection::Backlight => match input {
Input::Next => Selection::Contrast,
Input::Previous => Selection::Back,
Input::Select => Selection::BacklightEdit,
Input::Back => return Event::Screen(Screens::Home(Home::new())),
},
Selection::BacklightEdit => match input {
Input::Next => {
self.backlight = if self.backlight == u8::MAX {
u8::MAX
} else {
self.backlight + 1
};
return Event::Backlight(self.backlight);
}
Input::Previous => {
self.backlight = if self.backlight == u8::MIN {
u8::MIN
} else {
self.backlight - 1
};
return Event::Backlight(self.backlight);
}
Input::Select => {
block!(eeprom::write_byte(&BACKLIGHT, self.backlight)).unwrap();
Selection::Backlight
}
Input::Back => {
self.active = Selection::Backlight;
self.backlight = block!(eeprom::read_byte(&BACKLIGHT)).unwrap();
return Event::Backlight(self.backlight);
}
},
Selection::Contrast => match input {
Input::Next => Selection::Back,
Input::Previous => Selection::Backlight,
Input::Select => Selection::ContrastEdit,
Input::Back => return Event::Screen(Screens::Home(Home::new())),
},
Selection::ContrastEdit => match input {
Input::Next => {
self.contrast = if self.contrast >= 63 {
63
} else {
self.contrast + 1
};
return Event::Contrast(self.contrast);
}
Input::Previous => {
self.contrast = if self.contrast == u8::MIN {
u8::MIN
} else {
self.contrast - 1
};
return Event::Contrast(self.contrast);
}
Input::Select => {
block!(eeprom::write_byte(&CONTRAST, self.contrast)).unwrap();
Selection::Contrast
}
Input::Back => {
self.active = Selection::Contrast;
self.contrast = block!(eeprom::read_byte(&CONTRAST)).unwrap();
return Event::Contrast(self.contrast);
}
},
Selection::Back => match input {
Input::Next => Selection::Backlight,
Input::Previous => Selection::Contrast,
_ => return Event::Screen(Screens::Home(Home::new())),
},
};
Event::None
}
pub fn draw(&self, lcd: &mut Lcd) {
lcd.fill_area(0, 0, 33, 2, 0xFF);
lcd.print(33, 0, true, "SETUP");
lcd.fill_area(69, 0, 33, 2, 0xFF);
match &self.active {
Selection::Contrast => {
lcd.print(0, 2, false, "BACKLIGHT:");
lcd.print_u8(81, 2, 3, false, self.backlight);
lcd.print(0, 4, true, "CONTRAST:");
lcd.print_u8(87, 4, 2, false, self.contrast);
lcd.print(36, 6, false, "BACK");
}
Selection::ContrastEdit => {
lcd.print(0, 2, false, "BACKLIGHT:");
lcd.print_u8(81, 2, 3, false, self.backlight);
lcd.print(0, 4, false, "CONTRAST:");
lcd.print_u8(87, 4, 2, true, self.contrast);
lcd.print(36, 6, false, "BACK");
}
Selection::Backlight => {
lcd.print(0, 2, true, "BACKLIGHT:");
lcd.print_u8(81, 2, 3, false, self.backlight);
lcd.print(0, 4, false, "CONTRAST:");
lcd.print_u8(87, 4, 2, false, self.contrast);
lcd.print(36, 6, false, "BACK");
}
Selection::BacklightEdit => {
lcd.print(0, 2, false, "BACKLIGHT:");
lcd.print_u8(81, 2, 3, true, self.backlight);
lcd.print(0, 4, false, "CONTRAST:");
lcd.print_u8(87, 4, 2, false, self.contrast);
lcd.print(36, 6, false, "BACK");
}
Selection::Back => {
lcd.print(0, 2, false, "BACKLIGHT:");
lcd.print_u8(81, 2, 3, false, self.backlight);
lcd.print(0, 4, false, "CONTRAST:");
lcd.print_u8(87, 4, 2, false, self.contrast);
lcd.print(36, 6, true, "BACK");
}
}
}
}