dynamic badge map

This commit is contained in:
Andy Killorin 2024-12-05 17:01:57 -05:00
parent 6aca7422eb
commit a9f835220a
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
5 changed files with 84 additions and 4 deletions

View file

@ -6,7 +6,7 @@ pub struct ArrayMap<const N: usize, K,V> {
}
impl<const N: usize, K, V> ArrayMap<N, K, V> where K: Eq {
pub fn new() -> Self {
pub const fn new() -> Self {
Self { keys: [const { None };N], values: [const { None };N], index: 0 }
}

View file

@ -36,7 +36,7 @@ pub(crate) async fn send_badge(channel: Receiver<'static,CriticalSectionRawMutex
loop {
let card = channel.receive().await;
let name = Name::from_badge(card);
let name = Name::from_badge(card).await;
COMMANDS.send(MusicCommand::Introduce(name, name.authorized())).await;
// this is also checked on the backend (TODO)

View file

@ -46,6 +46,7 @@ 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;
@ -54,6 +55,8 @@ mod wiggle;
mod music;
mod scanner;
mod auth;
mod arraymap;
mod server;
bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => InterruptHandler<PIO0>;
@ -160,6 +163,7 @@ async fn main(spawner: Spawner) {
spawner.spawn(send_badge(CHANNEL.receiver(),stack)).unwrap();
spawner.spawn(button_manager(p.PIN_22)).unwrap();
spawner.spawn(wiggle_manager(p.PWM_SLICE5, p.PIN_26, p.PIN_27)).unwrap();
spawner.spawn(server_task(stack)).unwrap();
}

View file

@ -7,6 +7,8 @@ use embedded_io_async::Read;
use embedded_io_async::Write;
use rand::RngCore;
use crate::server::NAMES;
use super::WAGS;
use embassy_rp::uart::BufferedUart;
@ -23,6 +25,7 @@ use log::*;
pub static COMMANDS: Channel<CriticalSectionRawMutex, MusicCommand, 3> = Channel::new();
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum Name {
Andy,
Evan,
@ -79,9 +82,15 @@ impl Name {
}
}
pub fn from_badge(badge: u64) -> Self {
pub async fn from_badge(badge: u64) -> Self {
match badge {
_ => Name::Unknown,
badge => {
if let Some(name) = NAMES.lock().await.borrow().get(badge) {
*name
} else {
Name::Unknown
}
},
}
}
}

67
outside/src/server.rs Normal file
View file

@ -0,0 +1,67 @@
use core::{cell::RefCell, str::from_utf8};
use embassy_net::tcp::TcpSocket;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
use embedded_io_async::Write;
use log::*;
use crate::{arraymap::ArrayMap, music::Name, READ_CARD};
pub static NAMES: Mutex<CriticalSectionRawMutex, RefCell<ArrayMap<64, u64, Name>>> =
Mutex::new(RefCell::new(ArrayMap::new()));
#[embassy_executor::task]
pub async fn server_task(stack: embassy_net::Stack<'static>) {
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut buf = [0; 4096];
loop {
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
info!("Listening on TCP:1234...");
if let Err(e) = socket.accept(1234).await {
warn!("accept error: {:?}", e);
continue;
}
info!("Received connection from {:?}", socket.remote_endpoint());
loop {
let n = match socket.read(&mut buf).await {
Ok(0) => {
warn!("read EOF");
break;
}
Ok(n) => n,
Err(e) => {
warn!("read error: {:?}", e);
break;
}
};
info!("rxd {}", from_utf8(&buf[..n]).unwrap());
let mut segs = buf[..n].trim_ascii().split(|c| *c == ' ' as u8);
match char::from_u32(segs.next().unwrap()[0] as u32).unwrap() {
'B' => {
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();
},
'R' => {
let card = critical_section::with(|cs| {
READ_CARD.borrow(cs).clone().into_inner()
});
NAMES.lock().await.borrow_mut().insert(card, Name::Unknown);
},
_ => {}
}
}
}
}