fw-rust: Refactor everything
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Remove avr-eeprom dependency for now and introduce events.
This commit is contained in:
parent
6cd8df515d
commit
fdd1f4636d
9 changed files with 459 additions and 263 deletions
|
@ -1,84 +1,164 @@
|
|||
use super::{Home, Screen, Screens, Splash};
|
||||
use crate::{lcd::Lcd, Input, BACKLIGHT, CONTRAST};
|
||||
use core::sync::atomic::Ordering;
|
||||
use super::{Draw, Event, Home, Screens};
|
||||
use crate::{eeprom, lcd::Lcd, Input, BACKLIGHT, CONTRAST};
|
||||
|
||||
enum Selection {
|
||||
Contrast,
|
||||
Backlight,
|
||||
BacklightEdit,
|
||||
Contrast,
|
||||
ContrastEdit,
|
||||
Back,
|
||||
}
|
||||
|
||||
pub struct Setup {
|
||||
active: Selection,
|
||||
backlight: u8,
|
||||
contrast: u8,
|
||||
}
|
||||
|
||||
impl Setup {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
active: Selection::Contrast,
|
||||
active: Selection::Backlight,
|
||||
backlight: nb::block!(eeprom::read_byte(&BACKLIGHT)).unwrap(),
|
||||
contrast: nb::block!(eeprom::read_byte(&CONTRAST)).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input(&self, input: &Input) -> Screens {
|
||||
Screens::Setup(Self {
|
||||
active: match self.active {
|
||||
Selection::Contrast => match input {
|
||||
Input::Next => Selection::Backlight,
|
||||
Input::Previous => Selection::Back,
|
||||
Input::Select => return Screens::Splash(Splash),
|
||||
Input::Back => return Screens::Home(Home::new()),
|
||||
},
|
||||
Selection::Backlight => match input {
|
||||
Input::Next => Selection::Back,
|
||||
Input::Previous => Selection::Contrast,
|
||||
Input::Select => return Screens::Splash(Splash),
|
||||
Input::Back => return Screens::Home(Home::new()),
|
||||
},
|
||||
Selection::Back => match input {
|
||||
Input::Next => Selection::Contrast,
|
||||
Input::Previous => Selection::Backlight,
|
||||
Input::Select => return Screens::Home(Home::new()),
|
||||
Input::Back => return Screens::Home(Home::new()),
|
||||
},
|
||||
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::MIN
|
||||
} else {
|
||||
self.backlight + 1
|
||||
};
|
||||
|
||||
return Event::Backlight(self.backlight);
|
||||
}
|
||||
Input::Previous => {
|
||||
self.backlight = if self.backlight == u8::MIN {
|
||||
u8::MAX
|
||||
} else {
|
||||
self.backlight - 1
|
||||
};
|
||||
|
||||
return Event::Backlight(self.backlight);
|
||||
}
|
||||
Input::Select => {
|
||||
nb::block!(eeprom::write_byte(&BACKLIGHT, self.backlight)).unwrap();
|
||||
Selection::Backlight
|
||||
}
|
||||
Input::Back => {
|
||||
self.active = Selection::Backlight;
|
||||
self.backlight = nb::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 {
|
||||
u8::MIN
|
||||
} else {
|
||||
self.contrast + 1
|
||||
};
|
||||
|
||||
return Event::Contrast(self.contrast);
|
||||
}
|
||||
Input::Previous => {
|
||||
self.contrast = if self.contrast == u8::MIN {
|
||||
63
|
||||
} else {
|
||||
self.contrast - 1
|
||||
};
|
||||
|
||||
return Event::Contrast(self.contrast);
|
||||
}
|
||||
Input::Select => {
|
||||
nb::block!(eeprom::write_byte(&CONTRAST, self.contrast)).unwrap();
|
||||
Selection::Contrast
|
||||
}
|
||||
Input::Back => {
|
||||
self.active = Selection::Contrast;
|
||||
self.contrast = nb::block!(eeprom::read_byte(&CONTRAST)).unwrap();
|
||||
return Event::Contrast(self.contrast);
|
||||
}
|
||||
},
|
||||
Selection::Back => match input {
|
||||
Input::Next => Selection::Backlight,
|
||||
Input::Previous => Selection::Contrast,
|
||||
Input::Select => return Event::Screen(Screens::Home(Home::new())),
|
||||
Input::Back => return Event::Screen(Screens::Home(Home::new())),
|
||||
},
|
||||
};
|
||||
|
||||
Event::None
|
||||
}
|
||||
}
|
||||
|
||||
impl Screen for Setup {
|
||||
impl Draw for Setup {
|
||||
fn draw(&self, lcd: &mut Lcd) {
|
||||
let contrast = CONTRAST.load(Ordering::SeqCst);
|
||||
let backlight = BACKLIGHT.load(Ordering::SeqCst);
|
||||
|
||||
match &self.active {
|
||||
Selection::Contrast => {
|
||||
lcd.fill_area(0, 0, 33, 2, 0xFF);
|
||||
lcd.print_inverted(33, 0, "SETUP");
|
||||
lcd.fill_area(69, 0, 33, 2, 0xFF);
|
||||
lcd.print_inverted(0, 2, "CONTRAST:");
|
||||
lcd.print_u8(89, 2, 2, contrast);
|
||||
lcd.print(0, 4, "BACKLIGHT:");
|
||||
lcd.print_u8(83, 2, 3, backlight);
|
||||
lcd.print(0, 2, "BACKLIGHT:");
|
||||
lcd.print_u8(81, 2, 3, self.backlight);
|
||||
lcd.print_inverted(0, 4, "CONTRAST:");
|
||||
lcd.print_u8(87, 4, 2, self.contrast);
|
||||
lcd.print(36, 6, "BACK");
|
||||
}
|
||||
Selection::ContrastEdit => {
|
||||
lcd.fill_area(0, 0, 33, 2, 0xFF);
|
||||
lcd.print_inverted(33, 0, "SETUP");
|
||||
lcd.fill_area(69, 0, 33, 2, 0xFF);
|
||||
lcd.print(0, 2, "BACKLIGHT:");
|
||||
lcd.print_u8(81, 2, 3, self.backlight);
|
||||
lcd.print(0, 4, "CONTRAST:");
|
||||
lcd.print_u8_inverted(87, 4, 2, self.contrast);
|
||||
lcd.print(36, 6, "BACK");
|
||||
}
|
||||
Selection::Backlight => {
|
||||
lcd.fill_area(0, 0, 33, 2, 0xFF);
|
||||
lcd.print_inverted(33, 0, "SETUP");
|
||||
lcd.fill_area(69, 0, 33, 2, 0xFF);
|
||||
lcd.print(0, 2, "CONTRAST:");
|
||||
lcd.print_u8(89, 2, 2, contrast);
|
||||
lcd.print_inverted(0, 4, "BACKLIGHT:");
|
||||
lcd.print_u8(83, 2, 3, backlight);
|
||||
lcd.print_inverted(0, 2, "BACKLIGHT:");
|
||||
lcd.print_u8(81, 2, 3, self.backlight);
|
||||
lcd.print(0, 4, "CONTRAST:");
|
||||
lcd.print_u8(87, 4, 2, self.contrast);
|
||||
lcd.print(36, 6, "BACK");
|
||||
}
|
||||
Selection::BacklightEdit => {
|
||||
lcd.fill_area(0, 0, 33, 2, 0xFF);
|
||||
lcd.print_inverted(33, 0, "SETUP");
|
||||
lcd.fill_area(69, 0, 33, 2, 0xFF);
|
||||
lcd.print(0, 2, "BACKLIGHT:");
|
||||
lcd.print_u8_inverted(81, 2, 3, self.backlight);
|
||||
lcd.print(0, 4, "CONTRAST:");
|
||||
lcd.print_u8(87, 4, 2, self.contrast);
|
||||
lcd.print(36, 6, "BACK");
|
||||
}
|
||||
Selection::Back => {
|
||||
lcd.fill_area(0, 0, 33, 2, 0xFF);
|
||||
lcd.print_inverted(33, 0, "SETUP");
|
||||
lcd.fill_area(69, 0, 33, 2, 0xFF);
|
||||
lcd.print(0, 2, "CONTRAST:");
|
||||
lcd.print_u8(89, 2, 2, contrast);
|
||||
lcd.print(0, 4, "BACKLIGHT:");
|
||||
lcd.print_u8(83, 2, 3, backlight);
|
||||
lcd.print(0, 2, "BACKLIGHT:");
|
||||
lcd.print_u8(81, 2, 3, self.backlight);
|
||||
lcd.print(0, 4, "CONTRAST:");
|
||||
lcd.print_u8(87, 4, 2, self.contrast);
|
||||
lcd.print_inverted(36, 6, "BACK");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue