mission2teegarden_b_models/
lib.rs

1#![warn(rust_2018_idioms, unreachable_pub)]
2#![deny(rustdoc::bare_urls, rustdoc::broken_intra_doc_links)]
3#![forbid(unused_must_use, unsafe_code)]
4#![no_std]
5
6//! This create store models and protcoll data,
7//! which is used by the communication between pybadeg and pc.
8//!
9//! # Protocol:
10//! Every message is neither a Protocoll message or a Gamevent.
11//! The [`KeepAlive`](`MessageToPc::KeepAlive) is also definded at toplevel, to make it only one byte big,
12//! because it is send very frequently to pc.
13//! The pc assume that the [`KeepAlive`](`MessageToPc::KeepAlive) message is send at least once every second.
14//! Otherwise the pc assume that the pypbadge was disconnected.
15//!
16//! ### Connection Establishment
17//! The pc search for seriell devices at start and does send an [`ConnectionRequest`](ToPybadgeProtocol::ConnectionRequest)
18//! to each Seriell devices.
19//! The pybadge responds with [`ConnectionResponse`](ToPcProtocol::ConnectionResponse).
20//! After sending an [`ConnectionResponse`](ToPcProtocol::ConnectionResponse)
21//! the pybadge start listen to other incomming message too.
22
23use bincode::{Decode, Encode};
24
25mod cards;
26pub use cards::*;
27
28//todo:
29// new structure
30// event + game + keep alive message
31
32#[derive(Debug, Clone, Copy, Decode, Encode, PartialEq, Eq)]
33pub enum GameOver {
34	/// Player has drive outside the map.
35	DriveAway,
36	/// Player has crash.
37	Crash
38}
39
40#[derive(Debug, Clone, Decode, Encode, PartialEq)]
41pub enum Key {
42	A,
43	B,
44	Up,
45	Down,
46	Left,
47	Right,
48	Start,
49	Select
50}
51
52#[derive(Debug, Clone, Decode, Encode, PartialEq)]
53pub struct Log {
54	// data sending/de-/encoding is broken if array is to long.
55	// for example if messsage if is 128 long. This output will be send for "hiiiii":
56	// message: [104, 105, 105, 105, 105, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2], length: 2 }
57	// at some point the bytes become always 2.
58	pub message: [u8; 100],
59	pub length: u16
60}
61
62#[derive(Debug, Clone, Decode, Encode, PartialEq)]
63pub enum ToPcProtocol {
64	ConnectionResponse,
65	Log(Log)
66}
67
68#[derive(Debug, Clone, Decode, Encode, PartialEq)]
69pub enum ToPcGameEvent {
70	KeyPressed(Key),
71	/// The solution which the player has created for this level
72	// currently heapless::vec is not supported by bincode,
73	// so use Array<Option> as workaround.
74	// see https://github.com/bincode-org/bincode/issues/643
75	Solution([Option<Card>; 12])
76}
77
78#[derive(Debug, Clone, Decode, Encode, PartialEq)]
79pub enum MessageToPc {
80	Protocol(ToPcProtocol),
81	GameEvent(ToPcGameEvent),
82	///pybadge is still connected and work
83	KeepAlive
84}
85
86#[derive(Debug, Clone, Decode, Encode, PartialEq)]
87pub enum ToPybadgeProtocol {
88	ConnectionRequest
89}
90
91#[derive(Debug, Clone, Decode, Encode, PartialEq)]
92pub struct NeoPixelColor {
93	pub r: u8,
94	pub g: u8,
95	pub b: u8
96}
97
98#[derive(Debug, Clone, Decode, Encode, PartialEq)]
99pub enum ToPypadeGameEvent {
100	/// set all NeoPixel to this Color.
101	/// Regular used as indication for Player number
102	NeoPixelColor(NeoPixelColor),
103	Driving,
104	NewLevel(AvailableCards),
105	GameOver(GameOver),
106	/// Retry the current level,
107	/// with out clearing the solution of the player
108	Retry,
109	/// Index of the card which is currently evaluated
110	CurrentCardIndex(Option<u8>),
111	Wait
112}
113
114#[derive(Debug, Clone, Decode, Encode, PartialEq)]
115pub enum MessageToPyBadge {
116	Protocol(ToPybadgeProtocol),
117	GameEvent(ToPypadeGameEvent)
118}