From 9b15d4509ca5f5f6c31e9b3279f97f835e3ea891 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:50:16 -0500 Subject: [PATCH] moved shared types to common crate --- common/.gitignore | 1 + common/Cargo.lock | 65 +++++++++++++++++++++++++++++++++++++++++++ common/Cargo.toml | 7 +++++ common/src/lib.rs | 30 ++++++++++++++++++++ outside/Cargo.lock | 8 ++++++ outside/Cargo.toml | 1 + outside/src/auth.rs | 16 ++++++++++- outside/src/lib.rs | 62 +++++++++++++++++++++++++++++++++++++++++ outside/src/music.rs | 34 ++++------------------ outside/src/server.rs | 11 ++------ 10 files changed, 196 insertions(+), 39 deletions(-) create mode 100644 common/.gitignore create mode 100644 common/Cargo.lock create mode 100644 common/Cargo.toml create mode 100644 common/src/lib.rs create mode 100644 outside/src/lib.rs diff --git a/common/.gitignore b/common/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/common/.gitignore @@ -0,0 +1 @@ +target diff --git a/common/Cargo.lock b/common/Cargo.lock new file mode 100644 index 0000000..586e64e --- /dev/null +++ b/common/Cargo.lock @@ -0,0 +1,65 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "common" +version = "0.1.0" +dependencies = [ + "serde", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 0000000..81e97a8 --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "common" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.203", default-features = false, features = ["derive"] } diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 0000000..7c07e49 --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1,30 @@ +#![no_std] +use serde::Deserialize; +use serde::Serialize; + +#[derive(Serialize, Deserialize)] +pub enum Request { + RecentBadge, + SetRecentBadge(Name), + SetVolume(u8), +} + +#[derive(Clone, Copy, Serialize, Deserialize)] +#[repr(u8)] +pub enum Name { + Andy, + Evan, + Felix, + Phil, + Tess, + Amaia, + Prueky, + David, + Nathaniel, + Thia, + Michael, + Zoey, + Coke, + Unknown, +} + diff --git a/outside/Cargo.lock b/outside/Cargo.lock index c5058b1..070fb6e 100644 --- a/outside/Cargo.lock +++ b/outside/Cargo.lock @@ -260,6 +260,13 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "common" +version = "0.1.0" +dependencies = [ + "serde", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -1556,6 +1563,7 @@ dependencies = [ "assign-resources", "bt-hci", "byte-slice-cast 1.2.2", + "common", "cortex-m", "cortex-m-rt", "critical-section", diff --git a/outside/Cargo.toml b/outside/Cargo.toml index cb9cbb4..e14a10b 100644 --- a/outside/Cargo.toml +++ b/outside/Cargo.toml @@ -62,6 +62,7 @@ bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] } hex = { version = "0.4.3", default-features=false} itoa = "1.0.14" postcard = "1.0.0" +common = {path = "../common"} [profile.release] debug = 2 diff --git a/outside/src/auth.rs b/outside/src/auth.rs index a933cca..2fad6a0 100644 --- a/outside/src/auth.rs +++ b/outside/src/auth.rs @@ -1,5 +1,7 @@ -use crate::music::{MusicCommand, Name, COMMANDS}; +use crate::music::{MusicCommand, COMMANDS}; +use crate::server::NAMES; +use common::Name; use embassy_net::{IpEndpoint, Ipv4Address}; use embassy_time::Duration; @@ -16,6 +18,7 @@ use log::*; trait Authorized { /// allowed to open door fn authorized(&self) -> bool; + async fn from_badge(badge: u64) -> Self; } impl Authorized for Name { @@ -27,6 +30,17 @@ impl Authorized for Name { _ => false, } } + async fn from_badge(badge: u64) -> Self { + match badge { + badge => { + if let Some(name) = NAMES.lock().await.borrow().get(badge) { + *name + } else { + Name::Unknown + } + }, + } + } } #[embassy_executor::task] diff --git a/outside/src/lib.rs b/outside/src/lib.rs new file mode 100644 index 0000000..9ed52bc --- /dev/null +++ b/outside/src/lib.rs @@ -0,0 +1,62 @@ +#![no_std] +#![no_main] +#![allow(async_fn_in_trait)] + +use core::array; +use core::borrow::BorrowMut; +use core::cell::RefCell; +use core::fmt::Formatter; +use core::marker::PhantomData; +use core::mem::MaybeUninit; +use core::panic::PanicInfo; +use core::str::from_utf8; +use core::sync::atomic::{AtomicU8}; + +use auth::send_badge; +use bt_hci::cmd::info; +use critical_section::Mutex; +use cyw43::JoinOptions; +use cyw43_pio::PioSpi; +use embassy_futures::join::join; +use embassy_futures::yield_now; +use embassy_rp::interrupt::typelevel::{Handler, Interrupt, IO_IRQ_BANK0}; +use embassy_rp::multicore::{spawn_core1, Stack}; +use embassy_rp::pwm::{self}; +use embassy_rp::uart::{BufferedInterruptHandler}; +use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; +use embassy_sync::channel::{Channel}; +use fixed::FixedU16; +use log::*; +//use embassy_rp::i2c::InterruptHandler; +use embassy_executor::{InterruptExecutor, Spawner}; +use embassy_net::tcp::TcpSocket; +use embassy_net::{Config, IpEndpoint, Ipv4Address, StackResources}; +use embassy_rp::{bind_interrupts, interrupt}; +use embassy_rp::clocks::RoscRng; +use embassy_rp::gpio::{AnyPin, Input, InterruptTrigger, Level, Output}; +use embassy_rp::peripherals::{DMA_CH0, PIN_22, PIO0, UART1, USB}; +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 rand::RngCore; +use reqwless::response; +use scanner::{data_extractor, spawn_poller}; +use server::server_task; +use static_cell::StaticCell; +use defmt_rtt as _; +use wiggle::wiggle_manager; +pub mod wiggle; +pub mod music; +pub mod scanner; +pub mod auth; +pub mod arraymap; +pub mod server; + +bind_interrupts!(pub struct Irqs { + PIO0_IRQ_0 => InterruptHandler; + USBCTRL_IRQ => embassy_rp::usb::InterruptHandler; + UART1_IRQ => BufferedInterruptHandler; + +}); diff --git a/outside/src/music.rs b/outside/src/music.rs index bff9ed3..526e2c0 100644 --- a/outside/src/music.rs +++ b/outside/src/music.rs @@ -1,3 +1,4 @@ +use common::Name; use embassy_rp::clocks::RoscRng; use embassy_rp::uart; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; @@ -26,26 +27,12 @@ use log::*; pub static COMMANDS: Channel = Channel::new(); -#[derive(Clone, Copy, Serialize, Deserialize)] -#[repr(u8)] -pub enum Name { - Andy, - Evan, - Felix, - Phil, - Tess, - Amaia, - Prueky, - David, - Nathaniel, - Thia, - Michael, - Zoey, - Coke, - Unknown, +pub trait Tracks { + fn tracks(&self) -> u8; + fn prefix(&self) -> &'static str; } -impl Name { +impl Tracks for Name { fn tracks(&self) -> u8 { match self { Name::Andy => 7, @@ -84,17 +71,6 @@ impl Name { } } - pub async fn from_badge(badge: u64) -> Self { - match badge { - badge => { - if let Some(name) = NAMES.lock().await.borrow().get(badge) { - *name - } else { - Name::Unknown - } - }, - } - } } static ACCEPTED_SUFFIXABLE: [bool; 6] = [false, false, false, false, false, true]; diff --git a/outside/src/server.rs b/outside/src/server.rs index 6370144..ff8ae27 100644 --- a/outside/src/server.rs +++ b/outside/src/server.rs @@ -1,5 +1,6 @@ use core::{cell::RefCell, str::from_utf8}; +use common::{Name, Request}; use embassy_net::tcp::TcpSocket; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex}; use embedded_io_async::Write; @@ -7,19 +8,11 @@ use log::*; use postcard::from_bytes; use serde::{Deserialize, Serialize}; -use crate::{arraymap::ArrayMap, music::{Name, COMMANDS}, scanner::READ_CARD}; +use crate::{arraymap::ArrayMap, music::{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];