From 256c9fffe41e30f6082da4952352e49225095a9b Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Mon, 3 Oct 2022 14:42:53 +0200 Subject: [PATCH] Improve getting correct device infos for connector from Mega (#201) * initialize button on actual status * init input shifter with actual status of inputs * deleted not required declaration --- src/MF_Button/MFButton.cpp | 4 +-- src/MF_InputShifter/InputShifter.cpp | 1 - src/MF_InputShifter/MFInputShifter.cpp | 35 ++++++++++---------------- src/MF_InputShifter/MFInputShifter.h | 3 +-- 4 files changed, 16 insertions(+), 27 deletions(-) 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_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; };