Skip to content

Commit

Permalink
Merge branch 'MobiFlight:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
GioCC committed Oct 10, 2022
2 parents d832d0a + bf6ce6f commit 2595024
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 109 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# MobiFlight-FirmwareSource

[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/MobiFlight/MobiFlight-FirmwareSource)

PlatformIO version of the MobiFlight firmware source.

To build:
Expand Down
14 changes: 7 additions & 7 deletions _Boards/Atmel/Board_Mega/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
#define MFBoardMega_h

#ifndef MF_SEGMENT_SUPPORT
#define MF_SEGMENT_SUPPORT 1
#define MF_SEGMENT_SUPPORT 1
#endif
#ifndef MF_LCD_SUPPORT
#define MF_LCD_SUPPORT 1
#define MF_LCD_SUPPORT 1
#endif
#ifndef MF_STEPPER_SUPPORT
#define MF_STEPPER_SUPPORT 1
#define MF_STEPPER_SUPPORT 1
#endif
#ifndef MF_SERVO_SUPPORT
#define MF_SERVO_SUPPORT 1
#define MF_SERVO_SUPPORT 1
#endif
#ifndef MF_ANALOG_SUPPORT
#define MF_ANALOG_SUPPORT 1
#define MF_ANALOG_SUPPORT 1
#endif
#ifndef MF_OUTPUT_SHIFTER_SUPPORT
#define MF_OUTPUT_SHIFTER_SUPPORT 1
Expand All @@ -29,7 +29,7 @@
#define MF_INPUT_SHIFTER_SUPPORT 1
#endif
#ifndef MF_MUX_SUPPORT
#define MF_MUX_SUPPORT 1
#define MF_MUX_SUPPORT 1
#endif
#ifndef MF_DIGIN_MUX_SUPPORT
#define MF_MUX_SUPPORT 1
Expand All @@ -46,7 +46,7 @@
#define MAX_ANALOG_INPUTS 16
#define MAX_OUTPUT_SHIFTERS 4
#define MAX_INPUT_SHIFTERS 4
#define MAX_DIGIN_MUX 2
#define MAX_DIGIN_MUX 4

#define STEPS 64
#define STEPPER_SPEED 400 // 300 already worked, 467, too?
Expand Down
13 changes: 6 additions & 7 deletions _Boards/Atmel/Board_ProMicro/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
#define MFBoardMicro_h

#ifndef MF_SEGMENT_SUPPORT
#define MF_SEGMENT_SUPPORT 1
#define MF_SEGMENT_SUPPORT 1
#endif
#ifndef MF_LCD_SUPPORT
#define MF_LCD_SUPPORT 1
#define MF_LCD_SUPPORT 1
#endif
#ifndef MF_STEPPER_SUPPORT
#define MF_STEPPER_SUPPORT 1
#define MF_STEPPER_SUPPORT 1
#endif
#ifndef MF_SERVO_SUPPORT
#define MF_SERVO_SUPPORT 0
#endif
#ifndef MF_ANALOG_SUPPORT
#define MF_ANALOG_SUPPORT 1
#define MF_ANALOG_SUPPORT 1
#endif
#ifndef MF_OUTPUT_SHIFTER_SUPPORT
#define MF_OUTPUT_SHIFTER_SUPPORT 1
Expand All @@ -30,14 +30,13 @@
#define MF_INPUT_SHIFTER_SUPPORT 1
#endif
#ifndef MF_MUX_SUPPORT
#define MF_MUX_SUPPORT 1
#define MF_MUX_SUPPORT 1
#endif
#ifndef MF_DIGIN_MUX_SUPPORT
#define MF_MUX_SUPPORT 1
#define MF_DIGIN_MUX_SUPPORT 1
#endif


#define MAX_OUTPUTS 18
#define MAX_BUTTONS 18
#define MAX_LEDSEGMENTS 1
Expand All @@ -48,7 +47,7 @@
#define MAX_ANALOG_INPUTS 5
#define MAX_OUTPUT_SHIFTERS 2
#define MAX_INPUT_SHIFTERS 2
#define MAX_DIGIN_MUX 1
#define MAX_DIGIN_MUX 3

