Skip to content

Commit 9c6187f

Browse files
committed
Correct hysteresis and stop overflow
1 parent 7b10b13 commit 9c6187f

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/components/analogIO/Wippersnapper_AnalogIO.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ bool Wippersnapper_AnalogIO::encodePinEvent(
325325
*/
326326
/**********************************************************/
327327
void calculateHysteresis(analogInputPin pin, uint16_t pinValRaw,
328-
uint16_t *pinValThreshHi, uint16_t *pinValThreshLow) {
328+
uint16_t &pinValThreshHi, uint16_t &pinValThreshLow) {
329329
// All boards ADC values scaled to 16bit, in future we may need to
330330
// adjust dynamically
331331
uint16_t maxDecimalValue = 65535;
@@ -342,10 +342,19 @@ void calculateHysteresis(analogInputPin pin, uint16_t pinValRaw,
342342
} else {
343343
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS * 4;
344344
}
345-
346-
// get the threshold values for previous pin value
347-
*pinValThreshHi = pin.prvPinVal + CURRENT_HYSTERISIS;
348-
*pinValThreshLow = pin.prvPinVal - CURRENT_HYSTERISIS;
345+
// get the threshold values for previous pin value, but don't overflow
346+
float overflowableThHi = pin.prvPinVal + CURRENT_HYSTERISIS;
347+
float overflowableThLow = pin.prvPinVal - CURRENT_HYSTERISIS;
348+
if (overflowableThHi > maxDecimalValue) {
349+
pinValThreshHi = maxDecimalValue;
350+
} else {
351+
pinValThreshHi = overflowableThHi;
352+
}
353+
if (overflowableThLow < 0) {
354+
pinValThreshLow = 0;
355+
} else {
356+
pinValThreshLow = overflowableThLow;
357+
}
349358
}
350359

351360
/**********************************************************/
@@ -425,14 +434,8 @@ void Wippersnapper_AnalogIO::update() {
425434

426435
// check if pin value has changed enough
427436
uint16_t pinValThreshHi, pinValThreshLow;
428-
calculateHysteresis(_analog_input_pins[i], pinValRaw, &pinValThreshHi,
429-
&pinValThreshLow);
430-
WS_DEBUG_PRINT("Returned pinValThreshHi: ");
431-
WS_DEBUG_PRINTLN(pinValThreshHi);
432-
WS_DEBUG_PRINT("Returned pinValThreshLow: ");
433-
WS_DEBUG_PRINTLN(pinValThreshLow);
434-
WS_DEBUG_PRINT("Current pinValRaw: ");
435-
WS_DEBUG_PRINTLN(pinValRaw);
437+
calculateHysteresis(_analog_input_pins[i], pinValRaw, pinValThreshHi,
438+
pinValThreshLow);
436439

437440
if (_analog_input_pins[i].prvPeriod == 0 ||
438441
pinValRaw > pinValThreshHi || pinValRaw < pinValThreshLow) {
@@ -450,12 +453,10 @@ void Wippersnapper_AnalogIO::update() {
450453
// mark last execution time
451454
_analog_input_pins[i].prvPeriod = millis();
452455

453-
} else {
454-
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
456+
} else { // ADC has not changed enough
455457
continue;
456458
}
457-
// set the pin value in the digital pin object for comparison on next
458-
// run
459+
// set the pin value in the digital pin object for comparison next run
459460
_analog_input_pins[i].prvPinVal = pinValRaw;
460461
}
461462
}

0 commit comments

Comments
 (0)