From aad0ccef4e2e862fef104a4a43749b2edab26aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Wed, 18 Sep 2024 13:23:15 +0200 Subject: [PATCH] Fix menu --- Cargo.lock | 49 +++++++++++++++---- Cargo.toml | 3 +- espflash.toml | 13 +++++ partitions.csv | 5 ++ src/apps/menu.rs | 21 ++++++-- src/main.rs | 124 +++++++++++++++++++++++++++++++---------------- 6 files changed, 158 insertions(+), 57 deletions(-) create mode 100644 espflash.toml create mode 100644 partitions.csv diff --git a/Cargo.lock b/Cargo.lock index cd3f268..a83d9d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,7 +183,7 @@ checksum = "f86b9ec30048b1955da2038fcc3c017f419ab21bb0001879d16c0a3749dc6b7a" dependencies = [ "byte-slice-cast", "display-interface", - "embedded-hal", + "embedded-hal 1.0.0", "embedded-hal-async", ] @@ -202,7 +202,7 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9d2e857f87ac832df68fa498d18ddc679175cf3d2e4aa893988e5601baf9438" dependencies = [ - "nb", + "nb 1.1.0", ] [[package]] @@ -237,6 +237,16 @@ dependencies = [ "byteorder", ] +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", +] + [[package]] name = "embedded-hal" version = "1.0.0" @@ -249,7 +259,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884" dependencies = [ - "embedded-hal", + "embedded-hal 1.0.0", ] [[package]] @@ -259,7 +269,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d3980bf28e8577db59fe2bdb3df868a419469d2cecb363644eea2b6f7797669" dependencies = [ "critical-section", - "embedded-hal", + "embedded-hal 1.0.0", "portable-atomic", ] @@ -269,8 +279,8 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" dependencies = [ - "embedded-hal", - "nb", + "embedded-hal 1.0.0", + "nb 1.1.0", ] [[package]] @@ -349,7 +359,7 @@ dependencies = [ "document-features", "embedded-can", "embedded-dma", - "embedded-hal", + "embedded-hal 1.0.0", "embedded-hal-nb", "enumset", "esp-build", @@ -358,7 +368,7 @@ dependencies = [ "esp-riscv-rt", "esp32c6", "fugit", - "nb", + "nb 1.1.0", "paste", "portable-atomic", "rand_core", @@ -523,6 +533,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "keypad" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cd098757f3c79a71672ea320a457ba57ab42f25f628145473329fa59c320d37" +dependencies = [ + "embedded-hal 0.2.7", +] + [[package]] name = "kinded" version = "0.3.0" @@ -619,6 +638,15 @@ dependencies = [ "adler2", ] +[[package]] +name = "nb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" +dependencies = [ + "nb 1.1.0", +] + [[package]] name = "nb" version = "1.1.0" @@ -783,6 +811,7 @@ dependencies = [ "esp-hal", "esp-println", "heapless", + "keypad", "log", "maze", "nutype", @@ -829,7 +858,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f5c1b8bf41ea746266cdee443d1d1e9125c86ce1447e1a2615abd34330d33a9" dependencies = [ "critical-section", - "embedded-hal", + "embedded-hal 1.0.0", ] [[package]] @@ -1068,7 +1097,7 @@ checksum = "3e531b21e70dfc6294be2429e4f616f634c8ca1a328325dceefc4f92c12d6e9b" dependencies = [ "display-interface", "embedded-graphics", - "embedded-hal", + "embedded-hal 1.0.0", "embedded-hal-async", "maybe-async-cfg", "sealed", diff --git a/Cargo.toml b/Cargo.toml index c7a3f0e..a11e7a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ embedded-graphics = "0.8.1" embedded-hal-bus = "0.2.0" esp-backtrace = { version = "0.13.0", features = ["esp32c6", "exception-handler", "panic-handler", "println"] } esp-hal = { version = "0.19.0", features = ["esp32c6", "embedded-hal"] } -esp-println = { version = "0.10.0", default-features = false, features = ["esp32c6", "log", "jtag-serial"] } +esp-println = { version = "0.10.0", default-features = false, features = ["esp32c6", "log", "auto"] } heapless = "0.8.0" log = { version = "0.4.21" } nutype = { version = "0.5.0", default-features = false } @@ -20,6 +20,7 @@ weact-studio-epd = { version = "0.1.2", features = ["blocking"] } shared = { path = "./shared", features = ['trng'] } maze = { path = "./maze" } strum = { version = "0.26.3", default-features = false, features = ["derive"] } +keypad = "0.2.2" [profile.dev] # Rust debug is too slow. diff --git a/espflash.toml b/espflash.toml new file mode 100644 index 0000000..b8ccfc3 --- /dev/null +++ b/espflash.toml @@ -0,0 +1,13 @@ +partition_table = "partitions.csv" + +[connection] + +[[usb_device]] +vid = "1a86" +pid = "55d3" + +[[usb_device]] +vid = "04e8" +pid = "6860" + +[flash] diff --git a/partitions.csv b/partitions.csv new file mode 100644 index 0000000..85d225a --- /dev/null +++ b/partitions.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x00009000, 0x6000, +phy_init, data, phy, 0x0000f000, 0x1000, +factory, app, factory, 0x00010000, 0x003f0000, diff --git a/src/apps/menu.rs b/src/apps/menu.rs index ae8ed1e..5422e5b 100644 --- a/src/apps/menu.rs +++ b/src/apps/menu.rs @@ -1,7 +1,7 @@ use crate::Button; use super::*; -use profont::PROFONT_24_POINT; +use profont::PROFONT_18_POINT; use strum::IntoEnumIterator; #[derive(Debug, Clone, Copy, Default, PartialEq, strum::EnumIter)] @@ -21,8 +21,8 @@ impl MenuEntry { fn position(&self) -> Point { match self { - MenuEntry::Labirynth => Point::new(8, 68), - MenuEntry::Pairs => Point::new(38, 68), + MenuEntry::Labirynth => Point::new(20, 38), + MenuEntry::Pairs => Point::new(20, 68), } } } @@ -69,10 +69,21 @@ impl App for Menu { } fn draw(&self, display: &mut Display290TriColor) { - let style = MonoTextStyle::new(&PROFONT_24_POINT, TriColor::Black); - let selected_style = MonoTextStyle::new(&PROFONT_24_POINT, TriColor::Red); + let style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Black); + let selected_style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Red); MenuEntry::iter().for_each(|entry| { + if entry == self.selected { + let _ = Text::with_text_style( + "> ", + Point::new(2, entry.position().y), + selected_style, + TextStyle::default(), + ) + .draw(display) + .unwrap(); + }; + let _ = Text::with_text_style( entry.as_str(), entry.position(), diff --git a/src/main.rs b/src/main.rs index f17f7fb..c2b80e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use esp_hal::{ clock::ClockControl, delay::Delay, gpio::{ - Gpio0, Gpio1, Gpio2, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, GpioPin, Input, Io, Level, Output, + Gpio10, Gpio2, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Input, Io, Level, Output, Pull, NO_PIN, }, peripherals::Peripherals, @@ -24,6 +24,8 @@ use esp_hal::{ spi::{master::Spi, SpiMode}, system::SystemControl, }; +use esp_println::println; +use log::LevelFilter; use profont::PROFONT_24_POINT; use weact_studio_epd::{graphics::Display290TriColor, TriColor}; use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290TriColorDriver}; @@ -37,16 +39,16 @@ fn main() -> ! { let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let clocks = ClockControl::max(system.clock_control).freeze(); let delay = Delay::new(&clocks); - let mut trng = Trng::new(peripherals.RNG, peripherals.ADC1); - let _ = trng.random(); + esp_println::logger::init_logger(LevelFilter::Trace); + // esp_println::logger::init_logger_from_env(); - esp_println::logger::init_logger_from_env(); + println!("init"); log::info!("Intializing SPI Bus..."); // Pins for WeAct - let mosi = io.pins.gpio18; // D10 / GPIO18 + let mosi /* sda */ = io.pins.gpio18; // D10 / GPIO18 let sclk = io.pins.gpio19; // D8 / GPIO19 let cs = io.pins.gpio20; // D9 / GPIO20 let dc = io.pins.gpio21; // D3 / GPIO21 @@ -75,62 +77,71 @@ fn main() -> ! { log::info!("Intializing SPI Device..."); let spi_device = ExclusiveDevice::new(spi_bus, cs, delay).expect("SPI device initialize error"); let spi_interface = SPIInterface::new(spi_device, dc); + log::info!("Intializing SPI Device (DONE)"); // Setup EPD log::info!("Intializing EPD..."); let mut driver = WeActStudio290TriColorDriver::new(spi_interface, busy, rst, delay); let mut display = Display290TriColor::new(); display.set_rotation(DisplayRotation::Rotate90); + log::info!("Driver init..."); driver.init().unwrap(); + log::info!("Intializing EPD (DONE)"); - let style = MonoTextStyle::new(&PROFONT_24_POINT, TriColor::Black); - let _ = Text::with_text_style( - "Welcome in Reddy", - Point::new(8, 68), - style, - TextStyle::default(), - ) - .draw(&mut display); + display.clear(TriColor::White); + log::info!("Printing welcome msg..."); + let style = MonoTextStyle::new(&PROFONT_24_POINT, TriColor::Red); + let _ = Text::with_text_style("Witamy", Point::new(8, 68), style, TextStyle::default()) + .draw(&mut display); + log::info!("Printing welcome msg (DONE)"); - driver.full_update(&display).unwrap(); + log::info!("Full update..."); + let _ = driver.full_update(&display); + log::info!("Full update (DONE)"); log::info!("Sleeping for 0.5s..."); - driver.sleep().unwrap(); delay.delay(500.millis()); let kbd = Keyboard { - gpio0: io.pins.gpio0, - gpio1: io.pins.gpio1, - gpio2: io.pins.gpio2, - gpio3: io.pins.gpio3, - gpio4: io.pins.gpio4, - gpio5: io.pins.gpio5, - gpio6: io.pins.gpio6, - gpio7: io.pins.gpio7, + rows: Rows { + gpio2: Input::new(io.pins.gpio2, Pull::Up), + gpio3: Input::new(io.pins.gpio3, Pull::Up), + gpio4: Input::new(io.pins.gpio4, Pull::Up), + gpio5: Input::new(io.pins.gpio5, Pull::Up), + }, + cols: Cols { + gpio6: Output::new(io.pins.gpio6, Level::Low), + gpio7: Output::new(io.pins.gpio7, Level::Low), + gpio8: Output::new(io.pins.gpio8, Level::Low), + gpio10: Output::new(io.pins.gpio10, Level::Low), + }, }; let mut ctx = Context { button_pressed: None, }; let mut app = Application::Menu(Menu::new()); + let mut trng = Trng::new(peripherals.RNG, peripherals.ADC1); + + display.clear(TriColor::White); + app.draw(&mut display); + driver.full_update(&display).unwrap(); + driver.sleep().unwrap(); loop { - log::info!("Wake up!"); - driver.wake_up().unwrap(); - - display.clear(TriColor::White); - ctx.button_pressed = kbd.pressed(); if !ctx.button_pressed.is_none() { + log::info!("Wake up!"); + driver.wake_up().unwrap(); + display.clear(TriColor::White); app.update(&ctx, &mut trng); app.draw(&mut display); - // TODO: try fast update? driver.full_update(&display).unwrap(); } log::info!("Sleeping for 100ms..."); driver.sleep().unwrap(); - delay.delay(100.millis()); + delay.delay(300.millis()); } } @@ -170,20 +181,51 @@ pub struct Context { button_pressed: Option