Link protocol v2 receiver, ESP-NOW leg, Patrol gauge outputs, gear pulse

- 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>
This commit is contained in:
2026-09-21 19:12:38 +02:00
parent 3613387d41
commit 5fe313b7fa
18 changed files with 1658 additions and 514 deletions
+42 -45
View File
@@ -1,15 +1,14 @@
/**
* UART Receiver Module for ESP32-S3-LCD-1.3 (Controller B)
* Link Receiver Module (Controller B)
*
* Receives automotive data packets from Controller A (ESP32-S3-Touch-LCD-5B)
* via UART at 115200 baud, 5Hz update rate.
* 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.
*
* Features:
* - State machine-based packet parsing
* - XOR checksum validation
* - Thread-safe data access
* - Connection monitoring and timeout detection
* - Error statistics tracking
* The namespace keeps its historical name; it is the link receiver for both paths.
*/
#ifndef UART_RECEIVER_H
@@ -18,65 +17,63 @@
#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 {
/**
* @brief UART RX statistics structure
*/
typedef struct {
uint32_t packets_received; // Total valid packets received
uint32_t checksum_errors; // Packets with invalid checksum
uint32_t framing_errors; // Packets with framing errors
uint32_t timeout_errors; // Connection timeout events
uint32_t last_packet_time; // millis() when last valid packet received
uint32_t bytes_received; // Total bytes received
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;
/**
* @brief Initialize UART receiver module
*
* Initializes Serial1 hardware and creates FreeRTOS reception task on Core 0.
*
* @return true if initialization successful, false otherwise
* Create the mutex and the UART1 receive task on Core 0.
*/
bool init();
/**
* @brief Get the latest received automotive data
*
* Thread-safe access to the most recently received valid packet.
*
* @return Copy of the latest automotive_data_t structure
* Validate and apply one complete frame (start..end). Safe to call from the
* ESP-NOW receive callback or the UART task.
*/
automotive_data_t getLatestData();
void processFrame(const uint8_t* frame, size_t len, link_source_t source);
/**
* @brief Get UART reception statistics
*
* @return Copy of current RX statistics
*/
automotive_data_t getLatestData();
uart_rx_stats_t getStats();
/**
* @brief Check if connection is active
*
* Returns true if a valid packet was received within the last timeout period.
*
* @param timeout_ms Timeout in milliseconds (default: 1000ms)
* @return true if connection is active, false if timed out
* True if a DATA frame was applied within timeout_ms, on either transport.
*/
bool isConnected(uint32_t timeout_ms = 1000);
/**
* @brief Reset statistics counters
* True if the given transport delivered a valid frame within timeout_ms.
*/
void resetStats();
bool isSourceAlive(link_source_t source, uint32_t timeout_ms = 1000);
/**
* @brief Get time since last valid packet in milliseconds
*
* @return Milliseconds since last packet, or UINT32_MAX if no packets received
* 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