diff --git a/src/CommandMessenger.cpp b/src/CommandMessenger.cpp index 39100793..0d0dcd08 100644 --- a/src/CommandMessenger.cpp +++ b/src/CommandMessenger.cpp @@ -30,6 +30,9 @@ #if MF_OUTPUT_SHIFTER_SUPPORT == 1 #include "OutputShifter.h" #endif +#if MF_DIGIN_MUX_SUPPORT == 1 +#include "DigInMux.h" +#endif CmdMessenger cmdMessenger = CmdMessenger(Serial); unsigned long lastCommand; @@ -107,6 +110,12 @@ void OnTrigger() #if MF_INPUT_SHIFTER_SUPPORT == 1 InputShifter::OnTrigger(); #endif +#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 diff --git a/src/MF_Button/MFButton.cpp b/src/MF_Button/MFButton.cpp index 3e1b76f9..1a80e17e 100644 --- a/src/MF_Button/MFButton.cpp +++ b/src/MF_Button/MFButton.cpp @@ -12,8 +12,8 @@ MFButton::MFButton(uint8_t pin, const char *name) { _pin = pin; _name = name; - _state = 1; - pinMode(_pin, INPUT_PULLUP); // set pin to input + pinMode(_pin, INPUT_PULLUP); // set pin to input + _state = digitalRead(_pin); // initialize on actual status } void MFButton::update() diff --git a/src/MF_DigInMux/DigInMux.h b/src/MF_DigInMux/DigInMux.h index d467c3b7..8727f875 100644 --- a/src/MF_DigInMux/DigInMux.h +++ b/src/MF_DigInMux/DigInMux.h @@ -13,7 +13,7 @@ namespace DigInMux void Add(uint8_t dataPin, uint8_t nRegs, char const *name = "DigInMux", bool mode = MFDigInMux::MUX_MODE_FAST); void Clear(); void read(); - // void OnTrigger(); + void OnTrigger(); } // DigInMux.h \ No newline at end of file diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp index 51c52c33..cedf9429 100644 --- a/src/MF_InputShifter/InputShifter.cpp +++ b/src/MF_InputShifter/InputShifter.cpp @@ -33,7 +33,6 @@ namespace InputShifter } inputShifters[inputShiftersRegistered] = new (allocateMemory(sizeof(MFInputShifter))) MFInputShifter; inputShifters[inputShiftersRegistered]->attach(latchPin, clockPin, dataPin, modules, name); - inputShifters[inputShiftersRegistered]->clear(); MFInputShifter::attachHandler(handlerInputShifterOnChange); inputShiftersRegistered++; #ifdef DEBUG2CMDMESSENGER diff --git a/src/MF_InputShifter/MFInputShifter.cpp b/src/MF_InputShifter/MFInputShifter.cpp index dd6f1d60..bbaa1e4e 100644 --- a/src/MF_InputShifter/MFInputShifter.cpp +++ b/src/MF_InputShifter/MFInputShifter.cpp @@ -11,7 +11,6 @@ inputShifterEvent MFInputShifter::_inputHandler = NULL; MFInputShifter::MFInputShifter(const char *name) { _initialized = false; - clearLastState(); _name = name; } @@ -29,6 +28,14 @@ void MFInputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, pinMode(_clockPin, OUTPUT); pinMode(_dataPin, INPUT); _initialized = true; + +// And now initialize all buttons with the actual status + digitalWrite(_clockPin, HIGH); // Preset clock to retrieve first bit + digitalWrite(_latchPin, HIGH); // Disable input latching and enable shifting + for (int module = 0; module < _moduleCount; module++) { + _lastState[module] = shiftIn(_dataPin, _clockPin, MSBFIRST); + } + digitalWrite(_latchPin, LOW); // disable shifting and enable input latching } // Reads the values from the attached modules, compares them to the previously @@ -41,14 +48,14 @@ void MFInputShifter::update() // Multiple chained modules are handled one at a time. As shiftIn() keeps getting // called it will pull in the data from each chained module. - for (uint8_t i = 0; i < _moduleCount; i++) { + for (uint8_t i = 0; i < _moduleCount; i++) { uint8_t currentState; currentState = shiftIn(_dataPin, _clockPin, MSBFIRST); // If an input changed on the current module from the last time it was read // then hand it off to figure out which bits specifically changed. - if (currentState != _lastState[i]) { + if (currentState != _lastState[i]) { detectChanges(_lastState[i], currentState, i); _lastState[i] = currentState; } @@ -89,21 +96,20 @@ void MFInputShifter::retrigger() // The current state for all attached modules is stored in the _lastState // array so future update() calls will work off whatever was read by the // retrigger flow. - for (int module = 0; module < _moduleCount; module++) { + for (int module = 0; module < _moduleCount; module++) { _lastState[module] = shiftIn(_dataPin, _clockPin, MSBFIRST); } digitalWrite(_latchPin, LOW); // disable shifting and enable input latching - // Trigger all the released buttons - for (int module = 0; module < _moduleCount; module++) { + // Trigger all the released buttons + for (int module = 0; module < _moduleCount; module++) { state = _lastState[module]; for (uint8_t i = 0; i < 8; i++) { // Only trigger if the button is in the off position if (state & 1) { trigger(i + (module * 8), HIGH); } - state = state >> 1; } } @@ -146,19 +152,4 @@ void MFInputShifter::detach() _initialized = false; } -// Clears the internal state of the shifter, including all received bits -// and the timestamp for the last time the data was read. -void MFInputShifter::clear() -{ - clearLastState(); -} - -// Sets the last recorded state of every bit on every shifter to 0. -void MFInputShifter::clearLastState() -{ - for (uint8_t i = 0; i < MAX_CHAINED_INPUT_SHIFTERS; i++) { - _lastState[i] = 0; - } -} - // MFInputShifter.cpp diff --git a/src/MF_InputShifter/MFInputShifter.h b/src/MF_InputShifter/MFInputShifter.h index 9095b262..f36d540d 100644 --- a/src/MF_InputShifter/MFInputShifter.h +++ b/src/MF_InputShifter/MFInputShifter.h @@ -40,11 +40,10 @@ class MFInputShifter uint8_t _dataPin; // SDO (data) pin uint8_t _moduleCount; // Number of 8 bit modules in series. bool _initialized = false; - uint8_t _lastState[MAX_CHAINED_INPUT_SHIFTERS]; + uint8_t _lastState[MAX_CHAINED_INPUT_SHIFTERS] = {0}; void detectChanges(uint8_t lastState, uint8_t currentState, uint8_t module); void trigger(uint8_t pin, bool state); - void clearLastState(); static inputShifterEvent _inputHandler; };