docs: add per-subsystem README files

Add inline documentation for all src/ subsystems covering purpose,
responsibilities, public API, and usage notes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-14 22:16:03 +02:00
co-authored by Claude Sonnet 4.6
parent d4bd990e53
commit d4d8b1c380
29 changed files with 2961 additions and 0 deletions
@@ -0,0 +1,67 @@
# UARTDriver
## Summary
`UARTDriver` is the low-level UART I/O driver for the slave device interface (LCD panel, button board). It operates on `Serial2` using newline-delimited JSON framing. Incoming bytes are accumulated in a 1 KB ring buffer; when a newline is received, the accumulated line is parsed as JSON and the registered callback is invoked. Outgoing messages are sent with an appended newline and a blocking `flush()`. The driver must be polled via `loop()` — it does not run in its own task.
---
## Public API
| Method | Description |
|--------|-------------|
| `UARTDriver(uint8_t txPin, uint8_t rxPin, uint32_t baudRate)` | Constructor. Defaults: TX=GPIO 12, RX=GPIO 13, 115200 baud. Uses `Serial2`. |
| `void begin()` | Initialises `Serial2` with the configured pins and baud rate; flushes any garbage in the RX buffer. |
| `void loop()` | Reads all available bytes from `Serial2` into the line buffer. Calls `processLine()` when `'\n'` or `'\r'` is detected. **Must be called from the main loop or a polling task.** |
| `void setCallback(MessageCallback callback)` | Registers the callback invoked with a parsed `JsonDocument&` for each valid message. |
| `void send(const String& response)` | Sends `response` followed by `'\n'` and calls `flush()`. |
| `bool isReady() const` | Returns `true` after `begin()` has been called successfully. |
| `uint32_t getMessageCount() const` | Number of successfully parsed messages since boot. |
| `uint32_t getErrorCount() const` | Number of JSON parse errors or buffer overflows since boot. |
---
## Initialization
`UARTTransport::begin()` sets the message callback, then calls `UARTDriver::begin()`. The driver is a member of `CommunicationRouter` via `UARTTransport`.
---
## Wire Protocol
- **Physical**: Serial2, TX GPIO 12, RX GPIO 13, 115200 8N1
- **Framing**: newline-delimited (`'\n'` or `'\r'`)
- **Encoding**: JSON, max 1023 bytes per message (1024-byte buffer minus null terminator)
Example message from slave:
```
{"v":2,"cmd":"playback","contents":{"action":"stop"}}\n
```
Example response to slave:
```
{"status":"SUCCESS","type":"playback","message":"Playback command executed"}\n
```
JSON parse errors generate an automatic error response:
```
{"status":"ERROR","type":"parse_error","payload":"<error>"}\n
```
---
## Dependencies
- `HardwareSerial` (Arduino) — `Serial2`
- `ArduinoJson` — JSON parsing (`StaticJsonDocument<1024>`)
- `Logging`
---
## Gotchas / Notes
- **`loop()` must be called.** UART input is not interrupt-driven at the application level. Without regular `loop()` calls, the Serial2 hardware FIFO can overflow.
- **Buffer overflow drops the entire message.** When `_bufferIndex` reaches 1023 without a newline, the buffer is reset and `_errorCount` is incremented. The slave receives no error response in this case.
- **`send()` is blocking.** `Serial.flush()` blocks until the TX hardware buffer drains. At 115200 baud this is approximately 1 byte per 87 µs. A 100-byte response takes ~8.7 ms. This can affect main loop timing if the slave is slow to consume data.
- **`JsonDocument&` is passed by reference** to the callback. The document lives on the stack inside `processLine()` and is valid only for the duration of the callback. Do not store a reference to it.
- **No v1 legacy handling at this level.** The driver passes raw JSON to the callback. Legacy translation (if `"v": 1`) is handled upstream by `CommandBus::dispatch()`.