Skip to content

Commit 265ce3d

Browse files
committed
Refactor to calculateHysteresis
1 parent 0d4c7ef commit 265ce3d

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/components/analogIO/Wippersnapper_AnalogIO.cpp

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,41 @@ bool Wippersnapper_AnalogIO::encodePinEvent(
311311
return true;
312312
}
313313

314+
/**********************************************************/
315+
/*!
316+
@brief Calculates the hysteresis for the pin value.
317+
@param pin
318+
The desired analog pin to calculate hysteresis for.
319+
@param _pinValThreshHi
320+
The pin's high threshold value.
321+
@param _pinValThreshLow
322+
The pin's low threshold value.
323+
*/
324+
/**********************************************************/
325+
void calculateHysteresis(analogInputPin pin, uint16_t pinValRaw,
326+
uint16_t _pinValThreshHi, uint16_t _pinValThreshLow) {
327+
// All boards ADC values scaled to 16bit, in future we may need to
328+
// adjust dynamically
329+
uint16_t maxDecimalValue = 65535;
330+
331+
// Calculate threshold values - using DEFAULT_HYSTERISIS for first third
332+
// (1/3) of the range, then 2x DEFAULT_HYSTERISIS for the middle 1/3,
333+
// and 4x DEFAULT_HYSTERISIS for the last 1/3. This should allow a more
334+
// wifi blip tolerant threshold for the both ends of the range.
335+
float CURRENT_HYSTERISIS;
336+
if (pinValRaw < maxDecimalValue / 3) {
337+
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS;
338+
} else if (pinValRaw < (maxDecimalValue / 3) * 2) {
339+
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS * 2;
340+
} else {
341+
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS * 4;
342+
}
343+
344+
// get the threshold values for previous pin value
345+
_pinValThreshHi = pin.prvPinVal + CURRENT_HYSTERISIS;
346+
_pinValThreshLow = pin.prvPinVal - CURRENT_HYSTERISIS;
347+
}
348+
314349
/**********************************************************/
315350
/*!
316351
@brief Checks if pin's period is expired.
@@ -386,28 +421,10 @@ void Wippersnapper_AnalogIO::update() {
386421
// pin value
387422
uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName);
388423

389-
// All boards ADC values scaled to 16bit, in future we may need to
390-
// adjust dynamically
391-
uint16_t maxDecimalValue = 65535;
392-
393-
// Calculate threshold values - using DEFAULT_HYSTERISIS for first third
394-
// (1/3) of the range, then 2x DEFAULT_HYSTERISIS for the middle 1/3,
395-
// and 4x DEFAULT_HYSTERISIS for the last 1/3. This should allow a more
396-
// wifi blip tolerant threshold for the both ends of the range.
397-
float CURRENT_HYSTERISIS;
398-
if (pinValRaw < maxDecimalValue / 3) {
399-
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS;
400-
} else if (pinValRaw < (maxDecimalValue / 3) * 2) {
401-
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS * 2;
402-
} else {
403-
CURRENT_HYSTERISIS = maxDecimalValue * DEFAULT_HYSTERISIS * 4;
404-
}
405-
406-
// get the threshold values for previous pin value
407-
uint16_t _pinValThreshHi =
408-
_analog_input_pins[i].prvPinVal + CURRENT_HYSTERISIS;
409-
uint16_t _pinValThreshLow =
410-
_analog_input_pins[i].prvPinVal - CURRENT_HYSTERISIS;
424+
// check if pin value has changed enough
425+
uint16_t _pinValThreshHi, _pinValThreshLow;
426+
calculateHysteresis(_analog_input_pins[i], pinValRaw, _pinValThreshHi,
427+
_pinValThreshLow);
411428

412429
if (_analog_input_pins[i].prvPeriod == 0 ||
413430
pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {

0 commit comments

Comments
 (0)