Skip to content

Commit

Permalink
Use same naming convention for InputShifters, OutputShifters & Buttons (
Browse files Browse the repository at this point in the history
  • Loading branch information
elral authored May 27, 2024
1 parent 3e9f6ff commit 0c564d2
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 81 deletions.
7 changes: 5 additions & 2 deletions src/MF_Button/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */);
Expand All @@ -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"));
Expand Down
21 changes: 13 additions & 8 deletions src/MF_Button/MFButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
#include "MFButton.h"

buttonEvent MFButton::_handler = NULL;
buttonEvent MFButton::_inputHandler = NULL;

MFButton::MFButton()
{
Expand All @@ -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)
Expand All @@ -41,23 +46,23 @@ void MFButton::triggerOnPress()
{
if (!_initialized)
return;
if (_handler && _state == LOW) {
(*_handler)(btnOnPress, _name);
if (_inputHandler && _state == LOW) {
(*_inputHandler)(btnOnPress, _name);
}
}

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
10 changes: 6 additions & 4 deletions src/MF_Button/MFButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 20 additions & 15 deletions src/MF_InputShifter/InputShifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,52 +27,57 @@ 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
}

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
}

void read()
{
for (uint8_t i = 0; i < inputShiftersRegistered; i++) {
inputShifters[i].update();
for (uint8_t i = 0; i < inputShifterRegistered; i++) {
inputShifter[i].update();
}
}

void OnTrigger()
{
// 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();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/MF_InputShifter/InputShifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

#pragma once
#include <stdint.h>

namespace InputShifter
{
Expand Down
63 changes: 36 additions & 27 deletions src/MF_InputShifter/MFInputShifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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];
Expand All @@ -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.
Expand All @@ -142,9 +156,4 @@ void MFInputShifter::attachHandler(inputShifterEvent newHandler)
_inputHandler = newHandler;
}

void MFInputShifter::detach()
{
_initialized = false;
}

// MFInputShifter.cpp
15 changes: 8 additions & 7 deletions src/MF_InputShifter/MFInputShifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
};

Expand Down
18 changes: 9 additions & 9 deletions src/MF_OutputShifter/MFOutputShifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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();

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

0 comments on commit 0c564d2

Please sign in to comment.