diff --git a/src/MF_Button/Button.cpp b/src/MF_Button/Button.cpp index faac049d..5fa39def 100644 --- a/src/MF_Button/Button.cpp +++ b/src/MF_Button/Button.cpp @@ -14,7 +14,7 @@ namespace Button uint8_t buttonsRegistered = 0; uint8_t maxButtons = 0; - void handlerOnButton(uint8_t eventId, const char *name) + void handlerButtonOnChange(uint8_t eventId, const char *name) { cmdMessenger.sendCmdStart(kButtonChange); cmdMessenger.sendCmdArg(name); @@ -37,7 +37,7 @@ namespace Button return; buttons[buttonsRegistered] = MFButton(); buttons[buttonsRegistered].attach(pin, name); - MFButton::attachHandler(handlerOnButton); + MFButton::attachHandler(handlerButtonOnChange); buttonsRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added button ") /* + name */); @@ -46,6 +46,9 @@ namespace Button void Clear(void) { + for (uint8_t i = 0; i < buttonsRegistered; i++) { + buttons[i].detach(); + } buttonsRegistered = 0; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Cleared buttons")); diff --git a/src/MF_Button/MFButton.cpp b/src/MF_Button/MFButton.cpp index 561f154a..d51adb17 100644 --- a/src/MF_Button/MFButton.cpp +++ b/src/MF_Button/MFButton.cpp @@ -5,7 +5,7 @@ // #include "MFButton.h" -buttonEvent MFButton::_handler = NULL; +buttonEvent MFButton::_inputHandler = NULL; MFButton::MFButton() { @@ -16,11 +16,16 @@ void MFButton::attach(uint8_t pin, const char *name) { _pin = pin; _name = name; - pinMode(_pin, INPUT_PULLUP); // set pin to input - _state = digitalRead(_pin); // initialize on actual status + pinMode(_pin, INPUT_PULLUP); // set pin to input + _state = digitalRead(_pin); // initialize on actual status _initialized = true; } +void MFButton::detach() +{ + _initialized = false; +} + void MFButton::update() { if (!_initialized) @@ -41,8 +46,8 @@ void MFButton::triggerOnPress() { if (!_initialized) return; - if (_handler && _state == LOW) { - (*_handler)(btnOnPress, _name); + if (_inputHandler && _state == LOW) { + (*_inputHandler)(btnOnPress, _name); } } @@ -50,14 +55,14 @@ void MFButton::triggerOnRelease() { if (!_initialized) return; - if (_handler && _state == HIGH) { - (*_handler)(btnOnRelease, _name); + if (_inputHandler && _state == HIGH) { + (*_inputHandler)(btnOnRelease, _name); } } void MFButton::attachHandler(buttonEvent newHandler) { - _handler = newHandler; + _inputHandler = newHandler; } // MFButton.cpp \ No newline at end of file diff --git a/src/MF_Button/MFButton.h b/src/MF_Button/MFButton.h index 69920046..e7342718 100644 --- a/src/MF_Button/MFButton.h +++ b/src/MF_Button/MFButton.h @@ -26,17 +26,19 @@ class MFButton MFButton(); static void attachHandler(buttonEvent newHandler); void attach(uint8_t pin, const char *name); + void detach(); void update(); void trigger(uint8_t state); void triggerOnPress(); void triggerOnRelease(); + +private: const char *_name; uint8_t _pin; + bool _initialized; + bool _state; -private: - static buttonEvent _handler; - bool _state; - bool _initialized; + static buttonEvent _inputHandler; }; // MFButton.h \ No newline at end of file diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp index 143f9250..50671ac5 100644 --- a/src/MF_InputShifter/InputShifter.cpp +++ b/src/MF_InputShifter/InputShifter.cpp @@ -10,8 +10,8 @@ namespace InputShifter { - MFInputShifter *inputShifters; - uint8_t inputShiftersRegistered = 0; + MFInputShifter *inputShifter; + uint8_t inputShifterRegistered = 0; uint8_t maxInputShifter = 0; void handlerInputShifterOnChange(uint8_t eventId, uint8_t pin, const char *name) @@ -27,23 +27,22 @@ namespace InputShifter { if (!FitInMemory(sizeof(MFInputShifter) * count)) return false; - inputShifters = new (allocateMemory(sizeof(MFInputShifter) * count)) MFInputShifter; + inputShifter = new (allocateMemory(sizeof(MFInputShifter) * count)) MFInputShifter; maxInputShifter = count; return true; } void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name) { - if (inputShiftersRegistered == maxInputShifter) + if (inputShifterRegistered == maxInputShifter) return; - inputShifters[inputShiftersRegistered] = MFInputShifter(); - if (!inputShifters[inputShiftersRegistered].attach(latchPin, clockPin, dataPin, modules, name)) - { + inputShifter[inputShifterRegistered] = MFInputShifter(); + if (!inputShifter[inputShifterRegistered].attach(latchPin, clockPin, dataPin, modules, name)) { cmdMessenger.sendCmd(kStatus, F("InputShifter array does not fit into Memory")); return; } MFInputShifter::attachHandler(handlerInputShifterOnChange); - inputShiftersRegistered++; + inputShifterRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added input shifter")); #endif @@ -51,10 +50,10 @@ namespace InputShifter void Clear() { - for (uint8_t i = 0; i < inputShiftersRegistered; i++) { - inputShifters[i].detach(); + for (uint8_t i = 0; i < inputShifterRegistered; i++) { + inputShifter[i].detach(); } - inputShiftersRegistered = 0; + inputShifterRegistered = 0; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Cleared input shifter")); #endif @@ -62,8 +61,8 @@ namespace InputShifter void read() { - for (uint8_t i = 0; i < inputShiftersRegistered; i++) { - inputShifters[i].update(); + for (uint8_t i = 0; i < inputShifterRegistered; i++) { + inputShifter[i].update(); } } @@ -71,8 +70,14 @@ namespace InputShifter { // Retrigger all the input shifters. This automatically sends // the release events first followed by press events. - for (uint8_t i = 0; i < inputShiftersRegistered; i++) { - inputShifters[i].retrigger(); + + // Trigger all button release events first... + for (uint8_t i = 0; i < inputShifterRegistered; i++) { + inputShifter[i].triggerOnRelease(); + } + // ... then trigger all the press events + for (uint8_t i = 0; i < inputShifterRegistered; i++) { + inputShifter[i].triggerOnPress(); } } diff --git a/src/MF_InputShifter/InputShifter.h b/src/MF_InputShifter/InputShifter.h index 51b06031..0efb1c7c 100644 --- a/src/MF_InputShifter/InputShifter.h +++ b/src/MF_InputShifter/InputShifter.h @@ -5,7 +5,6 @@ // #pragma once -#include namespace InputShifter { diff --git a/src/MF_InputShifter/MFInputShifter.cpp b/src/MF_InputShifter/MFInputShifter.cpp index 8703eb85..d8bdfb9c 100644 --- a/src/MF_InputShifter/MFInputShifter.cpp +++ b/src/MF_InputShifter/MFInputShifter.cpp @@ -30,7 +30,7 @@ bool MFInputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, if (!FitInMemory(sizeof(uint8_t) * _moduleCount)) return false; - + _lastState = new (allocateMemory(sizeof(uint8_t) * _moduleCount)) uint8_t; for (uint8_t i = 0; i < _moduleCount; i++) { _lastState[i] = 0; @@ -43,6 +43,11 @@ bool MFInputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, return true; } +void MFInputShifter::detach() +{ + _initialized = false; +} + // Reads the values from the attached modules, compares them to the previously // read values, and calls the registered event handler for any inputs that // changed from the previously read state. @@ -93,27 +98,22 @@ void MFInputShifter::detectChanges(uint8_t lastState, uint8_t currentState, uint } } -// Reads the current state for all connected modules then fires -// release events for every released button followed by -// press events for every pressed button. -void MFInputShifter::retrigger() +// Triggers the event handler for the associated input shift register pin, +// if a handler is registered. +void MFInputShifter::trigger(uint8_t pin, bool state) +{ + (*_inputHandler)((state == LOW ? inputShifterOnPress : inputShifterOnRelease), pin, _name); +} + +void MFInputShifter::triggerOnPress() { + if (!_initialized || !_inputHandler) + return; + uint8_t state; poll(DONT_TRIGGER); - // 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; - } - } - // Trigger all the pressed buttons for (int module = 0; module < _moduleCount; module++) { state = _lastState[module]; @@ -128,12 +128,26 @@ void MFInputShifter::retrigger() } } -// Triggers the event handler for the associated input shift register pin, -// if a handler is registered. -void MFInputShifter::trigger(uint8_t pin, bool state) +void MFInputShifter::triggerOnRelease() { - if (!_inputHandler) return; - (*_inputHandler)((state == LOW ? inputShifterOnPress : inputShifterOnRelease), pin, _name); + if (!_initialized || !_inputHandler) + return; + + uint8_t state; + + poll(DONT_TRIGGER); + + // 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; + } + } } // Attaches a new event handler for the specified event. @@ -142,9 +156,4 @@ void MFInputShifter::attachHandler(inputShifterEvent newHandler) _inputHandler = newHandler; } -void MFInputShifter::detach() -{ - _initialized = false; -} - // MFInputShifter.cpp diff --git a/src/MF_InputShifter/MFInputShifter.h b/src/MF_InputShifter/MFInputShifter.h index 5c7405e6..287e109e 100644 --- a/src/MF_InputShifter/MFInputShifter.h +++ b/src/MF_InputShifter/MFInputShifter.h @@ -21,12 +21,12 @@ class MFInputShifter { public: MFInputShifter(); - bool attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t moduleCount, const char *name); static void attachHandler(inputShifterEvent newHandler); - void clear(); + bool attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t moduleCount, const char *name); void detach(); - void retrigger(); void update(); + void triggerOnPress(); + void triggerOnRelease(); private: enum { DONT_TRIGGER = 0, @@ -38,11 +38,12 @@ class MFInputShifter uint8_t _dataPin; // SDO (data) pin uint8_t _moduleCount; // Number of 8 bit modules in series. bool _initialized = false; - uint8_t *_lastState; + uint8_t *_lastState; + + void poll(uint8_t doTrigger); + void detectChanges(uint8_t lastState, uint8_t currentState, uint8_t module); + void trigger(uint8_t pin, bool state); - void poll(uint8_t doTrigger); - void detectChanges(uint8_t lastState, uint8_t currentState, uint8_t module); - void trigger(uint8_t pin, bool state); static inputShifterEvent _inputHandler; }; diff --git a/src/MF_OutputShifter/MFOutputShifter.cpp b/src/MF_OutputShifter/MFOutputShifter.cpp index 5ac264dd..c4d827e8 100644 --- a/src/MF_OutputShifter/MFOutputShifter.cpp +++ b/src/MF_OutputShifter/MFOutputShifter.cpp @@ -20,11 +20,11 @@ void MFOutputShifter::setPin(uint8_t pin, uint8_t value, uint8_t refresh) uint8_t msk = (0x01 << (pin & 0x07)); if (value != MF_LOW) { - _outputBuffer[idx] |= msk; + _lastState[idx] |= msk; } else { - _outputBuffer[idx] &= ~msk; + _lastState[idx] &= ~msk; } - if (refresh) updateShiftRegister(); + if (refresh) update(); } void MFOutputShifter::setPins(char *pins, uint8_t value) @@ -37,7 +37,7 @@ void MFOutputShifter::setPins(char *pins, uint8_t value) setPin(num, value, 0); pinTokens = strtok(0, "|"); } - updateShiftRegister(); + update(); } bool MFOutputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t moduleCount) @@ -55,7 +55,7 @@ bool MFOutputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin if (!FitInMemory(sizeof(uint8_t) * _moduleCount)) return false; - _outputBuffer = new (allocateMemory(sizeof(uint8_t) * _moduleCount)) uint8_t; + _lastState = new (allocateMemory(sizeof(uint8_t) * _moduleCount)) uint8_t; clear(); @@ -70,16 +70,16 @@ void MFOutputShifter::detach() void MFOutputShifter::clear() { for (uint8_t i = 0; i < _moduleCount; i++) { - _outputBuffer[i] = 0xFF * MF_LOW; + _lastState[i] = 0xFF * MF_LOW; } - updateShiftRegister(); + update(); } -void MFOutputShifter::updateShiftRegister() +void MFOutputShifter::update() { digitalWrite(_latchPin, LOW); for (uint8_t i = _moduleCount; i > 0; i--) { - shiftOut(_dataPin, _clockPin, MSBFIRST, _outputBuffer[i - 1]); // LSBFIRST, MSBFIRST, + shiftOut(_dataPin, _clockPin, MSBFIRST, _lastState[i - 1]); // LSBFIRST, MSBFIRST, } digitalWrite(_latchPin, HIGH); } diff --git a/src/MF_OutputShifter/MFOutputShifter.h b/src/MF_OutputShifter/MFOutputShifter.h index b69f601d..5f8ea737 100644 --- a/src/MF_OutputShifter/MFOutputShifter.h +++ b/src/MF_OutputShifter/MFOutputShifter.h @@ -25,14 +25,14 @@ class MFOutputShifter bool attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t moduleCount); void detach(); void clear(); - void updateShiftRegister(); + void update(); private: uint8_t _latchPin; // Latch pin uint8_t _clockPin; // Clock pin uint8_t _dataPin; // Data/SI pin uint8_t _moduleCount; // Number of 8 bit modules in series. For a shift register with 16 bit one needs to select 2 modules a 8...... - uint8_t *_outputBuffer; + uint8_t *_lastState; bool _initialized = false; }; diff --git a/src/MF_OutputShifter/OutputShifter.cpp b/src/MF_OutputShifter/OutputShifter.cpp index 2c34d25e..34305c5a 100644 --- a/src/MF_OutputShifter/OutputShifter.cpp +++ b/src/MF_OutputShifter/OutputShifter.cpp @@ -10,7 +10,7 @@ namespace OutputShifter { - MFOutputShifter *outputShifters; + MFOutputShifter *outputShifter; uint8_t outputShifterRegistered = 0; uint8_t maxOutputShifter = 0; @@ -18,7 +18,7 @@ namespace OutputShifter { if (!FitInMemory(sizeof(MFOutputShifter) * count)) return false; - outputShifters = new (allocateMemory(sizeof(MFOutputShifter) * count)) MFOutputShifter; + outputShifter = new (allocateMemory(sizeof(MFOutputShifter) * count)) MFOutputShifter; maxOutputShifter = count; return true; } @@ -27,8 +27,8 @@ namespace OutputShifter { if (outputShifterRegistered == maxOutputShifter) return; - outputShifters[outputShifterRegistered] = MFOutputShifter(); - if (!outputShifters[outputShifterRegistered].attach(latchPin, clockPin, dataPin, modules)) + outputShifter[outputShifterRegistered] = MFOutputShifter(); + if (!outputShifter[outputShifterRegistered].attach(latchPin, clockPin, dataPin, modules)) { cmdMessenger.sendCmd(kStatus, F("OutputShifter array does not fit into Memory")); return; @@ -43,7 +43,7 @@ namespace OutputShifter void Clear() { for (uint8_t i = 0; i < outputShifterRegistered; i++) { - outputShifters[i].detach(); + outputShifter[i].detach(); } outputShifterRegistered = 0; @@ -58,7 +58,7 @@ namespace OutputShifter int module = cmdMessenger.readInt16Arg(); char *pins = cmdMessenger.readStringArg(); int value = cmdMessenger.readInt16Arg(); - outputShifters[module].setPins(pins, value); + outputShifter[module].setPins(pins, value); } } // namespace