mirror of
https://git.alemi.dev/pc-monitor.git
synced 2024-11-22 15:34:49 +01:00
alemidev
ed96292606
adding a proper floating point operation added something like 6 kb to the resulting bin size, which is not acceptable, I'm pretty tight with the graphics library! Also added a separate packet/command for refreshing the display directly, and stopped redrawing the display for each led update. This allows for a mixed experience, with smooth leds with "hiccups" and a slow updating display.
97 lines
2 KiB
Rust
97 lines
2 KiB
Rust
const PACKET_BUFFER : usize = 32;
|
|
|
|
#[derive(Copy, Clone)]
|
|
pub enum PacketId {
|
|
Invalid = 0xFF,
|
|
Reset = 0x00,
|
|
SetLedsPacket = 0x01,
|
|
NetStatePacket = 0x02,
|
|
ScreenDrawPacket = 0x03,
|
|
}
|
|
|
|
impl From<u8> for PacketId {
|
|
fn from(x: u8) -> Self {
|
|
match x {
|
|
0 => PacketId::Reset,
|
|
1 => PacketId::SetLedsPacket,
|
|
2 => PacketId::NetStatePacket,
|
|
3 => PacketId::ScreenDrawPacket,
|
|
_ => PacketId::Invalid,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Packet<'a> {
|
|
pub id: PacketId,
|
|
pub payload: Option<&'a[u8]>,
|
|
}
|
|
|
|
enum PacketBuilderStep {
|
|
ID,
|
|
SIZE,
|
|
PAYLOAD,
|
|
}
|
|
|
|
pub struct PacketBuilder {
|
|
step: PacketBuilderStep,
|
|
id: PacketId,
|
|
size: u8,
|
|
index: usize,
|
|
buffer: [u8; PACKET_BUFFER],
|
|
}
|
|
|
|
impl PacketBuilder {
|
|
pub fn new() -> Self {
|
|
PacketBuilder {
|
|
step: PacketBuilderStep::ID,
|
|
id: PacketId::Invalid,
|
|
size: 0,
|
|
index: 0,
|
|
buffer: [0;PACKET_BUFFER],
|
|
}
|
|
}
|
|
|
|
pub fn update(&mut self, byte:u8) -> Option<Packet> {
|
|
let mut ret = None;
|
|
match self.step {
|
|
PacketBuilderStep::ID => {
|
|
let id = PacketId::from(byte);
|
|
match id {
|
|
PacketId::SetLedsPacket | PacketId::Reset
|
|
| PacketId::NetStatePacket | PacketId::ScreenDrawPacket => {
|
|
self.id = id;
|
|
self.step = PacketBuilderStep::SIZE;
|
|
},
|
|
_ => {
|
|
// TODO log it somehow?
|
|
},
|
|
}
|
|
},
|
|
PacketBuilderStep::SIZE => {
|
|
if byte as usize > PACKET_BUFFER {
|
|
self.step = PacketBuilderStep::ID;
|
|
// TODO log it somehow?
|
|
} else if byte == 0 {
|
|
// packet without payload
|
|
ret = Some(Packet{id: self.id, payload: None}); // jank zero size slice
|
|
self.step = PacketBuilderStep::ID;
|
|
} else {
|
|
self.size = byte;
|
|
self.index = 0;
|
|
self.step = PacketBuilderStep::PAYLOAD;
|
|
}
|
|
},
|
|
PacketBuilderStep::PAYLOAD => {
|
|
self.buffer[self.index] = byte;
|
|
self.index += 1;
|
|
if self.index >= self.size as usize {
|
|
ret = Some(Packet{id: self.id, payload: Some(&self.buffer[0..self.size as usize])});
|
|
self.step = PacketBuilderStep::ID;
|
|
}
|
|
},
|
|
}
|
|
ret
|
|
}
|
|
}
|
|
|
|
|