fix(v16): 6pos logic stuck at 6 when switched on (#5699)

This commit is contained in:
richardclli
2024-12-06 10:28:40 +10:00
committed by GitHub
parent f6084d5b18
commit 3d1e7a914f
+29 -20
View File
@@ -85,29 +85,36 @@ void boardBLInit()
#if defined(SIXPOS_SWITCH_INDEX) #if defined(SIXPOS_SWITCH_INDEX)
uint8_t lastADCState = 0; uint8_t lastADCState = 0;
uint8_t sixPosState = 0; uint8_t sixPosState = 0;
bool dirty = true; bool sixPosInit = false;
bool sixPosDirty = true;
uint16_t getSixPosAnalogValue(uint16_t adcValue) uint16_t getSixPosAnalogValue(uint16_t adcValue)
{ {
uint8_t currentADCState = 0; if (sixPosInit) {
if (adcValue > 3800) uint8_t currentADCState = 0;
currentADCState = 6; if (adcValue > 3800)
else if (adcValue > 3100) currentADCState = 6;
currentADCState = 5; else if (adcValue > 3100)
else if (adcValue > 2300) currentADCState = 5;
currentADCState = 4; else if (adcValue > 2300)
else if (adcValue > 1500) currentADCState = 4;
currentADCState = 3; else if (adcValue > 1500)
else if (adcValue > 1000) currentADCState = 3;
currentADCState = 2; else if (adcValue > 1000)
else if (adcValue > 400) currentADCState = 2;
currentADCState = 1; else if (adcValue > 400)
if (lastADCState != currentADCState) { currentADCState = 1;
lastADCState = currentADCState; if (lastADCState != currentADCState) {
} else if (lastADCState != 0 && lastADCState - 1 != sixPosState) { lastADCState = currentADCState;
sixPosState = lastADCState - 1; } else if (lastADCState != 0 && lastADCState - 1 != sixPosState) {
dirty = true; sixPosState = lastADCState - 1;
sixPosDirty = true;
}
} else if (adcValue < 300) {
// Wait for nothing is pressed to start sampling, otherwise may goes into unknown state
sixPosInit = true;
} }
if (dirty) {
if (sixPosDirty) {
for (uint8_t i = 0; i < 6; i++) { for (uint8_t i = 0; i < 6; i++) {
if (i == sixPosState) if (i == sixPosState)
ws2812_set_color(i, SIXPOS_LED_RED, SIXPOS_LED_GREEN, SIXPOS_LED_BLUE); ws2812_set_color(i, SIXPOS_LED_RED, SIXPOS_LED_GREEN, SIXPOS_LED_BLUE);
@@ -115,7 +122,9 @@ uint16_t getSixPosAnalogValue(uint16_t adcValue)
ws2812_set_color(i, 0, 0, 0); ws2812_set_color(i, 0, 0, 0);
} }
ws2812_update(&_led_timer); ws2812_update(&_led_timer);
sixPosDirty = false;
} }
return (4096/5)*(sixPosState); return (4096/5)*(sixPosState);
} }
#endif #endif