3 Commits

Author SHA1 Message Date
e95774ae16 WIP 2025-07-02 18:06:54 -06:00
bd7c4e3e27 WIP basic ui
fix
2025-06-30 21:43:20 -06:00
55b7c2970c add status bar to console 2025-06-30 19:37:55 -06:00
6 changed files with 268 additions and 176 deletions

55
Cargo.lock generated
View File

@@ -27,6 +27,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "arrform"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7cf566ecc5c9d82b973e81d30babf6583c9b497f86295c952d538c3254ef4e6"
[[package]]
name = "ascii-canvas"
version = "3.0.0"
@@ -759,6 +765,27 @@ dependencies = [
"embedded-io",
]
[[package]]
name = "embedded-layout"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a90553247f2b05c59ac7894ea13d830636c2b1203fa03bff400eddbd1fa9f52"
dependencies = [
"embedded-graphics",
"embedded-layout-macros",
]
[[package]]
name = "embedded-layout-macros"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f6e621fe4c7e05b695274b722dc0a60bacd1c8696b58191baa0154713d52400"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.104",
]
[[package]]
name = "embedded-sdmmc"
version = "0.8.0"
@@ -1006,9 +1033,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "indexmap"
version = "2.9.0"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"
checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661"
dependencies = [
"equivalent",
"hashbrown",
@@ -1336,6 +1363,7 @@ checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
name = "picocalc-os-rs"
version = "0.1.0"
dependencies = [
"arrform",
"bitflags 2.9.1",
"bt-hci",
"cortex-m",
@@ -1352,15 +1380,16 @@ dependencies = [
"embassy-time",
"embedded-graphics",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"embedded-hal-async",
"embedded-hal-bus",
"embedded-layout",
"embedded-sdmmc",
"heapless",
"panic-probe",
"portable-atomic",
"spin",
"st7365p-lcd",
"static_cell",
"talc",
"trouble-host",
]
@@ -1729,15 +1758,6 @@ 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"
@@ -1803,15 +1823,6 @@ 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"

View File

@@ -60,21 +60,22 @@ cyw43-pio = { version = "0.3.0", optional = true }
embedded-hal-bus = { version = "0.3.0", features = ["async"] }
embedded-hal = "0.2.7"
embedded-hal-1 = { version = "1.0.0", package = "embedded-hal" }
embedded-hal-async = "1.0.0"
cortex-m = { version = "0.7.7" }
cortex-m-rt = "0.7.5"
panic-probe = "0.3"
portable-atomic = { version = "1.11", features = ["critical-section"] }
static_cell = "2.1.1"
bitflags = "2.9.1"
heapless = "0.8.0"
arrform = "0.1.1"
defmt = { version = "0.3", optional = true }
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" }
# st7365p-lcd = { git = "https://github.com/legitcamper/st7365p-lcd-rs", branch = "async" }
st7365p-lcd = { path = "../ST7365P-lcd-rs" }
static_cell = "2.1.1"
bitflags = "2.9.1"
talc = "4.4.3"
spin = "0.10.0"

View File

@@ -1,3 +1,5 @@
use arrform::{ArrForm, arrform};
use core::fmt::Debug;
use defmt::info;
use embassy_rp::{
gpio::{Level, Output},
@@ -7,17 +9,35 @@ use embassy_rp::{
use embassy_time::{Delay, Timer};
use embedded_graphics::{
Drawable,
mono_font::{MonoFont, MonoTextStyle, ascii::FONT_10X20},
draw_target::DrawTarget,
mono_font::{
MonoTextStyle,
ascii::{FONT_6X10, FONT_9X15, FONT_10X20},
},
pixelcolor::Rgb565,
prelude::{Point, WebColors},
text::{Baseline, Text, TextStyle},
prelude::{Point, RgbColor, Size},
primitives::{PrimitiveStyle, Rectangle, StyledDrawable},
text::Text,
};
use embedded_hal_bus::spi::ExclusiveDevice;
use st7365p_lcd::{FrameBuffer, ST7365P};
use embedded_hal_1::digital::OutputPin;
use embedded_hal_async::spi::SpiDevice;
use embedded_hal_bus::spi::{ExclusiveDevice, NoDelay};
use embedded_layout::{
align::{horizontal, vertical},
layout::linear::LinearLayout,
object_chain::Chain,
prelude::*,
};
use heapless::{String, Vec};
use st7365p_lcd::{FrameBuffer, Orientation, ST7365P};
type SPI = Spi<'static, SPI1, Async>;
pub const SCREEN_WIDTH: usize = 320;
pub const SCREEN_HEIGHT: usize = 320;
type FRAMEBUFFER = FrameBuffer<
pub const STATUS_BAR_WIDTH: usize = 320;
pub const STATUS_BAR_HEIGHT: usize = 40;
pub type FRAMEBUFFER = FrameBuffer<
SCREEN_WIDTH,
SCREEN_HEIGHT,
ExclusiveDevice<Spi<'static, SPI1, Async>, Output<'static>, Delay>,
@@ -25,117 +45,136 @@ type FRAMEBUFFER = FrameBuffer<
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, cs: PIN_13, data: PIN_14, reset: PIN_15) {
let spi_device = ExclusiveDevice::new(spi, Output::new(cs, Level::Low), Delay).unwrap();
let display = ST7365P::new(
spi_device,
Output::new(data, Level::Low),
Some(Output::new(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_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 UI<const MAX_SELECTIONS: usize, const MAX_STR_LEN: usize> {
pub status_bar: StatusBar,
pub selections_list: SelectionList<MAX_SELECTIONS, MAX_STR_LEN>,
}
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 {
impl<const MAX_SELECTIONS: usize, const MAX_STR_LEN: usize> UI<MAX_SELECTIONS, MAX_STR_LEN> {
pub fn new() -> Self {
Self {
grid: [[None; SCREEN_COLS]; SCREEN_ROWS],
cursor: Cursor { x: 0, y: 0 },
status_bar: StatusBar {
battery: 100,
backlight: 100,
volume: 100,
},
selections_list: SelectionList::new(Vec::new()),
}
}
/// 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)
}
}
pub fn draw<D: DrawTarget<Color = Rgb565>>(&mut self, target: &mut D)
where
<D as DrawTarget>::Error: Debug,
{
self.draw_status_bar(target);
self.draw_selection(target);
}
fn draw_selection<D: DrawTarget<Color = Rgb565>>(&mut self, target: &mut D) {
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
let selection = Rectangle::new(
Point::new(0, STATUS_BAR_HEIGHT as i32 + 1),
Size::new(
SCREEN_WIDTH as u32,
(SCREEN_HEIGHT - STATUS_BAR_HEIGHT) as u32 - 1,
),
);
let _ = if self.selections_list.selections.is_empty() {
LinearLayout::horizontal(Chain::new(Text::new(
"No Programs found on SD Card\nEnsure programs end with '.rhai',\nand are located in the root directory",
Point::zero(),
text_style,
)))
.arrange()
.align_to(&selection, horizontal::Center, vertical::Center).draw(target)
} else {
LinearLayout::horizontal(
Chain::new(Text::new(
arrform!(20, "Bat: {}", self.status_bar.battery).as_str(),
Point::zero(),
text_style,
))
.append(Text::new(" ", Point::zero(), text_style))
.append(Text::new(
arrform!(20, "Lght: {}", self.status_bar.backlight).as_str(),
Point::zero(),
text_style,
))
.append(Text::new(" ", Point::zero(), text_style))
.append(Text::new(
arrform!(20, "Vol: {}", self.status_bar.volume).as_str(),
Point::zero(),
text_style,
)),
)
.arrange()
.align_to(&selection, horizontal::Left, vertical::Center)
.draw(target)
};
}
fn draw_status_bar<D: DrawTarget<Color = Rgb565>>(&mut self, target: &mut D) {
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
let status_bar = Rectangle::new(
Point::new(0, 0),
Size::new(STATUS_BAR_WIDTH as u32, STATUS_BAR_HEIGHT as u32),
);
let _ = LinearLayout::horizontal(
Chain::new(Text::new(
arrform!(20, "Bat: {}", self.status_bar.battery).as_str(),
Point::zero(),
text_style,
))
.append(Text::new(
arrform!(20, "Lght: {}", self.status_bar.backlight).as_str(),
Point::zero(),
text_style,
))
.append(Text::new(
arrform!(20, "Vol: {}", self.status_bar.volume).as_str(),
Point::zero(),
text_style,
)),
)
.arrange()
.align_to(&status_bar, horizontal::Center, vertical::Center)
.draw(target);
}
}
pub struct StatusBar {
pub battery: u8,
pub backlight: u8,
pub volume: u8,
}
pub struct SelectionList<const MAX_SELECTION: usize, const MAX_STR_LEN: usize> {
current_selection: u16,
selections: Vec<String<MAX_STR_LEN>, MAX_SELECTION>,
}
impl<const MAX_SELECTION: usize, const MAX_STR_LEN: usize>
SelectionList<MAX_SELECTION, MAX_STR_LEN>
{
pub fn new(selections: Vec<String<MAX_STR_LEN>, MAX_SELECTION>) -> Self {
Self {
selections,
current_selection: 0,
}
}
/// 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 down(&mut self) {
if self.current_selection + 1 < self.selections.len() as u16 {
self.current_selection += 1
}
}
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();
}
}
pub fn up(&mut self) {
if self.current_selection > self.selections.len() as u16 {
self.current_selection -= 1
}
}
}

View File

@@ -1,14 +1,23 @@
#![feature(impl_trait_in_assoc_type)]
#![feature(ascii_char)]
#![no_std]
#![no_main]
#[cfg(feature = "defmt")]
use defmt::*;
use embedded_graphics::{
pixelcolor::Rgb565,
prelude::{Point, RgbColor, Size},
primitives::{PrimitiveStyle, Rectangle, StyledDrawable},
};
use {defmt_rtt as _, panic_probe as _};
use crate::display::{FRAMEBUFFER, SCREEN_HEIGHT, SCREEN_WIDTH, UI};
use crate::peripherals::{
conf_peripherals,
keyboard::{KeyEvent, read_keyboard_fifo},
};
use embassy_executor::Spawner;
use embassy_rp::peripherals::I2C1;
use embassy_rp::peripherals::{I2C1, PIN_13, PIN_14, PIN_15, SPI1};
use embassy_rp::spi::Spi;
use embassy_rp::{
bind_interrupts,
@@ -19,42 +28,84 @@ use embassy_rp::{
};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::channel::Channel;
use embassy_time::Timer;
use embassy_time::{Delay, Timer};
use embedded_hal_1::spi::SpiDevice;
use embedded_hal_bus::spi::ExclusiveDevice;
use embedded_sdmmc::asynchronous::{File, SdCard, ShortFileName, VolumeIdx, VolumeManager};
use st7365p_lcd::{FrameBuffer, ST7365P};
use static_cell::StaticCell;
mod peripherals;
use peripherals::{keyboard::KeyEvent, peripherals_task};
mod display;
use display::display_task;
mod peripherals;
embassy_rp::bind_interrupts!(struct Irqs {
I2C1_IRQ => i2c::InterruptHandler<I2C1>;
});
embassy_rp::bind_interrupts!(
struct Irqs {
I2C1_IRQ => i2c::InterruptHandler<I2C1>;
}
);
#[embassy_executor::main]
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());
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;
// // 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(
p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, p.DMA_CH0, p.DMA_CH1, config,
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,
);
spawner
.spawn(display_task(spi1, p.PIN_13, p.PIN_14, p.PIN_15))
.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 ui: UI<50, 25> = UI::new();
// 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 {
info!("Done");
Timer::after_millis(500).await;
}
}

View File

@@ -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<KeyEvent> {
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;

View File

@@ -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