#define STEPS 64
#define STEPPER_SPEED 400 // 300 already worked, 467, too?
Expand Down
2 changes: 1 addition & 1 deletion _Boards/Atmel/Board_Uno/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#define MAX_ANALOG_INPUTS 6
#define MAX_OUTPUT_SHIFTERS 2
#define MAX_INPUT_SHIFTERS 2
#define MAX_DIGIN_MUX 2
#define MAX_DIGIN_MUX 3

#define STEPS 64
#define STEPPER_SPEED 400 // 300 already worked, 467, too?
Expand Down
12 changes: 8 additions & 4 deletions src/MF_DigInMux/MFDigInMux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ void MFDigInMux::attach(uint8_t dataPin, bool halfSize, char const *name)
if (halfSize) bitSet(_flags, MUX_HALFSIZE);
pinMode(_dataPin, INPUT_PULLUP);
bitSet(_flags, MUX_INITED);

// Initialize all inputs with current status
poll(DONT_TRIGGER, bitRead(_flags, MUX_LAZY));

}

void MFDigInMux::detach()
Expand All @@ -60,11 +64,11 @@ void MFDigInMux::detach()
// changed from the previously read state.
void MFDigInMux::update()
{
poll(true, bitRead(_flags, MUX_LAZY));
poll(DO_TRIGGER, bitRead(_flags, MUX_LAZY));
}

// Helper function for update() and retrigger()
void MFDigInMux::poll(bool detect, bool isLazy)
void MFDigInMux::poll(bool doTrigger, bool isLazy)
{
if (!_MUX) return;

Expand Down Expand Up @@ -120,7 +124,7 @@ void MFDigInMux::poll(bool detect, bool isLazy)
_MUX->restoreChannel(); // tidy up

if (_lastState != currentState) {
if (detect) detectChanges(_lastState, currentState);
if (doTrigger) detectChanges(_lastState, currentState);
_lastState = currentState;
}

Expand Down Expand Up @@ -170,7 +174,7 @@ void MFDigInMux::retrigger()
// The current state for all attached modules is stored,
// so future update() calls will work off whatever was read by the
// retrigger flow.
poll(false, false); // just read, do not retrigger
poll(DONT_TRIGGER, false); // just read, do not retrigger

// Pass 1/2: Trigger all the 'off' inputs (released buttons) first
detectChanges(0x0000, _lastState);
Expand Down
3 changes: 3 additions & 0 deletions src/MF_DigInMux/MFDigInMux.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class MFDigInMux
MUX_LAZY = 2,
};

enum { DONT_TRIGGER = 0,
DO_TRIGGER = 1 };

static MFMuxDriver *_MUX;
static MuxDigInEvent _inputHandler;

Expand Down
145 changes: 65 additions & 80 deletions src/MF_InputShifter/MFInputShifter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// MFInputShifter.cpp
//
// (C) MobiFlight Project 2022
Expand All @@ -10,146 +10,131 @@ inputShifterEvent MFInputShifter::_inputHandler = NULL;

MFInputShifter::MFInputShifter(const char *name)
{
_initialized = false;
_name = name;
_initialized = false;
_name = name;
}

