File SimGateway.h
File List > Firmware > Libraries > SimGateway > SimGateway.h
Go to the documentation of this file
#pragma once
#ifdef ARDUINO_ARCH_RP2040
#include <Arduino.h>
#include <HIDControls.h>
#include "AxisCal.h"
#include "LinkRecovery.h"
namespace OpenSkyhawk {
class HIDAxis {
public:
HIDAxis(uint16_t controlId, uint8_t axisIndex);
static HIDAxis* head();
uint16_t controlId() const;
uint8_t axisIndex() const;
void dispatch(uint16_t value);
HIDAxis* next() const;
private:
static HIDAxis* _head;
HIDAxis* _next;
uint16_t _controlId;
uint8_t _axisIndex;
};
class HIDButton {
public:
HIDButton(uint16_t controlId, uint8_t buttonIndex);
static HIDButton* head();
uint16_t controlId() const;
void dispatch(uint16_t value);
HIDButton* next() const;
private:
static HIDButton* _head;
HIDButton* _next;
uint16_t _controlId;
uint8_t _buttonIndex;
};
class HIDHatSwitch {
public:
HIDHatSwitch(uint16_t controlId, uint8_t hatIndex);
static HIDHatSwitch* head();
uint16_t controlId() const;
void dispatch(uint16_t value);
HIDHatSwitch* next() const;
private:
static HIDHatSwitch* _head;
HIDHatSwitch* _next;
uint16_t _controlId;
uint8_t _hatIndex;
};
} // namespace OpenSkyhawk
namespace SimGateway {
static constexpr uint8_t DEFAULT_UART_TX_PIN = 0;
static constexpr uint8_t DEFAULT_UART_RX_PIN = 1;
void setup(SerialUART& uart,
uint8_t txPin = DEFAULT_UART_TX_PIN,
uint8_t rxPin = DEFAULT_UART_RX_PIN);
static constexpr size_t UART_RX_QUEUE_BYTES = 1024;
static constexpr uint16_t RELAY_LINE_MAX = 128;
static constexpr uint16_t CDC_TO_UART_BUDGET = 64;
static constexpr uint32_t CDC_STALL_MS = 1000;
static constexpr uint32_t WATCHDOG_MS = 3000;
void loop();
struct GatewayDiag {
uint32_t firstUartErrMs;
uint32_t lastUartErrMs;
uint32_t firstUsbLossMs;
uint32_t lastUsbLossMs;
uint16_t uartOverrun;
uint16_t uartFraming;
uint16_t uartBreak;
uint16_t uartParity;
uint16_t uartRxOverflow;
uint16_t linesDropped;
uint16_t linesCorrupt;
uint16_t linesOverlong;
uint16_t framesDropped;
uint16_t uartRestarts;
uint16_t usbReattaches;
uint16_t cdcStalls;
uint8_t recoveryReboots;
uint8_t watchdogResets;
uint8_t resetReason;
};
const GatewayDiag& diagnostics();
const OpenSkyhawk::CalBlob& calibration();
// ── Status LEDs (Gateway_Bridge board) ────────────────────────────────────────
//
// Two board-mounted status LEDs report the gateway's USB / data / fault state at a
// glance: RED = GP3, GREEN = GP2, board-mounted 0805, active-high (HIGH = on). The
// animator is non-blocking (millis(), never delay()) and is ticked from loop(), so
// sketches need no LED code. The RP2040 module's onboard WS2812 is not used.
// See docs/architecture/sim-gateway.md for the user-facing table.
enum class LedState : uint8_t {
FAULT,
NO_HOST,
STREAMING,
USB_IDLE,
INIT,
};
enum class Anim : uint8_t {
OFF,
SOLID,
SLOW,
FAST,
ALT,
PULSE,
};
void statusLedBegin();
void statusTick();
#ifdef SIMGATEWAY_TEST
bool feedByte(uint8_t b);
void resetParser();
void cdcSetRoomForTest(int32_t room);
void diagResetForTest();
void diagUartErrorsForTest(uint32_t now, bool oe, bool fe, bool be, bool pe);
OpenSkyhawk::LinkAction linkStepForTest(uint32_t now, bool mounted, bool suspended,
bool cdcStalled = false);
bool cdcStallStepForTest(uint32_t now, bool dtr, uint16_t room, uint16_t capacity, uint16_t written);
void linkResetForTest(bool afterRecoveryReboot);
void resetCdcCapture();
size_t cdcCaptureCount();
uint8_t cdcCaptureByte(size_t index);
bool cdcCaptureOverflow();
// ── Axis calibration test hooks (test builds only) ────────────────────────────
// The transform itself is tested by calling OpenSkyhawk::axisCalApply() directly. These
// hooks exist to cover something that function-level test cannot: that HIDAxis::dispatch()
// actually *applies* it. That path is doubly hidden in test builds — the HID setters are
// no-op stubs, and the flash load is stubbed to a RAM clear so no calibration can be
// installed. Without both a writer and a reader, deleting the transform from dispatch()
// would leave every assertion passing.
void calSetForTest(const OpenSkyhawk::CalBlob& blob);
int16_t lastAxisValue();
uint8_t lastAxisIndex();
void resetAxisCapture();
uint8_t hidSendCount();
void resetHidSendCount();
// ── Calibration transport test hooks (test builds only) ───────────────────────
// The transport's safety property is that a rejected candidate is handed back to the relay
// byte-for-byte. Asserting that needs a capture on the UART side, which had no test seam.
bool feedCdcByte(uint8_t b);
void resetUartCapture();
size_t uartCaptureCount();
uint8_t uartCaptureByte(size_t index);
bool uartCaptureOverflow();
void calResetForTest();
bool calSessionOpen();
uint8_t calStreamAxis();
// ── Status-LED test hooks (test builds only) ──────────────────────────────────
// The pure state-selection + animation logic is exercised without GPIO, TinyUSB,
// or PL011 register reads by injecting inputs and reading back the resolved state /
// captured pin levels. Mirrors the feedByte/cdcCapture pattern above.
void statusInject(uint32_t now, bool mounted, uint32_t lastCdcRxMs, bool faultActive);
void statusResolve();
bool statusFaultStep(uint32_t now, bool rsrError, bool uartRxMoved);
bool statusFaultRestartCheck(uint32_t now);
LedState statusState();
Anim statusAnim();
bool statusRedLevel();
bool statusGreenLevel();
void statusResetForTest();
#endif
} // namespace SimGateway
#endif // ARDUINO_ARCH_RP2040