refactor(driver): move 6POS tracker into board-level ADC callback (#7309)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
0b4412d582
commit
5b5a0b3e1b
@@ -31,6 +31,10 @@
|
|||||||
#include "ads79xx.h"
|
#include "ads79xx.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(SIXPOS_SWITCH_INDEX)
|
||||||
|
#include "rgb_leds.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "definitions.h"
|
#include "definitions.h"
|
||||||
|
|
||||||
#include "myeeprom.h"
|
#include "myeeprom.h"
|
||||||
@@ -85,6 +89,9 @@ static void adc_wait_completion()
|
|||||||
if (n_ADC_spi > 0) ads79xx_adc_wait_completion(&_ADC_spi[0], _ADC_inputs);
|
if (n_ADC_spi > 0) ads79xx_adc_wait_completion(&_ADC_spi[0], _ADC_inputs);
|
||||||
#endif
|
#endif
|
||||||
stm32_hal_adc_wait_completion(_ADC_adc, n_ADC, _ADC_inputs, n_inputs);
|
stm32_hal_adc_wait_completion(_ADC_adc, n_ADC, _ADC_inputs, n_inputs);
|
||||||
|
#if defined(SIXPOS_SWITCH_INDEX)
|
||||||
|
sixPosUpdateFromAdc();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
const etx_hal_adc_driver_t _adc_driver = {
|
const etx_hal_adc_driver_t _adc_driver = {
|
||||||
|
|||||||
@@ -23,29 +23,36 @@
|
|||||||
|
|
||||||
#if defined(SIXPOS_SWITCH_INDEX) && !defined(SIMU)
|
#if defined(SIXPOS_SWITCH_INDEX) && !defined(SIMU)
|
||||||
|
|
||||||
|
#include "hal/adc_driver.h"
|
||||||
#include "rgb_leds.h"
|
#include "rgb_leds.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
// 6POS switch position tracking.
|
// 6POS switch position tracking.
|
||||||
//
|
//
|
||||||
// The ADC path (mixer task) feeds raw samples into getSixPosAnalogValue()
|
// sixPosUpdateFromAdc() runs once per ADC conversion from the board
|
||||||
// which maintains a sticky "last pressed" position — the switch reads
|
// adc_wait_completion callback. It reads the raw sample out of
|
||||||
// ~0 when no button is held, so a plain ADC threshold would bounce back
|
// adcValues[SIXPOS_SWITCH_INDEX], maintains a sticky "last pressed"
|
||||||
// to position 0. The mixer uses the returned value (0..4096) as the pot
|
// position (the switch reads ~0 when no button is held, so a plain
|
||||||
// position for the multipos source.
|
// threshold would bounce back to position 0), and writes the scaled
|
||||||
|
// 0..4096 sticky value back in place. Downstream consumers
|
||||||
|
// (getAnalogValue, diaganas, the mixer multipos source, …) then see a
|
||||||
|
// stable value without any of them driving the state machine.
|
||||||
//
|
//
|
||||||
// The LED update is NOT done here: _six_pos_state is written by the
|
// The LED update is NOT done here: _six_pos_state is consumed by
|
||||||
// mixer and consumed by rgbLedOnUpdate() below, which runs in the LED
|
// rgbLedOnUpdate() below, which runs in the LED refresh timer task.
|
||||||
// refresh timer task. That keeps the rgb_leds.cpp back buffer
|
// That keeps the rgb_leds.cpp back buffer single-writer (the LED
|
||||||
// single-writer (the LED timer task) for the 6POS range, so Lua's
|
// timer task) for the 6POS range, so Lua's applyRGBLedColors pattern
|
||||||
// applyRGBLedColors pattern cannot race with a mixer-context write.
|
// cannot race with an ADC-context write.
|
||||||
|
|
||||||
static uint8_t _last_adc_state = 0;
|
static uint8_t _last_adc_state = 0;
|
||||||
static volatile uint8_t _six_pos_state = 0;
|
static volatile uint8_t _six_pos_state = 0;
|
||||||
|
|
||||||
uint16_t getSixPosAnalogValue(uint16_t adcValue)
|
void sixPosUpdateFromAdc()
|
||||||
{
|
{
|
||||||
|
uint16_t* values = getAnalogValues();
|
||||||
|
uint16_t adcValue = values[SIXPOS_SWITCH_INDEX];
|
||||||
|
|
||||||
uint8_t current = 0;
|
uint8_t current = 0;
|
||||||
if (adcValue > 3800) current = 6;
|
if (adcValue > 3800) current = 6;
|
||||||
else if (adcValue > 3100) current = 5;
|
else if (adcValue > 3100) current = 5;
|
||||||
@@ -60,7 +67,7 @@ uint16_t getSixPosAnalogValue(uint16_t adcValue)
|
|||||||
_six_pos_state = _last_adc_state - 1;
|
_six_pos_state = _last_adc_state - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (4096 / 5) * _six_pos_state;
|
values[SIXPOS_SWITCH_INDEX] = (4096 / 5) * _six_pos_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rgbLedOnUpdate()
|
void rgbLedOnUpdate()
|
||||||
|
|||||||
@@ -43,3 +43,9 @@ void rgbLedColorApply();
|
|||||||
// indicator) implement this to update their LEDs in the timer task
|
// indicator) implement this to update their LEDs in the timer task
|
||||||
// context, keeping rgb_leds.cpp's back buffer single-writer.
|
// context, keeping rgb_leds.cpp's back buffer single-writer.
|
||||||
void rgbLedOnUpdate();
|
void rgbLedOnUpdate();
|
||||||
|
|
||||||
|
// Invoked by the board ADC wait_completion callback once per conversion
|
||||||
|
// on radios with a 6POS switch. Reads the raw sample from adcValues[],
|
||||||
|
// runs the sticky-position state machine, and writes the scaled sticky
|
||||||
|
// value back in place. Linked only when SIXPOS_SWITCH_INDEX is defined.
|
||||||
|
void sixPosUpdateFromAdc();
|
||||||
|
|||||||
@@ -242,11 +242,6 @@ void adcCalibStore()
|
|||||||
uint16_t getAnalogValue(uint8_t index)
|
uint16_t getAnalogValue(uint8_t index)
|
||||||
{
|
{
|
||||||
if (index >= MAX_ANALOG_INPUTS) return 0;
|
if (index >= MAX_ANALOG_INPUTS) return 0;
|
||||||
#if defined(SIXPOS_SWITCH_INDEX) && !defined(SIMU)
|
|
||||||
if (index == SIXPOS_SWITCH_INDEX)
|
|
||||||
return getSixPosAnalogValue(adcValues[index]);
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
return adcValues[index];
|
return adcValues[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -242,10 +242,6 @@ bool isBacklightEnabled();
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(RADIO_V16)
|
|
||||||
uint16_t getSixPosAnalogValue(uint16_t adcValue);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Audio driver
|
// Audio driver
|
||||||
void audioInit();
|
void audioInit();
|
||||||
|
|
||||||
|
|||||||
@@ -241,10 +241,6 @@ void usbChargerInit();
|
|||||||
bool usbChargerLed();
|
bool usbChargerLed();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(RADIO_V14) || defined(RADIO_V12)
|
|
||||||
uint16_t getSixPosAnalogValue(uint16_t adcValue);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// LED driver
|
// LED driver
|
||||||
void ledInit();
|
void ledInit();
|
||||||
void ledOff();
|
void ledOff();
|
||||||
|
|||||||
Reference in New Issue
Block a user