// Registers a new input shifter and configures the clock, data and latch pins as well
// as the number of modules to read from.
void MFInputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t moduleCount, const char *name)
{
_latchPin = latchPin;
_clockPin = clockPin;
_dataPin = dataPin;
_name = name;
_moduleCount = moduleCount;

pinMode(_latchPin, OUTPUT);
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
_latchPin = latchPin;
_clockPin = clockPin;
_dataPin = dataPin;
_name = name;
_moduleCount = moduleCount;

pinMode(_latchPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
pinMode(_dataPin, INPUT);
_initialized = true;

// And now initialize all buttons with the actual status
poll(DONT_TRIGGER);
}

// 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.
void MFInputShifter::update()
{
digitalWrite(_clockPin, HIGH); // Preset clock to retrieve first bit
digitalWrite(_latchPin, HIGH); // Disable input latching and enable shifting

// 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++) {
uint8_t currentState;

currentState = shiftIn(_dataPin, _clockPin, MSBFIRST);
poll(DO_TRIGGER);
}

// 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]) {
detectChanges(_lastState[i], currentState, i);
_lastState[i] = currentState;
void MFInputShifter::poll(uint8_t doTrigger)
{
digitalWrite(_clockPin, HIGH); // Preset clock to retrieve first bit
digitalWrite(_latchPin, HIGH); // Disable input latching and enable shifting

// 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 module = 0; module < _moduleCount; module++) {
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[module]) {
if (doTrigger) detectChanges(_lastState[module], currentState, module);
_lastState[module] = currentState;
}
}
}

digitalWrite(_latchPin, LOW); // disable shifting and enable input latching
digitalWrite(_latchPin, LOW); // disable shifting and enable input latching
}

// Detects changes between the current state and the previously saved state
// of a byte's worth of input.
void MFInputShifter::detectChanges(uint8_t lastState, uint8_t currentState, uint8_t module)
{
for (uint8_t i = 0; i < 8; i++) {
// If last and current input state for the bit are different
// then the input changed and the handler for the bit needs to fire
// If last and current input state for the bit are different
// then the input changed and the handler for the bit needs to fire
if ((lastState & 1) ^ (currentState & 1)) {
// When triggering event the pin is the actual pin on the chip offset by 8 bits for each
// module beyond the first that it's on. The state of the trigger is the bit currently
// in position 0 of currentState.
trigger(i + (module * 8), currentState & 1);
// When triggering event the pin is the actual pin on the chip offset by 8 bits for each
// module beyond the first that it's on. The state of the trigger is the bit currently
// in position 0 of currentState.
trigger(i + (module * 8), currentState & 1);
}

lastState = lastState >> 1;
currentState = currentState >> 1;
}

lastState = lastState >> 1;
currentState = currentState >> 1;
}
}

// 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()
{
uint8_t state;

digitalWrite(_clockPin, HIGH); // Preset clock to retrieve first bit
digitalWrite(_latchPin, HIGH); // Disable input latching and enable shifting
uint8_t state;

// 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++) {
_lastState[module] = shiftIn(_dataPin, _clockPin, MSBFIRST);
}
poll(DONT_TRIGGER);

digitalWrite(_latchPin, LOW); // disable shifting and enable input latching

// Trigger all the released buttons
for (int module = 0; module < _moduleCount; module++) {
state = _lastState[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;
trigger(i + (module * 8), HIGH);
}
state = state >> 1;
}
}
}

// Trigger all the pressed buttons
for (int module = 0; module < _moduleCount; module++) {
state = _lastState[module];
state = _lastState[module];
for (uint8_t i = 0; i < 8; i++) {
// Only trigger if the button is in the on position
if (!(state & 1)) {
trigger(i + (module * 8), LOW);
}
trigger(i + (module * 8), LOW);
}

state = state >> 1;
state = state >> 1;
}
}
}
}

// Triggers the event handler for the associated input shift register pin,
// if a handler is registered.
void MFInputShifter::trigger(uint8_t pin, bool state)
{
if (state == LOW && _inputHandler != NULL) {
(*_inputHandler)(inputShifterOnPress, pin, _name);
} else if (_inputHandler != NULL) {
(*_inputHandler)(inputShifterOnRelease, pin, _name);
}
if (!_inputHandler) return;
(*_inputHandler)((state == LOW ? inputShifterOnPress : inputShifterOnRelease), pin, _name);
}

// Attaches a new event handler for the specified event.
void MFInputShifter::attachHandler(inputShifterEvent newHandler)
{
_inputHandler = newHandler;
_inputHandler = newHandler;
}

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

// MFInputShifter.cpp
Loading

0 comments on commit 2595024

Please sign in to comment.