clock_generator/firmware/rust/src/screen/mod.rs
finga 7f14974146
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
fw-rust: Remove unnecessary Draw trait
2022-04-03 01:23:06 +02:00

134 lines
3.2 KiB
Rust

use atmega_hal::{
pac::TC0,
port::{mode::Output, Pin, PB0, PB1, PD5},
Spi,
};
use si5351::{Si5351, Si5351Device};
mod home;
mod setup;
mod splash;
use crate::{eeprom, lcd::Lcd, I2c, Input, BACKLIGHT};
pub use home::Home;
pub use setup::Setup;
pub use splash::Splash;
// TODO: Only update changes instead of whole screen
pub enum Event {
Screen(Screens),
Backlight(u8),
Contrast(u8),
None,
}
pub enum Screens {
Splash(Splash),
Home(Home),
Setup(Setup),
}
impl Screens {
pub fn input(&mut self, input: &Input) -> Event {
match self {
Screens::Splash(_) => Event::None,
Screens::Home(home) => home.input(input),
Screens::Setup(setup) => setup.input(input),
}
}
}
pub struct Screen {
lcd: Lcd,
tc0: TC0,
pwm: Pin<Output, PD5>,
screen: Screens,
si5351: Si5351Device<I2c>,
}
impl Screen {
pub fn new(
tc0: TC0,
spi: Spi,
pwm: Pin<Output, PD5>,
cd: Pin<Output, PB0>,
rst: Pin<Output, PB1>,
i2c: I2c,
) -> Self {
Self {
lcd: Lcd::new(spi, cd, rst),
tc0,
pwm,
screen: Screens::Splash(Splash),
si5351: Si5351Device::new_adafruit_module(i2c),
}
}
pub fn init(&mut self) {
// Init display backlight
self.tc0.ocr0a.write(|w| unsafe { w.bits(255) });
self.tc0.tccr0a.write(|w| {
w.wgm0().pwm_fast();
w.com0b().match_clear()
});
self.set_backlight(nb::block!(eeprom::read_byte(&BACKLIGHT)).unwrap());
// Init lcd display
self.lcd.init();
self.draw();
// Init Si5351
self.si5351.init_adafruit_module().unwrap();
}
fn set_backlight(&mut self, backlight: u8) {
match backlight {
0 => {
self.tc0.tccr0b.write(|w| w.cs0().no_clock());
self.pwm.set_low();
}
1..=5 => {
self.tc0.tccr0b.write(|w| {
w.wgm02().set_bit();
w.cs0().prescale_256()
});
self.tc0.ocr0b.write(|w| unsafe { w.bits(backlight - 1) });
}
_ => {
self.tc0.tccr0b.write(|w| {
w.wgm02().set_bit();
w.cs0().prescale_64()
});
self.tc0.ocr0b.write(|w| unsafe { w.bits(backlight - 6) });
}
}
}
pub fn draw(&mut self) {
self.lcd.fill_area(0, 0, 102, 8, 0x00);
match &self.screen {
Screens::Splash(splash) => splash.draw(&mut self.lcd),
Screens::Home(home) => home.draw(&mut self.lcd),
Screens::Setup(setup) => setup.draw(&mut self.lcd),
}
}
pub fn input(&mut self, input: &Input) {
match self.screen.input(input) {
Event::Screen(screen) => self.screen = screen,
Event::Backlight(backlight) => self.set_backlight(backlight),
Event::Contrast(contrast) => self.lcd.set_contrast(contrast),
Event::None => {}
}
self.draw();
}
pub fn change(&mut self, screen: Screens) {
self.screen = screen;
self.draw();
}
}