Skip to content

Commit 3e14c55

Browse files
committed
WIP: swapping hysteresis logic
1 parent 708f830 commit 3e14c55

File tree

3 files changed

+104
-12
lines changed

3 files changed

+104
-12
lines changed

src/Wippersnapper.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
// Define actual debug output functions when necessary.
4848
#ifdef WS_DEBUG
4949
#define WS_DEBUG_PRINT(...) \
50-
{ WS_PRINTER.print(__VA_ARGS__); } ///< Prints debug output.
50+
{ WS_PRINTER.print(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints debug output.
5151
#define WS_DEBUG_PRINTLN(...) \
52-
{ WS_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output.
52+
{ WS_PRINTER.println(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints line from debug output.
5353
#define WS_DEBUG_PRINTHEX(...) \
54-
{ WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints debug output.
54+
{ WS_PRINTER.print(__VA_ARGS__, HEX); WS_PRINTER.flush(); } ///< Prints debug output.
5555
#else
5656
#define WS_DEBUG_PRINT(...) \
5757
{} ///< Prints debug output

src/components/analogIO/Wippersnapper_AnalogIO.cpp

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,34 +370,126 @@ void Wippersnapper_AnalogIO::update() {
370370
}
371371
// Does the pin execute on_change?
372372
else if (_analog_input_pins[i].period == 0L) {
373-
373+
if (_analog_input_pins[i].prvPeriod == 0L)
374+
{
375+
// last time was a clean event, passed hyteresis or 300ms had elapsed
376+
WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, "
377+
"passed hyteresis or 500ms had elapsed");
378+
}
379+
else
380+
{
381+
// We're waiting 300ms before posting, to avoid a flood of events
382+
WS_DEBUG_PRINTLN("prvPeriod is not 0, probably waiting 300ms before posting, "
383+
"to avoid a flood of events");
384+
WS_DEBUG_PRINT("CurrentTime: ");
385+
WS_DEBUG_PRINTLN(millis());
386+
WS_DEBUG_PRINT("PrvPeriod: ");
387+
WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod);
388+
WS_DEBUG_PRINT("Diff: ");
389+
WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod);
390+
}
374391
// note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv
375392
// pin value
376393
uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName);
394+
WS_DEBUG_PRINT("PinValRaw: ");
395+
WS_DEBUG_PRINTLN(pinValRaw);
396+
397+
double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0)
398+
double lastLogValue = log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0)
399+
WS_DEBUG_PRINT("CurrentLogValue: ");
400+
WS_DEBUG_PRINTLN(currentLogValue);
401+
WS_DEBUG_PRINT("LastLogValue: ");
402+
WS_DEBUG_PRINTLN(lastLogValue);
403+
bool passed_hysterisys = false;
404+
// Check if the logarithmic change exceeds the threshold
405+
if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS)
406+
{
407+
passed_hysterisys = true;
408+
WS_DEBUG_PRINTLN("ADC passed hysteresis");
409+
} else {
410+
WS_DEBUG_PRINTLN("ADC did not pass hysteresis");
411+
}
377412

413+
414+
// old technique
378415
uint16_t _pinValThreshHi =
379416
_analog_input_pins[i].prvPinVal +
380417
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
381418
uint16_t _pinValThreshLow =
382419
_analog_input_pins[i].prvPinVal -
383420
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
421+
WS_DEBUG_PRINT("PinValThreshHi: ");
422+
WS_DEBUG_PRINTLN(_pinValThreshHi);
423+
WS_DEBUG_PRINT("PinValThreshLow: ");
424+
WS_DEBUG_PRINTLN(_pinValThreshLow);
425+
384426

385-
if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
386-
// Perform voltage conversion if we need to
387-
if (_analog_input_pins[i].readMode ==
388-
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
389-
pinValVolts = pinValRaw * getAref() / 65536;
390-
}
391427

428+
// if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
429+
if (_analog_input_pins[i].readMode ==
430+
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE)
431+
{
432+
pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName);
433+
WS_DEBUG_PRINT("PinValVolts: ");
434+
WS_DEBUG_PRINTLN(pinValVolts);
435+
}
436+
else if (
437+
_analog_input_pins[i].readMode ==
438+
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE)
439+
{
440+
WS_DEBUG_PRINT("PinValRaw: ");
441+
WS_DEBUG_PRINTLN(pinValRaw);
442+
}
443+
else
444+
{
445+
WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine "
446+
"analog read mode!");
447+
pinValRaw = 0.0;
448+
}
449+
if (passed_hysterisys && ((long)millis() - _analog_input_pins[i].prvPeriod) > 300)
450+
{
451+
// WS_DEBUG_PRINTLN("ADC has changed enough, publishing event...");
452+
_analog_input_pins[i].prvPinVal = pinValRaw;
453+
_analog_input_pins[i].prvPeriod = millis();
392454
// Publish pin event to IO
393455
encodePinEvent(_analog_input_pins[i].pinName,
394456
_analog_input_pins[i].readMode, pinValRaw,
395457
pinValVolts);
458+
}
459+
else if (_analog_input_pins[i].prvPeriod != 0L && pinValRaw != _analog_input_pins[i].prvPinVal &&
460+
((long)millis() - _analog_input_pins[i].prvPeriod) > 300)
461+
{
462+
// failed hysterisys, but we were waiting 500ms before posting, to avoid a flood of events
463+
WS_DEBUG_PRINTLN("ADC has only mildly changed, but we were waiting 300ms before posting, to avoid a flood of events and this is the final value");
464+
_analog_input_pins[i].prvPeriod = 0L;
465+
_analog_input_pins[i].prvPinVal = pinValRaw;
466+
// Publish pin event to IO
467+
encodePinEvent(_analog_input_pins[i].pinName,
468+
_analog_input_pins[i].readMode, pinValRaw,
469+
pinValVolts);
396470

397-
} else {
471+
}
472+
else
473+
{
398474
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
475+
_analog_input_pins[i].prvPeriod = millis();
476+
_analog_input_pins[i].prvPinVal = pinValRaw;
399477
continue;
400478
}
479+
480+
// if (_analog_input_pins[i].readMode ==
481+
// wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
482+
// pinValVolts = pinValRaw * getAref() / 65536;
483+
// }
484+
485+
// // Publish pin event to IO
486+
// encodePinEvent(_analog_input_pins[i].pinName,
487+
// _analog_input_pins[i].readMode, pinValRaw,
488+
// pinValVolts);
489+
// } else {
490+
// // WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
491+
// continue;
492+
// }
401493
// set the pin value in the digital pin object for comparison on next
402494
// run
403495
_analog_input_pins[i].prvPinVal = pinValRaw;

src/components/analogIO/Wippersnapper_AnalogIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "Wippersnapper.h"
2020

21-
#define DEFAULT_HYSTERISIS 0.02 ///< Default DEFAULT_HYSTERISIS of 2%
21+
#define DEFAULT_HYSTERISIS 0.04 ///< Default DEFAULT_HYSTERISIS of 0.04 (log10)
2222

2323
/** Data about an analog input pin */
2424
struct analogInputPin {

0 commit comments

Comments
 (0)