mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2026-10-23 06:57:36 -06:00
Squash merge keyboard-writer into main
This commit is contained in:
1 parent
5e537be5a3
commit
2d31e62d82
6 files changed
+313
-192
No files matched your search
+57
-83
@@ -1,21 +1,35 @@
|
||||
use core::sync::atomic::Ordering;
|
||||
|
||||
use defmt::info;
|
||||
use embassy_rp::{
|
||||
gpio::{Level, Output},
|
||||
peripherals::{PIN_13, PIN_14, PIN_15, SPI1},
|
||||
spi::{Blocking, Spi},
|
||||
spi::{Async, Spi},
|
||||
};
|
||||
use embassy_time::{Delay, Timer};
|
||||
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, signal::Signal};
|
||||
use embassy_time::{Delay, Instant, Timer};
|
||||
use embedded_graphics::{
|
||||
Drawable, Pixel,
|
||||
pixelcolor::{Rgb565, raw::RawU16},
|
||||
prelude::{DrawTarget, OriginDimensions, Point, Primitive, RawData, RgbColor, Size},
|
||||
primitives::{PrimitiveStyle, Rectangle},
|
||||
Drawable,
|
||||
draw_target::DrawTarget,
|
||||
mono_font::{MonoTextStyle, ascii::FONT_10X20},
|
||||
pixelcolor::Rgb565,
|
||||
prelude::{Dimensions, Point, RgbColor, Size},
|
||||
primitives::Rectangle,
|
||||
text::{Alignment, Text},
|
||||
};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use st7365p_lcd::{Orientation, ST7365P};
|
||||
use portable_atomic::AtomicBool;
|
||||
use st7365p_lcd::{FrameBuffer, ST7365P};
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn display_task(
|
||||
spi: Spi<'static, SPI1, Blocking>,
|
||||
use crate::LAST_TEXT_RECT;
|
||||
|
||||
const SCREEN_WIDTH: usize = 320;
|
||||
const SCREEN_HEIGHT: usize = 320;
|
||||
|
||||
pub static DISPLAY_SIGNAL: Signal<ThreadModeRawMutex, ()> = Signal::new();
|
||||
|
||||
pub async fn display_handler(
|
||||
spi: Spi<'static, SPI1, Async>,
|
||||
cs: PIN_13,
|
||||
data: PIN_14,
|
||||
reset: PIN_15,
|
||||
@@ -25,89 +39,49 @@ pub async fn display_task(
|
||||
spi_device,
|
||||
Output::new(data, Level::Low),
|
||||
Some(Output::new(reset, Level::High)),
|
||||
true,
|
||||
false,
|
||||
320,
|
||||
320,
|
||||
true,
|
||||
Delay,
|
||||
);
|
||||
display.init(&mut Delay).unwrap();
|
||||
display.set_orientation(&Orientation::Landscape).unwrap();
|
||||
let mut virtual_display = VirtualDisplay::new(display, 320 / 2, 320 / 2);
|
||||
let mut framebuffer: FrameBuffer<
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT,
|
||||
{ SCREEN_WIDTH * SCREEN_HEIGHT },
|
||||
> = FrameBuffer::new();
|
||||
display.init().await.unwrap();
|
||||
display.set_custom_orientation(0x40).await.unwrap();
|
||||
framebuffer.draw(&mut display).await.unwrap();
|
||||
display.set_on().await.unwrap();
|
||||
|
||||
let thin_stroke = PrimitiveStyle::with_stroke(Rgb565::RED, 20);
|
||||
|
||||
Rectangle::new(Point::new(10, 10), Size::new(100, 100))
|
||||
.into_styled(thin_stroke)
|
||||
.draw(&mut virtual_display)
|
||||
.unwrap();
|
||||
DISPLAY_SIGNAL.signal(());
|
||||
|
||||
loop {
|
||||
Timer::after_millis(500).await;
|
||||
}
|
||||
}
|
||||
DISPLAY_SIGNAL.wait().await;
|
||||
|
||||
/// simple abstraction over real display & resolution to reduce frame buffer size
|
||||
/// by cutting the resolution by 1/4
|
||||
struct VirtualDisplay {
|
||||
display: ST7365P<
|
||||
ExclusiveDevice<Spi<'static, SPI1, Blocking>, Output<'static>, Delay>,
|
||||
Output<'static>,
|
||||
Output<'static>,
|
||||
>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
let text_string = crate::STRING.lock().await.clone();
|
||||
|
||||
impl VirtualDisplay {
|
||||
pub fn new(
|
||||
display: ST7365P<
|
||||
ExclusiveDevice<Spi<'static, SPI1, Blocking>, Output<'static>, Delay>,
|
||||
Output<'static>,
|
||||
Output<'static>,
|
||||
>,
|
||||
new_width: u32,
|
||||
new_height: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
display,
|
||||
width: new_width,
|
||||
height: new_height,
|
||||
}
|
||||
}
|
||||
}
|
||||
let text = Text::with_alignment(
|
||||
&text_string,
|
||||
Point::new(160, 160),
|
||||
MonoTextStyle::new(&FONT_10X20, Rgb565::RED),
|
||||
Alignment::Center,
|
||||
);
|
||||
|
||||
impl DrawTarget for VirtualDisplay {
|
||||
type Color = Rgb565;
|
||||
type Error = ();
|
||||
|
||||
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
|
||||
where
|
||||
I: IntoIterator<Item = Pixel<Self::Color>>,
|
||||
{
|
||||
for Pixel(coord, color) in pixels.into_iter() {
|
||||
// Check bounds on the *virtual* (already reduced) resolution
|
||||
if coord.x >= 0
|
||||
&& coord.y >= 0
|
||||
&& coord.x < self.width as i32
|
||||
&& coord.y < self.height as i32
|
||||
{
|
||||
let px = coord.x as u16 * 2;
|
||||
let py = coord.y as u16 * 2;
|
||||
let raw_color = RawU16::from(color).into_inner();
|
||||
|
||||
// Draw the 2x2 block on the underlying hardware
|
||||
self.display.set_pixel(px, py, raw_color)?;
|
||||
self.display.set_pixel(px + 1, py, raw_color)?;
|
||||
self.display.set_pixel(px, py + 1, raw_color)?;
|
||||
self.display.set_pixel(px + 1, py + 1, raw_color)?;
|
||||
{
|
||||
let rect = LAST_TEXT_RECT.lock().await;
|
||||
if let Some(rect) = *rect.borrow() {
|
||||
framebuffer.fill_solid(&rect, Rgb565::BLACK).unwrap();
|
||||
}
|
||||
*rect.borrow_mut() = Some(text.bounding_box());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl OriginDimensions for VirtualDisplay {
|
||||
fn size(&self) -> Size {
|
||||
Size::new(self.width, self.height)
|
||||
text.draw(&mut framebuffer).unwrap();
|
||||
|
||||
let start = Instant::now();
|
||||
framebuffer
|
||||
.partial_draw_batched(&mut display)
|
||||
.await
|
||||
.unwrap();
|
||||
info!("Elapsed {}ms", start.elapsed().as_millis());
|
||||
}
|
||||
}
|
||||
+56
-31
@@ -1,57 +1,82 @@
|
||||
#![feature(impl_trait_in_assoc_type)]
|
||||
#![feature(ascii_char)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
use defmt::*;
|
||||
use crate::{
|
||||
display::DISPLAY_SIGNAL,
|
||||
peripherals::keyboard::{KeyCode, KeyState, read_keyboard_fifo},
|
||||
};
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
use core::cell::RefCell;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::join::join;
|
||||
use embassy_rp::peripherals::I2C1;
|
||||
use embassy_rp::spi::Spi;
|
||||
use embassy_rp::{
|
||||
bind_interrupts,
|
||||
gpio::{Level, Output},
|
||||
i2c,
|
||||
i2c::I2c,
|
||||
spi,
|
||||
};
|
||||
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
||||
use embassy_sync::channel::Channel;
|
||||
use embassy_rp::{i2c, i2c::I2c, spi};
|
||||
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_time::Timer;
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use embedded_sdmmc::asynchronous::{File, SdCard, ShortFileName, VolumeIdx, VolumeManager};
|
||||
use static_cell::StaticCell;
|
||||
use embedded_graphics::primitives::Rectangle;
|
||||
use heapless::String;
|
||||
|
||||
mod peripherals;
|
||||
use peripherals::{keyboard::KeyEvent, peripherals_task};
|
||||
use peripherals::conf_peripherals;
|
||||
mod display;
|
||||
use display::display_task;
|
||||
use display::display_handler;
|
||||
|
||||
embassy_rp::bind_interrupts!(struct Irqs {
|
||||
I2C1_IRQ => i2c::InterruptHandler<I2C1>;
|
||||
});
|
||||
|
||||
static STRING: Mutex<ThreadModeRawMutex, String<25>> = Mutex::new(String::new());
|
||||
static LAST_TEXT_RECT: Mutex<ThreadModeRawMutex, RefCell<Option<Rectangle>>> =
|
||||
Mutex::new(RefCell::new(None));
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_rp::init(Default::default());
|
||||
|
||||
static KEYBOARD_EVENTS: StaticCell<Channel<NoopRawMutex, KeyEvent, 10>> = StaticCell::new();
|
||||
let keyboard_events = KEYBOARD_EVENTS.init(Channel::new());
|
||||
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);
|
||||
spawner
|
||||
.spawn(peripherals_task(i2c1, keyboard_events.sender()))
|
||||
.unwrap();
|
||||
conf_peripherals(i2c1).await;
|
||||
|
||||
// // configure display handler
|
||||
// let mut config = spi::Config::default();
|
||||
// config.frequency = 16_000_000;
|
||||
// let spi1 = spi::Spi::new_blocking(p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, config);
|
||||
// spawner
|
||||
// .spawn(display_task(spi1, p.PIN_13, p.PIN_14, p.PIN_15))
|
||||
// .unwrap();
|
||||
let mut config = spi::Config::default();
|
||||
config.frequency = 16_000_000;
|
||||
let spi1 = spi::Spi::new(
|
||||
p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, p.DMA_CH0, p.DMA_CH1, config,
|
||||
);
|
||||
|
||||
join(
|
||||
async {
|
||||
loop {
|
||||
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(());
|
||||
}
|
||||
}
|
||||
},
|
||||
display_handler(spi1, p.PIN_13, p.PIN_14, p.PIN_15),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -1,12 +1,3 @@
|
||||
use defmt::{info, warn};
|
||||
use embassy_rp::{
|
||||
i2c::{Async, I2c},
|
||||
peripherals::I2C1,
|
||||
};
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
|
||||
channel::Sender,
|
||||
};
|
||||
|
||||
use crate::peripherals::PERIPHERAL_BUS;
|
||||
|
||||
@@ -17,7 +8,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<KeyEvent> {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
@@ -40,16 +31,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;
|
||||
|
||||
+8
-16
@@ -5,13 +5,10 @@ use embassy_rp::{
|
||||
i2c::{Async, I2c},
|
||||
peripherals::I2C1,
|
||||
};
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::NoopRawMutex, channel::Sender, lazy_lock::LazyLock, mutex::Mutex,
|
||||
};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, lazy_lock::LazyLock, mutex::Mutex};
|
||||
use embassy_time::Timer;
|
||||
|
||||
pub mod keyboard;
|
||||
use keyboard::{KeyCode, KeyEvent, KeyState};
|
||||
|
||||
use crate::peripherals::keyboard::{configure_keyboard, read_keyboard_fifo};
|
||||
|
||||
@@ -25,23 +22,18 @@ 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);
|
||||
|
||||
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;
|
||||
}
|
||||
// empty keys
|
||||
while read_keyboard_fifo().await.is_some() {}
|
||||
|
||||
// set_lcd_backlight(255).await;
|
||||
set_key_backlight(0).await;
|
||||
}
|
||||
|
||||
/// return major & minor mcu version
|
||||
|
||||
Reference in new issue
Block a user