From 40b569409734d743ddf810fac17ba94efc098f2a Mon Sep 17 00:00:00 2001 From: GioCC <25667790+GioCC@users.noreply.github.com> Date: Mon, 3 Oct 2022 15:15:13 +0200 Subject: [PATCH] (#898)(Connector) Fix missing retrigger for ANALOG inputs at startup (#199) fixes #202 --- src/CommandMessenger.cpp | 3 +++ src/MF_Analog/Analog.cpp | 21 +++++++++++++++------ src/MF_Analog/Analog.h | 1 + src/MF_Analog/MFAnalog.cpp | 23 ++++++++++++++++++++--- src/MF_Analog/MFAnalog.h | 12 ++++++++---- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/CommandMessenger.cpp b/src/CommandMessenger.cpp index 7847f158..0d0dcd08 100644 --- a/src/CommandMessenger.cpp +++ b/src/CommandMessenger.cpp @@ -113,6 +113,9 @@ void OnTrigger() #if MF_DIGIN_MUX_SUPPORT == 1 DigInMux::OnTrigger(); #endif +#if MF_ANALOG_SUPPORT == 1 + Analog::OnTrigger(); +#endif } // commandmessenger.cpp diff --git a/src/MF_Analog/Analog.cpp b/src/MF_Analog/Analog.cpp index e109af49..70a186cb 100644 --- a/src/MF_Analog/Analog.cpp +++ b/src/MF_Analog/Analog.cpp @@ -1,6 +1,6 @@ // // Analog.cpp -// +// // (C) MobiFlight Project 2022 // @@ -14,7 +14,7 @@ namespace Analog MFAnalog *analog[MAX_ANALOG_INPUTS]; uint8_t analogRegistered = 0; - void handlerOnAnalogChange(int value, uint8_t pin, const char *name) + void handlerOnAnalogChange(int value, uint8_t pin, const char *name) { cmdMessenger.sendCmdStart(kAnalogChange); cmdMessenger.sendCmdArg(name); @@ -35,17 +35,17 @@ namespace Analog analog[analogRegistered] = new (allocateMemory(sizeof(MFAnalog))) MFAnalog(pin, name, sensitivity); MFAnalog::attachHandler(handlerOnAnalogChange); analogRegistered++; - #ifdef DEBUG2CMDMESSENGER +#ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added analog device ")); - #endif +#endif } void Clear(void) { analogRegistered = 0; - #ifdef DEBUG2CMDMESSENGER +#ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Cleared analog devices")); - #endif +#endif } void read(void) @@ -61,6 +61,15 @@ namespace Analog analog[i]->readBuffer(); } } + + void OnTrigger() + { + // Scan and transit current values + for (uint8_t i = 0; i < analogRegistered; i++) { + analog[i]->retrigger(); + } + } + } // namespace Analog #endif diff --git a/src/MF_Analog/Analog.h b/src/MF_Analog/Analog.h index f3dccc01..61fb0f5f 100644 --- a/src/MF_Analog/Analog.h +++ b/src/MF_Analog/Analog.h @@ -13,6 +13,7 @@ namespace Analog void Clear(); void read(); void readAverage(); + void OnTrigger(); } // Analog.h \ No newline at end of file diff --git a/src/MF_Analog/MFAnalog.cpp b/src/MF_Analog/MFAnalog.cpp index da8212bb..69566581 100644 --- a/src/MF_Analog/MFAnalog.cpp +++ b/src/MF_Analog/MFAnalog.cpp @@ -15,13 +15,20 @@ MFAnalog::MFAnalog(uint8_t pin, const char *name, uint8_t sensitivity) _name = name; _lastValue = 0; pinMode(_pin, INPUT_PULLUP); // set pin to input. Could use OUTPUT for analog, but shows the intention :-) - analogRead(_pin); // turn on pullup resistors + for (uint8_t i = 0; i < ADC_MAX_AVERAGE; i++) { + readBuffer(); + } } -void MFAnalog::update() +bool MFAnalog::valueHasChanged(int16_t newValue) +{ + return (abs(newValue - _lastValue) >= _sensitivity); +} + +void MFAnalog::readChannel(uint8_t alwaysTrigger) { int16_t newValue = ADC_Average_Total >> ADC_MAX_AVERAGE_LOG2; - if (abs(newValue - _lastValue) >= _sensitivity) { + if (alwaysTrigger || valueHasChanged(newValue)) { _lastValue = newValue; if (_handler != NULL) { (*_handler)(_lastValue, _pin, _name); @@ -29,6 +36,16 @@ void MFAnalog::update() } } +void MFAnalog::update() +{ + readChannel(false); +} + +void MFAnalog::retrigger() +{ + readChannel(true); +} + void MFAnalog::readBuffer() { // read ADC and calculate floating average, call it every ~10ms ADC_Average_Total -= ADC_Buffer[(ADC_Average_Pointer)]; // subtract oldest value to save the newest value diff --git a/src/MF_Analog/MFAnalog.h b/src/MF_Analog/MFAnalog.h index 6b059eb0..b6256e5c 100644 --- a/src/MF_Analog/MFAnalog.h +++ b/src/MF_Analog/MFAnalog.h @@ -24,6 +24,7 @@ class MFAnalog MFAnalog(uint8_t pin = 1, const char *name = "Analog Input", uint8_t sensitivity = 2); static void attachHandler(analogEvent handler); void update(); + void retrigger(); void readBuffer(); const char *_name; uint8_t _pin; @@ -33,10 +34,13 @@ class MFAnalog int _lastValue; uint8_t _sensitivity; - uint16_t ADC_Buffer[ADC_MAX_AVERAGE] = {0}; // Buffer for all values from each channel - uint16_t ADC_Average_Total = 0; // sum of sampled values, must be divided by ADC_MAX_AVERAGE to get actual value - volatile uint8_t ADC_Average_Pointer = 0; // points to the actual position in ADC_BUFFER - uint32_t _lastReadBuffer; + uint16_t ADC_Buffer[ADC_MAX_AVERAGE] = {0}; // Buffer for all values from each channel + uint16_t ADC_Average_Total = 0; // sum of sampled values, must be divided by ADC_MAX_AVERAGE to get actual value + volatile uint8_t ADC_Average_Pointer = 0; // points to the actual position in ADC_BUFFER + uint32_t _lastReadBuffer; + + void readChannel(uint8_t compare); + bool valueHasChanged(int16_t newValue); }; // MFAnalog.h \ No newline at end of file