Skip to content

Commit

Permalink
(#898)(Connector) Fix missing retrigger for ANALOG inputs at startup (M…
Browse files Browse the repository at this point in the history
  • Loading branch information
GioCC authored Oct 3, 2022
1 parent 256c9ff commit 40b5694
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/CommandMessenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 15 additions & 6 deletions src/MF_Analog/Analog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Analog.cpp
//
//
// (C) MobiFlight Project 2022
//

Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/MF_Analog/Analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Analog
void Clear();
void read();
void readAverage();
void OnTrigger();
}

// Analog.h
23 changes: 20 additions & 3 deletions src/MF_Analog/MFAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,37 @@ 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);
}
}
}

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
Expand Down
12 changes: 8 additions & 4 deletions src/MF_Analog/MFAnalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

0 comments on commit 40b5694

Please sign in to comment.