From e95774ae16c212a083aabba3b17709d9f6e49bc7 Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Wed, 2 Jul 2025 18:06:54 -0600 Subject: [PATCH] WIP --- Cargo.lock | 1 - Cargo.toml | 3 +- src/display.rs | 40 +++++------------ src/main.rs | 89 ++++++++++++++++++++++++++++--------- src/peripherals/keyboard.rs | 15 +++---- src/peripherals/mod.rs | 11 +---- 6 files changed, 89 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9448429..87cd793 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1761,7 +1761,6 @@ dependencies = [ [[package]] name = "st7365p-lcd" version = "0.10.0" -source = "git+https://github.com/legitcamper/st7365p-lcd-rs?branch=async#9f8da568ff695a00afb6b8b8cf9573fad408a66f" dependencies = [ "embedded-graphics-core", "embedded-hal 1.0.0", diff --git a/Cargo.toml b/Cargo.toml index d085ab9..424a79a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,4 +77,5 @@ defmt-rtt = "0.4.2" embedded-graphics = { version = "0.8.1" } embedded-layout = "0.4.2" embedded-sdmmc = { git = "https://github.com/Be-ing/embedded-sdmmc-rs", branch = "bisync", default-features = false } -st7365p-lcd = { git = "https://github.com/legitcamper/st7365p-lcd-rs", branch = "async" } +# st7365p-lcd = { git = "https://github.com/legitcamper/st7365p-lcd-rs", branch = "async" } +st7365p-lcd = { path = "../ST7365P-lcd-rs" } diff --git a/src/display.rs b/src/display.rs index b9d919a..91b39ea 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,4 +1,6 @@ use arrform::{ArrForm, arrform}; +use core::fmt::Debug; +use defmt::info; use embassy_rp::{ gpio::{Level, Output}, peripherals::{PIN_13, PIN_14, PIN_15, SPI1}, @@ -14,12 +16,12 @@ use embedded_graphics::{ }, pixelcolor::Rgb565, prelude::{Point, RgbColor, Size}, - primitives::Rectangle, + primitives::{PrimitiveStyle, Rectangle, StyledDrawable}, text::Text, }; use embedded_hal_1::digital::OutputPin; use embedded_hal_async::spi::SpiDevice; -use embedded_hal_bus::spi::ExclusiveDevice; +use embedded_hal_bus::spi::{ExclusiveDevice, NoDelay}; use embedded_layout::{ align::{horizontal, vertical}, layout::linear::LinearLayout, @@ -35,36 +37,13 @@ pub const SCREEN_HEIGHT: usize = 320; pub const STATUS_BAR_WIDTH: usize = 320; pub const STATUS_BAR_HEIGHT: usize = 40; -pub async fn init_display( - spi: Spi<'static, SPI1, Async>, - cs: PIN_13, - data: PIN_14, - reset: PIN_15, -) -> FrameBuffer< +pub type FRAMEBUFFER = FrameBuffer< SCREEN_WIDTH, SCREEN_HEIGHT, ExclusiveDevice, Output<'static>, Delay>, Output<'static>, Output<'static>, -> { - let spi_device = ExclusiveDevice::new(spi, Output::new(cs, Level::Low), Delay).unwrap(); - let mut display = ST7365P::new( - spi_device, - Output::new(data, Level::Low), - Some(Output::new(reset, Level::High)), - true, - false, - 320, - 320, - ); - display.init(&mut Delay).await.unwrap(); - display - .set_orientation(&Orientation::Landscape) - .await - .unwrap(); - - FrameBuffer::new(display) -} +>; pub struct UI { pub status_bar: StatusBar, @@ -83,7 +62,10 @@ impl UI>(&mut self, target: &mut D) { + pub fn draw>(&mut self, target: &mut D) + where + ::Error: Debug, + { self.draw_status_bar(target); self.draw_selection(target); } @@ -114,11 +96,13 @@ impl UI i2c::InterruptHandler; -}); +embassy_rp::bind_interrupts!( + struct Irqs { + I2C1_IRQ => i2c::InterruptHandler; + } +); #[embassy_executor::main] async fn main(spawner: Spawner) { let p = embassy_rp::init(Default::default()); - static KEYBOARD_EVENTS: StaticCell> = StaticCell::new(); - let keyboard_events = KEYBOARD_EVENTS.init(Channel::new()); + let mut i2c1_config = i2c::Config::default(); + i2c1_config.frequency = 100_000; + let i2c1 = I2c::new_async(p.I2C1, p.PIN_7, p.PIN_6, Irqs, i2c1_config); + conf_peripherals(i2c1).await; + + let mut spi1_config = spi::Config::default(); + spi1_config.frequency = 16_000_000; + let spi1 = Spi::new( + p.SPI1, + p.PIN_10, + p.PIN_11, + p.PIN_12, + p.DMA_CH0, + p.DMA_CH1, + spi1_config, + ); - // configure keyboard event handler - let mut config = i2c::Config::default(); - config.frequency = 100_000; - let i2c1 = I2c::new_async(p.I2C1, p.PIN_7, p.PIN_6, Irqs, config); spawner - .spawn(peripherals_task(i2c1, keyboard_events.sender())) + .spawn(main_task(spi1, p.PIN_13, p.PIN_14, p.PIN_15)) + .unwrap(); +} + +#[embassy_executor::task] +async fn main_task( + spi1: Spi<'static, SPI1, spi::Async>, + spi1_cs: PIN_13, + spi1_data: PIN_14, + spi1_reset: PIN_15, +) { + let spi_device = ExclusiveDevice::new(spi1, Output::new(spi1_cs, Level::Low), Delay).unwrap(); + let display = ST7365P::new( + spi_device, + Output::new(spi1_data, Level::Low), + Some(Output::new(spi1_reset, Level::High)), + false, + true, + SCREEN_WIDTH as u32, + SCREEN_HEIGHT as u32, + ); + let mut framebuffer: FRAMEBUFFER = FrameBuffer::new(display); + framebuffer.init(&mut Delay).await.unwrap(); + framebuffer + .display + .set_custom_orientation(0x60) + .await .unwrap(); - let mut config = spi::Config::default(); - config.frequency = 16_000_000; - let spi1 = Spi::new( - p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, p.DMA_CH0, p.DMA_CH1, config, - ); - let mut framebuffer = init_display(spi1, p.PIN_13, p.PIN_14, p.PIN_15).await; + // let mut ui: UI<50, 25> = UI::new(); - let mut ui: UI<50, 25> = UI::new(); - ui.draw(&mut framebuffer); + // read_keyboard_fifo().await; + Rectangle::new(Point::new(0, 0), Size::new(319, 319)) + .draw_styled(&PrimitiveStyle::with_fill(Rgb565::RED), &mut framebuffer) + .unwrap(); + // ui.draw(&mut framebuffer); + framebuffer.draw().await.unwrap(); loop { - framebuffer.draw().await.unwrap(); + info!("Done"); Timer::after_millis(500).await; } } diff --git a/src/peripherals/keyboard.rs b/src/peripherals/keyboard.rs index eaa07c7..4d33755 100644 --- a/src/peripherals/keyboard.rs +++ b/src/peripherals/keyboard.rs @@ -17,7 +17,7 @@ const KEY_CAPSLOCK: u8 = 1 << 5; const KEY_NUMLOCK: u8 = 1 << 6; const KEY_COUNT_MASK: u8 = 0x1F; // 0x1F == 31 -pub async fn read_keyboard_fifo(channel: &mut Sender<'static, NoopRawMutex, KeyEvent, 10>) { +pub async fn read_keyboard_fifo() -> Option { let mut i2c = PERIPHERAL_BUS.get().lock().await; let i2c = i2c.as_mut().unwrap(); @@ -40,16 +40,15 @@ pub async fn read_keyboard_fifo(channel: &mut Sender<'static, NoopRawMutex, KeyE .await .is_ok() { - channel - .try_send(KeyEvent { - state: KeyState::from(event[0]), - key: KeyCode::from(event[1]), - mods: Modifiers::NONE, - }) - .expect("Failed to push key"); + return Some(KeyEvent { + state: KeyState::from(event[0]), + key: KeyCode::from(event[1]), + mods: Modifiers::NONE, + }); } } } + None } const REG_ID_DEB: u8 = 0x06; diff --git a/src/peripherals/mod.rs b/src/peripherals/mod.rs index b3e7d1a..7602555 100644 --- a/src/peripherals/mod.rs +++ b/src/peripherals/mod.rs @@ -25,11 +25,7 @@ const REG_ID_VER: u8 = 0x01; const REG_ID_RST: u8 = 0x08; const REG_ID_INT: u8 = 0x03; -#[embassy_executor::task] -pub async fn peripherals_task( - i2c: I2CBUS, - mut keyboard_channel: Sender<'static, NoopRawMutex, KeyEvent, 10>, -) { +pub async fn conf_peripherals(i2c: I2CBUS) { Timer::after(embassy_time::Duration::from_millis(100)).await; PERIPHERAL_BUS.get().lock().await.replace(i2c); @@ -37,11 +33,6 @@ pub async fn peripherals_task( configure_keyboard(200, 100).await; set_lcd_backlight(255).await; set_key_backlight(0).await; - - loop { - Timer::after(Duration::from_millis(200)).await; - read_keyboard_fifo(&mut keyboard_channel).await; - } } /// return major & minor mcu version