set badge names from desktop client

This commit is contained in:
Andy Killorin 2024-12-05 20:50:52 -05:00
parent 8f2510588f
commit 0d5fb05abe
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
7 changed files with 38 additions and 45 deletions

View file

@ -2,14 +2,15 @@
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Debug)]
pub enum Request { pub enum Request {
RecentBadge, RecentBadge,
SetRecentBadge(Name), SetRecentBadge(Name),
SetBadge(Name, u64),
SetVolume(u8), SetVolume(u8),
} }
#[derive(Clone, Copy, Serialize, Deserialize)] #[derive(Clone, Copy, Serialize, Deserialize, Debug)]
#[repr(u8)] #[repr(u8)]
pub enum Name { pub enum Name {
Andy, Andy,

View file

@ -1,4 +1,5 @@
/// no_std, no_alloc map /// no_std, no_alloc map
#[derive(Debug)]
pub struct ArrayMap<const N: usize, K,V> { pub struct ArrayMap<const N: usize, K,V> {
keys: [Option<K>; N], keys: [Option<K>; N],
values: [Option<V>; N], values: [Option<V>; N],

View file

@ -1,5 +1,5 @@
use crate::music::{MusicCommand, COMMANDS}; use crate::music::{MusicCommand, COMMANDS};
use crate::server::NAMES; use crate::server::{LAST_BADGE, NAMES};
use common::Name; use common::Name;
use embassy_net::{IpEndpoint, Ipv4Address}; use embassy_net::{IpEndpoint, Ipv4Address};
@ -50,11 +50,12 @@ pub async fn send_badge(channel: Receiver<'static,CriticalSectionRawMutex,u64,1>
loop { loop {
let card = channel.receive().await; let card = channel.receive().await;
*LAST_BADGE.lock().await.borrow_mut() = card;
let name = Name::from_badge(card).await; let name = Name::from_badge(card).await;
COMMANDS.send(MusicCommand::Introduce(name, name.authorized())).await;
// this is also checked on the backend (TODO) // this is also checked on the backend (TODO)
if !name.authorized() { if !name.authorized() {
COMMANDS.send(MusicCommand::Introduce(name, name.authorized())).await;
continue; continue;
} }
@ -85,5 +86,7 @@ pub async fn send_badge(channel: Receiver<'static,CriticalSectionRawMutex,u64,1>
socket.close(); socket.close();
info!("disconnected"); info!("disconnected");
COMMANDS.send(MusicCommand::Introduce(name, name.authorized())).await;
} }
} }

View file

@ -152,7 +152,7 @@ async fn button_manager(pin: PIN_22) -> ! {
let mut button = Input::new(pin, embassy_rp::gpio::Pull::Up); let mut button = Input::new(pin, embassy_rp::gpio::Pull::Up);
loop { loop {
button.wait_for_rising_edge().await; button.wait_for_rising_edge().await;
COMMANDS.send(music::MusicCommand::Play("d5")).await; // TODO: record COMMANDS.send(music::MusicCommand::Play("a1")).await; // TODO: record
Timer::after_millis(800).await; Timer::after_millis(800).await;
} }
} }

View file

@ -157,7 +157,7 @@ async fn play_song<'a>(prefix: &str, index: Option<u8>, uart: &mut BufferedUart<
let output = read_line(&mut buffer, uart).await; let output = read_line(&mut buffer, uart).await;
if let Ok(length) = output.trim().parse::<u64>() { if let Ok(length) = output.trim().parse::<u64>() {
WAGS.store(length as u8, core::sync::atomic::Ordering::SeqCst); WAGS.store((length as u8) * 2 + 1, core::sync::atomic::Ordering::SeqCst);
Timer::after_secs(length).await; Timer::after_secs(length).await;
} }
} }

View file

@ -14,6 +14,9 @@ use crate::{arraymap::ArrayMap, music::{COMMANDS}, scanner::READ_CARD};
pub static NAMES: Mutex<CriticalSectionRawMutex, RefCell<ArrayMap<64, u64, Name>>> = pub static NAMES: Mutex<CriticalSectionRawMutex, RefCell<ArrayMap<64, u64, Name>>> =
Mutex::new(RefCell::new(ArrayMap::new())); Mutex::new(RefCell::new(ArrayMap::new()));
pub static LAST_BADGE: Mutex<CriticalSectionRawMutex, RefCell<u64>> =
Mutex::new(RefCell::new(0));
#[embassy_executor::task] #[embassy_executor::task]
pub async fn server_task(stack: embassy_net::Stack<'static>) { pub async fn server_task(stack: embassy_net::Stack<'static>) {
let mut rx_buffer = [0; 4096]; let mut rx_buffer = [0; 4096];
@ -44,52 +47,33 @@ pub async fn server_task(stack: embassy_net::Stack<'static>) {
} }
}; };
info!("rxd: {:?}", from_bytes::<Request>(&buf[..n]));
match from_bytes::<Request>(&buf[..n]) { match from_bytes::<Request>(&buf[..n]) {
Ok(Request::RecentBadge) => { Ok(Request::RecentBadge) => {
let card = critical_section::with(|cs| { let lock = LAST_BADGE.lock().await;
READ_CARD.borrow(cs).clone().into_inner() let mut borrow_mut = lock.borrow_mut();
}); let card = borrow_mut.clone();
*borrow_mut = 0;
socket.write_all(itoa::Buffer::new().format(card).as_bytes()).await.unwrap(); socket.write_all(&card.to_be_bytes()).await.unwrap();
socket.flush().await.unwrap();
}, },
Ok(Request::SetRecentBadge(name)) => { Ok(Request::SetRecentBadge(name)) => {
let card = critical_section::with(|cs| { let card = LAST_BADGE.lock().await.borrow().clone();
READ_CARD.borrow(cs).clone().into_inner()
});
NAMES.lock().await.borrow_mut().insert(card, name); NAMES.lock().await.borrow_mut().insert(card, name);
info!("names: {:?}", NAMES.lock().await.borrow());
}
Ok(Request::SetBadge(name, card)) => {
info!("setting {name:?} to {card}");
NAMES.lock().await.borrow_mut().insert(card, name);
} }
Ok(Request::SetVolume(vol)) => { Ok(Request::SetVolume(vol)) => {
COMMANDS.send(crate::music::MusicCommand::SetVolume(vol)).await; COMMANDS.send(crate::music::MusicCommand::SetVolume(vol)).await;
} }
Err(e) => { info!("parse error: {e}") } 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);
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);
},
_ => {}
}
*/
} }
} }
} }

View file

@ -1,6 +1,6 @@
use common::Request; use common::Request;
use postcard::{to_allocvec, to_vec}; use postcard::to_allocvec;
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::TcpStream}; use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::TcpStream};
use anyhow::Result; use anyhow::Result;
@ -12,16 +12,20 @@ async fn main() -> Result<()>{
let req = Request::RecentBadge; let req = Request::RecentBadge;
conn.write_all(&to_allocvec(&req)?).await?; conn.write_all(&to_allocvec(&req)?).await?;
conn.flush().await?;
let badge = conn.read_u64().await?;
println!("read: {badge}");
if badge != 0 {
let req = Request::SetBadge(common::Name::Evan, badge);
conn.write_all(&to_allocvec(&req)?).await?;
conn.flush().await?;
}
let req = Request::SetVolume(23); let req = Request::SetVolume(23);
println!("{:?}", to_allocvec(&req).unwrap()); println!("{:?}", to_allocvec(&req).unwrap());
conn.write_all(&to_allocvec(&req)?).await?; conn.write_all(&to_allocvec(&req)?).await?;
conn.flush().await?;
let mut buf = String::new();
conn.read_to_string(&mut buf).await?;
println!("recv: {}", buf.trim());
Ok(()) Ok(())
} }