Compare commits
8 Commits
working-di
...
display-fr
| Author | SHA1 | Date | |
|---|---|---|---|
| 06a158623d | |||
| a687122696 | |||
| a42cfb6bc5 | |||
| cf479cc00a | |||
| 5e537be5a3 | |||
| b3e721a8be | |||
| 8aa7a40fa5 | |||
| 8f3c3053c8 |
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -1336,6 +1336,7 @@ checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
||||
name = "picocalc-os-rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bitflags 2.9.1",
|
||||
"bt-hci",
|
||||
"cortex-m",
|
||||
"cortex-m-rt",
|
||||
@@ -1356,8 +1357,10 @@ dependencies = [
|
||||
"embedded-sdmmc",
|
||||
"panic-probe",
|
||||
"portable-atomic",
|
||||
"spin",
|
||||
"st7365p-lcd",
|
||||
"static_cell",
|
||||
"talc",
|
||||
"trouble-host",
|
||||
]
|
||||
|
||||
@@ -1726,13 +1729,22 @@ dependencies = [
|
||||
"rgb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "st7365p-lcd"
|
||||
version = "0.10.0"
|
||||
source = "git+https://github.com/legitcamper/st7365p-lcd-rs#d751e8d30f1a3f964ffe05e4bb16f82112fbefce"
|
||||
dependencies = [
|
||||
"embedded-graphics-core",
|
||||
"embedded-hal 1.0.0",
|
||||
"embedded-hal-async",
|
||||
"nb 1.1.0",
|
||||
]
|
||||
|
||||
@@ -1791,6 +1803,15 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "talc"
|
||||
version = "4.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a3ae828aa394de34c7de08f522d1b86bd1c182c668d27da69caadda00590f26d"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term"
|
||||
version = "0.7.0"
|
||||
|
||||
@@ -71,7 +71,10 @@ defmt-rtt = "0.4.2"
|
||||
|
||||
embedded-graphics = { version = "0.8.1" }
|
||||
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" }
|
||||
# st7365p-lcd = { path = "../ST7365P-lcd-rs" }
|
||||
# st7365p-lcd = { git = "https://github.com/legitcamper/st7365p-lcd-rs" }
|
||||
st7365p-lcd = { path = "../ST7365P-lcd-rs" }
|
||||
|
||||
static_cell = "2.1.1"
|
||||
bitflags = "2.9.1"
|
||||
talc = "4.4.3"
|
||||
spin = "0.10.0"
|
||||
|
||||
2
memory.x
2
memory.x
@@ -4,7 +4,7 @@ MEMORY {
|
||||
*
|
||||
* 2 MiB is a safe default here, although a Pico 2 has 4 MiB.
|
||||
*/
|
||||
FLASH : ORIGIN = 0x10000000, LENGTH = 2048K
|
||||
FLASH : ORIGIN = 0x10000000, LENGTH = 4096K
|
||||
/*
|
||||
* RAM consists of 8 banks, SRAM0-SRAM7, with a striped mapping.
|
||||
* This is usually good for performance, as it distributes load on
|
||||
|
||||
137
src/display.rs
137
src/display.rs
@@ -1,46 +1,141 @@
|
||||
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 embedded_graphics::{
|
||||
Drawable,
|
||||
pixelcolor::{BinaryColor, Rgb555, Rgb565},
|
||||
prelude::{Point, Primitive, RgbColor, Size},
|
||||
primitives::{PrimitiveStyle, Rectangle, Triangle},
|
||||
mono_font::{MonoFont, MonoTextStyle, ascii::FONT_10X20},
|
||||
pixelcolor::Rgb565,
|
||||
prelude::{Point, WebColors},
|
||||
text::{Baseline, Text, TextStyle},
|
||||
};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use st7365p_lcd::{Orientation, ST7365P};
|
||||
use st7365p_lcd::{FrameBuffer, ST7365P};
|
||||
|
||||
type SPI = Spi<'static, SPI1, Async>;
|
||||
|
||||
type FRAMEBUFFER = FrameBuffer<
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT,
|
||||
ExclusiveDevice<Spi<'static, SPI1, Async>, Output<'static>, Delay>,
|
||||
Output<'static>,
|
||||
Output<'static>,
|
||||
>;
|
||||
|
||||
const SCREEN_WIDTH: usize = 320;
|
||||
const SCREEN_HEIGHT: usize = 320;
|
||||
const SCREEN_ROWS: usize = 15;
|
||||
const SCREEN_COLS: usize = 31;
|
||||
const FONT: MonoFont = FONT_10X20;
|
||||
const COLOR: Rgb565 = Rgb565::CSS_LAWN_GREEN;
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn display_task(
|
||||
spi: Spi<'static, SPI1, Blocking>,
|
||||
cs: PIN_13,
|
||||
data: PIN_14,
|
||||
reset: PIN_15,
|
||||
) {
|
||||
pub async fn display_task(spi: SPI, cs: PIN_13, data: PIN_14, reset: PIN_15) {
|
||||
let spi_device = ExclusiveDevice::new(spi, Output::new(cs, Level::Low), Delay).unwrap();
|
||||
let mut display = ST7365P::new(
|
||||
let display = ST7365P::new(
|
||||
spi_device,
|
||||
Output::new(data, Level::Low),
|
||||
Some(Output::new(reset, Level::High)),
|
||||
false,
|
||||
true,
|
||||
320,
|
||||
320,
|
||||
SCREEN_WIDTH as u32,
|
||||
SCREEN_HEIGHT as u32,
|
||||
);
|
||||
display.init(&mut Delay).unwrap();
|
||||
display.set_orientation(&Orientation::Landscape).unwrap();
|
||||
let mut framebuffer: FRAMEBUFFER = FrameBuffer::new(display);
|
||||
|
||||
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 display)
|
||||
framebuffer.init(&mut Delay).await.unwrap();
|
||||
framebuffer.display.set_offset(0, 0);
|
||||
framebuffer
|
||||
.display
|
||||
.set_custom_orientation(0x60)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut textbuffer = TextBuffer::new();
|
||||
textbuffer.fill('A');
|
||||
textbuffer.draw(&mut framebuffer);
|
||||
info!("finished rendering");
|
||||
|
||||
loop {
|
||||
framebuffer.draw().await.unwrap();
|
||||
Timer::after_millis(500).await;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Cursor {
|
||||
x: u16,
|
||||
y: u16,
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
fn new(x: u16, y: u16) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TextBuffer {
|
||||
grid: [[Option<char>; SCREEN_COLS]; SCREEN_ROWS],
|
||||
cursor: Cursor,
|
||||
}
|
||||
|
||||
impl TextBuffer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
grid: [[None; SCREEN_COLS]; SCREEN_ROWS],
|
||||
cursor: Cursor { x: 0, y: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
/// writes char at cursor
|
||||
pub fn write_char(&mut self, ch: char) {
|
||||
for (i, row) in self.grid.iter_mut().enumerate() {
|
||||
for (j, col) in row.iter_mut().enumerate() {
|
||||
if i as u16 == self.cursor.x && j as u16 == self.cursor.y {
|
||||
*col = Some(ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// fills text buffer with char
|
||||
pub fn fill(&mut self, ch: char) {
|
||||
for i in 0..SCREEN_ROWS {
|
||||
for j in 0..SCREEN_COLS {
|
||||
self.cursor = Cursor::new(i as u16, j as u16);
|
||||
self.write_char(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_up(&mut self) {
|
||||
let (top, bottom) = self.grid.split_at_mut(SCREEN_ROWS - 1);
|
||||
for (dest, src) in top.iter_mut().zip(&bottom[1..]) {
|
||||
dest.copy_from_slice(src);
|
||||
}
|
||||
|
||||
self.grid[SCREEN_ROWS - 1].fill(None);
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, target: &mut FRAMEBUFFER) {
|
||||
let baseline = TextStyle::with_baseline(Baseline::Top);
|
||||
let style = MonoTextStyle::new(&FONT, COLOR);
|
||||
|
||||
for (i, row) in self.grid.iter().enumerate() {
|
||||
for (j, cell) in row.iter().enumerate() {
|
||||
if let Some(ch) = cell {
|
||||
let pos = Point::new(
|
||||
(j as i32) * FONT.character_size.width as i32,
|
||||
(i as i32) * FONT.character_size.height as i32 + baseline.baseline as i32,
|
||||
);
|
||||
|
||||
Text::new(ch.as_ascii().unwrap().as_str(), pos, style)
|
||||
.draw(target)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
src/main.rs
24
src/main.rs
@@ -1,4 +1,5 @@
|
||||
#![feature(impl_trait_in_assoc_type)]
|
||||
#![feature(ascii_char)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
@@ -39,24 +40,21 @@ async fn main(spawner: Spawner) {
|
||||
static KEYBOARD_EVENTS: StaticCell<Channel<NoopRawMutex, KeyEvent, 10>> = StaticCell::new();
|
||||
let keyboard_events = KEYBOARD_EVENTS.init(Channel::new());
|
||||
|
||||
// configure keyboard event handler
|
||||
let config = i2c::Config::default();
|
||||
let i2c1 = I2c::new_async(p.I2C1, p.PIN_7, p.PIN_6, Irqs, config);
|
||||
spawner
|
||||
.spawn(peripherals_task(i2c1, keyboard_events.sender()))
|
||||
.unwrap();
|
||||
// // 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()))
|
||||
// .unwrap();
|
||||
|
||||
// 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);
|
||||
let spi1 = spi::Spi::new(
|
||||
p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, p.DMA_CH0, p.DMA_CH1, config,
|
||||
);
|
||||
spawner
|
||||
.spawn(display_task(spi1, p.PIN_13, p.PIN_14, p.PIN_15))
|
||||
.unwrap();
|
||||
|
||||
let receiver = keyboard_events.receiver();
|
||||
loop {
|
||||
let key = receiver.receive().await;
|
||||
info!("got key: {}", key.key as u8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
use embassy_rp::{
|
||||
i2c::{Async, I2c},
|
||||
peripherals::I2C1,
|
||||
};
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex, watch::Watch};
|
||||
|
||||
const REG_ID_BAT: u8 = 0x0b;
|
||||
|
||||
pub static BATTERY_PCT: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new();
|
||||
|
||||
pub async fn read_battery(i2c: &mut I2c<'static, I2C1, Async>) {
|
||||
let mut buf = [0_u8; 2];
|
||||
i2c.write_read_async(super::MCU_ADDR, [REG_ID_BAT], &mut buf)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if buf[0] == REG_ID_BAT {
|
||||
BATTERY_PCT.sender().send(buf[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use defmt::{info, warn};
|
||||
use embassy_rp::{
|
||||
i2c::{Async, I2c},
|
||||
peripherals::I2C1,
|
||||
@@ -7,6 +8,8 @@ use embassy_sync::{
|
||||
channel::Sender,
|
||||
};
|
||||
|
||||
use crate::peripherals::PERIPHERAL_BUS;
|
||||
|
||||
const REG_ID_KEY: u8 = 0x04;
|
||||
const REG_ID_FIF: u8 = 0x09;
|
||||
|
||||
@@ -14,10 +17,10 @@ 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(
|
||||
i2c: &mut I2c<'static, I2C1, Async>,
|
||||
channel: &mut Sender<'static, NoopRawMutex, KeyEvent, 10>,
|
||||
) {
|
||||
pub async fn read_keyboard_fifo(channel: &mut Sender<'static, NoopRawMutex, KeyEvent, 10>) {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let mut key_status = [0_u8; 1];
|
||||
|
||||
if i2c
|
||||
@@ -25,9 +28,8 @@ pub async fn read_keyboard_fifo(
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
// TODO: use caps & num lock
|
||||
let caps = key_status[0] & KEY_CAPSLOCK == KEY_CAPSLOCK;
|
||||
let num = key_status[0] & KEY_NUMLOCK == KEY_NUMLOCK;
|
||||
let _caps = key_status[0] & KEY_CAPSLOCK == KEY_CAPSLOCK;
|
||||
let _num = key_status[0] & KEY_NUMLOCK == KEY_NUMLOCK;
|
||||
let fifo_count = key_status[0] & KEY_COUNT_MASK;
|
||||
|
||||
if fifo_count >= 1 {
|
||||
@@ -38,78 +40,99 @@ pub async fn read_keyboard_fifo(
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
if let Ok(state) = KeyState::try_from(event[0]) {
|
||||
if let Ok(key) = KeyCode::try_from(event[1]) {
|
||||
let _ = channel.try_send(KeyEvent { key, state });
|
||||
}
|
||||
}
|
||||
channel
|
||||
.try_send(KeyEvent {
|
||||
state: KeyState::from(event[0]),
|
||||
key: KeyCode::from(event[1]),
|
||||
mods: Modifiers::NONE,
|
||||
})
|
||||
.expect("Failed to push key");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const REG_ID_DEB: u8 = 0x06;
|
||||
const REG_ID_FRQ: u8 = 0x07;
|
||||
|
||||
pub async fn configure_keyboard(debounce: u8, poll_freq: u8) {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let _ = i2c
|
||||
.write_read_async(super::MCU_ADDR, [REG_ID_DEB], &mut [debounce])
|
||||
.await;
|
||||
|
||||
let _ = i2c
|
||||
.write_read_async(super::MCU_ADDR, [REG_ID_FRQ], &mut [poll_freq])
|
||||
.await;
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Modifiers: u8 {
|
||||
const NONE = 0;
|
||||
const CTRL = 1;
|
||||
const ALT = 2;
|
||||
const LSHIFT = 4;
|
||||
const RSHIFT = 8;
|
||||
const SYM = 16;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct KeyEvent {
|
||||
pub key: KeyCode,
|
||||
pub state: KeyState,
|
||||
pub mods: Modifiers,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum KeyState {
|
||||
Idle = 0,
|
||||
Pressed,
|
||||
Hold,
|
||||
Released,
|
||||
Pressed = 1,
|
||||
Hold = 2,
|
||||
Released = 3,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for KeyState {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
impl From<u8> for KeyState {
|
||||
fn from(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Ok(KeyState::Idle),
|
||||
1 => Ok(KeyState::Pressed),
|
||||
2 => Ok(KeyState::Hold),
|
||||
3 => Ok(KeyState::Released),
|
||||
_ => Err(()),
|
||||
1 => KeyState::Pressed,
|
||||
2 => KeyState::Hold,
|
||||
3 => KeyState::Released,
|
||||
0 | _ => KeyState::Idle,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum KeyCode {
|
||||
// Joystick
|
||||
JoyUp = 0x01,
|
||||
JoyDown = 0x02,
|
||||
JoyLeft = 0x03,
|
||||
JoyRight = 0x04,
|
||||
JoyCenter = 0x05,
|
||||
|
||||
// Buttons
|
||||
BtnLeft1 = 0x06,
|
||||
BtnRight1 = 0x07,
|
||||
BtnLeft2 = 0x11,
|
||||
BtnRight2 = 0x12,
|
||||
|
||||
// Basic Keys
|
||||
Backspace = 0x08,
|
||||
Tab = 0x09,
|
||||
Enter = 0x0A,
|
||||
|
||||
// Modifiers
|
||||
ModAlt = 0xA1,
|
||||
ModShiftLeft = 0xA2,
|
||||
ModShiftRight = 0xA3,
|
||||
ModSym = 0xA4,
|
||||
ModCtrl = 0xA5,
|
||||
|
||||
// Navigation
|
||||
Esc = 0xB1,
|
||||
Left = 0xB4,
|
||||
Up = 0xB5,
|
||||
Down = 0xB6,
|
||||
Right = 0xB7,
|
||||
|
||||
// Specials
|
||||
Break = 0xD0,
|
||||
Insert = 0xD1,
|
||||
Home = 0xD2,
|
||||
@@ -117,11 +140,7 @@ pub enum KeyCode {
|
||||
End = 0xD5,
|
||||
PageUp = 0xD6,
|
||||
PageDown = 0xD7,
|
||||
|
||||
// Locks
|
||||
CapsLock = 0xC1,
|
||||
|
||||
// Function keys
|
||||
F1 = 0x81,
|
||||
F2 = 0x82,
|
||||
F3 = 0x83,
|
||||
@@ -132,55 +151,57 @@ pub enum KeyCode {
|
||||
F8 = 0x88,
|
||||
F9 = 0x89,
|
||||
F10 = 0x90,
|
||||
Char(char),
|
||||
Unknown(u8),
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for KeyCode {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
use KeyCode::*;
|
||||
impl From<u8> for KeyCode {
|
||||
fn from(value: u8) -> Self {
|
||||
match value {
|
||||
0x01 => Ok(JoyUp),
|
||||
0x02 => Ok(JoyDown),
|
||||
0x03 => Ok(JoyLeft),
|
||||
0x04 => Ok(JoyRight),
|
||||
0x05 => Ok(JoyCenter),
|
||||
0x06 => Ok(BtnLeft1),
|
||||
0x07 => Ok(BtnRight1),
|
||||
0x08 => Ok(Backspace),
|
||||
0x09 => Ok(Tab),
|
||||
0x0A => Ok(Enter),
|
||||
0x11 => Ok(BtnLeft2),
|
||||
0x12 => Ok(BtnRight2),
|
||||
0xA1 => Ok(ModAlt),
|
||||
0xA2 => Ok(ModShiftLeft),
|
||||
0xA3 => Ok(ModShiftRight),
|
||||
0xA4 => Ok(ModSym),
|
||||
0xA5 => Ok(ModCtrl),
|
||||
0xB1 => Ok(Esc),
|
||||
0xB4 => Ok(Left),
|
||||
0xB5 => Ok(Up),
|
||||
0xB6 => Ok(Down),
|
||||
0xB7 => Ok(Right),
|
||||
0xC1 => Ok(CapsLock),
|
||||
0xD0 => Ok(Break),
|
||||
0xD1 => Ok(Insert),
|
||||
0xD2 => Ok(Home),
|
||||
0xD4 => Ok(Del),
|
||||
0xD5 => Ok(End),
|
||||
0xD6 => Ok(PageUp),
|
||||
0xD7 => Ok(PageDown),
|
||||
0x81 => Ok(F1),
|
||||
0x82 => Ok(F2),
|
||||
0x83 => Ok(F3),
|
||||
0x84 => Ok(F4),
|
||||
0x85 => Ok(F5),
|
||||
0x86 => Ok(F6),
|
||||
0x87 => Ok(F7),
|
||||
0x88 => Ok(F8),
|
||||
0x89 => Ok(F9),
|
||||
0x90 => Ok(F10),
|
||||
_ => Err(()),
|
||||
0x01 => Self::JoyUp,
|
||||
0x02 => Self::JoyDown,
|
||||
0x03 => Self::JoyLeft,
|
||||
0x04 => Self::JoyRight,
|
||||
0x05 => Self::JoyCenter,
|
||||
0x06 => Self::BtnLeft1,
|
||||
0x07 => Self::BtnRight1,
|
||||
0x08 => Self::Backspace,
|
||||
0x09 => Self::Tab,
|
||||
0x0A => Self::Enter,
|
||||
0x11 => Self::BtnLeft2,
|
||||
0x12 => Self::BtnRight2,
|
||||
0xA1 => Self::ModAlt,
|
||||
0xA2 => Self::ModShiftLeft,
|
||||
0xA3 => Self::ModShiftRight,
|
||||
0xA4 => Self::ModSym,
|
||||
0xA5 => Self::ModCtrl,
|
||||
0xB1 => Self::Esc,
|
||||
0xB4 => Self::Left,
|
||||
0xB5 => Self::Up,
|
||||
0xB6 => Self::Down,
|
||||
0xB7 => Self::Right,
|
||||
0xC1 => Self::CapsLock,
|
||||
0xD0 => Self::Break,
|
||||
0xD1 => Self::Insert,
|
||||
0xD2 => Self::Home,
|
||||
0xD4 => Self::Del,
|
||||
0xD5 => Self::End,
|
||||
0xD6 => Self::PageUp,
|
||||
0xD7 => Self::PageDown,
|
||||
0x81 => Self::F1,
|
||||
0x82 => Self::F2,
|
||||
0x83 => Self::F3,
|
||||
0x84 => Self::F4,
|
||||
0x85 => Self::F5,
|
||||
0x86 => Self::F6,
|
||||
0x87 => Self::F7,
|
||||
0x88 => Self::F8,
|
||||
0x89 => Self::F9,
|
||||
0x90 => Self::F10,
|
||||
_ => match char::from_u32(value as u32) {
|
||||
Some(c) => Self::Char(c),
|
||||
None => Self::Unknown(value),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +1,106 @@
|
||||
//! handles all the peripherals exposed by mcu through i2c (keyboard & battery registers)
|
||||
//!
|
||||
|
||||
use embassy_futures::join::join;
|
||||
use embassy_rp::{
|
||||
i2c::{Async, I2c},
|
||||
peripherals::I2C1,
|
||||
};
|
||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Sender, mutex::Mutex};
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::NoopRawMutex, channel::Sender, lazy_lock::LazyLock, mutex::Mutex,
|
||||
};
|
||||
use embassy_time::{Duration, Timer};
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
use defmt::info;
|
||||
|
||||
pub mod keyboard;
|
||||
use keyboard::{KeyCode, KeyEvent, KeyState};
|
||||
mod battery;
|
||||
pub use battery::BATTERY_PCT;
|
||||
use battery::read_battery;
|
||||
|
||||
use crate::peripherals::keyboard::read_keyboard_fifo;
|
||||
use crate::peripherals::keyboard::{configure_keyboard, read_keyboard_fifo};
|
||||
|
||||
const MCU_ADDR: u8 = 0x1F;
|
||||
|
||||
type I2CBUS = I2c<'static, I2C1, Async>;
|
||||
pub static PERIPHERAL_BUS: LazyLock<Mutex<NoopRawMutex, Option<I2CBUS>>> =
|
||||
LazyLock::new(|| Mutex::new(None));
|
||||
|
||||
const REG_ID_VER: u8 = 0x01;
|
||||
const REG_ID_CFG: u8 = 0x02;
|
||||
const REG_ID_INT: u8 = 0x03;
|
||||
const REG_ID_KEY: u8 = 0x04;
|
||||
const REG_ID_BKL: u8 = 0x05;
|
||||
const REG_ID_DEB: u8 = 0x06;
|
||||
const REG_ID_FRQ: u8 = 0x07;
|
||||
const REG_ID_RST: u8 = 0x08;
|
||||
const REG_ID_FIF: u8 = 0x09;
|
||||
const REG_ID_BK2: u8 = 0x0A;
|
||||
const REG_ID_C64_MTX: u8 = 0x0c;
|
||||
const REG_ID_C64_JS: u8 = 0x0d;
|
||||
const REG_ID_INT: u8 = 0x03;
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn peripherals_task(
|
||||
mut i2c: I2c<'static, I2C1, Async>,
|
||||
i2c: I2CBUS,
|
||||
mut keyboard_channel: Sender<'static, NoopRawMutex, KeyEvent, 10>,
|
||||
) {
|
||||
Timer::after(embassy_time::Duration::from_millis(100)).await;
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
{
|
||||
let mut ver = [0_u8; 1];
|
||||
if let Ok(firm_ver) = i2c.write_read_async(MCU_ADDR, [REG_ID_VER], &mut ver).await {
|
||||
info!("stm32 firmware version: v{}", ver[0]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
let i2c: Mutex<NoopRawMutex, I2c<'static, I2C1, Async>> = Mutex::new(i2c);
|
||||
|
||||
join(
|
||||
async {
|
||||
loop {
|
||||
Timer::after(Duration::from_secs(10)).await;
|
||||
let mut guard = i2c.lock().await;
|
||||
read_battery(&mut guard).await;
|
||||
}
|
||||
},
|
||||
async {
|
||||
loop {
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
let mut guard = i2c.lock().await;
|
||||
read_keyboard_fifo(&mut guard, &mut keyboard_channel).await;
|
||||
}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
/// return major & minor mcu version
|
||||
async fn get_version() -> (u8, u8) {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let mut ver = [0_u8; 1];
|
||||
let _ = i2c.write_read_async(MCU_ADDR, [REG_ID_VER], &mut ver).await;
|
||||
|
||||
(ver[0] >> 4, ver[0] & 0x0F)
|
||||
}
|
||||
|
||||
const REG_ID_BKL: u8 = 0x05;
|
||||
pub async fn set_lcd_backlight(brightness: u8) {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let _ = i2c
|
||||
.write_read_async(MCU_ADDR, [REG_ID_BKL], &mut [brightness])
|
||||
.await;
|
||||
}
|
||||
pub async fn get_lcd_backlight() -> u8 {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let mut buf = [0_u8; 2];
|
||||
|
||||
let _ = i2c.write_read_async(MCU_ADDR, [REG_ID_BKL], &mut buf).await;
|
||||
buf[1]
|
||||
}
|
||||
|
||||
const REG_ID_BK2: u8 = 0x0A;
|
||||
pub async fn set_key_backlight(brightness: u8) {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let _ = i2c
|
||||
.write_read_async(MCU_ADDR, [REG_ID_BK2], &mut [brightness])
|
||||
.await;
|
||||
}
|
||||
pub async fn get_key_backlight() -> u8 {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let mut buf = [0_u8; 2];
|
||||
|
||||
let _ = i2c.write_read_async(MCU_ADDR, [REG_ID_BK2], &mut buf).await;
|
||||
buf[1]
|
||||
}
|
||||
|
||||
const REG_ID_BAT: u8 = 0x0b;
|
||||
pub async fn get_battery() -> u8 {
|
||||
let mut i2c = PERIPHERAL_BUS.get().lock().await;
|
||||
let i2c = i2c.as_mut().unwrap();
|
||||
|
||||
let mut buf = [0_u8; 2];
|
||||
|
||||
let _ = i2c.write_read_async(MCU_ADDR, [REG_ID_BAT], &mut buf).await;
|
||||
|
||||
buf[1]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user