From 8133aba470cf91ec1c71c2826d38eee27db08b18 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 20 Aug 2024 22:37:32 +0100 Subject: [PATCH 01/11] Switch Analog Voltage on esp32 to use analogReadMilliVolts --- src/components/analogIO/Wippersnapper_AnalogIO.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index 660208de2..1655e68b4 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -232,8 +232,12 @@ uint16_t Wippersnapper_AnalogIO::getPinValue(int pin) { */ /**********************************************************/ float Wippersnapper_AnalogIO::getPinValueVolts(int pin) { +#ifdef ARDUINO_ARCH_ESP32 + return analogReadMilliVolts(pin) / 1000.0; +#else uint16_t rawValue = getPinValue(pin); return rawValue * getAref() / 65536; +#endif } /******************************************************************/ From e9852b64f39e921ccb88dd2eaf376b9b7ba1b24a Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 20 Aug 2024 23:01:41 +0100 Subject: [PATCH 02/11] Add logging to check which branch is executing --- src/components/analogIO/Wippersnapper_AnalogIO.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index 1655e68b4..95a2a0837 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -87,8 +87,8 @@ void Wippersnapper_AnalogIO::setADCResolution(int resolution) { analogReadResolution(16); _nativeResolution = 12; #elif defined(ARDUINO_ARCH_ESP32) - scaleAnalogRead = true; - _nativeResolution = 13; + scaleAnalogRead = false; // handled in bsp + _nativeResolution = 13; // S3 ADC is 13-bit, others are 12-bit #elif defined(ARDUINO_ARCH_RP2040) scaleAnalogRead = true; _nativeResolution = 10; @@ -96,7 +96,6 @@ void Wippersnapper_AnalogIO::setADCResolution(int resolution) { scaleAnalogRead = true; _nativeResolution = 10; #endif - _adcResolution = resolution; } @@ -233,8 +232,10 @@ uint16_t Wippersnapper_AnalogIO::getPinValue(int pin) { /**********************************************************/ float Wippersnapper_AnalogIO::getPinValueVolts(int pin) { #ifdef ARDUINO_ARCH_ESP32 + WS_DEBUG_PRINTLN("ESP32: Using analogReadMilliVolts()"); return analogReadMilliVolts(pin) / 1000.0; #else + WS_DEBUG_PRINTLN("Using old getPinValueVolts()"); uint16_t rawValue = getPinValue(pin); return rawValue * getAref() / 65536; #endif From c8431324c92e1d4978a3d14782b2f938fcf7eba0 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 21 Aug 2024 16:12:24 +0100 Subject: [PATCH 03/11] Test a wild theory about Serial1 --- src/components/analogIO/Wippersnapper_AnalogIO.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index 95a2a0837..e2d4ad389 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -15,6 +15,7 @@ */ #include "Wippersnapper_AnalogIO.h" +#include "Wippersnapper.h" /***********************************************************************************/ /*! @@ -87,7 +88,7 @@ void Wippersnapper_AnalogIO::setADCResolution(int resolution) { analogReadResolution(16); _nativeResolution = 12; #elif defined(ARDUINO_ARCH_ESP32) - scaleAnalogRead = false; // handled in bsp + scaleAnalogRead = true; // probably should be false, handled in bsp _nativeResolution = 13; // S3 ADC is 13-bit, others are 12-bit #elif defined(ARDUINO_ARCH_RP2040) scaleAnalogRead = true; From 57fe2d93f657ddfd47e49ab100237162b3a466eb Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 22 Aug 2024 16:31:28 +0100 Subject: [PATCH 04/11] WIP: swapping hysteresis logic --- src/Wippersnapper.h | 6 +- .../analogIO/Wippersnapper_AnalogIO.cpp | 108 ++++++++++++++++-- .../analogIO/Wippersnapper_AnalogIO.h | 2 +- 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index ccc55c9f9..1b069da80 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -47,11 +47,11 @@ // Define actual debug output functions when necessary. #ifdef WS_DEBUG #define WS_DEBUG_PRINT(...) \ - { WS_PRINTER.print(__VA_ARGS__); } ///< Prints debug output. + { WS_PRINTER.print(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints debug output. #define WS_DEBUG_PRINTLN(...) \ - { WS_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output. + { WS_PRINTER.println(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints line from debug output. #define WS_DEBUG_PRINTHEX(...) \ - { WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints debug output. + { WS_PRINTER.print(__VA_ARGS__, HEX); WS_PRINTER.flush(); } ///< Prints debug output. #else #define WS_DEBUG_PRINT(...) \ {} ///< Prints debug output diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index e2d4ad389..d13d38a89 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -370,34 +370,126 @@ void Wippersnapper_AnalogIO::update() { } // Does the pin execute on_change? else if (_analog_input_pins[i].period == 0L) { - + if (_analog_input_pins[i].prvPeriod == 0L) + { + // last time was a clean event, passed hyteresis or 300ms had elapsed + WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " + "passed hyteresis or 500ms had elapsed"); + } + else + { + // We're waiting 300ms before posting, to avoid a flood of events + WS_DEBUG_PRINTLN("prvPeriod is not 0, probably waiting 300ms before posting, " + "to avoid a flood of events"); + WS_DEBUG_PRINT("CurrentTime: "); + WS_DEBUG_PRINTLN(millis()); + WS_DEBUG_PRINT("PrvPeriod: "); + WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod); + WS_DEBUG_PRINT("Diff: "); + WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); + } // note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv // pin value uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName); + WS_DEBUG_PRINT("PinValRaw: "); + WS_DEBUG_PRINTLN(pinValRaw); + + double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0) + double lastLogValue = log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) + WS_DEBUG_PRINT("CurrentLogValue: "); + WS_DEBUG_PRINTLN(currentLogValue); + WS_DEBUG_PRINT("LastLogValue: "); + WS_DEBUG_PRINTLN(lastLogValue); + bool passed_hysterisys = false; + // Check if the logarithmic change exceeds the threshold + if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) + { + passed_hysterisys = true; + WS_DEBUG_PRINTLN("ADC passed hysteresis"); + } else { + WS_DEBUG_PRINTLN("ADC did not pass hysteresis"); + } + + // old technique uint16_t _pinValThreshHi = _analog_input_pins[i].prvPinVal + (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); uint16_t _pinValThreshLow = _analog_input_pins[i].prvPinVal - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); + WS_DEBUG_PRINT("PinValThreshHi: "); + WS_DEBUG_PRINTLN(_pinValThreshHi); + WS_DEBUG_PRINT("PinValThreshLow: "); + WS_DEBUG_PRINTLN(_pinValThreshLow); + - if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { - // Perform voltage conversion if we need to - if (_analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { - pinValVolts = pinValRaw * getAref() / 65536; - } + // if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { + if (_analog_input_pins[i].readMode == + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) + { + pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); + WS_DEBUG_PRINT("PinValVolts: "); + WS_DEBUG_PRINTLN(pinValVolts); + } + else if ( + _analog_input_pins[i].readMode == + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) + { + WS_DEBUG_PRINT("PinValRaw: "); + WS_DEBUG_PRINTLN(pinValRaw); + } + else + { + WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " + "analog read mode!"); + pinValRaw = 0.0; + } + if (passed_hysterisys && ((long)millis() - _analog_input_pins[i].prvPeriod) > 300) + { + // WS_DEBUG_PRINTLN("ADC has changed enough, publishing event..."); + _analog_input_pins[i].prvPinVal = pinValRaw; + _analog_input_pins[i].prvPeriod = millis(); // Publish pin event to IO encodePinEvent(_analog_input_pins[i].pinName, _analog_input_pins[i].readMode, pinValRaw, pinValVolts); + } + else if (_analog_input_pins[i].prvPeriod != 0L && pinValRaw != _analog_input_pins[i].prvPinVal && + ((long)millis() - _analog_input_pins[i].prvPeriod) > 300) + { + // failed hysterisys, but we were waiting 500ms before posting, to avoid a flood of events + 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"); + _analog_input_pins[i].prvPeriod = 0L; + _analog_input_pins[i].prvPinVal = pinValRaw; + // Publish pin event to IO + encodePinEvent(_analog_input_pins[i].pinName, + _analog_input_pins[i].readMode, pinValRaw, + pinValVolts); - } else { + } + else + { // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); + _analog_input_pins[i].prvPeriod = millis(); + _analog_input_pins[i].prvPinVal = pinValRaw; continue; } + + // if (_analog_input_pins[i].readMode == + // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { + // pinValVolts = pinValRaw * getAref() / 65536; + // } + + // // Publish pin event to IO + // encodePinEvent(_analog_input_pins[i].pinName, + // _analog_input_pins[i].readMode, pinValRaw, + // pinValVolts); + // } else { + // // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); + // continue; + // } // set the pin value in the digital pin object for comparison on next // run _analog_input_pins[i].prvPinVal = pinValRaw; diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.h b/src/components/analogIO/Wippersnapper_AnalogIO.h index 91b62a489..02b13ace4 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.h +++ b/src/components/analogIO/Wippersnapper_AnalogIO.h @@ -18,7 +18,7 @@ #include "Wippersnapper.h" -#define DEFAULT_HYSTERISIS 0.02 ///< Default DEFAULT_HYSTERISIS of 2% +#define DEFAULT_HYSTERISIS 0.04 ///< Default DEFAULT_HYSTERISIS of 0.04 (log10) /** Data about an analog input pin */ struct analogInputPin { From 603eaa8886d26630fe3a9cc1cbc845fbc7f57f10 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Fri, 23 Aug 2024 10:47:10 +0100 Subject: [PATCH 05/11] WIP: Tweak Process --- .../analogIO/Wippersnapper_AnalogIO.cpp | 311 +++++++++--------- 1 file changed, 162 insertions(+), 149 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index d13d38a89..fa49b7df4 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -338,162 +338,175 @@ void Wippersnapper_AnalogIO::update() { // Process analog input pins for (int i = 0; i < _totalAnalogInputPins; i++) { // TODO: Can we collapse the conditionals below? - if (_analog_input_pins[i].enabled == true) { - - // Does the pin execute on-period? - if ((long)millis() - _analog_input_pins[i].prvPeriod > - _analog_input_pins[i].period && - _analog_input_pins[i].period != 0L) { - WS_DEBUG_PRINT("Executing periodic event on A"); - WS_DEBUG_PRINTLN(_analog_input_pins[i].pinName); - - // Read from analog pin - if (_analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { - pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); - } else if ( - _analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) { - pinValRaw = getPinValue(_analog_input_pins[i].pinName); - } else { - WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " - "analog read mode!"); - pinValRaw = 0.0; - } - - // Publish a new pin event - encodePinEvent(_analog_input_pins[i].pinName, - _analog_input_pins[i].readMode, pinValRaw, pinValVolts); + if (!_analog_input_pins[i].enabled){ + continue; + } - // IMPT - reset the digital pin - _analog_input_pins[i].prvPeriod = millis(); + // Does the pin execute on-period? + if (timerExpired((long)millis(), _analog_input_pins[i])) { + WS_DEBUG_PRINT("Executing periodic event on A"); + WS_DEBUG_PRINTLN(_analog_input_pins[i].pinName); + + // Read from analog pin + if (_analog_input_pins[i].readMode == + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { + pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); + } else if ( + _analog_input_pins[i].readMode == + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) { + pinValRaw = getPinValue(_analog_input_pins[i].pinName); + } else { + WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " + "analog read mode!"); + pinValRaw = 0.0; } - // Does the pin execute on_change? - else if (_analog_input_pins[i].period == 0L) { - if (_analog_input_pins[i].prvPeriod == 0L) - { - // last time was a clean event, passed hyteresis or 300ms had elapsed - WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " - "passed hyteresis or 500ms had elapsed"); - } - else - { - // We're waiting 300ms before posting, to avoid a flood of events - WS_DEBUG_PRINTLN("prvPeriod is not 0, probably waiting 300ms before posting, " - "to avoid a flood of events"); - WS_DEBUG_PRINT("CurrentTime: "); - WS_DEBUG_PRINTLN(millis()); - WS_DEBUG_PRINT("PrvPeriod: "); - WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod); - WS_DEBUG_PRINT("Diff: "); - WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); - } - // note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv - // pin value - uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName); + + // Publish a new pin event + encodePinEvent(_analog_input_pins[i].pinName, + _analog_input_pins[i].readMode, pinValRaw, pinValVolts); + + // IMPT - reset the digital pin + _analog_input_pins[i].prvPeriod = millis(); + } + // Does the pin execute on_change? + else if (_analog_input_pins[i].period == 0L) { + + WS_DEBUG_PRINT("CurrentTime: "); + WS_DEBUG_PRINTLN(millis()); + WS_DEBUG_PRINT("PrvPeriod: "); + WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod); + WS_DEBUG_PRINT("Diff: "); + WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); + + if (_analog_input_pins[i].prvPeriod == 0L) + { + // last time was a clean event, passed hyteresis or 300ms had elapsed + WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " + "passed hyteresis or 500ms had elapsed"); + } + else + { + // We're waiting 300ms before posting, to avoid a flood of events + WS_DEBUG_PRINTLN("prvPeriod is not 0, probably waiting 300ms before posting, " + "to avoid a flood of events"); + } + // note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv + // pin value + uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName); + WS_DEBUG_PRINT("PinValRaw: "); + WS_DEBUG_PRINTLN(pinValRaw); + + double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0) + double lastLogValue = log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) + WS_DEBUG_PRINT("CurrentLogValue: "); + WS_DEBUG_PRINTLN(currentLogValue); + WS_DEBUG_PRINT("LastLogValue: "); + WS_DEBUG_PRINTLN(lastLogValue); + bool passed_hysterisys = false; + // Check if the logarithmic change exceeds the threshold + if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) + { + passed_hysterisys = true; + WS_DEBUG_PRINTLN("ADC passed hysteresis"); + } else { + WS_DEBUG_PRINTLN("ADC did not pass hysteresis"); + } + + + // old technique + uint16_t _pinValThreshHi = + _analog_input_pins[i].prvPinVal + + (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); + uint16_t _pinValThreshLow = + _analog_input_pins[i].prvPinVal - + (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); + WS_DEBUG_PRINT("PinValThreshHi: "); + WS_DEBUG_PRINTLN(_pinValThreshHi); + WS_DEBUG_PRINT("PinValThreshLow: "); + WS_DEBUG_PRINTLN(_pinValThreshLow); + + + + if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { + // passed_hysterisys = true; + WS_DEBUG_PRINTLN("ADC passed OLD hysteresis"); + } else { + WS_DEBUG_PRINTLN("ADC did not pass OLD hysteresis"); + } + + if (_analog_input_pins[i].readMode == + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) + { + pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); + WS_DEBUG_PRINT("PinValVolts: "); + WS_DEBUG_PRINTLN(pinValVolts); + } + else if ( + _analog_input_pins[i].readMode == + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) + { + // already fetched raw value, just print it WS_DEBUG_PRINT("PinValRaw: "); WS_DEBUG_PRINTLN(pinValRaw); + } + else + { + WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " + "analog read mode!"); + pinValRaw = 0.0; + } - double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0) - double lastLogValue = log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) - WS_DEBUG_PRINT("CurrentLogValue: "); - WS_DEBUG_PRINTLN(currentLogValue); - WS_DEBUG_PRINT("LastLogValue: "); - WS_DEBUG_PRINTLN(lastLogValue); - bool passed_hysterisys = false; - // Check if the logarithmic change exceeds the threshold - if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) - { - passed_hysterisys = true; - WS_DEBUG_PRINTLN("ADC passed hysteresis"); - } else { - WS_DEBUG_PRINTLN("ADC did not pass hysteresis"); - } - - - // old technique - uint16_t _pinValThreshHi = - _analog_input_pins[i].prvPinVal + - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); - uint16_t _pinValThreshLow = - _analog_input_pins[i].prvPinVal - - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); - WS_DEBUG_PRINT("PinValThreshHi: "); - WS_DEBUG_PRINTLN(_pinValThreshHi); - WS_DEBUG_PRINT("PinValThreshLow: "); - WS_DEBUG_PRINTLN(_pinValThreshLow); - - - - // if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { - if (_analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) - { - pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); - WS_DEBUG_PRINT("PinValVolts: "); - WS_DEBUG_PRINTLN(pinValVolts); - } - else if ( - _analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) - { - WS_DEBUG_PRINT("PinValRaw: "); - WS_DEBUG_PRINTLN(pinValRaw); - } - else - { - WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " - "analog read mode!"); - pinValRaw = 0.0; - } - if (passed_hysterisys && ((long)millis() - _analog_input_pins[i].prvPeriod) > 300) - { - // WS_DEBUG_PRINTLN("ADC has changed enough, publishing event..."); - _analog_input_pins[i].prvPinVal = pinValRaw; - _analog_input_pins[i].prvPeriod = millis(); - // Publish pin event to IO - encodePinEvent(_analog_input_pins[i].pinName, - _analog_input_pins[i].readMode, pinValRaw, - pinValVolts); - } - else if (_analog_input_pins[i].prvPeriod != 0L && pinValRaw != _analog_input_pins[i].prvPinVal && - ((long)millis() - _analog_input_pins[i].prvPeriod) > 300) - { - // failed hysterisys, but we were waiting 500ms before posting, to avoid a flood of events - 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"); - _analog_input_pins[i].prvPeriod = 0L; - _analog_input_pins[i].prvPinVal = pinValRaw; - // Publish pin event to IO - encodePinEvent(_analog_input_pins[i].pinName, - _analog_input_pins[i].readMode, pinValRaw, - pinValVolts); - - } - else - { - // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); - _analog_input_pins[i].prvPeriod = millis(); - _analog_input_pins[i].prvPinVal = pinValRaw; - continue; - } - - // if (_analog_input_pins[i].readMode == - // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { - // pinValVolts = pinValRaw * getAref() / 65536; - // } - - // // Publish pin event to IO - // encodePinEvent(_analog_input_pins[i].pinName, - // _analog_input_pins[i].readMode, pinValRaw, - // pinValVolts); - // } else { - // // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); - // continue; - // } - // set the pin value in the digital pin object for comparison on next - // run + // prvPeriod is 0 means we just sent a final movement event, so we can + // send another one immediately if the ADC has changed enough while also + // waiting 200ms before posting the next final movement event (or + // continued movement events), to avoid a flood of events when twisting pots + if (passed_hysterisys && + (((long)millis() - _analog_input_pins[i].prvPeriod) > 200 || + _analog_input_pins[i].prvPeriod == 0L)) + { + WS_DEBUG_PRINTLN("ADC has changed enough, publishing event..."); _analog_input_pins[i].prvPinVal = pinValRaw; + _analog_input_pins[i].prvPeriod = millis(); + // Publish pin event to IO + encodePinEvent(_analog_input_pins[i].pinName, + _analog_input_pins[i].readMode, pinValRaw, pinValVolts); + // } else if (_analog_input_pins[i].prvPeriod != 0L && + // pinValRaw != _analog_input_pins[i].prvPinVal && + // ((long)millis() - _analog_input_pins[i].prvPeriod) > 200) { + // // failed hysterisys, but we were waiting 500ms before posting, to avoid + // // a flood of events + // WS_DEBUG_PRINTLN( + // "ADC has only mildly changed, but we were waiting 200ms before " + // "posting, to avoid a flood of events and this is the final value"); + // _analog_input_pins[i].prvPeriod = 0L; + // _analog_input_pins[i].prvPinVal = pinValRaw; + // // Publish pin event to IO + // encodePinEvent(_analog_input_pins[i].pinName, + // _analog_input_pins[i].readMode, pinValRaw, pinValVolts); + + // } else { + // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); + // _analog_input_pins[i].prvPeriod = millis(); + // _analog_input_pins[i].prvPinVal = pinValRaw; + // continue; } + + // if (_analog_input_pins[i].readMode == + // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { + // pinValVolts = pinValRaw * getAref() / 65536; + // } + + // // Publish pin event to IO + // encodePinEvent(_analog_input_pins[i].pinName, + // _analog_input_pins[i].readMode, pinValRaw, + // pinValVolts); + // } else { + // // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); + // continue; + // } + // set the pin value in the digital pin object for comparison on next + // run } + } } \ No newline at end of file From 8c05f82b07314737561499bcce56601cf2943894 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Fri, 23 Aug 2024 10:47:58 +0100 Subject: [PATCH 06/11] format analogIO --- .../analogIO/Wippersnapper_AnalogIO.cpp | 90 +++++++++---------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index fa49b7df4..b4c3dd2be 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -89,7 +89,7 @@ void Wippersnapper_AnalogIO::setADCResolution(int resolution) { _nativeResolution = 12; #elif defined(ARDUINO_ARCH_ESP32) scaleAnalogRead = true; // probably should be false, handled in bsp - _nativeResolution = 13; // S3 ADC is 13-bit, others are 12-bit + _nativeResolution = 13; // S3 ADC is 13-bit, others are 12-bit #elif defined(ARDUINO_ARCH_RP2040) scaleAnalogRead = true; _nativeResolution = 10; @@ -338,7 +338,7 @@ void Wippersnapper_AnalogIO::update() { // Process analog input pins for (int i = 0; i < _totalAnalogInputPins; i++) { // TODO: Can we collapse the conditionals below? - if (!_analog_input_pins[i].enabled){ + if (!_analog_input_pins[i].enabled) { continue; } @@ -357,13 +357,13 @@ void Wippersnapper_AnalogIO::update() { pinValRaw = getPinValue(_analog_input_pins[i].pinName); } else { WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " - "analog read mode!"); + "analog read mode!"); pinValRaw = 0.0; } // Publish a new pin event encodePinEvent(_analog_input_pins[i].pinName, - _analog_input_pins[i].readMode, pinValRaw, pinValVolts); + _analog_input_pins[i].readMode, pinValRaw, pinValVolts); // IMPT - reset the digital pin _analog_input_pins[i].prvPeriod = millis(); @@ -378,17 +378,15 @@ void Wippersnapper_AnalogIO::update() { WS_DEBUG_PRINT("Diff: "); WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); - if (_analog_input_pins[i].prvPeriod == 0L) - { + if (_analog_input_pins[i].prvPeriod == 0L) { // last time was a clean event, passed hyteresis or 300ms had elapsed WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " - "passed hyteresis or 500ms had elapsed"); - } - else - { + "passed hyteresis or 500ms had elapsed"); + } else { // We're waiting 300ms before posting, to avoid a flood of events - WS_DEBUG_PRINTLN("prvPeriod is not 0, probably waiting 300ms before posting, " - "to avoid a flood of events"); + WS_DEBUG_PRINTLN( + "prvPeriod is not 0, probably waiting 300ms before posting, " + "to avoid a flood of events"); } // note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv // pin value @@ -397,22 +395,21 @@ void Wippersnapper_AnalogIO::update() { WS_DEBUG_PRINTLN(pinValRaw); double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0) - double lastLogValue = log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) + double lastLogValue = + log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) WS_DEBUG_PRINT("CurrentLogValue: "); WS_DEBUG_PRINTLN(currentLogValue); WS_DEBUG_PRINT("LastLogValue: "); WS_DEBUG_PRINTLN(lastLogValue); bool passed_hysterisys = false; // Check if the logarithmic change exceeds the threshold - if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) - { + if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) { passed_hysterisys = true; WS_DEBUG_PRINTLN("ADC passed hysteresis"); } else { WS_DEBUG_PRINTLN("ADC did not pass hysteresis"); } - // old technique uint16_t _pinValThreshHi = _analog_input_pins[i].prvPinVal + @@ -425,8 +422,6 @@ void Wippersnapper_AnalogIO::update() { WS_DEBUG_PRINT("PinValThreshLow: "); WS_DEBUG_PRINTLN(_pinValThreshLow); - - if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { // passed_hysterisys = true; WS_DEBUG_PRINTLN("ADC passed OLD hysteresis"); @@ -435,56 +430,55 @@ void Wippersnapper_AnalogIO::update() { } if (_analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) - { + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); WS_DEBUG_PRINT("PinValVolts: "); WS_DEBUG_PRINTLN(pinValVolts); - } - else if ( + } else if ( _analog_input_pins[i].readMode == - wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) - { + wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) { // already fetched raw value, just print it WS_DEBUG_PRINT("PinValRaw: "); WS_DEBUG_PRINTLN(pinValRaw); - } - else - { + } else { WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " - "analog read mode!"); + "analog read mode!"); pinValRaw = 0.0; } // prvPeriod is 0 means we just sent a final movement event, so we can // send another one immediately if the ADC has changed enough while also // waiting 200ms before posting the next final movement event (or - // continued movement events), to avoid a flood of events when twisting pots + // continued movement events), to avoid a flood of events when twisting + // pots if (passed_hysterisys && (((long)millis() - _analog_input_pins[i].prvPeriod) > 200 || - _analog_input_pins[i].prvPeriod == 0L)) - { + _analog_input_pins[i].prvPeriod == 0L)) { WS_DEBUG_PRINTLN("ADC has changed enough, publishing event..."); _analog_input_pins[i].prvPinVal = pinValRaw; _analog_input_pins[i].prvPeriod = millis(); // Publish pin event to IO encodePinEvent(_analog_input_pins[i].pinName, _analog_input_pins[i].readMode, pinValRaw, pinValVolts); - // } else if (_analog_input_pins[i].prvPeriod != 0L && - // pinValRaw != _analog_input_pins[i].prvPinVal && - // ((long)millis() - _analog_input_pins[i].prvPeriod) > 200) { - // // failed hysterisys, but we were waiting 500ms before posting, to avoid - // // a flood of events - // WS_DEBUG_PRINTLN( - // "ADC has only mildly changed, but we were waiting 200ms before " - // "posting, to avoid a flood of events and this is the final value"); - // _analog_input_pins[i].prvPeriod = 0L; - // _analog_input_pins[i].prvPinVal = pinValRaw; - // // Publish pin event to IO - // encodePinEvent(_analog_input_pins[i].pinName, - // _analog_input_pins[i].readMode, pinValRaw, pinValVolts); - - // } else { + // } else if (_analog_input_pins[i].prvPeriod != 0L && + // pinValRaw != _analog_input_pins[i].prvPinVal && + // ((long)millis() - _analog_input_pins[i].prvPeriod) > 200) + // { + // // failed hysterisys, but we were waiting 500ms before posting, to + // avoid + // // a flood of events + // WS_DEBUG_PRINTLN( + // "ADC has only mildly changed, but we were waiting 200ms before + // " "posting, to avoid a flood of events and this is the final + // value"); + // _analog_input_pins[i].prvPeriod = 0L; + // _analog_input_pins[i].prvPinVal = pinValRaw; + // // Publish pin event to IO + // encodePinEvent(_analog_input_pins[i].pinName, + // _analog_input_pins[i].readMode, pinValRaw, + // pinValVolts); + + // } else { // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); // _analog_input_pins[i].prvPeriod = millis(); // _analog_input_pins[i].prvPinVal = pinValRaw; @@ -492,7 +486,8 @@ void Wippersnapper_AnalogIO::update() { } // if (_analog_input_pins[i].readMode == - // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { + // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) + // { // pinValVolts = pinValRaw * getAref() / 65536; // } @@ -507,6 +502,5 @@ void Wippersnapper_AnalogIO::update() { // set the pin value in the digital pin object for comparison on next // run } - } } \ No newline at end of file From 47de1cec47ba53c29ad5f36df79578a404e78893 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Fri, 23 Aug 2024 11:00:43 +0100 Subject: [PATCH 07/11] Fstring the world for SAMD --- .../analogIO/Wippersnapper_AnalogIO.cpp | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index b4c3dd2be..2b7c0f753 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -157,7 +157,7 @@ void Wippersnapper_AnalogIO::initAnalogInputPin( break; } } - WS_DEBUG_PRINT("Configured Analog Input pin with polling time (ms):"); + WS_DEBUG_PRINT(F("Configured Analog Input pin with polling time (ms):")); WS_DEBUG_PRINTLN(periodMs); } @@ -189,11 +189,11 @@ void Wippersnapper_AnalogIO::disableAnalogInPin(int pin) { /***********************************************************************************/ void Wippersnapper_AnalogIO::deinitAnalogPin( wippersnapper_pin_v1_ConfigurePinRequest_Direction direction, int pin) { - WS_DEBUG_PRINT("Deinitializing analog pin A"); + WS_DEBUG_PRINT(F("Deinitializing analog pin A")); WS_DEBUG_PRINTLN(pin); if (direction == wippersnapper_pin_v1_ConfigurePinRequest_Direction_DIRECTION_INPUT) { - WS_DEBUG_PRINTLN("Deinitialized analog input pin obj."); + WS_DEBUG_PRINTLN(F("Deinitialized analog input pin obj.")); disableAnalogInPin(pin); } pinMode(pin, INPUT); // hi-z @@ -233,10 +233,10 @@ uint16_t Wippersnapper_AnalogIO::getPinValue(int pin) { /**********************************************************/ float Wippersnapper_AnalogIO::getPinValueVolts(int pin) { #ifdef ARDUINO_ARCH_ESP32 - WS_DEBUG_PRINTLN("ESP32: Using analogReadMilliVolts()"); + WS_DEBUG_PRINTLN(F("ESP32: Using analogReadMilliVolts()")); return analogReadMilliVolts(pin) / 1000.0; #else - WS_DEBUG_PRINTLN("Using old getPinValueVolts()"); + WS_DEBUG_PRINTLN(F("Using old getPinValueVolts()")); uint16_t rawValue = getPinValue(pin); return rawValue * getAref() / 65536; #endif @@ -293,7 +293,7 @@ bool Wippersnapper_AnalogIO::encodePinEvent( pb_ostream_from_buffer(WS._buffer_outgoing, sizeof(WS._buffer_outgoing)); if (!ws_pb_encode(&stream, wippersnapper_signal_v1_CreateSignalRequest_fields, &outgoingSignalMsg)) { - WS_DEBUG_PRINTLN("ERROR: Unable to encode signal message"); + WS_DEBUG_PRINTLN(F("ERROR: Unable to encode signal message")); return false; } @@ -302,9 +302,9 @@ bool Wippersnapper_AnalogIO::encodePinEvent( pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_CreateSignalRequest_fields, &outgoingSignalMsg); - WS_DEBUG_PRINT("Publishing pinEvent..."); + WS_DEBUG_PRINT(F("Publishing pinEvent...")); WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1); - WS_DEBUG_PRINTLN("Published!"); + WS_DEBUG_PRINTLN(F("Published!")); return true; } @@ -344,7 +344,7 @@ void Wippersnapper_AnalogIO::update() { // Does the pin execute on-period? if (timerExpired((long)millis(), _analog_input_pins[i])) { - WS_DEBUG_PRINT("Executing periodic event on A"); + WS_DEBUG_PRINT(F("Executing periodic event on A")); WS_DEBUG_PRINTLN(_analog_input_pins[i].pinName); // Read from analog pin @@ -356,8 +356,8 @@ void Wippersnapper_AnalogIO::update() { wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) { pinValRaw = getPinValue(_analog_input_pins[i].pinName); } else { - WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " - "analog read mode!"); + WS_DEBUG_PRINTLN(F("ERROR: Unable to read pin value, cannot determine " + "analog read mode!")); pinValRaw = 0.0; } @@ -371,11 +371,11 @@ void Wippersnapper_AnalogIO::update() { // Does the pin execute on_change? else if (_analog_input_pins[i].period == 0L) { - WS_DEBUG_PRINT("CurrentTime: "); + WS_DEBUG_PRINT(F("CurrentTime: ")); WS_DEBUG_PRINTLN(millis()); - WS_DEBUG_PRINT("PrvPeriod: "); + WS_DEBUG_PRINT(F("PrvPeriod: ")); WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod); - WS_DEBUG_PRINT("Diff: "); + WS_DEBUG_PRINT(F("Diff: ")); WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); if (_analog_input_pins[i].prvPeriod == 0L) { @@ -391,23 +391,23 @@ void Wippersnapper_AnalogIO::update() { // note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv // pin value uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName); - WS_DEBUG_PRINT("PinValRaw: "); + WS_DEBUG_PRINT(F("PinValRaw: ")); WS_DEBUG_PRINTLN(pinValRaw); double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0) double lastLogValue = log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) - WS_DEBUG_PRINT("CurrentLogValue: "); + WS_DEBUG_PRINT(F("CurrentLogValue: ")); WS_DEBUG_PRINTLN(currentLogValue); - WS_DEBUG_PRINT("LastLogValue: "); + WS_DEBUG_PRINT(F("LastLogValue: ")); WS_DEBUG_PRINTLN(lastLogValue); bool passed_hysterisys = false; // Check if the logarithmic change exceeds the threshold if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) { passed_hysterisys = true; - WS_DEBUG_PRINTLN("ADC passed hysteresis"); + WS_DEBUG_PRINTLN(F("ADC passed hysteresis")); } else { - WS_DEBUG_PRINTLN("ADC did not pass hysteresis"); + WS_DEBUG_PRINTLN(F("ADC did not pass hysteresis")); } // old technique @@ -417,28 +417,28 @@ void Wippersnapper_AnalogIO::update() { uint16_t _pinValThreshLow = _analog_input_pins[i].prvPinVal - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); - WS_DEBUG_PRINT("PinValThreshHi: "); + WS_DEBUG_PRINT(F("PinValThreshHi: ")); WS_DEBUG_PRINTLN(_pinValThreshHi); - WS_DEBUG_PRINT("PinValThreshLow: "); + WS_DEBUG_PRINT(F("PinValThreshLow: ")); WS_DEBUG_PRINTLN(_pinValThreshLow); if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { // passed_hysterisys = true; - WS_DEBUG_PRINTLN("ADC passed OLD hysteresis"); + WS_DEBUG_PRINTLN(F("ADC passed OLD hysteresis")); } else { - WS_DEBUG_PRINTLN("ADC did not pass OLD hysteresis"); + WS_DEBUG_PRINTLN(F("ADC did not pass OLD hysteresis")); } if (_analog_input_pins[i].readMode == wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) { pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName); - WS_DEBUG_PRINT("PinValVolts: "); + WS_DEBUG_PRINT(F("PinValVolts: ")); WS_DEBUG_PRINTLN(pinValVolts); } else if ( _analog_input_pins[i].readMode == wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) { // already fetched raw value, just print it - WS_DEBUG_PRINT("PinValRaw: "); + WS_DEBUG_PRINT(F("PinValRaw: ")); WS_DEBUG_PRINTLN(pinValRaw); } else { WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " @@ -454,7 +454,7 @@ void Wippersnapper_AnalogIO::update() { if (passed_hysterisys && (((long)millis() - _analog_input_pins[i].prvPeriod) > 200 || _analog_input_pins[i].prvPeriod == 0L)) { - WS_DEBUG_PRINTLN("ADC has changed enough, publishing event..."); + WS_DEBUG_PRINTLN(F("ADC has changed enough, publishing event...")); _analog_input_pins[i].prvPinVal = pinValRaw; _analog_input_pins[i].prvPeriod = millis(); // Publish pin event to IO @@ -479,7 +479,7 @@ void Wippersnapper_AnalogIO::update() { // pinValVolts); // } else { - // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); + // WS_DEBUG_PRINTLN(F("ADC has not changed enough, continue...")); // _analog_input_pins[i].prvPeriod = millis(); // _analog_input_pins[i].prvPinVal = pinValRaw; // continue; @@ -496,7 +496,7 @@ void Wippersnapper_AnalogIO::update() { // _analog_input_pins[i].readMode, pinValRaw, // pinValVolts); // } else { - // // WS_DEBUG_PRINTLN("ADC has not changed enough, continue..."); + // // WS_DEBUG_PRINTLN(F("ADC has not changed enough, continue...")); // continue; // } // set the pin value in the digital pin object for comparison on next From 3195a586a0560a6c2ba6403de42e95df7ee4c224 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Fri, 23 Aug 2024 11:26:24 +0100 Subject: [PATCH 08/11] WIP: tweak log10 logic --- src/components/analogIO/Wippersnapper_AnalogIO.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index 2b7c0f753..b7aae0310 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -392,14 +392,15 @@ void Wippersnapper_AnalogIO::update() { // pin value uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName); WS_DEBUG_PRINT(F("PinValRaw: ")); - WS_DEBUG_PRINTLN(pinValRaw); + WS_DEBUG_PRINT(pinValRaw); - double currentLogValue = log10(pinValRaw + 1); // +1 to avoid log(0) + // +1 to avoid log(0), 0-8 is fluctuation about 0, log(8) = 0.9, hysterisys=0.04 + double currentLogValue = log10(pinValRaw + 10); double lastLogValue = - log10(_analog_input_pins[i].prvPinVal + 1); // +1 to avoid log(0) - WS_DEBUG_PRINT(F("CurrentLogValue: ")); - WS_DEBUG_PRINTLN(currentLogValue); - WS_DEBUG_PRINT(F("LastLogValue: ")); + log10(_analog_input_pins[i].prvPinVal + 10); // +1 to avoid log(0) + WS_DEBUG_PRINT(F("\tCurrentLogValue: ")); + WS_DEBUG_PRINT(currentLogValue); + WS_DEBUG_PRINT(F("\tLastLogValue: ")); WS_DEBUG_PRINTLN(lastLogValue); bool passed_hysterisys = false; // Check if the logarithmic change exceeds the threshold From 5c59d5a13719b61717d37de8e705664e226b1a09 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Fri, 23 Aug 2024 16:08:40 +0100 Subject: [PATCH 09/11] WIP: update logging --- .../analogIO/Wippersnapper_AnalogIO.cpp | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index b7aae0310..3e44947f3 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -378,16 +378,18 @@ void Wippersnapper_AnalogIO::update() { WS_DEBUG_PRINT(F("Diff: ")); WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); - if (_analog_input_pins[i].prvPeriod == 0L) { - // last time was a clean event, passed hyteresis or 300ms had elapsed - WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " - "passed hyteresis or 500ms had elapsed"); - } else { - // We're waiting 300ms before posting, to avoid a flood of events - WS_DEBUG_PRINTLN( - "prvPeriod is not 0, probably waiting 300ms before posting, " - "to avoid a flood of events"); - } + // if (_analog_input_pins[i].prvPeriod == 0L) { + // // last time was a clean event, passed hyteresis or 300ms had elapsed + // WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " + // "passed hyteresis or 500ms had elapsed"); + // } else { + // // We're waiting 300ms before posting, to avoid a flood of events + // WS_DEBUG_PRINTLN( + // "prvPeriod is not 0, probably waiting 300ms before posting, " + // "to avoid a flood of events"); + // } + + // note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv // pin value uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName); @@ -401,14 +403,14 @@ void Wippersnapper_AnalogIO::update() { WS_DEBUG_PRINT(F("\tCurrentLogValue: ")); WS_DEBUG_PRINT(currentLogValue); WS_DEBUG_PRINT(F("\tLastLogValue: ")); - WS_DEBUG_PRINTLN(lastLogValue); + WS_DEBUG_PRINT(lastLogValue); bool passed_hysterisys = false; // Check if the logarithmic change exceeds the threshold if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) { passed_hysterisys = true; - WS_DEBUG_PRINTLN(F("ADC passed hysteresis")); + WS_DEBUG_PRINTLN(F("\tADC passed hysteresis")); } else { - WS_DEBUG_PRINTLN(F("ADC did not pass hysteresis")); + WS_DEBUG_PRINTLN(F("\tADC did not pass hysteresis")); } // old technique @@ -419,15 +421,15 @@ void Wippersnapper_AnalogIO::update() { _analog_input_pins[i].prvPinVal - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); WS_DEBUG_PRINT(F("PinValThreshHi: ")); - WS_DEBUG_PRINTLN(_pinValThreshHi); + WS_DEBUG_PRINT(_pinValThreshHi); WS_DEBUG_PRINT(F("PinValThreshLow: ")); - WS_DEBUG_PRINTLN(_pinValThreshLow); + WS_DEBUG_PRINT(_pinValThreshLow); if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) { // passed_hysterisys = true; - WS_DEBUG_PRINTLN(F("ADC passed OLD hysteresis")); + WS_DEBUG_PRINTLN(F("\tADC passed OLD hysteresis")); } else { - WS_DEBUG_PRINTLN(F("ADC did not pass OLD hysteresis")); + WS_DEBUG_PRINTLN(F("\tADC did not pass OLD hysteresis")); } if (_analog_input_pins[i].readMode == From d1bb038c7fa1ffbc013f3a788866ae9af892de9d Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 23 Aug 2024 16:59:15 +0100 Subject: [PATCH 10/11] WIP: Stop over sending --- src/components/analogIO/Wippersnapper_AnalogIO.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index 3e44947f3..bc750f4f6 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -371,13 +371,6 @@ void Wippersnapper_AnalogIO::update() { // Does the pin execute on_change? else if (_analog_input_pins[i].period == 0L) { - WS_DEBUG_PRINT(F("CurrentTime: ")); - WS_DEBUG_PRINTLN(millis()); - WS_DEBUG_PRINT(F("PrvPeriod: ")); - WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod); - WS_DEBUG_PRINT(F("Diff: ")); - WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod); - // if (_analog_input_pins[i].prvPeriod == 0L) { // // last time was a clean event, passed hyteresis or 300ms had elapsed // WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, " @@ -454,9 +447,7 @@ void Wippersnapper_AnalogIO::update() { // waiting 200ms before posting the next final movement event (or // continued movement events), to avoid a flood of events when twisting // pots - if (passed_hysterisys && - (((long)millis() - _analog_input_pins[i].prvPeriod) > 200 || - _analog_input_pins[i].prvPeriod == 0L)) { + if (passed_hysterisys && ((long)millis() - _analog_input_pins[i].prvPeriod) > 200) { WS_DEBUG_PRINTLN(F("ADC has changed enough, publishing event...")); _analog_input_pins[i].prvPinVal = pinValRaw; _analog_input_pins[i].prvPeriod = millis(); From caae70204de0792f3b5fd136f3426453e9067876 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 27 Aug 2024 19:23:15 +0100 Subject: [PATCH 11/11] WIP: stash - swapping to battery test --- .../analogIO/Wippersnapper_AnalogIO.cpp | 32 +++++++++++++++++-- .../analogIO/Wippersnapper_AnalogIO.h | 2 +- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.cpp b/src/components/analogIO/Wippersnapper_AnalogIO.cpp index bc750f4f6..afbf51158 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.cpp +++ b/src/components/analogIO/Wippersnapper_AnalogIO.cpp @@ -406,13 +406,36 @@ void Wippersnapper_AnalogIO::update() { WS_DEBUG_PRINTLN(F("\tADC did not pass hysteresis")); } + + + // new plan - add 100 then use 0.1 as the hysteresis, ten percent of current raw value + + + + + + + + + + // take diff in raw value and convert to bits in native resolution, then check for more than 4 bit change (0 to 8 is the noise floor, scaled) + float diff = abs(pinValRaw - _analog_input_pins[i].prvPinVal); + //convert diff to original resolution + diff = diff * (1 << (getADCresolution() - getNativeResolution())); + if (diff > 4) { + passed_hysterisys = true; + WS_DEBUG_PRINTLN(F("\tADC passed hysteresis")); + } else { + WS_DEBUG_PRINTLN(F("\tADC did not pass hysteresis")); + } + // old technique uint16_t _pinValThreshHi = _analog_input_pins[i].prvPinVal + - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); + (_analog_input_pins[i].prvPinVal * 0.02);// DEFAULT_HYSTERISIS); uint16_t _pinValThreshLow = _analog_input_pins[i].prvPinVal - - (_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS); + (_analog_input_pins[i].prvPinVal * 0.02);//DEFAULT_HYSTERISIS); WS_DEBUG_PRINT(F("PinValThreshHi: ")); WS_DEBUG_PRINT(_pinValThreshHi); WS_DEBUG_PRINT(F("PinValThreshLow: ")); @@ -435,7 +458,10 @@ void Wippersnapper_AnalogIO::update() { wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) { // already fetched raw value, just print it WS_DEBUG_PRINT(F("PinValRaw: ")); - WS_DEBUG_PRINTLN(pinValRaw); + WS_DEBUG_PRINT(pinValRaw); + WS_DEBUG_PRINT(F("PinValPrev: ")); + WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPinVal); + } else { WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine " "analog read mode!"); diff --git a/src/components/analogIO/Wippersnapper_AnalogIO.h b/src/components/analogIO/Wippersnapper_AnalogIO.h index 02b13ace4..677f96ff9 100644 --- a/src/components/analogIO/Wippersnapper_AnalogIO.h +++ b/src/components/analogIO/Wippersnapper_AnalogIO.h @@ -18,7 +18,7 @@ #include "Wippersnapper.h" -#define DEFAULT_HYSTERISIS 0.04 ///< Default DEFAULT_HYSTERISIS of 0.04 (log10) +#define DEFAULT_HYSTERISIS 0.3 ///< Default DEFAULT_HYSTERISIS of 0.3 (log10) /** Data about an analog input pin */ struct analogInputPin {