Skip to content

Commit

Permalink
new message formats
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyTurtleDev committed May 15, 2023
1 parent c01f51f commit 1f9e465
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
29 changes: 24 additions & 5 deletions models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 7 additions & 4 deletions pc/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
});
Expand Down Expand Up @@ -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<u8> =
buffer[..len].iter().map(|f| f.to_owned()).collect();
println!("recieve data: {new_data:?}");
Expand Down

0 comments on commit 1f9e465

Please sign in to comment.