5fe313b7fa
- protocol header v2 copied byte-identical from the 5B repo - uart_receiver: processFrame() shared by the UART framing task and ESP-NOW, version check, dedupe on packet_id+timestamp, per-transport liveness, CONFIG cache with sequence number - espnow_receiver: STA mode, channel hopping while silent, locks on frames - gauge_outputs: tach on IO7 (LEDC ch2, 14-bit), coolant PWM on IO8 (LEDC ch4, 8-bit), config cached in NVS, overrides applied but not saved - main: gear label from lever + engaged/target (pulses while shifting, red on mismatch), icons driven by A's alert bits, gauge update at 5 Hz, link stats - wiki: temp_stage.cfd (user's old CANNY program) decoded as prior art; controller-link, link-redundancy, patrol-gauge-outputs, open-questions updated with the bench-verified v2 results and calibration procedure Co-Authored-By: Claude <noreply@anthropic.com>
82 lines
2.6 KiB
C++
82 lines
2.6 KiB
C++
/**
|
|
* Link Receiver Module (Controller B)
|
|
*
|
|
* Receives v2 frames from Controller A on two transports:
|
|
* - UART1 (byte stream, framed here with a state machine)
|
|
* - ESP-NOW (whole frames, delivered by EspNowReceiver)
|
|
* Both feed processFrame(). DATA frames are deduplicated on packet_id so a frame
|
|
* that arrives on both paths is applied once; CONFIG frames update the cached
|
|
* link_config_t and bump a sequence number the main loop watches.
|
|
*
|
|
* The namespace keeps its historical name; it is the link receiver for both paths.
|
|
*/
|
|
|
|
#ifndef UART_RECEIVER_H
|
|
#define UART_RECEIVER_H
|
|
|
|
#include <Arduino.h>
|
|
#include "protocol/automotive_data.h"
|
|
|
|
typedef enum {
|
|
LINK_SOURCE_NONE = 0,
|
|
LINK_SOURCE_UART = 1,
|
|
LINK_SOURCE_ESPNOW = 2
|
|
} link_source_t;
|
|
|
|
namespace UARTReceiver {
|
|
|
|
typedef struct {
|
|
uint32_t packets_received; // DATA frames applied (after dedupe)
|
|
uint32_t packets_uart; // valid DATA frames seen on UART
|
|
uint32_t packets_espnow; // valid DATA frames seen on ESP-NOW
|
|
uint32_t duplicates; // DATA frames dropped as already applied
|
|
uint32_t config_received; // CONFIG frames accepted
|
|
uint32_t checksum_errors;
|
|
uint32_t framing_errors;
|
|
uint32_t version_errors; // frames from a different PACKET_VERSION
|
|
uint32_t timeout_errors;
|
|
uint32_t last_packet_time; // millis() of the last applied DATA frame
|
|
uint32_t last_uart_time; // millis() of the last valid UART frame
|
|
uint32_t last_espnow_time; // millis() of the last valid ESP-NOW frame
|
|
uint32_t bytes_received; // UART bytes
|
|
link_source_t last_source; // transport of the last applied DATA frame
|
|
} uart_rx_stats_t;
|
|
|
|
/**
|
|
* Create the mutex and the UART1 receive task on Core 0.
|
|
*/
|
|
bool init();
|
|
|
|
/**
|
|
* Validate and apply one complete frame (start..end). Safe to call from the
|
|
* ESP-NOW receive callback or the UART task.
|
|
*/
|
|
void processFrame(const uint8_t* frame, size_t len, link_source_t source);
|
|
|
|
automotive_data_t getLatestData();
|
|
uart_rx_stats_t getStats();
|
|
|
|
/**
|
|
* True if a DATA frame was applied within timeout_ms, on either transport.
|
|
*/
|
|
bool isConnected(uint32_t timeout_ms = 1000);
|
|
|
|
/**
|
|
* True if the given transport delivered a valid frame within timeout_ms.
|
|
*/
|
|
bool isSourceAlive(link_source_t source, uint32_t timeout_ms = 1000);
|
|
|
|
/**
|
|
* Latest CONFIG payload from A (defaults until one arrives) and a sequence
|
|
* number that increments each time a new one is accepted.
|
|
*/
|
|
link_config_t getConfig();
|
|
uint32_t getConfigSeq();
|
|
|
|
void resetStats();
|
|
uint32_t getTimeSinceLastPacket();
|
|
|
|
} // namespace UARTReceiver
|
|
|
|
#endif // UART_RECEIVER_H
|