Skip to content

Commit

Permalink
Improve getting correct device infos for connector from Mega (MobiFli…
Browse files Browse the repository at this point in the history
…ght#201)

* initialize button on actual status
* init input shifter with actual status of inputs
* deleted not required declaration
  • Loading branch information
elral authored Oct 3, 2022
1 parent db94768 commit 256c9ff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/MF_Button/MFButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion src/MF_InputShifter/InputShifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 13 additions & 22 deletions src/MF_InputShifter/MFInputShifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ inputShifterEvent MFInputShifter::_inputHandler = NULL;
MFInputShifter::MFInputShifter(const char *name)
{
_initialized = false;
clearLastState();
_name = name;
}

Expand All @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions src/MF_InputShifter/MFInputShifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 256c9ff

Please sign in to comment.