From 7f8e10d33d67c1aacc493cb8c22715622560588b Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Sun, 13 Jul 2025 01:14:42 -0600 Subject: [PATCH] working better, but keypresses delayed --- src/display.rs | 31 ++++++++++++++++++++++++++----- src/main.rs | 34 +++++++++++++++++++++++++++------- src/peripherals/mod.rs | 6 +++++- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/display.rs b/src/display.rs index 6d6550f..9e09bb3 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,22 +1,31 @@ +use core::sync::atomic::Ordering; + use defmt::info; use embassy_rp::{ gpio::{Level, Output}, peripherals::{PIN_13, PIN_14, PIN_15, SPI1}, spi::{Async, Spi}, }; +use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, signal::Signal}; use embassy_time::{Delay, Timer}; use embedded_graphics::{ Drawable, + draw_target::DrawTarget, mono_font::{MonoTextStyle, ascii::FONT_10X20}, pixelcolor::Rgb565, - prelude::{Point, RgbColor}, - text::Text, + prelude::{Point, RgbColor, Size}, + primitives::Rectangle, + text::{Alignment, Text}, }; use embedded_hal_bus::spi::ExclusiveDevice; +use portable_atomic::AtomicBool; use st7365p_lcd::{FrameBuffer, ST7365P}; const SCREEN_WIDTH: usize = 320; const SCREEN_HEIGHT: usize = 320; +const REFRESH_INTERVAL_MS: u64 = 20; + +pub static DISPLAY_SIGNAL: Signal = Signal::new(); pub async fn display_handler( spi: Spi<'static, SPI1, Async>, @@ -40,16 +49,28 @@ pub async fn display_handler( display.set_on().await.unwrap(); loop { + DISPLAY_SIGNAL.wait().await; + + framebuffer + .fill_solid( + &Rectangle::new( + Point::new(0, 0), + Size::new(SCREEN_HEIGHT as u32 - 1, SCREEN_WIDTH as u32 - 1), + ), + Rgb565::BLACK, + ) + .unwrap(); + let text = crate::STRING.lock().await.clone(); + Text::with_alignment( - &crate::STRING.lock().await.as_str(), + &text, Point::new(160, 160), MonoTextStyle::new(&FONT_10X20, Rgb565::RED), - embedded_graphics::text::Alignment::Center, + Alignment::Center, ) .draw(&mut framebuffer) .unwrap(); framebuffer.draw(&mut display).await.unwrap(); - Timer::after_millis(100).await } } diff --git a/src/main.rs b/src/main.rs index 0bb41bd..41fee93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,16 @@ #![no_std] #![no_main] -use crate::peripherals::keyboard::{KeyCode, read_keyboard_fifo}; +use core::sync::atomic::Ordering; + +use crate::{ + display::DISPLAY_SIGNAL, + peripherals::keyboard::{KeyCode, KeyState, read_keyboard_fifo}, +}; use {defmt_rtt as _, panic_probe as _}; +use defmt::info; use embassy_executor::Spawner; use embassy_futures::join::join; use embassy_rp::peripherals::I2C1; @@ -31,11 +37,11 @@ static STRING: Mutex> = Mutex::new(String::new()) async fn main(_spawner: Spawner) { let p = embassy_rp::init(Default::default()); - STRING.lock().await.push_str("T: ").unwrap(); + STRING.lock().await.push_str("Press Del").unwrap(); // configure keyboard event handler let mut config = i2c::Config::default(); - config.frequency = 100_000; + config.frequency = 400_000; let i2c1 = I2c::new_async(p.I2C1, p.PIN_7, p.PIN_6, Irqs, config); conf_peripherals(i2c1).await; @@ -48,12 +54,26 @@ async fn main(_spawner: Spawner) { join( async { loop { - Timer::after_millis(100).await; - if let Some(key) = read_keyboard_fifo().await { - if let KeyCode::Char(c) = key.key { - STRING.lock().await.push(c).unwrap(); + Timer::after_millis(20).await; + if let Some(key) = read_keyboard_fifo().await + && key.state == KeyState::Pressed + { + let mut string = STRING.lock().await; + match key.key { + KeyCode::Backspace => { + string.pop().unwrap(); + } + KeyCode::Del => { + string.clear(); + } + KeyCode::Char(c) => { + string.push(c).unwrap(); + } + _ => (), } + DISPLAY_SIGNAL.signal(()); } + Timer::after_millis(10).await; } }, display_handler(spi1, p.PIN_13, p.PIN_14, p.PIN_15), diff --git a/src/peripherals/mod.rs b/src/peripherals/mod.rs index df5ab23..d00c7ec 100644 --- a/src/peripherals/mod.rs +++ b/src/peripherals/mod.rs @@ -10,7 +10,7 @@ use embassy_time::Timer; pub mod keyboard; -use crate::peripherals::keyboard::configure_keyboard; +use crate::peripherals::keyboard::{configure_keyboard, read_keyboard_fifo}; const MCU_ADDR: u8 = 0x1F; @@ -28,6 +28,10 @@ pub async fn conf_peripherals(i2c: I2CBUS) { PERIPHERAL_BUS.get().lock().await.replace(i2c); configure_keyboard(200, 100).await; + + // empty keys + while read_keyboard_fifo().await.is_some() {} + // set_lcd_backlight(255).await; set_key_backlight(0).await; }