From cffa942ac958c4636db99a81266ddeaed29a6cab Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:40:11 -0500 Subject: [PATCH] converted outside to a lib crate and integrated postcard --- outside/Cargo.lock | 87 ++++++++++-------------------------------- outside/Cargo.toml | 3 +- outside/src/auth.rs | 2 +- outside/src/main.rs | 32 +++------------- outside/src/music.rs | 10 +++-- outside/src/scanner.rs | 15 +++++--- outside/src/server.rs | 37 +++++++++++++++++- outside/src/wiggle.rs | 6 ++- 8 files changed, 83 insertions(+), 109 deletions(-) diff --git a/outside/Cargo.lock b/outside/Cargo.lock index 6156bae..c5058b1 100644 --- a/outside/Cargo.lock +++ b/outside/Cargo.lock @@ -126,12 +126,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.7" @@ -250,6 +244,12 @@ dependencies = [ "inout", ] +[[package]] +name = "cobs" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15" + [[package]] name = "codespan-reporting" version = "0.11.1" @@ -260,12 +260,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "const-default" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b396d1f76d455557e1218ec8066ae14bba60b4b36ecd55577ba979f5db7ecaa" - [[package]] name = "const-oid" version = "0.9.6" @@ -813,18 +807,6 @@ dependencies = [ "log", ] -[[package]] -name = "embedded-alloc" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f2de9133f68db0d4627ad69db767726c99ff8585272716708227008d3f1bddd" -dependencies = [ - "const-default", - "critical-section", - "linked_list_allocator", - "rlsf", -] - [[package]] name = "embedded-graphics" version = "0.7.1" @@ -1300,6 +1282,7 @@ dependencies = [ "atomic-polyfill", "hash32 0.2.1", "rustc_version 0.4.1", + "serde", "spin", "stable_deref_trait", ] @@ -1449,12 +1432,6 @@ dependencies = [ "libc", ] -[[package]] -name = "linked_list_allocator" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286" - [[package]] name = "litrs" version = "0.4.1" @@ -1597,7 +1574,6 @@ dependencies = [ "embassy-time", "embassy-usb", "embassy-usb-logger", - "embedded-alloc", "embedded-graphics", "embedded-hal 1.0.0", "embedded-hal-async", @@ -1615,6 +1591,7 @@ dependencies = [ "pio", "pio-proc", "portable-atomic", + "postcard", "rand", "reqwless", "serde", @@ -1622,7 +1599,6 @@ dependencies = [ "smart-leds", "st7789", "static_cell", - "talc", "usbd-hid", ] @@ -1767,6 +1743,17 @@ dependencies = [ "critical-section", ] +[[package]] +name = "postcard" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "170a2601f67cc9dba8edd8c4870b15f71a6a2dc196daec8c83f72b59dff628a8" +dependencies = [ + "cobs", + "heapless 0.7.17", + "serde", +] + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -1919,7 +1906,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66584b58c3b0cd8cfea750408a932fe86df79ee05ccc87ee745a8e6be4a1abe0" dependencies = [ - "base64 0.21.7", + "base64", "buffered-io", "defmt", "embedded-io", @@ -1943,18 +1930,6 @@ dependencies = [ "bytemuck", ] -[[package]] -name = "rlsf" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222fb240c3286247ecdee6fa5341e7cdad0ffdf8e7e401d9937f2d58482a20bf" -dependencies = [ - "cfg-if", - "const-default", - "libc", - "svgbobdoc", -] - [[package]] name = "rp-pac" version = "6.0.0" @@ -2206,19 +2181,6 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" -[[package]] -name = "svgbobdoc" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2c04b93fc15d79b39c63218f15e3fdffaa4c227830686e3b7c5f41244eb3e50" -dependencies = [ - "base64 0.13.1", - "proc-macro2", - "quote", - "syn 1.0.109", - "unicode-width", -] - [[package]] name = "syn" version = "1.0.109" @@ -2241,15 +2203,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "talc" -version = "4.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fcad3be1cfe36eb7d716a04791eba36a197da9d9b6ea1e28e64ac569da3701d" -dependencies = [ - "lock_api", -] - [[package]] name = "term" version = "0.7.0" diff --git a/outside/Cargo.toml b/outside/Cargo.toml index b2cbf79..cb9cbb4 100644 --- a/outside/Cargo.toml +++ b/outside/Cargo.toml @@ -61,8 +61,7 @@ embedded-sdmmc = "0.7.0" bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] } hex = { version = "0.4.3", default-features=false} itoa = "1.0.14" -embedded-alloc = "0.6.0" -talc = "4.4.2" +postcard = "1.0.0" [profile.release] debug = 2 diff --git a/outside/src/auth.rs b/outside/src/auth.rs index 7fa06a8..a933cca 100644 --- a/outside/src/auth.rs +++ b/outside/src/auth.rs @@ -30,7 +30,7 @@ impl Authorized for Name { } #[embassy_executor::task] -pub(crate) async fn send_badge(channel: Receiver<'static,CriticalSectionRawMutex,u64,1>,stack: embassy_net::Stack<'static>) -> ! { +pub async fn send_badge(channel: Receiver<'static,CriticalSectionRawMutex,u64,1>,stack: embassy_net::Stack<'static>) -> ! { let mut rx_buffer = [0; 4096]; let mut tx_buffer = [0; 4096]; loop { diff --git a/outside/src/main.rs b/outside/src/main.rs index 27a023a..bb94622 100644 --- a/outside/src/main.rs +++ b/outside/src/main.rs @@ -15,7 +15,7 @@ use core::panic::PanicInfo; use core::str::from_utf8; use core::sync::atomic::{AtomicU8}; -use auth::send_badge; +use outside::auth::send_badge; use bt_hci::cmd::info; use critical_section::Mutex; use cyw43::JoinOptions; @@ -42,35 +42,15 @@ use embassy_rp::pio::{InterruptHandler, Pio}; use embassy_rp::usb::Driver; use embassy_time::{Timer}; use embedded_io_async::{Read, ReadReady, Write}; -use music::{music_manager, COMMANDS}; +use outside::music::{self, music_manager, COMMANDS}; use rand::RngCore; use reqwless::response; -use scanner::{data_extractor, spawn_poller}; -use server::server_task; +use outside::scanner::{data_extractor, spawn_poller, CHANNEL}; +use outside::server::server_task; use static_cell::StaticCell; use defmt_rtt as _; -use wiggle::wiggle_manager; - -mod wiggle; -mod music; -mod scanner; -mod auth; -mod arraymap; -mod server; - -bind_interrupts!(struct Irqs { - PIO0_IRQ_0 => InterruptHandler; - USBCTRL_IRQ => embassy_rp::usb::InterruptHandler; - UART1_IRQ => BufferedInterruptHandler; - -}); - -static READ_CARD: Mutex> = - Mutex::new(RefCell::new(0)); -static BIT: AtomicU8 = AtomicU8::new(0); - -static CHANNEL: Channel = Channel::new(); -static WAGS: AtomicU8 = AtomicU8::new(0); +use outside::wiggle::wiggle_manager; +use outside::Irqs; #[embassy_executor::task] async fn logger_task(driver: Driver<'static, USB>) { diff --git a/outside/src/music.rs b/outside/src/music.rs index 70b3798..bff9ed3 100644 --- a/outside/src/music.rs +++ b/outside/src/music.rs @@ -6,10 +6,13 @@ use embassy_time::Timer; use embedded_io_async::Read; use embedded_io_async::Write; use rand::RngCore; +use serde::Deserialize; +use serde::Serialize; use crate::server::NAMES; +use crate::wiggle::WAGS; +use crate::Irqs; -use super::WAGS; use embassy_rp::uart::BufferedUart; @@ -17,14 +20,13 @@ use embassy_rp::peripherals::PIN_21; use embassy_rp::peripherals::PIN_20; -use super::Irqs; use embassy_rp::peripherals::UART1; use log::*; pub static COMMANDS: Channel = Channel::new(); -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Serialize, Deserialize)] #[repr(u8)] pub enum Name { Andy, @@ -107,7 +109,7 @@ pub enum MusicCommand { } #[embassy_executor::task] -pub(crate) async fn music_manager(uart: UART1, irqs: Irqs, txp: PIN_20, rxp: PIN_21) -> ! { +pub async fn music_manager(uart: UART1, irqs: Irqs, txp: PIN_20, rxp: PIN_21) -> ! { let mut config = uart::Config::default(); config.baudrate = 115200; let mut rx = [0; 2048]; diff --git a/outside/src/scanner.rs b/outside/src/scanner.rs index 3028621..e9102ef 100644 --- a/outside/src/scanner.rs +++ b/outside/src/scanner.rs @@ -1,4 +1,5 @@ +use core::cell::RefCell; use core::sync::atomic::AtomicBool; use embassy_rp::gpio::Input; @@ -7,20 +8,24 @@ use embassy_rp::multicore::Stack; use embassy_rp::peripherals::CORE1; use embassy_rp::peripherals::PIN_16; use embassy_rp::peripherals::PIN_17; +use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; +use embassy_sync::channel::Channel; use embassy_time::Timer; use log::info; +use critical_section::Mutex; +use core::sync::atomic::{AtomicU8}; -use super::CHANNEL; +pub static READ_CARD: Mutex> = + Mutex::new(RefCell::new(0)); +static BIT: AtomicU8 = AtomicU8::new(0); -use super::BIT; - -use super::READ_CARD; +pub static CHANNEL: Channel = Channel::new(); /// disable scanner when doing emf heavy operations pub static LOCKOUT: AtomicBool = AtomicBool::new(false); #[embassy_executor::task] -pub(crate) async fn data_extractor() -> ! { +pub async fn data_extractor() -> ! { let mut last_bit = 0; loop { Timer::after_millis(75).await; diff --git a/outside/src/server.rs b/outside/src/server.rs index 602b774..6370144 100644 --- a/outside/src/server.rs +++ b/outside/src/server.rs @@ -4,12 +4,22 @@ use embassy_net::tcp::TcpSocket; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex}; use embedded_io_async::Write; use log::*; +use postcard::from_bytes; +use serde::{Deserialize, Serialize}; -use crate::{arraymap::ArrayMap, music::Name, READ_CARD}; +use crate::{arraymap::ArrayMap, music::{Name, COMMANDS}, scanner::READ_CARD}; pub static NAMES: Mutex>> = Mutex::new(RefCell::new(ArrayMap::new())); + +#[derive(Serialize, Deserialize)] +pub enum Request { + RecentBadge, + SetRecentBadge(Name), + SetVolume(u8), +} + #[embassy_executor::task] pub async fn server_task(stack: embassy_net::Stack<'static>) { let mut rx_buffer = [0; 4096]; @@ -39,9 +49,31 @@ pub async fn server_task(stack: embassy_net::Stack<'static>) { } }; + match from_bytes::(&buf[..n]) { + Ok(Request::RecentBadge) => { + let card = critical_section::with(|cs| { + READ_CARD.borrow(cs).clone().into_inner() + }); + + socket.write_all(itoa::Buffer::new().format(card).as_bytes()).await.unwrap(); + }, + Ok(Request::SetRecentBadge(name)) => { + let card = critical_section::with(|cs| { + READ_CARD.borrow(cs).clone().into_inner() + }); + + NAMES.lock().await.borrow_mut().insert(card, name); + } + Ok(Request::SetVolume(vol)) => { + COMMANDS.send(crate::music::MusicCommand::SetVolume(vol)).await; + } + Err(e) => { info!("parse error: {e}") } + } + + info!("rxd {}", from_utf8(&buf[..n]).unwrap()); - let mut segs = buf[..n].trim_ascii().split(|c| *c == ' ' as u8); + /*let mut segs = buf[..n].trim_ascii().split(|c| *c == ' ' as u8); match char::from_u32(segs.next().unwrap()[0] as u32).unwrap() { 'B' => { @@ -62,6 +94,7 @@ pub async fn server_task(stack: embassy_net::Stack<'static>) { _ => {} } + */ } } } diff --git a/outside/src/wiggle.rs b/outside/src/wiggle.rs index e5a6891..f61699c 100644 --- a/outside/src/wiggle.rs +++ b/outside/src/wiggle.rs @@ -1,3 +1,5 @@ +use core::sync::atomic::AtomicU8; + use embassy_rp::clocks::RoscRng; use embassy_rp::pwm; @@ -6,7 +8,7 @@ use rand::RngCore; use crate::scanner::LOCKOUT; -use super::WAGS; +pub static WAGS: AtomicU8 = AtomicU8::new(0); use embassy_rp::pwm::Pwm; @@ -17,7 +19,7 @@ use embassy_rp::peripherals::PIN_26; use embassy_rp::peripherals::PWM_SLICE5; #[embassy_executor::task] -pub(crate) async fn wiggle_manager(pwm: PWM_SLICE5, head: PIN_26, tail: PIN_27) -> ! { +pub async fn wiggle_manager(pwm: PWM_SLICE5, head: PIN_26, tail: PIN_27) -> ! { let mut c: pwm::Config = Default::default(); c.divider = 40.into(); c.top = 62_500; // 20ms