feat: added leds and packet for net tx/rx

This commit is contained in:
əlemi 2022-08-11 02:48:36 +02:00
parent def620665e
commit 0eb1ec059a
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 29 additions and 1 deletions

View file

@ -13,9 +13,14 @@ def cpu_load_serial_driver(device:str, retry_interval:float=5.0):
avg_usage_to_serial(port)
except serial.SerialException as e:
print(f"[!] Could not connect to device: {str(e)}", file=sys.stderr)
else:
port.close()
sleep(retry_interval)
def avg_usage_to_serial(port:serial.Serial):
net = psutil.net_io_counters()
net_tx = net.bytes_sent
net_rx = net.bytes_recv
port.write(struct.pack("BB", 0, 0))
port.flush()
while True:
@ -27,6 +32,15 @@ def avg_usage_to_serial(port:serial.Serial):
except serial.SerialException as e:
print(f"[!] Failed writing payload to device: {str(e)}", file=sys.stderr)
break
net = psutil.net_io_counters()
try:
port.write(struct.pack("BBBB", 2, 2, int(net.bytes_sent > net_tx), int(net.bytes_recv > net_rx)))
port.flush()
except serial.SerialException as e:
print(f"[!] Failed writing payload to device: {str(e)}", file=sys.stderr)
break
net_rx = net.bytes_recv
net_tx = net.bytes_sent
if __name__ == "__main__":

View file

@ -23,6 +23,8 @@ fn main() -> ! {
let timer2 = Timer2Pwm::new(dp.TC2, Prescaler::Direct);
let pins = arduino_hal::pins!(dp);
let mut led_load = pins.d6.into_output();
let mut led_rx = pins.d5.into_output(); // green
let mut led_tx = pins.d4.into_output(); // red
let button = pins.d2.into_pull_up_input();
let mut cpu_leds = FourLedDisplay::new(
pins.d3.into_output().into_pwm(&timer2),
@ -36,6 +38,8 @@ fn main() -> ! {
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);
led_load.set_high();
led_rx.set_high();
led_tx.set_high();
// prepare display
let mut display: GraphicsMode<_> = Builder::new().with_size(sh1106::prelude::DisplaySize::Display128x64).connect_i2c(i2c).into();
@ -58,6 +62,8 @@ fn main() -> ! {
cpu_leds.set(4, 255);
led_load.set_low();
led_tx.set_low();
led_rx.set_low();
let mut pkt_builder = PacketBuilder::new();
@ -83,6 +89,12 @@ fn main() -> ! {
cpu_leds.set_many(payload[0], payload[1], payload[2], payload[3]);
}
},
PacketId::NetStatePacket => {
if let Some(payload) = pkt.payload && payload.len() == 2 {
if payload[0] == 0 { led_tx.set_low() } else { led_tx.set_high() };
if payload[1] == 0 { led_rx.set_low() } else { led_rx.set_high() };
}
},
_ => {}, // TODO log it?
}
}

View file

@ -5,6 +5,7 @@ pub enum PacketId {
Invalid = 0xFF,
Reset = 0x00,
SetLedsPacket = 0x01,
NetStatePacket = 0x02,
}
impl From<u8> for PacketId {
@ -12,6 +13,7 @@ impl From<u8> for PacketId {
match x {
0 => PacketId::Reset,
1 => PacketId::SetLedsPacket,
2 => PacketId::NetStatePacket,
_ => PacketId::Invalid,
}
}
@ -53,7 +55,7 @@ impl PacketBuilder {
PacketBuilderStep::ID => {
let id = PacketId::from(byte);
match id {
PacketId::SetLedsPacket | PacketId::Reset => {
PacketId::SetLedsPacket | PacketId::Reset | PacketId::NetStatePacket => {
self.id = id;
self.step = PacketBuilderStep::SIZE;
},