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 authored Oct 3, 2022
2 parents 1f8c827 + 40b5694 commit d832d0a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 41 deletions.
9 changes: 9 additions & 0 deletions src/CommandMessenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#if MF_OUTPUT_SHIFTER_SUPPORT == 1
#include "OutputShifter.h"
#endif
#if MF_DIGIN_MUX_SUPPORT == 1
#include "DigInMux.h"
#endif

CmdMessenger cmdMessenger = CmdMessenger(Serial);
unsigned long lastCommand;
Expand Down Expand Up @@ -107,6 +110,12 @@ void OnTrigger()
#if MF_INPUT_SHIFTER_SUPPORT == 1
InputShifter::OnTrigger();
#endif
#if MF_DIGIN_MUX_SUPPORT == 1
DigInMux::OnTrigger();
#endif
#if MF_ANALOG_SUPPORT == 1
Analog::OnTrigger();
#endif
}

// commandmessenger.cpp
21 changes: 15 additions & 6 deletions src/MF_Analog/Analog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Analog.cpp
//
//
// (C) MobiFlight Project 2022
//

Expand All @@ -14,7 +14,7 @@ namespace Analog
MFAnalog *analog[MAX_ANALOG_INPUTS];
uint8_t analogRegistered = 0;

void handlerOnAnalogChange(int value, uint8_t pin, const char *name)
void handlerOnAnalogChange(int value, uint8_t pin, const char *name)
{
cmdMessenger.sendCmdStart(kAnalogChange);
cmdMessenger.sendCmdArg(name);
Expand All @@ -35,17 +35,17 @@ namespace Analog
analog[analogRegistered] = new (allocateMemory(sizeof(MFAnalog))) MFAnalog(pin, name, sensitivity);
MFAnalog::attachHandler(handlerOnAnalogChange);
analogRegistered++;
#ifdef DEBUG2CMDMESSENGER
#ifdef DEBUG2CMDMESSENGER
cmdMessenger.sendCmd(kDebug, F("Added analog device "));
#endif
#endif
}

void Clear(void)
{
analogRegistered = 0;
#ifdef DEBUG2CMDMESSENGER
#ifdef DEBUG2CMDMESSENGER
cmdMessenger.sendCmd(kDebug, F("Cleared analog devices"));
#endif
#endif
}

void read(void)
Expand All @@ -61,6 +61,15 @@ namespace Analog
analog[i]->readBuffer();
}
}

void OnTrigger()
{
// Scan and transit current values
for (uint8_t i = 0; i < analogRegistered; i++) {
analog[i]->retrigger();
}
}

} // namespace Analog
#endif

Expand Down
1 change: 1 addition & 0 deletions src/MF_Analog/Analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Analog
void Clear();
void read();
void readAverage();
void OnTrigger();
}

// Analog.h
23 changes: 20 additions & 3 deletions src/MF_Analog/MFAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,37 @@ MFAnalog::MFAnalog(uint8_t pin, const char *name, uint8_t sensitivity)
_name = name;
_lastValue = 0;
pinMode(_pin, INPUT_PULLUP); // set pin to input. Could use OUTPUT for analog, but shows the intention :-)
analogRead(_pin); // turn on pullup resistors
for (uint8_t i = 0; i < ADC_MAX_AVERAGE; i++) {
readBuffer();
}
}

void MFAnalog::update()
bool MFAnalog::valueHasChanged(int16_t newValue)
{
return (abs(newValue - _lastValue) >= _sensitivity);
}

void MFAnalog::readChannel(uint8_t alwaysTrigger)
{
int16_t newValue = ADC_Average_Total >> ADC_MAX_AVERAGE_LOG2;
if (abs(newValue - _lastValue) >= _sensitivity) {
if (alwaysTrigger || valueHasChanged(newValue)) {
_lastValue = newValue;
if (_handler != NULL) {
(*_handler)(_lastValue, _pin, _name);
}
}
}

void MFAnalog::update()
{
readChannel(false);
}

void MFAnalog::retrigger()
{
readChannel(true);
}

void MFAnalog::readBuffer()
{ // read ADC and calculate floating average, call it every ~10ms
ADC_Average_Total -= ADC_Buffer[(ADC_Average_Pointer)]; // subtract oldest value to save the newest value
Expand Down
12 changes: 8 additions & 4 deletions src/MF_Analog/MFAnalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MFAnalog
MFAnalog(uint8_t pin = 1, const char *name = "Analog Input", uint8_t sensitivity = 2);
static void attachHandler(analogEvent handler);
void update();
void retrigger();
void readBuffer();
const char *_name;
uint8_t _pin;
Expand All @@ -33,10 +34,13 @@ class MFAnalog
int _lastValue;
uint8_t _sensitivity;

uint16_t ADC_Buffer[ADC_MAX_AVERAGE] = {0}; // Buffer for all values from each channel
uint16_t ADC_Average_Total = 0; // sum of sampled values, must be divided by ADC_MAX_AVERAGE to get actual value
volatile uint8_t ADC_Average_Pointer = 0; // points to the actual position in ADC_BUFFER
uint32_t _lastReadBuffer;
uint16_t ADC_Buffer[ADC_MAX_AVERAGE] = {0}; // Buffer for all values from each channel
uint16_t ADC_Average_Total = 0; // sum of sampled values, must be divided by ADC_MAX_AVERAGE to get actual value
volatile uint8_t ADC_Average_Pointer = 0; // points to the actual position in ADC_BUFFER
uint32_t _lastReadBuffer;

void readChannel(uint8_t compare);
bool valueHasChanged(int16_t newValue);
};

// MFAnalog.h
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
2 changes: 1 addition & 1 deletion src/MF_DigInMux/DigInMux.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace DigInMux
void Add(uint8_t dataPin, uint8_t nRegs, char const *name = "DigInMux", bool mode = MFDigInMux::MUX_MODE_FAST);
void Clear();
void read();
// void OnTrigger();
void OnTrigger();
}

// DigInMux.h
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 d832d0a

Please sign in to comment.