From 1f9e465aa5384ef23b80fe774bb6571dab4e642c Mon Sep 17 00:00:00 2001 From: luckyturtledev Date: Mon, 15 May 2023 21:15:20 +0200 Subject: [PATCH] new message formats --- models/src/lib.rs | 29 ++++++++++++++++++++++++----- pc/src/usb.rs | 11 +++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/models/src/lib.rs b/models/src/lib.rs index 1cd49ac..6f65895 100644 --- a/models/src/lib.rs +++ b/models/src/lib.rs @@ -4,20 +4,39 @@ use bincode::{Decode, Encode}; mod cards; pub use cards::*; - //todo: // new structure // event + game + keep alive message +#[derive(Debug, Clone, Decode, Encode)] +pub enum ToPcProtocol { + ConnectionResponse +} + +#[derive(Debug, Clone, Decode, Encode)] +pub enum ToPcGameEvent {} #[derive(Debug, Clone, Decode, Encode)] pub enum MessageToPc { - ConnectionResponse + Protocol(ToPcProtocol), + GameEvent(ToPcGameEvent), + ///pybadge is still connected and work + KeepAlive } -#[derive(Debug, Clone, Decode, Encode, PartialEq)] -pub enum MessageToPyBadge { +#[derive(Debug, Clone, Decode, Encode)] +pub enum ToPybadgeProtocol { ConnectionRequest, - ConnectionConfirmation, + ConnectionConfirmation +} + +#[derive(Debug, Clone, Decode, Encode)] +pub enum ToPypadeGameEvent { NewLevel(AvailableCards) } + +#[derive(Debug, Clone, Decode, Encode, PartialEq)] +pub enum MessageToPyBadge { + Protocol(ToPybadgeProtocol), + GameEvent(ToPypadeGameEvent) +} diff --git a/pc/src/usb.rs b/pc/src/usb.rs index e307c86..133d3dd 100644 --- a/pc/src/usb.rs +++ b/pc/src/usb.rs @@ -60,13 +60,15 @@ impl Players { loop { match receiver_to_pydage.try_recv() { Err(err) => match err { - TryRecvError::Empty => {} - TryRecvError::Disconnected => panic!("channel disconnected") //or should I just break? + TryRecvError::Empty => {}, + TryRecvError::Disconnected => panic!("channel disconnected") /* or should I just break and close the thread? */ }, Ok(message) => pybadge.write(&message) } if let Some(message) = pybadge.try_next_event() { - sender_to_pc.send(Ok(message)); + if message != MessageToPc::KeepAlive { + sender_to_pc.send(Ok(message)); + } } } }); @@ -119,7 +121,8 @@ impl Pybadge { }; let mut buffer = [0_u8; 16]; let len = self.port.read(&mut buffer).unwrap(); - if len != 0 { //if it even possibel to get len 0? Since it does blocking wait for messages + if len != 0 { + //if it even possibel to get len 0? Since it does blocking wait for messages let mut new_data: Vec = buffer[..len].iter().map(|f| f.to_owned()).collect(); println!("recieve data: {new_data:?}");