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
+36
View File
@@ -0,0 +1,36 @@
/**
* ESP-NOW Receiver Module (Controller B)
*
* Wireless leg of the A -> B link. Frames are byte-identical to the UART ones
* and are handed to UARTReceiver::processFrame(), which dedupes on packet_id.
*
* Channel discovery: A transmits on ESPNOW_CHANNEL unless it is associated to
* an access point. B listens on ESPNOW_CHANNEL first and, while it hears
* nothing, hops through channels 1..13 until frames arrive, then locks.
*/
#ifndef ESPNOW_RECEIVER_H
#define ESPNOW_RECEIVER_H
#include <Arduino.h>
namespace EspNowReceiver {
/**
* Bring the radio up in STA mode and register the receive callback.
* @return true if ESP-NOW is listening
*/
bool init();
/**
* Channel-hopping housekeeping. Call from loop().
*/
void tick();
bool isReady();
uint8_t currentChannel();
uint32_t framesReceived();
} // namespace EspNowReceiver
#endif // ESPNOW_RECEIVER_H
+56
View File
@@ -0,0 +1,56 @@
/**
* Gauge Outputs Module (Controller B)
*
* Drives the Nissan Patrol cluster from the data Controller A sends:
* - tachometer: variable-frequency square wave on IO7 (LEDC timer 1)
* - coolant gauge: fixed-frequency PWM with variable duty on IO8 (LEDC timer 2)
*
* Everything tunable comes from link_config_t (pushed by A in CONFIG frames,
* cached in NVS so a reboot of B does not wait for A). Both outputs are held
* OFF while the link is down or the source value is invalid, unless A has set
* a calibration override.
*
* External driver stages are mandatory: 3.3 V logic drives neither gauge
* directly. See wiki/pages/patrol-gauge-outputs.md
*/
#ifndef GAUGE_OUTPUTS_H
#define GAUGE_OUTPUTS_H
#include <Arduino.h>
#include "protocol/automotive_data.h"
namespace GaugeOutputs {
#define GAUGE_TACH_PIN 7
#define GAUGE_TEMP_PIN 8
/**
* Load the last config from NVS (defaults if none) and set up both LEDC channels
* with the outputs OFF.
*/
void init();
/**
* Apply a configuration received from A. Persists it to NVS when persist is true
* (overrides arrive unsaved by design: A does not persist them either).
*/
void applyConfig(const link_config_t& cfg, bool persist);
link_config_t getConfig();
/**
* Recompute both outputs from the latest data. Call at the data rate (5 Hz).
* @param d latest automotive_data_t from A
* @param link_ok false when no valid frame has arrived within the link timeout
*/
void update(const automotive_data_t& d, bool link_ok);
/**
* Print a one-line status (frequency, duty, source of each output) to Serial.
*/
void printStatus();
} // namespace GaugeOutputs
#endif // GAUGE_OUTPUTS_H
+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