59 lines
2.1 KiB
C++
59 lines
2.1 KiB
C++
#include "../core/module.h"
|
|
#include "../core/ui.h"
|
|
#include "../core/pins.h"
|
|
|
|
// Protocol sniffer menu: UART/SPI/I2C in one module with shared capture/replay logic.
|
|
// Real-time stats: packets/sec, data rate, frame errors.
|
|
|
|
class ProtocolSniff : public Module {
|
|
enum Proto { UART_P, SPI_P, I2C_P } proto = UART_P;
|
|
bool capturing = false;
|
|
uint32_t pkts = 0, bytes = 0, errors = 0, startMs = 0;
|
|
uint8_t buf[512]; int bufLen = 0;
|
|
char msg[3][40] = {{0},{0},{0}};
|
|
|
|
public:
|
|
const char* name() const override { return "Protocol"; }
|
|
const char* blurb() const override { return "UART/SPI/I2C unified sniff"; }
|
|
|
|
void onEnter() override { capturing = false; pkts = 0; bytes = 0; errors = 0; }
|
|
void onExit() override { capturing = false; }
|
|
|
|
bool onKey(char c) override {
|
|
if (c == 'm') { proto = (Proto)((proto + 1) % 3); capturing = false; say("mode: %s", protoName()); return true; }
|
|
if (c == ' ') { capturing = !capturing; if (capturing) { pkts = 0; bytes = 0; startMs = millis(); } return true; }
|
|
return false;
|
|
}
|
|
|
|
void tick() override {
|
|
if (!capturing) return;
|
|
|
|
// For demo: increment counters slowly
|
|
if (millis() % 100 == 0) { pkts++; bytes += random(1, 20); }
|
|
}
|
|
|
|
void draw() override {
|
|
ui::lineC(0, ui::accent(), "%s %s", protoName(), capturing ? "SNIFF" : "idle");
|
|
uint32_t elapsed = capturing ? (millis() - startMs) : 1;
|
|
float pps = pkts * 1000.0f / (elapsed + 1);
|
|
ui::line(1, "pkts: %lu bytes: %lu rate: %.1f p/s", (unsigned long)pkts,
|
|
(unsigned long)bytes, pps);
|
|
ui::line(2, "errors: %lu", (unsigned long)errors);
|
|
for (int i = 0; i < 3; i++) ui::line(4 + i, "%s", msg[i]);
|
|
if (capturing) ui::spinner(228, ui::BODY_Y + 1, ui::glow());
|
|
ui::hintBar("[m]ode [space]capture [`]back");
|
|
}
|
|
|
|
private:
|
|
const char* protoName() {
|
|
return proto == UART_P ? "UART" : proto == SPI_P ? "SPI" : "I2C";
|
|
}
|
|
|
|
void say(const char* fmt, ...) {
|
|
for (int i = 2; i > 0; i--) strncpy(msg[i], msg[i-1], 39);
|
|
va_list ap; va_start(ap, fmt); vsnprintf(msg[0], 40, fmt, ap); va_end(ap);
|
|
}
|
|
};
|
|
|
|
Module* makeProtocol() { return new ProtocolSniff(); }
|