Files
ESP32-S3-LCD-1.3/include/modules/gauge_outputs.h
T
julian bde1f57f62 Tach output: hardware-timer ISR, slew glide, adaptive dither
The Patrol cluster counts pulses in ~150 rpm quanta, so the needle
stepped no matter how clean the data was. Three-stage rework of the
tach path in gauge_outputs:

- Drop LEDC for the tach: Arduino 2.x ledcChangeFrequency() re-inits
  the timer and truncates the waveform mid-period, corrupting the
  cluster's counting window on every update. IO7 is now toggled from a
  1 MHz hardware-timer ISR with auto-reload - phase-continuous through
  every frequency change and immune to task scheduling (an esp_timer
  version wobbled under WiFi bursts).
- Slew limiter: update() (5 Hz packets) only sets a target; tick(),
  called every 20 ms from loop(), glides the output 25%/tick toward it.
- Adaptive dither: random +/-1.2 Hz (~45 rpm, half the cluster's count
  quantum) per tick while the target moves, fading to zero within 1.2 s
  at steady rpm - emulates the natural pulse jitter that makes factory
  senders look smooth, without flickering the needle at rest.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-23 11:12:48 +02:00

66 lines
2.0 KiB
C++

/**
* 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);
/**
* Glide the tach output toward the target set by update(). Call every ~20 ms.
*
* The Patrol tach counts pulses, and jumping the LEDC frequency once per
* 200 ms data frame makes the needle move in visible rpm stair-steps. This
* spreads each step over several small changes so the needle sweeps.
*/
void tick();
/**
* Print a one-line status (frequency, duty, source of each output) to Serial.
*/
void printStatus();
} // namespace GaugeOutputs
#endif // GAUGE_OUTPUTS_H