working better, but keypresses delayed

This commit is contained in:
2025-07-13 01:14:42 -06:00
parent a537d9ea69
commit 7f8e10d33d
3 changed files with 58 additions and 13 deletions

View File

@@ -1,22 +1,31 @@
use core::sync::atomic::Ordering;
use defmt::info; use defmt::info;
use embassy_rp::{ use embassy_rp::{
gpio::{Level, Output}, gpio::{Level, Output},
peripherals::{PIN_13, PIN_14, PIN_15, SPI1}, peripherals::{PIN_13, PIN_14, PIN_15, SPI1},
spi::{Async, Spi}, spi::{Async, Spi},
}; };
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, signal::Signal};
use embassy_time::{Delay, Timer}; use embassy_time::{Delay, Timer};
use embedded_graphics::{ use embedded_graphics::{
Drawable, Drawable,
draw_target::DrawTarget,
mono_font::{MonoTextStyle, ascii::FONT_10X20}, mono_font::{MonoTextStyle, ascii::FONT_10X20},
pixelcolor::Rgb565, pixelcolor::Rgb565,
prelude::{Point, RgbColor}, prelude::{Point, RgbColor, Size},
text::Text, primitives::Rectangle,
text::{Alignment, Text},
}; };
use embedded_hal_bus::spi::ExclusiveDevice; use embedded_hal_bus::spi::ExclusiveDevice;
use portable_atomic::AtomicBool;
use st7365p_lcd::{FrameBuffer, ST7365P}; use st7365p_lcd::{FrameBuffer, ST7365P};
const SCREEN_WIDTH: usize = 320; const SCREEN_WIDTH: usize = 320;
const SCREEN_HEIGHT: usize = 320; const SCREEN_HEIGHT: usize = 320;
const REFRESH_INTERVAL_MS: u64 = 20;
pub static DISPLAY_SIGNAL: Signal<ThreadModeRawMutex, ()> = Signal::new();
pub async fn display_handler( pub async fn display_handler(
spi: Spi<'static, SPI1, Async>, spi: Spi<'static, SPI1, Async>,
@@ -40,16 +49,28 @@ pub async fn display_handler(
display.set_on().await.unwrap(); display.set_on().await.unwrap();
loop { 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( Text::with_alignment(
&crate::STRING.lock().await.as_str(), &text,
Point::new(160, 160), Point::new(160, 160),
MonoTextStyle::new(&FONT_10X20, Rgb565::RED), MonoTextStyle::new(&FONT_10X20, Rgb565::RED),
embedded_graphics::text::Alignment::Center, Alignment::Center,
) )
.draw(&mut framebuffer) .draw(&mut framebuffer)
.unwrap(); .unwrap();
framebuffer.draw(&mut display).await.unwrap(); framebuffer.draw(&mut display).await.unwrap();
Timer::after_millis(100).await
} }
} }

View File

@@ -3,10 +3,16 @@
#![no_std] #![no_std]
#![no_main] #![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_rtt as _, panic_probe as _};
use defmt::info;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_futures::join::join; use embassy_futures::join::join;
use embassy_rp::peripherals::I2C1; use embassy_rp::peripherals::I2C1;
@@ -31,11 +37,11 @@ static STRING: Mutex<ThreadModeRawMutex, String<25>> = Mutex::new(String::new())
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default()); 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 // configure keyboard event handler
let mut config = i2c::Config::default(); 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); let i2c1 = I2c::new_async(p.I2C1, p.PIN_7, p.PIN_6, Irqs, config);
conf_peripherals(i2c1).await; conf_peripherals(i2c1).await;
@@ -48,12 +54,26 @@ async fn main(_spawner: Spawner) {
join( join(
async { async {
loop { loop {
Timer::after_millis(100).await; Timer::after_millis(20).await;
if let Some(key) = read_keyboard_fifo().await { if let Some(key) = read_keyboard_fifo().await
if let KeyCode::Char(c) = key.key { && key.state == KeyState::Pressed
STRING.lock().await.push(c).unwrap(); {
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), display_handler(spi1, p.PIN_13, p.PIN_14, p.PIN_15),

View File

@@ -10,7 +10,7 @@ use embassy_time::Timer;
pub mod keyboard; pub mod keyboard;
use crate::peripherals::keyboard::configure_keyboard; use crate::peripherals::keyboard::{configure_keyboard, read_keyboard_fifo};
const MCU_ADDR: u8 = 0x1F; const MCU_ADDR: u8 = 0x1F;
@@ -28,6 +28,10 @@ pub async fn conf_peripherals(i2c: I2CBUS) {
PERIPHERAL_BUS.get().lock().await.replace(i2c); PERIPHERAL_BUS.get().lock().await.replace(i2c);
configure_keyboard(200, 100).await; configure_keyboard(200, 100).await;
// empty keys
while read_keyboard_fifo().await.is_some() {}
// set_lcd_backlight(255).await; // set_lcd_backlight(255).await;
set_key_backlight(0).await; set_key_backlight(0).await;
} }