From ec955d938819cfb5194f33dd6dd56e6c19628f37 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 8 Feb 2022 14:07:24 +0100 Subject: [PATCH 01/12] Split mobiflight.cpp into multiple files Fixes #148 --- _Boards/Atmel/MFEEPROM.cpp | 10 +- include/CommandMessenger.h | 70 + include/MFEEPROM.h | 7 +- include/config.h | 14 + include/mobiflight.h | 133 +- platformio.ini | 13 + src/CommandMessenger.cpp | 106 ++ src/Config.cpp | 445 ++++++ src/MF_Analog/Analog.cpp | 66 + src/MF_Analog/Analog.h | 9 + src/{MF_Modules => MF_Analog}/MFAnalog.cpp | 4 - {include => src/MF_Analog}/MFAnalog.h | 11 +- src/MF_Button/Button.cpp | 70 + src/MF_Button/Button.h | 9 + src/{MF_Modules => MF_Button}/MFButton.cpp | 4 - {include => src/MF_Button}/MFButton.h | 10 +- src/MF_Encoder/Encoder.cpp | 57 + src/MF_Encoder/Encoder.h | 8 + src/{MF_Modules => MF_Encoder}/MFEncoder.cpp | 0 {include => src/MF_Encoder}/MFEncoder.h | 10 +- src/MF_InputShifter/InputShifter.cpp | 72 + src/MF_InputShifter/InputShifter.h | 9 + .../MFInputShifter.cpp | 1 - .../MF_InputShifter}/MFInputShifter.h | 11 +- src/MF_LCDDIsplay/LCDDIsplay.h | 8 + src/MF_LCDDIsplay/LCDDisplay.cpp | 51 + .../MFLCDDisplay.cpp | 37 +- {include => src/MF_LCDDIsplay}/MFLCDDisplay.h | 12 +- src/{MF_Modules => MF_Output}/MFOutput.cpp | 4 - {include => src/MF_Output}/MFOutput.h | 10 +- src/MF_Output/Output.cpp | 58 + src/MF_Output/Output.h | 9 + .../MFOutputShifter.cpp | 0 .../MF_OutputShifter}/MFOutputShifter.h | 9 +- src/MF_OutputShifter/OutputShifter.cpp | 63 + src/MF_OutputShifter/OutputShifter.h | 9 + src/MF_Segment/LedSegment.cpp | 80 + src/MF_Segment/LedSegment.h | 11 + src/{MF_Modules => MF_Segment}/MFSegments.cpp | 33 +- {include => src/MF_Segment}/MFSegments.h | 12 +- src/{MF_Modules => MF_Servo}/MFServo.cpp | 19 +- {include => src/MF_Servo}/MFServo.h | 14 +- src/MF_Servo/Servos.cpp | 62 + src/MF_Servo/Servos.h | 9 + src/{MF_Modules => MF_Stepper}/MFStepper.cpp | 5 +- {include => src/MF_Stepper}/MFStepper.h | 14 +- src/MF_Stepper/Stepper.cpp | 94 ++ src/MF_Stepper/Stepper.h | 11 + src/allocateMem.cpp | 9 +- src/mobiflight.cpp | 1300 ++--------------- 50 files changed, 1573 insertions(+), 1519 deletions(-) create mode 100644 include/CommandMessenger.h create mode 100644 include/config.h create mode 100644 src/CommandMessenger.cpp create mode 100644 src/Config.cpp create mode 100644 src/MF_Analog/Analog.cpp create mode 100644 src/MF_Analog/Analog.h rename src/{MF_Modules => MF_Analog}/MFAnalog.cpp (96%) rename {include => src/MF_Analog}/MFAnalog.h (92%) create mode 100644 src/MF_Button/Button.cpp create mode 100644 src/MF_Button/Button.h rename src/{MF_Modules => MF_Button}/MFButton.cpp (94%) rename {include => src/MF_Button}/MFButton.h (87%) create mode 100644 src/MF_Encoder/Encoder.cpp create mode 100644 src/MF_Encoder/Encoder.h rename src/{MF_Modules => MF_Encoder}/MFEncoder.cpp (100%) rename {include => src/MF_Encoder}/MFEncoder.h (95%) create mode 100644 src/MF_InputShifter/InputShifter.cpp create mode 100644 src/MF_InputShifter/InputShifter.h rename src/{MF_Modules => MF_InputShifter}/MFInputShifter.cpp (99%) rename {include => src/MF_InputShifter}/MFInputShifter.h (90%) create mode 100644 src/MF_LCDDIsplay/LCDDIsplay.h create mode 100644 src/MF_LCDDIsplay/LCDDisplay.cpp rename src/{MF_Modules => MF_LCDDIsplay}/MFLCDDisplay.cpp (59%) rename {include => src/MF_LCDDIsplay}/MFLCDDisplay.h (83%) rename src/{MF_Modules => MF_Output}/MFOutput.cpp (87%) rename {include => src/MF_Output}/MFOutput.h (81%) create mode 100644 src/MF_Output/Output.cpp create mode 100644 src/MF_Output/Output.h rename src/{MF_Modules => MF_OutputShifter}/MFOutputShifter.cpp (100%) rename {include => src/MF_OutputShifter}/MFOutputShifter.h (91%) create mode 100644 src/MF_OutputShifter/OutputShifter.cpp create mode 100644 src/MF_OutputShifter/OutputShifter.h create mode 100644 src/MF_Segment/LedSegment.cpp create mode 100644 src/MF_Segment/LedSegment.h rename src/{MF_Modules => MF_Segment}/MFSegments.cpp (62%) rename {include => src/MF_Segment}/MFSegments.h (84%) rename src/{MF_Modules => MF_Servo}/MFServo.cpp (74%) rename {include => src/MF_Servo}/MFServo.h (86%) create mode 100644 src/MF_Servo/Servos.cpp create mode 100644 src/MF_Servo/Servos.h rename src/{MF_Modules => MF_Stepper}/MFStepper.cpp (96%) rename {include => src/MF_Stepper}/MFStepper.h (86%) create mode 100644 src/MF_Stepper/Stepper.cpp create mode 100644 src/MF_Stepper/Stepper.h diff --git a/_Boards/Atmel/MFEEPROM.cpp b/_Boards/Atmel/MFEEPROM.cpp index 97234c4b..f6872d06 100644 --- a/_Boards/Atmel/MFEEPROM.cpp +++ b/_Boards/Atmel/MFEEPROM.cpp @@ -7,11 +7,13 @@ #include -#include "MFBoards.h" #include "MFEEPROM.h" #include -MFEEPROM::MFEEPROM() {} +MFEEPROM::MFEEPROM() +{ + eepromLength = EEPROM.length(); +} uint16_t MFEEPROM::get_length(void) { return eepromLength; @@ -23,10 +25,6 @@ void MFEEPROM::read_block(uint16_t adr, char data[], uint16_t len) { } } -void MFEEPROM::init() { - eepromLength = EEPROM.length(); -} - void MFEEPROM::write_block (uint16_t adr, char data[], uint16_t len) { if (adr + len >= eepromLength) return; for (uint16_t i = 0; i + +enum +{ + kTypeNotSet, // 0 + kTypeButton, // 1 + kTypeEncoderSingleDetent, // 2 (retained for backwards compatibility, use kTypeEncoder for new configs) + kTypeOutput, // 3 + kTypeLedSegment, // 4 + kTypeStepperDeprecated, // 5 (keep for backwards compatibility, doesn't support autohome) + kTypeServo, // 6 + kTypeLcdDisplayI2C, // 7 + kTypeEncoder, // 8 + kTypeStepper, // 9 (new stepper type with auto zero support if btnPin is > 0) + kShiftRegister, // 10 Shift register support (example: 74HC595, TLC592X) + kTypeAnalogInput, // 11 Analog Device with 1 pin + kTypeInputShifter // 12 Input shift register support (example: 74HC165) +}; + +// This is the list of recognized commands. These can be commands that can either be sent or received. +// In order to receive, attach a callback function to these events +// +// If you increase this list, make sure to check that the MAXCALLBACKS value +// in CmdMessenger.h is set apropriately +enum +{ + kInitModule, // 0 + kSetModule, // 1 + kSetPin, // 2 + kSetStepper, // 3 + kSetServo, // 4 + kStatus, // 5, Command to report status + kEncoderChange, // 6 + kButtonChange, // 7 + kStepperChange, // 8 + kGetInfo, // 9 + kInfo, // 10 + kSetConfig, // 11 + kGetConfig, // 12 + kResetConfig, // 13 + kSaveConfig, // 14 + kConfigSaved, // 15 + kActivateConfig, // 16 + kConfigActivated, // 17 + kSetPowerSavingMode, // 18 + kSetName, // 19 + kGenNewSerial, // 20 + kResetStepper, // 21 + kSetZeroStepper, // 22 + kTrigger, // 23 + kResetBoard, // 24 + kSetLcdDisplayI2C, // 25 + kSetModuleBrightness, // 26 + kSetShiftRegisterPins, // 27 + kAnalogChange, // 28 + kInputShifterChange, // 29 + kDebug = 0xFF // 255 -> for Debug print later, changes in UI are required +}; + +void attachCommandCallbacks(); +uint32_t getLastCommandMillis(); +void setLastCommandMillis(); + +extern CmdMessenger cmdMessenger; + + +#endif diff --git a/include/MFEEPROM.h b/include/MFEEPROM.h index 5a57dd80..01dd7e90 100644 --- a/include/MFEEPROM.h +++ b/include/MFEEPROM.h @@ -5,15 +5,13 @@ /// \version 1.0 Initial release // Copyright (C) 2021 -#ifndef MFEEPROM_H -#define MFEEPROM_H +#pragma once class MFEEPROM { public: MFEEPROM(); - void init(void); uint16_t get_length(void); void read_block(uint16_t addr, char data[], uint16_t len); void write_block (uint16_t addr, char data[], uint16_t len); @@ -24,6 +22,3 @@ class MFEEPROM uint16_t eepromLength = 0; }; - - -#endif diff --git a/include/config.h b/include/config.h new file mode 100644 index 00000000..94cee131 --- /dev/null +++ b/include/config.h @@ -0,0 +1,14 @@ +#pragma once + +void OnSetConfig(); +void OnResetConfig(); +void OnSaveConfig(); +void OnActivateConfig(); +void loadConfig(); +void OnGetConfig(); +void OnGetInfo(); +bool getStatusConfig(); +void generateSerial(bool); +void OnGenNewSerial(); +void OnSetName(); +void _restoreName(); diff --git a/include/mobiflight.h b/include/mobiflight.h index 80f96bab..b9e268bc 100644 --- a/include/mobiflight.h +++ b/include/mobiflight.h @@ -1,137 +1,6 @@ #ifndef _mobiflight_h #define _mobiflight_h -#include -#include -#include - -#define MF_BUTTON_DEBOUNCE_MS 10 // time between updating the buttons -#define MF_SERVO_DELAY_MS 5 // Time between servo updates -#define MF_ANALOGAVERAGE_DELAY_MS 10 // time between updating the analog average calculation -#define MF_ANALOGREAD_DELAY_MS 50 // time between sending analog values - -enum -{ - kTypeNotSet, // 0 - kTypeButton, // 1 - kTypeEncoderSingleDetent, // 2 (retained for backwards compatibility, use kTypeEncoder for new configs) - kTypeOutput, // 3 - kTypeLedSegment, // 4 - kTypeStepperDeprecated, // 5 (keep for backwards compatibility, doesn't support autohome) - kTypeServo, // 6 - kTypeLcdDisplayI2C, // 7 - kTypeEncoder, // 8 - kTypeStepper, // 9 (new stepper type with auto zero support if btnPin is > 0) - kShiftRegister, // 10 Shift register support (example: 74HC595, TLC592X) - kTypeAnalogInput, // 11 Analog Device with 1 pin - kTypeInputShifter // 12 Input shift register support (example: 74HC165) -}; - -// This is the list of recognized commands. These can be commands that can either be sent or received. -// In order to receive, attach a callback function to these events -// -// If you increase this list, make sure to check that the MAXCALLBACKS value -// in CmdMessenger.h is set apropriately -enum -{ - kInitModule, // 0 - kSetModule, // 1 - kSetPin, // 2 - kSetStepper, // 3 - kSetServo, // 4 - kStatus, // 5, Command to report status - kEncoderChange, // 6 - kButtonChange, // 7 - kStepperChange, // 8 - kGetInfo, // 9 - kInfo, // 10 - kSetConfig, // 11 - kGetConfig, // 12 - kResetConfig, // 13 - kSaveConfig, // 14 - kConfigSaved, // 15 - kActivateConfig, // 16 - kConfigActivated, // 17 - kSetPowerSavingMode, // 18 - kSetName, // 19 - kGenNewSerial, // 20 - kResetStepper, // 21 - kSetZeroStepper, // 22 - kTrigger, // 23 - kResetBoard, // 24 - kSetLcdDisplayI2C, // 25 - kSetModuleBrightness, // 26 - kSetShiftRegisterPins, // 27 - kAnalogChange, // 28 - kInputShifterChange, // 29 - kDebug = 0xFF // 255 -> for Debug print later, changes in UI are required -}; - -void attachCommandCallbacks(); void OnResetBoard(); -void generateSerial(bool force); -void loadConfig(); -void _storeConfig(); -void SetPowerSavingMode(bool state); -void updatePowerSaving(); -void AddOutput(uint8_t pin, char const *name); -void ClearOutputs(); -void AddButton(uint8_t pin, char const *name, bool repeat); -void ClearButtons(); -void AddEncoder(uint8_t pin1, uint8_t pin2, uint8_t encoder_type, char const *name); -void ClearEncoders(); -void AddLedSegment(int dataPin, int csPin, int clkPin, int numDevices, int brightness); -void ClearLedSegments(); -void PowerSaveLedSegment(bool state); -void AddStepper(int pin1, int pin2, int pin3, int pin4, int btnPin1); -void ClearSteppers(); -void AddServo(int pin); -void ClearServos(); -void AddLcdDisplay(uint8_t address, uint8_t cols, uint8_t lines, char const *name); -void ClearLcdDisplays(); -void handlerOnButton(uint8_t eventId, uint8_t pin, const char *name); -void handlerOnEncoder(uint8_t eventId, uint8_t pin, const char *name); -void OnSetConfig(); -void resetConfig(); -void OnResetConfig(); -void OnSaveConfig(); -void OnActivateConfig(); -void _activateConfig(); -void readConfig(); -void OnUnknownCommand(); -void OnGetInfo(); -void OnGetConfig(); -void OnSetPin(); -void OnInitModule(); -void OnSetModule(); -void OnSetModuleBrightness(); -void OnSetStepper(); -void OnResetStepper(); -void OnSetZeroStepper(); -void updateSteppers(); -void OnSetServo(); -void updateServos(); -void OnSetLcdDisplayI2C(); -void readButtons(); -void readEncoder(); -void OnGenNewSerial(); -void OnSetName(); -void _storeName(); -void _restoreName(); -void OnTrigger(); -void readAnalog(); -void AddAnalog(uint8_t pin, char const *name, uint8_t sensitivity); -void ClearAnalog(); -void handlerOnAnalogChange(int value, uint8_t pin, const char *name); -void OnInitOutputShifter(); -void OnSetOutputShifterPins(); -void AddOutputShifter(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name); -void AddInputShifter(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name); -void ClearInputShifters(); -void readInputShifters(); -void handlerInputShifterOnChange(uint8_t eventId, uint8_t pin, const char *name); -void loadConfig(); - -extern CmdMessenger cmdMessenger; -#endif \ No newline at end of file +#endif diff --git a/platformio.ini b/platformio.ini index c13f8315..2745a67c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,6 +31,19 @@ build_flags = -DMESSENGERBUFFERSIZE=96 -DMAXSTREAMBUFFERSIZE=96 -DDEFAULT_TIMEOUT=5000 +; -DDEBUG2CMDMESSENGER=1 + -I./src/MF_Analog + -I./src/MF_Button + -I./src/MF_Encoder + -I./src/MF_InputShifter + -I./src/MF_LCDDisplay + -I./src/MF_Output + -I./src/MF_OutputShifter + -I./src/MF_Segment + -I./src/MF_OutputShifter + -I./src/MF_Servo + -I./src/MF_OutputShifter + -I./src/MF_Stepper src_filter = +<*> extra_scripts = diff --git a/src/CommandMessenger.cpp b/src/CommandMessenger.cpp new file mode 100644 index 00000000..3a3f1097 --- /dev/null +++ b/src/CommandMessenger.cpp @@ -0,0 +1,106 @@ +#include +#include "MFBoards.h" +#include "commandmessenger.h" +#include "config.h" +#include "mobiflight.h" +#include "Button.h" +#include "Encoder.h" +#if MF_ANALOG_SUPPORT == 1 +#include "Analog.h" +#endif +#if MF_INPUT_SHIFTER_SUPPORT == 1 +#include "InputShifter.h" +#endif +#include "Output.h" +#if MF_SEGMENT_SUPPORT == 1 +#include "LedSegment.h" +#endif +#if MF_STEPPER_SUPPORT == 1 +#include "Stepper.h" +#endif +#if MF_SERVO_SUPPORT == 1 +#include "Servos.h" +#endif +#if MF_LCD_SUPPORT == 1 +#include "LCDDisplay.h" +#endif +#if MF_OUTPUT_SHIFTER_SUPPORT == 1 +#include "OutputShifter.h" +#endif + + +CmdMessenger cmdMessenger = CmdMessenger(Serial); +unsigned long lastCommand; + +void OnTrigger(); +void OnUnknownCommand(); + +// Callbacks define on which received commands we take action +void attachCommandCallbacks() +{ + // Attach callback methods + cmdMessenger.attach(OnUnknownCommand); + +#if MF_SEGMENT_SUPPORT == 1 + cmdMessenger.attach(kInitModule, LedSegment::OnInitModule); + cmdMessenger.attach(kSetModule, LedSegment::OnSetModule); + cmdMessenger.attach(kSetModuleBrightness, LedSegment::OnSetModuleBrightness); +#endif + + cmdMessenger.attach(kSetPin, Output::OnSet); + +#if MF_STEPPER_SUPPORT == 1 + cmdMessenger.attach(kSetStepper, Stepper::OnSet); + cmdMessenger.attach(kResetStepper, Stepper::OnReset); + cmdMessenger.attach(kSetZeroStepper, Stepper::OnSetZero); +#endif + +#if MF_SERVO_SUPPORT == 1 + cmdMessenger.attach(kSetServo, Servos::OnSet); +#endif + + cmdMessenger.attach(kGetInfo, OnGetInfo); + cmdMessenger.attach(kGetConfig, OnGetConfig); + cmdMessenger.attach(kSetConfig, OnSetConfig); + cmdMessenger.attach(kResetConfig, OnResetConfig); + cmdMessenger.attach(kSaveConfig, OnSaveConfig); + cmdMessenger.attach(kActivateConfig, OnActivateConfig); + cmdMessenger.attach(kSetName, OnSetName); + cmdMessenger.attach(kGenNewSerial, OnGenNewSerial); + cmdMessenger.attach(kTrigger, OnTrigger); + +#if MF_LCD_SUPPORT == 1 + cmdMessenger.attach(kSetLcdDisplayI2C, LCDDisplay::OnSet); +#endif + +#if MF_OUTPUT_SHIFTER_SUPPORT + cmdMessenger.attach(kSetShiftRegisterPins, OutputShifter::OnSet); +#endif + +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Attached callbacks")); +#endif +} + +// Called when a received command has no attached function +void OnUnknownCommand() +{ + lastCommand = millis(); + cmdMessenger.sendCmd(kStatus, F("n/a")); +} + +uint32_t getLastCommandMillis() { + return lastCommand; +} + +void setLastCommandMillis() { + lastCommand = millis(); +} + +void OnTrigger() +{ + Button::OnTrigger(); +#if MF_INPUT_SHIFTER_SUPPORT == 1 + InputShifter::OnTrigger(); +#endif +} diff --git a/src/Config.cpp b/src/Config.cpp new file mode 100644 index 00000000..b93ab4ef --- /dev/null +++ b/src/Config.cpp @@ -0,0 +1,445 @@ +#include +#include "MFBoards.h" +#include "config.h" +#include "MFEEPROM.h" +#include "commandmessenger.h" +#include "Button.h" +#include "Encoder.h" +#include "Output.h" +#if MF_ANALOG_SUPPORT == 1 +#include "Analog.h" +#endif +#if MF_INPUT_SHIFTER_SUPPORT == 1 +#include "InputShifter.h" +#endif +#if MF_SEGMENT_SUPPORT == 1 +#include "LedSegment.h" +#endif +#if MF_STEPPER_SUPPORT == 1 +#include "Stepper.h" +#endif +#if MF_SERVO_SUPPORT == 1 +#include "Servos.h" +#endif +#if MF_LCD_SUPPORT == 1 +#include "LCDDisplay.h" +#endif +#if MF_OUTPUT_SHIFTER_SUPPORT == 1 +#include "OutputShifter.h" +#endif + +// The build version comes from an environment variable +#define STRINGIZER(arg) #arg +#define STR_VALUE(arg) STRINGIZER(arg) +#define VERSION STR_VALUE(BUILD_VERSION) + +MFEEPROM MFeeprom; + +const uint8_t MEM_OFFSET_NAME = 0; +const uint8_t MEM_LEN_NAME = 48; +const uint8_t MEM_OFFSET_SERIAL = MEM_OFFSET_NAME + MEM_LEN_NAME; +const uint8_t MEM_LEN_SERIAL = 11; +const uint8_t MEM_OFFSET_CONFIG = MEM_OFFSET_NAME + MEM_LEN_NAME + MEM_LEN_SERIAL; + +const char type[sizeof(MOBIFLIGHT_TYPE)] = MOBIFLIGHT_TYPE; +char serial[MEM_LEN_SERIAL] = MOBIFLIGHT_SERIAL; +char name[MEM_LEN_NAME] = MOBIFLIGHT_NAME; +const int MEM_LEN_CONFIG = MEMLEN_CONFIG; +char nameBuffer[MEM_LEN_CONFIG] = ""; +uint16_t configLength = 0; +boolean configActivated = false; + +void resetConfig(); +void readConfig(); +void _activateConfig(); + +// ************************************************************ +// configBuffer handling +// ************************************************************ +// reads the EEPRROM until NULL termination and returns the number of characters incl. NULL termination, starting from given address +bool readConfigLength() +{ + char temp = 0; + uint16_t addreeprom = MEM_OFFSET_CONFIG; + uint16_t length = MFeeprom.get_length(); + configLength = 0; + do + { + temp = MFeeprom.read_char(addreeprom++); + configLength++; + if (addreeprom > length) // abort if EEPROM size will be exceeded + { + cmdMessenger.sendCmd(kStatus, F("Loading config failed")); // text or "-1" like config upload? + return false; + } + } + while (temp != 0x00); // reads until NULL + configLength--; + return true; +} + +void loadConfig() +{ +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Load config")); +#endif + if (readConfigLength()) + { + readConfig(); + _activateConfig(); + } +} + +void OnSetConfig() +{ +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Setting config start")); +#endif + setLastCommandMillis(); + char *cfg = cmdMessenger.readStringArg(); + uint8_t cfgLen = strlen(cfg); + + if (configLength + cfgLen + 1 < MEM_LEN_CONFIG) + { + MFeeprom.write_block(MEM_OFFSET_CONFIG + configLength, cfg, cfgLen+1); // save the received config string including the terminatung NULL (+1) to EEPROM + configLength += cfgLen; + cmdMessenger.sendCmd(kStatus, configLength); + } + else + cmdMessenger.sendCmd(kStatus, -1); // last successfull saving block is already NULL terminated, nothing more todo +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Setting config end")); +#endif +} + +void resetConfig() +{ + Button::Clear(); + Encoder::Clear(); + Output::Clear(); + +#if MF_SEGMENT_SUPPORT == 1 + LedSegment::Clear(); +#endif + +#if MF_SERVO_SUPPORT == 1 + Servos::Clear(); +#endif + +#if MF_STEPPER_SUPPORT == 1 + Stepper::Clear(); +#endif + +#if MF_LCD_SUPPORT == 1 + LCDDisplay::Clear(); +#endif + +#if MF_ANALOG_SUPPORT == 1 + Analog::Clear(); +#endif + +#if MF_OUTPUT_SHIFTER_SUPPORT == 1 + OutputShifter::Clear(); +#endif + +#if MF_INPUT_SHIFTER_SUPPORT == 1 + InputShifter::Clear(); +#endif + + configLength = 0; + configActivated = false; +} + +void OnResetConfig() +{ + resetConfig(); + cmdMessenger.sendCmd(kStatus, F("OK")); +} + +void OnSaveConfig() +{ + cmdMessenger.sendCmd(kConfigSaved, F("OK")); +} + +void OnActivateConfig() +{ + readConfig(); + _activateConfig(); +} + +void _activateConfig() +{ + configActivated = true; + cmdMessenger.sendCmd(kConfigActivated, F("OK")); +} + +// reads an ascii value which is '.' terminated from EEPROM and returns it's value +uint8_t readUintFromEEPROM (volatile uint16_t *addreeprom) { + char params[4] = {0}; // max 3 (255) digits NULL terminated + uint8_t counter = 0; + do + { + params[counter++] = MFeeprom.read_char((*addreeprom)++); // read character from eeprom and locate next buffer and eeprom location + } + while ( params[counter-1] != '.' && counter < sizeof(params)); // reads until limiter '.' and for safety reason not more then size of params[] + params[counter-1] = 0x00; // replace '.' by NULL to terminate the string + return atoi(params); +} + +// reads a string from EEPROM at given address which is ':' terminated and saves it in the nameBuffer +// once the nameBuffer is not needed anymore, just read until the ":" termination -> see function below +bool readNameFromEEPROM(uint16_t *addreeprom, char* buffer, uint16_t *addrbuffer) { + char temp = 0; + do + { + temp = MFeeprom.read_char((*addreeprom)++); // read the first character + buffer[(*addrbuffer)++] = temp; // save character and locate next buffer position + if (*addrbuffer >= MEMLEN_CONFIG_BUFFER) { // nameBuffer will be exceeded + return false; // abort copying from EEPROM to nameBuffer + } + } while (temp != ':'); // reads until limiter ':' and locates the next free buffer position + buffer[(*addrbuffer)-1] = 0x00; // replace ':' by NULL, terminates the string + return true; +} + +// reads the EEPRROM until end of command which ':' terminated +bool readEndCommandFromEEPROM(uint16_t *addreeprom) { + char temp = 0; + uint16_t length = MFeeprom.get_length(); + do + { + temp = MFeeprom.read_char((*addreeprom)++); + if (*addreeprom > length) // abort if EEPROM size will be exceeded + return false; + } + while (temp != ':'); // reads until limiter ':' + return true; +} + +void readConfig() +{ + if (configLength == 0) // do nothing if no config is available + return; + uint16_t addreeprom = MEM_OFFSET_CONFIG; // define first memory location where config is saved in EEPROM + uint16_t addrbuffer = 0; // and start with first memory location from nameBuffer + char params[6] = ""; + char command = readUintFromEEPROM(&addreeprom); // read the first value from EEPROM, it's a device definition + bool copy_success = true; // will be set to false if copying input names to nameBuffer exceeds array dimensions + // not required anymore when pins instead of names are transferred to the UI + + if (command == 0) // just to be sure, configLength should also be 0 + return; + + do // go through the EEPROM until it is NULL terminated + { + switch (command) + { + case kTypeButton: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin number + Button::Add(params[0], &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name + copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to nameBuffer and set to next free memory location + break; + + case kTypeOutput: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin number + Output::Add(params[0]); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; + +#if MF_SEGMENT_SUPPORT == 1 + case kTypeLedSegment: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin Data number + params[1] = readUintFromEEPROM(&addreeprom); // get the Pin CS number + params[2] = readUintFromEEPROM(&addreeprom); // get the Pin CLK number + params[3] = readUintFromEEPROM(&addreeprom); // get the brightness + params[4] = readUintFromEEPROM(&addreeprom); // get the number of modules + LedSegment::Add(params[0], params[1], params[2], params[4], params[3]); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; +#endif + +#if MF_STEPPER_SUPPORT == 1 + case kTypeStepperDeprecated: + // this is for backwards compatibility + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number + params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number + params[2] = readUintFromEEPROM(&addreeprom); // get the Pin3 number + params[3] = readUintFromEEPROM(&addreeprom); // get the Pin4 number + params[4] = readUintFromEEPROM(&addreeprom); // get the Button number + Stepper::Add(params[0], params[1], params[2], params[3], 0); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; +#endif + +#if MF_STEPPER_SUPPORT == 1 + case kTypeStepper: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number + params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number + params[2] = readUintFromEEPROM(&addreeprom); // get the Pin3 number + params[3] = readUintFromEEPROM(&addreeprom); // get the Pin4 number + params[4] = readUintFromEEPROM(&addreeprom); // get the Button number + Stepper::Add(params[0], params[1], params[2], params[3], params[4]); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; +#endif + +#if MF_SERVO_SUPPORT == 1 + case kTypeServo: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin number + Servos::Add(params[0]); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; +#endif + + case kTypeEncoderSingleDetent: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number + params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number + Encoder::Add(params[0], params[1], 0, &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name + copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to nameBuffer and set to next free memory location +// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before + break; + + case kTypeEncoder: + params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number + params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number + params[2] = readUintFromEEPROM(&addreeprom); // get the type + Encoder::Add(params[0], params[1], params[2], &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name + copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location +// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before + break; + +#if MF_LCD_SUPPORT == 1 + case kTypeLcdDisplayI2C: + params[0] = readUintFromEEPROM(&addreeprom); // get the address + params[1] = readUintFromEEPROM(&addreeprom); // get the columns + params[2] = readUintFromEEPROM(&addreeprom); // get the lines + LCDDisplay::Add(params[0], params[1], params[2]); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; +#endif + +#if MF_ANALOG_SUPPORT == 1 + case kTypeAnalogInput: + params[0] = readUintFromEEPROM(&addreeprom); // get the pin number + params[1] = readUintFromEEPROM(&addreeprom); // get the sensitivity + Analog::Add(params[0], &nameBuffer[addrbuffer], params[1]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name + copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location +// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before + break; +#endif + +#if MF_OUTPUT_SHIFTER_SUPPORT == 1 + case kShiftRegister: + params[0] = readUintFromEEPROM(&addreeprom); // get the latch Pin + params[1] = readUintFromEEPROM(&addreeprom); // get the clock Pin + params[2] = readUintFromEEPROM(&addreeprom); // get the data Pin + params[3] = readUintFromEEPROM(&addreeprom); // get the number of daisy chained modules + OutputShifter::Add(params[0], params[1], params[2], params[3]); + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + break; +#endif + +#if MF_INPUT_SHIFTER_SUPPORT == 1 + case kTypeInputShifter: + params[0] = readUintFromEEPROM(&addreeprom); // get the latch Pin + params[1] = readUintFromEEPROM(&addreeprom); // get the clock Pin + params[2] = readUintFromEEPROM(&addreeprom); // get the data Pin + params[3] = readUintFromEEPROM(&addreeprom); // get the number of daisy chained modules + InputShifter::Add(params[0], params[1], params[2], params[3], &nameBuffer[addrbuffer]); + copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location +// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before + break; +#endif + + default: + copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name + } + command = readUintFromEEPROM(&addreeprom); + } while (command && copy_success); + if (!copy_success) { // too much/long names for input devices + nameBuffer[MEMLEN_CONFIG_BUFFER-1] = 0x00; // terminate the last copied (part of) string with 0x00 + cmdMessenger.sendCmd(kStatus, F("Failure on reading config")); + } +} + +void OnGetConfig() +{ + setLastCommandMillis(); + cmdMessenger.sendCmdStart(kInfo); + if (configLength > 0) + { + cmdMessenger.sendCmdArg(MFeeprom.read_char(MEM_OFFSET_CONFIG)); + for (uint16_t i = 1; i < configLength; i++) + { + cmdMessenger.sendArg(MFeeprom.read_char(MEM_OFFSET_CONFIG + i)); + } + } + cmdMessenger.sendCmdEnd(); +} + +void OnGetInfo() +{ + setLastCommandMillis(); + cmdMessenger.sendCmdStart(kInfo); + cmdMessenger.sendCmdArg(type); + cmdMessenger.sendCmdArg(name); + cmdMessenger.sendCmdArg(serial); + cmdMessenger.sendCmdArg(VERSION); + cmdMessenger.sendCmdEnd(); +} + +bool getStatusConfig() +{ + return configActivated; +} + +// ************************************************************ +// serial number handling +// ************************************************************ +void generateSerial(bool force) +{ + MFeeprom.read_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL); + if (!force && serial[0] == 'S' && serial[1] == 'N') + return; + randomSeed(analogRead(RANDOM_SEED_INPUT)); + sprintf(serial, "SN-%03x-", (unsigned int)random(4095)); + sprintf(&serial[7], "%03x", (unsigned int)random(4095)); + MFeeprom.write_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL); + if (!force) { + MFeeprom.write_byte(MEM_OFFSET_CONFIG, 0x00); // First byte of config to 0x00 to ensure to start 1st time with empty config, but not if forced from the connector to generate a new one + } +} + +void OnGenNewSerial() +{ + generateSerial(true); + cmdMessenger.sendCmd(kInfo,serial); +} + +// ************************************************************ +// Naming handling +// ************************************************************ +void _storeName() +{ + char prefix[] = "#"; + MFeeprom.write_block(MEM_OFFSET_NAME, prefix, 1); + MFeeprom.write_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1); +} + +void _restoreName() +{ + char testHasName[1] = ""; + MFeeprom.read_block(MEM_OFFSET_NAME, testHasName, 1); + if (testHasName[0] != '#') + return; + + MFeeprom.read_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1); +} + +void OnSetName() +{ + char *cfg = cmdMessenger.readStringArg(); + memcpy(name, cfg, MEM_LEN_NAME); + _storeName(); + cmdMessenger.sendCmd(kStatus,name); +} diff --git a/src/MF_Analog/Analog.cpp b/src/MF_Analog/Analog.cpp new file mode 100644 index 00000000..0f2ea519 --- /dev/null +++ b/src/MF_Analog/Analog.cpp @@ -0,0 +1,66 @@ +#include +#include "MFAnalog.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "analog.h" +#include "MFBoards.h" + +#if MF_ANALOG_SUPPORT == 1 +namespace Analog +{ +MFAnalog *analog[MAX_ANALOG_INPUTS]; +uint8_t analogRegistered = 0; + +void handlerOnAnalogChange(int value, uint8_t pin, const char *name) +{ + cmdMessenger.sendCmdStart(kAnalogChange); + cmdMessenger.sendCmdArg(name); + cmdMessenger.sendCmdArg(value); + cmdMessenger.sendCmdEnd(); +}; + +void Add(uint8_t pin = 1, char const *name = "AnalogInput", uint8_t sensitivity = 3) +{ + if (analogRegistered == MAX_ANALOG_INPUTS) + return; + + if (!FitInMemory(sizeof(MFAnalog))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("AnalogIn does not fit in Memory")); + return; + } + analog[analogRegistered] = new (allocateMemory(sizeof(MFAnalog))) MFAnalog(pin, name, sensitivity); + analog[analogRegistered]->attachHandler(handlerOnAnalogChange); + analogRegistered++; + #ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added analog device ")); + #endif +} + +void Clear() +{ + analogRegistered = 0; + #ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared analog devices")); + #endif +} + +void read() +{ + for (int i = 0; i != analogRegistered; i++) + { + analog[i]->update(); + } +} + +void readAverage() +{ + for (int i = 0; i != analogRegistered; i++) + { + analog[i]->readBuffer(); + } +} +} // end of namespace Analog +#endif diff --git a/src/MF_Analog/Analog.h b/src/MF_Analog/Analog.h new file mode 100644 index 00000000..11fb59d2 --- /dev/null +++ b/src/MF_Analog/Analog.h @@ -0,0 +1,9 @@ +#pragma once + +namespace Analog +{ +void Add(uint8_t pin, char const *name, uint8_t sensitivity); +void Clear(); +void read(); +void readAverage(); +} diff --git a/src/MF_Modules/MFAnalog.cpp b/src/MF_Analog/MFAnalog.cpp similarity index 96% rename from src/MF_Modules/MFAnalog.cpp rename to src/MF_Analog/MFAnalog.cpp index 408d632e..874b66ea 100644 --- a/src/MF_Modules/MFAnalog.cpp +++ b/src/MF_Analog/MFAnalog.cpp @@ -1,7 +1,3 @@ -// MFSegments.cpp -// -// Copyright (C) 2021 - #include "MFAnalog.h" analogEvent MFAnalog::_handler = NULL; diff --git a/include/MFAnalog.h b/src/MF_Analog/MFAnalog.h similarity index 92% rename from include/MFAnalog.h rename to src/MF_Analog/MFAnalog.h index daa14192..d04e568b 100644 --- a/include/MFAnalog.h +++ b/src/MF_Analog/MFAnalog.h @@ -1,19 +1,15 @@ -// MFSegments.h +// MFAnalog.h // /// \mainpage MF MFAnalog module for MobiFlight Framework /// \par Revision History /// \version 1.0 Initial release /// \author Manfred Berry (manfred@nystedberry.info) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2021 Manfred Berry +/// \version 1.1 Floating Average added -#ifndef MFAnalog_h -#define MFAnalog_h +#pragma once -#if ARDUINO >= 100 #include -#else -#include -#endif #define ADC_MAX_AVERAGE 8 // must be 2^n #define ADC_MAX_AVERAGE_LOG2 3 // please calculate LOG2(ADC_MAX_AVERAGE) @@ -47,4 +43,3 @@ class MFAnalog volatile uint8_t ADC_Average_Pointer = 0; // points to the actual position in ADC_BUFFER uint32_t _lastReadBuffer; }; -#endif \ No newline at end of file diff --git a/src/MF_Button/Button.cpp b/src/MF_Button/Button.cpp new file mode 100644 index 00000000..3b3d243a --- /dev/null +++ b/src/MF_Button/Button.cpp @@ -0,0 +1,70 @@ +#include +#include "MFButton.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "button.h" +#include "MFBoards.h" + +namespace Button +{ +MFButton *buttons[MAX_BUTTONS]; +uint8_t buttonsRegistered = 0; + +void handlerOnButton(uint8_t eventId, uint8_t pin, const char *name) +{ + cmdMessenger.sendCmdStart(kButtonChange); + cmdMessenger.sendCmdArg(name); + cmdMessenger.sendCmdArg(eventId); + cmdMessenger.sendCmdEnd(); +}; + +void Add(uint8_t pin = 1, char const *name = "Button") +{ + if (buttonsRegistered == MAX_BUTTONS) + return; + + if (!FitInMemory(sizeof(MFButton))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("Button does not fit in Memory")); + return; + } + buttons[buttonsRegistered] = new (allocateMemory(sizeof(MFButton))) MFButton(pin, name); + buttons[buttonsRegistered]->attachHandler(handlerOnButton); + buttonsRegistered++; + #ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added button ") /* + name */); + #endif +} + +void Clear() +{ + buttonsRegistered = 0; + #ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared buttons")); + #endif +} + +void read() +{ + for (int i = 0; i != buttonsRegistered; i++) + { + buttons[i]->update(); + } +} + +void OnTrigger() +{ + // Trigger all button release events first... + for (int i = 0; i != buttonsRegistered; i++) + { + buttons[i]->triggerOnRelease(); + } + // ... then trigger all the press events + for (int i = 0; i != buttonsRegistered; i++) + { + buttons[i]->triggerOnPress(); + } +} +} // end of namespace button diff --git a/src/MF_Button/Button.h b/src/MF_Button/Button.h new file mode 100644 index 00000000..8146dacc --- /dev/null +++ b/src/MF_Button/Button.h @@ -0,0 +1,9 @@ +#pragma once + +namespace Button +{ +void Add(uint8_t pin, char const *name); +void Clear(); +void read(); +void OnTrigger(); +} diff --git a/src/MF_Modules/MFButton.cpp b/src/MF_Button/MFButton.cpp similarity index 94% rename from src/MF_Modules/MFButton.cpp rename to src/MF_Button/MFButton.cpp index c4381fb8..68f86ce8 100644 --- a/src/MF_Modules/MFButton.cpp +++ b/src/MF_Button/MFButton.cpp @@ -1,7 +1,3 @@ -// MFButton.cpp -// -// Copyright (C) 2013-2014 - #include "MFButton.h" buttonEvent MFButton::_handler = NULL; diff --git a/include/MFButton.h b/src/MF_Button/MFButton.h similarity index 87% rename from include/MFButton.h rename to src/MF_Button/MFButton.h index eb71b6ad..532545dc 100644 --- a/include/MFButton.h +++ b/src/MF_Button/MFButton.h @@ -6,16 +6,9 @@ /// \author Sebastian Moebius (mobiflight@moebiuz.de) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2013-2014 Sebastian Moebius -#ifndef MFButton_h -#define MFButton_h +#pragma once -#include -#if ARDUINO >= 100 #include -#else -#include -#include -#endif extern "C" { @@ -48,4 +41,3 @@ class MFButton bool _state; }; -#endif \ No newline at end of file diff --git a/src/MF_Encoder/Encoder.cpp b/src/MF_Encoder/Encoder.cpp new file mode 100644 index 00000000..8ea33a2a --- /dev/null +++ b/src/MF_Encoder/Encoder.cpp @@ -0,0 +1,57 @@ +#include +#include "MFEncoder.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "encoder.h" +#include "MFBoards.h" + +namespace Encoder +{ +MFEncoder *encoders[MAX_ENCODERS]; +uint8_t encodersRegistered = 0; + +void handlerOnEncoder(uint8_t eventId, uint8_t pin, const char *name) +{ + cmdMessenger.sendCmdStart(kEncoderChange); + cmdMessenger.sendCmdArg(name); + cmdMessenger.sendCmdArg(eventId); + cmdMessenger.sendCmdEnd(); +}; + +void Add(uint8_t pin1 = 1, uint8_t pin2 = 2, uint8_t encoder_type = 0, char const *name = "Encoder") +{ + if (encodersRegistered == MAX_ENCODERS) + return; + + if (!FitInMemory(sizeof(MFEncoder))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("Encoders does not fit in Memory")); + return; + } + encoders[encodersRegistered] = new (allocateMemory(sizeof(MFEncoder))) MFEncoder; + encoders[encodersRegistered]->attach(pin1, pin2, encoder_type, name); + encoders[encodersRegistered]->attachHandler(handlerOnEncoder); + encodersRegistered++; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added encoder")); +#endif +} + +void Clear() +{ + encodersRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared encoders")); +#endif +} + +void read() +{ + for (int i = 0; i != encodersRegistered; i++) + { + encoders[i]->update(); + } +} +} // end of namespace encoder diff --git a/src/MF_Encoder/Encoder.h b/src/MF_Encoder/Encoder.h new file mode 100644 index 00000000..bc9d4d52 --- /dev/null +++ b/src/MF_Encoder/Encoder.h @@ -0,0 +1,8 @@ +#pragma once + +namespace Encoder +{ +void Add(uint8_t pin1, uint8_t pin2, uint8_t encoder_type, char const *name); +void Clear(); +void read(); +} diff --git a/src/MF_Modules/MFEncoder.cpp b/src/MF_Encoder/MFEncoder.cpp similarity index 100% rename from src/MF_Modules/MFEncoder.cpp rename to src/MF_Encoder/MFEncoder.cpp diff --git a/include/MFEncoder.h b/src/MF_Encoder/MFEncoder.h similarity index 95% rename from include/MFEncoder.h rename to src/MF_Encoder/MFEncoder.h index cc487d90..04dc2c24 100644 --- a/include/MFEncoder.h +++ b/src/MF_Encoder/MFEncoder.h @@ -18,16 +18,9 @@ // 18.01.2014 created by Matthias Hertel // ----- -#ifndef MFEncoder_h -#define MFEncoder_h +#pragma once -#include -#if ARDUINO >= 100 #include -#else -#include -#include -#endif extern "C" { @@ -93,4 +86,3 @@ class MFEncoder uint32_t _positionTimePrev; // time previous position change was detected uint32_t _lastFastDec; }; -#endif \ No newline at end of file diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp new file mode 100644 index 00000000..0a8192aa --- /dev/null +++ b/src/MF_InputShifter/InputShifter.cpp @@ -0,0 +1,72 @@ +#include +#include "MFInputShifter.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "inputShifter.h" +#include "MFBoards.h" + +namespace InputShifter +{ +MFInputShifter *inputShifters[MAX_INPUT_SHIFTERS]; +uint8_t inputShiftersRegistered = 0; + +void handlerInputShifterOnChange(uint8_t eventId, uint8_t pin, const char *name) +{ + cmdMessenger.sendCmdStart(kInputShifterChange); + cmdMessenger.sendCmdArg(name); + cmdMessenger.sendCmdArg(pin); + cmdMessenger.sendCmdArg(eventId); + cmdMessenger.sendCmdEnd(); +}; + +void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name = "Shifter") +{ + if (inputShiftersRegistered == MAX_INPUT_SHIFTERS) + return; + if (!FitInMemory(sizeof(MFInputShifter))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("InputShifter does not fit in Memory")); + return; + } + inputShifters[inputShiftersRegistered] = new (allocateMemory(sizeof(MFInputShifter))) MFInputShifter; + inputShifters[inputShiftersRegistered]->attach(latchPin, clockPin, dataPin, modules, name); + inputShifters[inputShiftersRegistered]->clear(); + inputShifters[inputShiftersRegistered]->attachHandler(handlerInputShifterOnChange); + inputShiftersRegistered++; + #ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added input shifter")); + #endif +} + +void Clear() +{ + for (int i = 0; i < inputShiftersRegistered; i++) + { + inputShifters[inputShiftersRegistered]->detach(); + } + inputShiftersRegistered = 0; + #ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared input shifter")); + #endif +} + +void read() +{ + for (int i = 0; i != inputShiftersRegistered; i++) + { + inputShifters[i]->update(); + } +} + +void OnTrigger() +{ + // Retrigger all the input shifters. This automatically sends + // the release events first followed by press events. + for (int i = 0; i != inputShiftersRegistered; i++) + { + inputShifters[i]->retrigger(); + } +} +} // end of namespace InputShifter diff --git a/src/MF_InputShifter/InputShifter.h b/src/MF_InputShifter/InputShifter.h new file mode 100644 index 00000000..a60ca136 --- /dev/null +++ b/src/MF_InputShifter/InputShifter.h @@ -0,0 +1,9 @@ +#pragma once + +namespace InputShifter +{ +void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name); +void Clear(); +void read(); +void OnTrigger(); +} diff --git a/src/MF_Modules/MFInputShifter.cpp b/src/MF_InputShifter/MFInputShifter.cpp similarity index 99% rename from src/MF_Modules/MFInputShifter.cpp rename to src/MF_InputShifter/MFInputShifter.cpp index 26ddc6a8..6ee2e33e 100644 --- a/src/MF_Modules/MFInputShifter.cpp +++ b/src/MF_InputShifter/MFInputShifter.cpp @@ -2,7 +2,6 @@ // // Copyright (C) 2021 #include "MFInputShifter.h" -#include "mobiflight.h" inputShifterEvent MFInputShifter::_inputHandler = NULL; diff --git a/include/MFInputShifter.h b/src/MF_InputShifter/MFInputShifter.h similarity index 90% rename from include/MFInputShifter.h rename to src/MF_InputShifter/MFInputShifter.h index a2da6678..d2ce6fe0 100644 --- a/include/MFInputShifter.h +++ b/src/MF_InputShifter/MFInputShifter.h @@ -1,14 +1,6 @@ -// MFInputShifter.h +#pragma once -#ifndef MFInputShifter_h -#define MFInputShifter_h - -#if ARDUINO >= 100 #include -#else -#include -#include -#endif // Maximum number of shifters allowed on an individual chain. While this is currently set to 4 // there is no technical limit in the code for how many can be chained. It is constrained only @@ -53,4 +45,3 @@ class MFInputShifter void clearLastState(); static inputShifterEvent _inputHandler; }; -#endif diff --git a/src/MF_LCDDIsplay/LCDDIsplay.h b/src/MF_LCDDIsplay/LCDDIsplay.h new file mode 100644 index 00000000..8aa27e1f --- /dev/null +++ b/src/MF_LCDDIsplay/LCDDIsplay.h @@ -0,0 +1,8 @@ +#pragma once + +namespace LCDDisplay +{ +void Add(uint8_t address, uint8_t cols, uint8_t lines); +void Clear(); +void OnSet(); +} diff --git a/src/MF_LCDDIsplay/LCDDisplay.cpp b/src/MF_LCDDIsplay/LCDDisplay.cpp new file mode 100644 index 00000000..af786022 --- /dev/null +++ b/src/MF_LCDDIsplay/LCDDisplay.cpp @@ -0,0 +1,51 @@ +#include +#include "MFLCDDisplay.h" +#include "allocateMem.h" +#include "commandmessenger.h" +#include "LCDDisplay.h" +#include "MFBoards.h" +namespace LCDDisplay +{ +MFLCDDisplay *lcd_I2C[MAX_MFLCD_I2C]; +uint8_t lcd_12cRegistered = 0; + + +void Add(uint8_t address = 0x24, uint8_t cols = 16, uint8_t lines = 2) +{ + if (lcd_12cRegistered == MAX_MFLCD_I2C) + return; + + if (!FitInMemory(sizeof(MFLCDDisplay))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("LCD does not fit in Memory!")); + return; + } + lcd_I2C[lcd_12cRegistered] = new (allocateMemory(sizeof(MFLCDDisplay))) MFLCDDisplay; + lcd_I2C[lcd_12cRegistered]->attach(address, cols, lines); + lcd_12cRegistered++; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added lcdDisplay")); +#endif +} + +void Clear() +{ + for (int i = 0; i != lcd_12cRegistered; i++) + { + lcd_I2C[lcd_12cRegistered]->detach(); + } + lcd_12cRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared lcdDisplays")); +#endif +} + +void OnSet() +{ + int address = cmdMessenger.readInt16Arg(); + char *output = cmdMessenger.readStringArg(); + lcd_I2C[address]->display(output); + setLastCommandMillis(); +} +} // end of namespace LCDDisplay diff --git a/src/MF_Modules/MFLCDDisplay.cpp b/src/MF_LCDDIsplay/MFLCDDisplay.cpp similarity index 59% rename from src/MF_Modules/MFLCDDisplay.cpp rename to src/MF_LCDDIsplay/MFLCDDisplay.cpp index baad1529..c72f3ef5 100644 --- a/src/MF_Modules/MFLCDDisplay.cpp +++ b/src/MF_LCDDIsplay/MFLCDDisplay.cpp @@ -3,8 +3,6 @@ // Copyright (C) 2013-2014 #include "MFLCDDisplay.h" -#include "allocateMem.h" -#include "mobiflight.h" MFLCDDisplay::MFLCDDisplay() { @@ -17,8 +15,8 @@ void MFLCDDisplay::display(const char *string) return; for (uint8_t line = 0; line != _lines; line++) { - _lcdDisplay->setCursor(0, line); - _lcdDisplay->writeString(&string[line*_cols], _cols); + _lcdDisplay.setCursor(0, line); + _lcdDisplay.writeString(&string[line*_cols], _cols); } } @@ -27,18 +25,9 @@ void MFLCDDisplay::attach(byte address, byte cols, byte lines) _address = address; _cols = cols; _lines = lines; - if (!FitInMemory(sizeof(LiquidCrystal_I2C))) - { - // Error Message to Connector - cmdMessenger.sendCmdStart(kDebug); - cmdMessenger.sendCmdArg(F("LCD does not fit in Memory!")); - cmdMessenger.sendCmdEnd(); - return; - } - _lcdDisplay = new (allocateMemory(sizeof(LiquidCrystal_I2C))) LiquidCrystal_I2C; _initialized = true; - _lcdDisplay->init((uint8_t)address, (uint8_t)cols, (uint8_t)lines); - _lcdDisplay->backlight(); + _lcdDisplay.init((uint8_t)address, (uint8_t)cols, (uint8_t)lines); + _lcdDisplay.backlight(); Wire.setClock(400000); test(); } @@ -53,9 +42,9 @@ void MFLCDDisplay::detach() void MFLCDDisplay::powerSavingMode(bool state) { if (state) - _lcdDisplay->noBacklight(); + _lcdDisplay.noBacklight(); else - _lcdDisplay->backlight(); + _lcdDisplay.backlight(); } void MFLCDDisplay::test() @@ -63,7 +52,7 @@ void MFLCDDisplay::test() if (!_initialized) return; uint8_t preLines = 0; - _lcdDisplay->clear(); + _lcdDisplay.clear(); if (_lines > 2) { @@ -76,19 +65,19 @@ void MFLCDDisplay::test() _printCentered("Happy Holidays!!", preLines++); } - _lcdDisplay->setCursor(0, 0); + _lcdDisplay.setCursor(0, 0); } void MFLCDDisplay::_printCentered(const char *str, uint8_t line) { - _lcdDisplay->setCursor(0, line); + _lcdDisplay.setCursor(0, line); for (byte c = 0; c != ((_cols - strlen(str)) / 2); c++) { - _lcdDisplay->print(F(" ")); + _lcdDisplay.print(F(" ")); } - _lcdDisplay->print(str); + _lcdDisplay.print(str); for (byte c = 0; c != ((_cols - strlen(str)) / 2); c++) { - _lcdDisplay->print(F(" ")); + _lcdDisplay.print(F(" ")); } -} \ No newline at end of file +} diff --git a/include/MFLCDDisplay.h b/src/MF_LCDDIsplay/MFLCDDisplay.h similarity index 83% rename from include/MFLCDDisplay.h rename to src/MF_LCDDIsplay/MFLCDDisplay.h index 4dadb463..5eab13c2 100644 --- a/include/MFLCDDisplay.h +++ b/src/MF_LCDDIsplay/MFLCDDisplay.h @@ -6,16 +6,9 @@ /// \author Sebastian Moebius (info@mobiflight.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2013-2014 Sebastian Moebius -#ifndef MFLCDDisplay_h -#define MFLCDDisplay_h +#pragma once -#if ARDUINO >= 100 #include -#else -#include -#include -#endif - #include ///////////////////////////////////////////////////////////////////// @@ -31,7 +24,7 @@ class MFLCDDisplay void powerSavingMode(bool state); private: - LiquidCrystal_I2C *_lcdDisplay; + LiquidCrystal_I2C _lcdDisplay; bool _initialized; byte _address; byte _cols; @@ -39,4 +32,3 @@ class MFLCDDisplay void _printCentered(const char *str, uint8_t line); }; -#endif \ No newline at end of file diff --git a/src/MF_Modules/MFOutput.cpp b/src/MF_Output/MFOutput.cpp similarity index 87% rename from src/MF_Modules/MFOutput.cpp rename to src/MF_Output/MFOutput.cpp index 6d507b10..ee68f951 100644 --- a/src/MF_Modules/MFOutput.cpp +++ b/src/MF_Output/MFOutput.cpp @@ -1,7 +1,3 @@ -// MFOutput.cpp -// -// Copyright (C) 2013-2014 - #include "MFOutput.h" MFOutput::MFOutput(uint8_t pin) diff --git a/include/MFOutput.h b/src/MF_Output/MFOutput.h similarity index 81% rename from include/MFOutput.h rename to src/MF_Output/MFOutput.h index 342bdfaa..fe8911f6 100644 --- a/include/MFOutput.h +++ b/src/MF_Output/MFOutput.h @@ -6,16 +6,9 @@ /// \author Sebastian Moebius (mobiflight@moebiuz.de) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2013-2014 Sebastian Moebius -#ifndef MFOutput_h -#define MFOutput_h +#pragma once -#include -#if ARDUINO >= 100 #include -#else -#include -#include -#endif ///////////////////////////////////////////////////////////////////// /// \class MFOutput MFOutput.h @@ -30,4 +23,3 @@ class MFOutput uint8_t _pin; uint8_t _value; }; -#endif \ No newline at end of file diff --git a/src/MF_Output/Output.cpp b/src/MF_Output/Output.cpp new file mode 100644 index 00000000..4f80def2 --- /dev/null +++ b/src/MF_Output/Output.cpp @@ -0,0 +1,58 @@ +#include +#include "MFOutput.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "Output.h" +#include "MFBoards.h" + +namespace Output +{ +MFOutput *outputs[MAX_OUTPUTS]; +uint8_t outputsRegistered = 0; + +void Add(uint8_t pin = 1) +{ + if (outputsRegistered == MAX_OUTPUTS) + return; + + if (!FitInMemory(sizeof(MFOutput))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("Output does not fit in Memory")); + return; + } + outputs[outputsRegistered] = new (allocateMemory(sizeof(MFOutput))) MFOutput(pin); + outputsRegistered++; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added output")); +#endif +} + +void Clear() +{ + outputsRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared outputs")); +#endif +} + +void OnSet() +{ + // Read led state argument, interpret string as boolean + int pin = cmdMessenger.readInt16Arg(); + int state = cmdMessenger.readInt16Arg(); + // Set led + analogWrite(pin, state); // why does the UI sends the pin number and not the x.th output number like other devices? +// output[pin]->set(state); // once this is changed uncomment this + setLastCommandMillis(); +} + +void PowerSave(bool state) +{ + for (int i = 0; i != outputsRegistered; ++i) + { + outputs[i]->powerSavingMode(state); + } +} +} // end of namespace Output diff --git a/src/MF_Output/Output.h b/src/MF_Output/Output.h new file mode 100644 index 00000000..5058bc6e --- /dev/null +++ b/src/MF_Output/Output.h @@ -0,0 +1,9 @@ +#pragma once + +namespace Output +{ +void Add(uint8_t pin); +void Clear(); +void OnSet(); +void PowerSave(bool state); +} diff --git a/src/MF_Modules/MFOutputShifter.cpp b/src/MF_OutputShifter/MFOutputShifter.cpp similarity index 100% rename from src/MF_Modules/MFOutputShifter.cpp rename to src/MF_OutputShifter/MFOutputShifter.cpp diff --git a/include/MFOutputShifter.h b/src/MF_OutputShifter/MFOutputShifter.h similarity index 91% rename from include/MFOutputShifter.h rename to src/MF_OutputShifter/MFOutputShifter.h index 55e95b4b..6d88b9e5 100644 --- a/include/MFOutputShifter.h +++ b/src/MF_OutputShifter/MFOutputShifter.h @@ -7,15 +7,9 @@ // Copyright (C) 2021 Manfred Berry // Modified 2022 Giorgio Croci Candiani -#ifndef MFOutputShifter_h -#define MFOutputShifter_h +#pragma once -#if ARDUINO >= 100 #include -#else -#include -#include -#endif // Maximum number of shifters allowed on an individual chain. While this is currently set to 4 // there is no technical limit in the code for how many can be chained. It is constrained only @@ -45,4 +39,3 @@ class MFOutputShifter uint8_t _outputBuffer[MAX_CHAINED_OUTPUT_SHIFTERS]; bool _initialized = false; }; -#endif diff --git a/src/MF_OutputShifter/OutputShifter.cpp b/src/MF_OutputShifter/OutputShifter.cpp new file mode 100644 index 00000000..f0ab37ee --- /dev/null +++ b/src/MF_OutputShifter/OutputShifter.cpp @@ -0,0 +1,63 @@ +#include +#include "MFOutputShifter.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "OutputShifter.h" +#include "MFBoards.h" + +namespace OutputShifter +{ +MFOutputShifter *outputShifters[MAX_OUTPUT_SHIFTERS]; +uint8_t outputShifterRegistered = 0; + +void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules) +{ + if (outputShifterRegistered == MAX_OUTPUT_SHIFTERS) + return; + if (!FitInMemory(sizeof(MFOutputShifter))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("OutputShifter does not fit in Memory")); + return; + } + outputShifters[outputShifterRegistered] = new (allocateMemory(sizeof(MFOutputShifter))) MFOutputShifter; + outputShifters[outputShifterRegistered]->attach(latchPin, clockPin, dataPin, modules); + outputShifters[outputShifterRegistered]->clear(); + outputShifterRegistered++; + +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added Output Shifter")); +#endif +} + +void Clear() +{ + for (int i = 0; i != outputShifterRegistered; i++) + { + outputShifters[i]->detach(); + } + + outputShifterRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared Output Shifter")); +#endif +} + +void OnInit() // not used anywhere!? +{ + int module = cmdMessenger.readInt16Arg(); + outputShifters[module]->clear(); + setLastCommandMillis(); +} + +void OnSet() +{ + + int module = cmdMessenger.readInt16Arg(); + char *pins = cmdMessenger.readStringArg(); + int value = cmdMessenger.readInt16Arg(); + outputShifters[module]->setPins(pins, value); + setLastCommandMillis(); +} +} // end of namespace OutputShifter diff --git a/src/MF_OutputShifter/OutputShifter.h b/src/MF_OutputShifter/OutputShifter.h new file mode 100644 index 00000000..e018bb18 --- /dev/null +++ b/src/MF_OutputShifter/OutputShifter.h @@ -0,0 +1,9 @@ +#pragma once + +namespace OutputShifter +{ +void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules); +void Clear(); +//void OnInit(); // this is defined but not used!? +void OnSet(); +} diff --git a/src/MF_Segment/LedSegment.cpp b/src/MF_Segment/LedSegment.cpp new file mode 100644 index 00000000..5908ae9c --- /dev/null +++ b/src/MF_Segment/LedSegment.cpp @@ -0,0 +1,80 @@ +#include +#include "MFSegments.h" +#include "allocateMem.h" +#include "commandmessenger.h" +#include "LedSegment.h" +#include "MFBoards.h" + +namespace LedSegment +{ +MFSegments *ledSegments[MAX_LEDSEGMENTS]; +uint8_t ledSegmentsRegistered = 0; + +void Add(int dataPin, int csPin, int clkPin, int numDevices, int brightness) +{ + if (ledSegmentsRegistered == MAX_LEDSEGMENTS) + return; + + if (!FitInMemory(sizeof(MFSegments))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("7Segment does not fit in Memory!")); + return; + } + ledSegments[ledSegmentsRegistered] = new (allocateMemory(sizeof(MFSegments))) MFSegments; + ledSegments[ledSegmentsRegistered]->attach(dataPin, csPin, clkPin, numDevices, brightness); // lc is our object + ledSegmentsRegistered++; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added Led Segment")); +#endif +} + +void Clear() +{ + for (int i = 0; i != ledSegmentsRegistered; i++) + { + ledSegments[ledSegmentsRegistered]->detach(); + } + ledSegmentsRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared segments")); +#endif +} + +void PowerSave(bool state) +{ + for (int i = 0; i != ledSegmentsRegistered; ++i) + { + ledSegments[i]->powerSavingMode(state); + } +} + +void OnInitModule() +{ + int module = cmdMessenger.readInt16Arg(); + int subModule = cmdMessenger.readInt16Arg(); + int brightness = cmdMessenger.readInt16Arg(); + ledSegments[module]->setBrightness(subModule, brightness); + setLastCommandMillis(); +} + +void OnSetModule() +{ + int module = cmdMessenger.readInt16Arg(); + int subModule = cmdMessenger.readInt16Arg(); + char *value = cmdMessenger.readStringArg(); + uint8_t points = (uint8_t)cmdMessenger.readInt16Arg(); + uint8_t mask = (uint8_t)cmdMessenger.readInt16Arg(); + ledSegments[module]->display(subModule, value, points, mask); + setLastCommandMillis(); +} + +void OnSetModuleBrightness() +{ + int module = cmdMessenger.readInt16Arg(); + int subModule = cmdMessenger.readInt16Arg(); + int brightness = cmdMessenger.readInt16Arg(); + ledSegments[module]->setBrightness(subModule, brightness); + setLastCommandMillis(); +} +} // end of namespace LedSegment diff --git a/src/MF_Segment/LedSegment.h b/src/MF_Segment/LedSegment.h new file mode 100644 index 00000000..d31cd6aa --- /dev/null +++ b/src/MF_Segment/LedSegment.h @@ -0,0 +1,11 @@ +#pragma once + +namespace LedSegment +{ +void Add(int dataPin, int csPin, int clkPin, int numDevices, int brightness); +void Clear(); +void PowerSave(bool state); +void OnInitModule(); +void OnSetModule(); +void OnSetModuleBrightness(); +} diff --git a/src/MF_Modules/MFSegments.cpp b/src/MF_Segment/MFSegments.cpp similarity index 62% rename from src/MF_Modules/MFSegments.cpp rename to src/MF_Segment/MFSegments.cpp index 1718f28a..5c45166c 100644 --- a/src/MF_Modules/MFSegments.cpp +++ b/src/MF_Segment/MFSegments.cpp @@ -3,8 +3,6 @@ // Copyright (C) 2013-2021 #include "MFSegments.h" -#include "allocateMem.h" -#include "mobiflight.h" MFSegments::MFSegments() { @@ -22,7 +20,7 @@ void MFSegments::display(byte module, char *string, byte points, byte mask, bool digit--; if (((1 << digit) & mask) == 0) continue; - _ledControl->setChar(module, digit, string[pos], ((1 << digit) & points)); + _ledControl.setChar(module, digit, string[pos], ((1 << digit) & points)); pos++; } } @@ -35,32 +33,23 @@ void MFSegments::setBrightness(byte module, byte value) { if(value) { - _ledControl->setIntensity(module, value-1); - _ledControl->shutdown(module, false); + _ledControl.setIntensity(module, value-1); + _ledControl.shutdown(module, false); } else { - _ledControl->shutdown(module, true); + _ledControl.shutdown(module, true); } } } void MFSegments::attach(int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness) { - if (!FitInMemory(sizeof(LedControl))) - { - // Error Message to Connector - cmdMessenger.sendCmdStart(kDebug); - cmdMessenger.sendCmdArg(F("7Segment does not fit in Memory")); - cmdMessenger.sendCmdEnd(); - return; - } - _ledControl = new (allocateMemory(sizeof(LedControl))) LedControl; - _ledControl->begin(dataPin, clkPin, csPin, moduleCount); + _ledControl.begin(dataPin, clkPin, csPin, moduleCount); _moduleCount = moduleCount; for (int i = 0; i != _moduleCount; ++i) { setBrightness(i, brightness); - _ledControl->shutdown(i, false); - _ledControl->clearDisplay(i); + _ledControl.shutdown(i, false); + _ledControl.clearDisplay(i); } } @@ -73,7 +62,7 @@ void MFSegments::powerSavingMode(bool state) { for (byte i = 0; i != _moduleCount; ++i) { - _ledControl->shutdown(i, state); + _ledControl.shutdown(i, state); } } @@ -89,7 +78,7 @@ void MFSegments::test() { for (module = 0; module != _moduleCount; ++module) { - _ledControl->setDigit(module, digit, 8, 1); + _ledControl.setDigit(module, digit, 8, 1); } delay(_delay); } @@ -98,7 +87,7 @@ void MFSegments::test() { for (module = 0; module != _moduleCount; ++module) { - _ledControl->setChar(module, 7 - digit, '-', false); + _ledControl.setChar(module, 7 - digit, '-', false); } delay(_delay); } @@ -107,7 +96,7 @@ void MFSegments::test() { for (module = 0; module != _moduleCount; ++module) { - _ledControl->setChar(module, 7 - digit, ' ', false); + _ledControl.setChar(module, 7 - digit, ' ', false); } delay(_delay); } diff --git a/include/MFSegments.h b/src/MF_Segment/MFSegments.h similarity index 84% rename from include/MFSegments.h rename to src/MF_Segment/MFSegments.h index 525f5e2b..67289090 100644 --- a/include/MFSegments.h +++ b/src/MF_Segment/MFSegments.h @@ -6,16 +6,9 @@ /// \author Sebastian Moebius (mobiflight@moebiuz.de) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2013-2014 Sebastian Moebius -#ifndef MFSegments_h -#define MFSegments_h +#pragma once -#if ARDUINO >= 100 #include -#else -#include -#include -#endif - #include ///////////////////////////////////////////////////////////////////// @@ -32,7 +25,6 @@ class MFSegments void setBrightness(byte module, byte value); private: - LedControl *_ledControl; + LedControl _ledControl; byte _moduleCount; }; -#endif \ No newline at end of file diff --git a/src/MF_Modules/MFServo.cpp b/src/MF_Servo/MFServo.cpp similarity index 74% rename from src/MF_Modules/MFServo.cpp rename to src/MF_Servo/MFServo.cpp index e08b507b..0bd3333f 100644 --- a/src/MF_Modules/MFServo.cpp +++ b/src/MF_Servo/MFServo.cpp @@ -3,8 +3,6 @@ // Copyright (C) 2013-2014 #include "MFServo.h" -#include "allocateMem.h" -#include "mobiflight.h" void MFServo::moveTo(int absolute) { @@ -13,7 +11,7 @@ void MFServo::moveTo(int absolute) { _targetPos = newValue; if (!_initialized) { - _servo->attach(_pin); + _servo.attach(_pin); _initialized = true; } } @@ -30,27 +28,18 @@ void MFServo::update() { if (_currentPos > _targetPos) _currentPos--; else _currentPos++; - _servo->write(_currentPos); + _servo.write(_currentPos); } void MFServo::detach() { if (_initialized) { - _servo->detach(); + _servo.detach(); _initialized = false; } } void MFServo::attach(uint8_t pin, bool enable) { - if (!FitInMemory(sizeof(Servo))) - { - // Error Message to Connector - cmdMessenger.sendCmdStart(kDebug); - cmdMessenger.sendCmdArg(F("Servo does not fit in Memory")); - cmdMessenger.sendCmdEnd(); - return; - } - _servo = new (allocateMemory(sizeof(Servo))) Servo; _initialized = false; _targetPos = 0; _currentPos = 0; @@ -76,4 +65,4 @@ void MFServo::setInternalRange(int min, int max) { _mapRange[2] = min; _mapRange[3] = max; -} +} \ No newline at end of file diff --git a/include/MFServo.h b/src/MF_Servo/MFServo.h similarity index 86% rename from include/MFServo.h rename to src/MF_Servo/MFServo.h index cdd18044..6bbaa00e 100644 --- a/include/MFServo.h +++ b/src/MF_Servo/MFServo.h @@ -6,17 +6,10 @@ /// \author Sebastian Moebius (mobiflight@moebiuz.de) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2013-2014 Sebastian Moebius -#ifndef MFServo_h -#define MFServo_h +#pragma once -#include -#include - -#if ARDUINO >= 100 #include -#else -#include -#endif +#include ///////////////////////////////////////////////////////////////////// /// \class MFServo MFServo.h @@ -36,9 +29,8 @@ class MFServo uint8_t _pin; int _mapRange[4]; bool _initialized; - Servo *_servo; + Servo _servo; long _targetPos; long _currentPos; int speed; }; -#endif diff --git a/src/MF_Servo/Servos.cpp b/src/MF_Servo/Servos.cpp new file mode 100644 index 00000000..f9066c1e --- /dev/null +++ b/src/MF_Servo/Servos.cpp @@ -0,0 +1,62 @@ +#include +#include "MFServo.h" +#include "allocateMem.h" +#include "commandmessenger.h" +#include "Servos.h" +#include "MFBoards.h" + +namespace Servos +{ +MFServo *servos[MAX_MFSERVOS]; +uint8_t servosRegistered = 0; + + +void Add(int pin) +{ + if (servosRegistered == MAX_MFSERVOS) + return; + + if (!FitInMemory(sizeof(MFServo))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("Servo does not fit in Memory!")); + return; + } + servos[servosRegistered] = new (allocateMemory(sizeof(MFServo))) MFServo; + servos[servosRegistered]->attach(pin, true); + servosRegistered++; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added servos")); +#endif +} + +void Clear() +{ + for (int i = 0; i != servosRegistered; i++) + { + servos[servosRegistered]->detach(); + } + servosRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared servos")); +#endif +} + +void OnSet() +{ + int servo = cmdMessenger.readInt16Arg(); + int newValue = cmdMessenger.readInt16Arg(); + if (servo >= servosRegistered) + return; + servos[servo]->moveTo(newValue); + setLastCommandMillis(); +} + +void update() +{ + for (int i = 0; i != servosRegistered; i++) + { + servos[i]->update(); + } +} +} // end of namespace Servo diff --git a/src/MF_Servo/Servos.h b/src/MF_Servo/Servos.h new file mode 100644 index 00000000..03271a50 --- /dev/null +++ b/src/MF_Servo/Servos.h @@ -0,0 +1,9 @@ +#pragma once + +namespace Servos +{ +void Add(int pin); +void Clear(); +void OnSet(); +void update(); +} diff --git a/src/MF_Modules/MFStepper.cpp b/src/MF_Stepper/MFStepper.cpp similarity index 96% rename from src/MF_Modules/MFStepper.cpp rename to src/MF_Stepper/MFStepper.cpp index 980d3342..c74dacb9 100644 --- a/src/MF_Modules/MFStepper.cpp +++ b/src/MF_Stepper/MFStepper.cpp @@ -2,11 +2,10 @@ // // Copyright (C) 2013-2014 -#include "MFBoards.h" #include "MFStepper.h" -#include "MFButton.h" #include "allocateMem.h" #include "mobiflight.h" +#include "commandmessenger.h" MFStepper::MFStepper() { @@ -18,7 +17,7 @@ void MFStepper::attach(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, u if (!FitInMemory(sizeof(AccelStepper))) { // Error Message to Connector - cmdMessenger.sendCmdStart(kDebug); + cmdMessenger.sendCmdStart(kStatus); cmdMessenger.sendCmdArg(F("Stepper does not fit in Memory")); cmdMessenger.sendCmdEnd(); return; diff --git a/include/MFStepper.h b/src/MF_Stepper/MFStepper.h similarity index 86% rename from include/MFStepper.h rename to src/MF_Stepper/MFStepper.h index 9ed09aed..dbf7c511 100644 --- a/include/MFStepper.h +++ b/src/MF_Stepper/MFStepper.h @@ -6,19 +6,10 @@ /// \author Sebastian Moebius (mobiflight@moebiuz.de) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2013-2014 Sebastian Moebius -#ifndef MFStepper_h -#define MFStepper_h +#pragma once -#include -#include -#include "MFButton.h" - -#if ARDUINO >= 100 #include -#else -#include -#include -#endif +#include ///////////////////////////////////////////////////////////////////// /// \class MFStepper MFStepper.h @@ -48,4 +39,3 @@ class MFStepper void setZeroInReset(void); }; -#endif \ No newline at end of file diff --git a/src/MF_Stepper/Stepper.cpp b/src/MF_Stepper/Stepper.cpp new file mode 100644 index 00000000..42e24ffe --- /dev/null +++ b/src/MF_Stepper/Stepper.cpp @@ -0,0 +1,94 @@ +#include +#include "MFStepper.h" +#include "allocateMem.h" +#include "mobiflight.h" +#include "commandmessenger.h" +#include "Stepper.h" +#include "MFBoards.h" + +namespace Stepper +{ +MFStepper *steppers[MAX_STEPPERS]; +uint8_t steppersRegistered = 0; + +void Add(int pin1, int pin2, int pin3, int pin4, int btnPin1) +{ + if (steppersRegistered == MAX_STEPPERS) + return; + + if (!FitInMemory(sizeof(MFStepper))) + { + // Error Message to Connector + cmdMessenger.sendCmd(kStatus, F("Stepper does not fit in Memory!")); + return; + } + steppers[steppersRegistered] = new (allocateMemory(sizeof(MFStepper))) MFStepper; + steppers[steppersRegistered]->attach(pin1, pin2, pin3, pin4, btnPin1); + steppers[steppersRegistered]->setMaxSpeed(STEPPER_SPEED); + steppers[steppersRegistered]->setAcceleration(STEPPER_ACCEL); + // autoreset is not released yet + if (btnPin1 > 0) + { + // this triggers the auto reset if we need to reset + steppers[steppersRegistered]->reset(); + } + + // all set + steppersRegistered++; + +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Added stepper")); +#endif +} + +void Clear() +{ + for (int i = 0; i != steppersRegistered; i++) + { + steppers[i]->detach(); + } + steppersRegistered = 0; +#ifdef DEBUG2CMDMESSENGER + cmdMessenger.sendCmd(kStatus, F("Cleared steppers")); +#endif +} + +void OnSet() +{ + int stepper = cmdMessenger.readInt16Arg(); + long newPos = cmdMessenger.readInt32Arg(); + + if (stepper >= steppersRegistered) + return; + steppers[stepper]->moveTo(newPos); + setLastCommandMillis(); +} + +void OnReset() +{ + int stepper = cmdMessenger.readInt16Arg(); + + if (stepper >= steppersRegistered) + return; + steppers[stepper]->reset(); + setLastCommandMillis(); +} + +void OnSetZero() +{ + int stepper = cmdMessenger.readInt16Arg(); + + if (stepper >= steppersRegistered) + return; + steppers[stepper]->setZero(); + setLastCommandMillis(); +} + +void update() +{ + for (int i = 0; i != steppersRegistered; i++) + { + steppers[i]->update(); + } +} +} // end of namespace Stepper diff --git a/src/MF_Stepper/Stepper.h b/src/MF_Stepper/Stepper.h new file mode 100644 index 00000000..ddc2dd8b --- /dev/null +++ b/src/MF_Stepper/Stepper.h @@ -0,0 +1,11 @@ +#pragma once + +namespace Stepper +{ +void Add(int pin1, int pin2, int pin3, int pin4, int btnPin1); +void Clear(); +void OnSet(); +void OnReset(); +void OnSetZero(); +void update(); +} diff --git a/src/allocateMem.cpp b/src/allocateMem.cpp index c284215b..3e7bfdbb 100644 --- a/src/allocateMem.cpp +++ b/src/allocateMem.cpp @@ -2,6 +2,7 @@ #include "MFBoards.h" #include "mobiflight.h" #include "allocateMem.h" +#include "CommandMessenger.h" char deviceBuffer[MF_MAX_DEVICEMEM] = {0}; uint16_t nextPointer = 0; @@ -12,14 +13,10 @@ char * allocateMemory(uint8_t size) nextPointer = actualPointer + size; if (nextPointer >= MF_MAX_DEVICEMEM) { -#ifdef DEBUG - cmdMessenger.sendCmdStart(kStatus); - cmdMessenger.sendCmdArg(F("BufferOverflow!")); - cmdMessenger.sendCmdEnd(); -#endif + cmdMessenger.sendCmd(kStatus,F("DeviceBuffer Overflow!")); return nullptr; } -#ifdef DEBUG +#ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmdStart(kStatus); cmdMessenger.sendCmdArg(F("BufferUsage")); cmdMessenger.sendCmdArg(nextPointer); diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 12ac2f00..90dbe0a8 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -1,1274 +1,182 @@ -/** - * Includes Core Arduino functionality - **/ -char foo; -#if ARDUINO < 100 -#include -#else #include -#endif - #include "mobiflight.h" -#include -#include "allocateMem.h" - -// The build version comes from an environment variable -#define STRINGIZER(arg) #arg -#define STR_VALUE(arg) STRINGIZER(arg) -#define VERSION STR_VALUE(BUILD_VERSION) - -//#define DEBUG 1 - -#include "MFEEPROM.h" - -#if MF_SEGMENT_SUPPORT == 1 -#include +#include "commandmessenger.h" +#include "MFBoards.h" +#include "config.h" +#include "Button.h" +#include "Encoder.h" +#if MF_ANALOG_SUPPORT == 1 +#include "Analog.h" #endif - -#include - #if MF_INPUT_SHIFTER_SUPPORT == 1 -#include +#include "InputShifter.h" +#endif +#include "Output.h" +#if MF_SEGMENT_SUPPORT == 1 +#include "LedSegment.h" #endif - #if MF_STEPPER_SUPPORT == 1 -#include -#include +#include "Stepper.h" #endif - #if MF_SERVO_SUPPORT == 1 -#include -#include -#endif - -#include - -#if MF_LCD_SUPPORT == 1 -#include -#include +#include "Servos.h" #endif - -#if MF_ANALOG_SUPPORT == 1 -#include -#endif - #if MF_OUTPUT_SHIFTER_SUPPORT == 1 -#include +#include "OutputShifter.h" #endif -const uint8_t MEM_OFFSET_NAME = 0; -const uint8_t MEM_LEN_NAME = 48; -const uint8_t MEM_OFFSET_SERIAL = MEM_OFFSET_NAME + MEM_LEN_NAME; -const uint8_t MEM_LEN_SERIAL = 11; -const uint8_t MEM_OFFSET_CONFIG = MEM_OFFSET_NAME + MEM_LEN_NAME + MEM_LEN_SERIAL; +#define MF_BUTTON_DEBOUNCE_MS 10 // time between updating the buttons +#define MF_SERVO_DELAY_MS 5 // time between servo updates +#define MF_ANALOGAVERAGE_DELAY_MS 10 // time between updating the analog average calculation +#define MF_ANALOGREAD_DELAY_MS 50 // time between sending analog values +#define MF_ENCODER_DEBOUNCE_MS 1 // time between updating encoders -uint32_t lastButtonUpdate = 0; +bool powerSavingMode = false; +const unsigned long POWER_SAVING_TIME = 60 * 15; // in seconds +uint32_t lastButtonUpdate= 0; uint32_t lastEncoderUpdate = 0; - +#if MF_SERVO_SUPPORT == 1 +uint32_t lastServoUpdate = 0; +#endif #if MF_ANALOG_SUPPORT == 1 uint32_t lastAnalogAverage = 0; uint32_t lastAnalogRead = 0; #endif - -#if MF_SERVO_SUPPORT == 1 -uint32_t lastServoUpdate = 0; -#endif - #if MF_INPUT_SHIFTER_SUPPORT == 1 uint32_t lastInputShifterUpdate = 0; #endif -char serial[MEM_LEN_SERIAL] = MOBIFLIGHT_SERIAL; -char name[MEM_LEN_NAME] = MOBIFLIGHT_NAME; -const int MEM_LEN_CONFIG = MEMLEN_CONFIG; - -char nameBuffer[MEMLEN_CONFIG_BUFFER] = ""; - -uint16_t configLength = 0; -boolean configActivated = false; - -bool powerSavingMode = false; -const unsigned long POWER_SAVING_TIME = 60 * 15; // in seconds - -CmdMessenger cmdMessenger = CmdMessenger(Serial); -unsigned long lastCommand; - -MFEEPROM MFeeprom; - -MFOutput *outputs[MAX_OUTPUTS]; -uint8_t outputsRegistered = 0; - -MFButton *buttons[MAX_BUTTONS]; -uint8_t buttonsRegistered = 0; - -#if MF_SEGMENT_SUPPORT == 1 -MFSegments ledSegments[MAX_LEDSEGMENTS]; -uint8_t ledSegmentsRegistered = 0; -#endif - -MFEncoder *encoders[MAX_ENCODERS]; -uint8_t encodersRegistered = 0; - -#if MF_STEPPER_SUPPORT == 1 -MFStepper steppers[MAX_STEPPERS]; // -uint8_t steppersRegistered = 0; -#endif - -#if MF_SERVO_SUPPORT == 1 -MFServo servos[MAX_MFSERVOS]; -uint8_t servosRegistered = 0; -#endif - -#if MF_LCD_SUPPORT == 1 -MFLCDDisplay lcd_I2C[MAX_MFLCD_I2C]; -uint8_t lcd_12cRegistered = 0; -#endif - -#if MF_ANALOG_SUPPORT == 1 -MFAnalog *analog[MAX_ANALOG_INPUTS]; -uint8_t analogRegistered = 0; -#endif - -#if MF_OUTPUT_SHIFTER_SUPPORT == 1 -MFOutputShifter *outputShifters[MAX_OUTPUT_SHIFTERS]; -uint8_t outputShifterRegistered = 0; -#endif - -#if MF_INPUT_SHIFTER_SUPPORT == 1 -MFInputShifter *inputShifters[MAX_INPUT_SHIFTERS]; -uint8_t inputShiftersRegistered = 0; -#endif -// Callbacks define on which received commands we take action -void attachCommandCallbacks() +// ************************************************************ +// Power saving +// ************************************************************ +void SetPowerSavingMode(bool state) { - // Attach callback methods - cmdMessenger.attach(OnUnknownCommand); - + // disable the lights ;) + powerSavingMode = state; +// Output::PowerSave(state); // why is this uncommeted in the main branch?? #if MF_SEGMENT_SUPPORT == 1 - cmdMessenger.attach(kInitModule, OnInitModule); - cmdMessenger.attach(kSetModule, OnSetModule); - cmdMessenger.attach(kSetModuleBrightness, OnSetModuleBrightness); -#endif - - cmdMessenger.attach(kSetPin, OnSetPin); - -#if MF_STEPPER_SUPPORT == 1 - cmdMessenger.attach(kSetStepper, OnSetStepper); -#endif - -#if MF_SERVO_SUPPORT == 1 - cmdMessenger.attach(kSetServo, OnSetServo); -#endif - - cmdMessenger.attach(kGetInfo, OnGetInfo); - cmdMessenger.attach(kGetConfig, OnGetConfig); - cmdMessenger.attach(kSetConfig, OnSetConfig); // 3rd step, uploading new config block wise - cmdMessenger.attach(kResetConfig, OnResetConfig); // 2nd step for uploading a new config - cmdMessenger.attach(kSaveConfig, OnSaveConfig); // 4th step, not really required anymore, config is stored directly to EEPROM, changes in UI required as feedback is required for now - cmdMessenger.attach(kActivateConfig, OnActivateConfig); // 5th step, reading config and activate - cmdMessenger.attach(kSetName, OnSetName); // 1st step, write name - cmdMessenger.attach(kGenNewSerial, OnGenNewSerial); - -#if MF_STEPPER_SUPPORT == 1 - cmdMessenger.attach(kResetStepper, OnResetStepper); - cmdMessenger.attach(kSetZeroStepper, OnSetZeroStepper); -#endif - - cmdMessenger.attach(kTrigger, OnTrigger); -// cmdMessenger.attach(kResetBoard, OnResetBoard); // why is this command coming from the UI additional to OnActivateConfig? - -#if MF_LCD_SUPPORT == 1 - cmdMessenger.attach(kSetLcdDisplayI2C, OnSetLcdDisplayI2C); -#endif - -#if MF_OUTPUT_SHIFTER_SUPPORT - cmdMessenger.attach(kSetShiftRegisterPins, OnSetOutputShifterPins); + LedSegment::PowerSave(state); #endif -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Attached callbacks")); +#ifdef DEBUG2CMDMESSENGER + if (state) + cmdMessenger.sendCmd(kStatus, F("On")); + else + cmdMessenger.sendCmd(kStatus, F("Off")); #endif - } -// Callbacks that define what commands we issue upon internal events -void attachEventCallbacks() +void updatePowerSaving() { - MFButton::attachHandler(handlerOnButton); - MFEncoder::attachHandler(handlerOnEncoder); -#if MF_ANALOG_SUPPORT == 1 - MFAnalog::attachHandler(handlerOnAnalogChange); -#endif + if (!powerSavingMode && ((millis() - getLastCommandMillis()) > (POWER_SAVING_TIME * 1000))) + { + // enable power saving + SetPowerSavingMode(true); + } + else if (powerSavingMode && ((millis() - getLastCommandMillis()) < (POWER_SAVING_TIME * 1000))) + { + // disable power saving + SetPowerSavingMode(false); + } } +// ************************************************************ +// Reset Board +// ************************************************************ void ResetBoard() { - MFeeprom.init(); - nameBuffer[0] = '\0'; generateSerial(false); - lastCommand = millis(); + setLastCommandMillis(); loadConfig(); _restoreName(); } -/* -void OnResetBoard() -{ - resetConfig(); // was part of loadConfig(), but not needed on initial start up - ResetBoard(); -} -*/ -// Setup function + +// ************************************************************ +// Setup +// ************************************************************ void setup() { Serial.begin(115200); attachCommandCallbacks(); - attachEventCallbacks(); cmdMessenger.printLfCr(); ResetBoard(); // Time Gap between Inputs, do not read at the same loop - lastButtonUpdate = millis(); - lastEncoderUpdate = millis() + 2; + lastButtonUpdate = millis() + 0; + lastEncoderUpdate = millis(); // encoders will be updated every 1ms + +#if MF_SERVO_SUPPORT == 1 + lastServoUpdate = millis() + 2; +#endif #if MF_ANALOG_SUPPORT == 1 lastAnalogAverage = millis() + 4; lastAnalogRead = millis() + 4; #endif -#if MF_SERVO_SUPPORT == 1 - lastServoUpdate = millis(); -#endif - #if MF_INPUT_SHIFTER_SUPPORT == 1 lastInputShifterUpdate = millis() + 6; #endif -} - -void generateSerial(bool force) -{ - MFeeprom.read_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL); - if (!force && serial[0] == 'S' && serial[1] == 'N') - return; - randomSeed(analogRead(RANDOM_SEED_INPUT)); - sprintf(serial, "SN-%03x-", (unsigned int)random(4095)); - sprintf(&serial[7], "%03x", (unsigned int)random(4095)); - MFeeprom.write_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL); - if (!force) - MFeeprom.write_byte(MEM_OFFSET_CONFIG, 0x00); // First byte of config to 0x00 to ensure to start 1st time with empty config, but not if forced from the connector to generate a new one -} - -// reads the EEPRROM until NULL termination and returns the number of characters incl. NULL termination, starting from given address -bool readConfigLength() -{ - char temp = 0; - uint16_t addreeprom = MEM_OFFSET_CONFIG; - uint16_t length = MFeeprom.get_length(); - configLength = 0; - do - { - temp = MFeeprom.read_char(addreeprom++); - configLength++; - if (addreeprom > length) // abort if EEPROM size will be exceeded - { - cmdMessenger.sendCmd(kStatus, F("Loading config failed")); // text or "-1" like config upload? - return false; - } - } - while (temp != 0x00); // reads until NULL - configLength--; - return true; -} - -void loadConfig() -{ -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Restored config")); - cmdMessenger.sendCmd(kStatus, nameBuffer); -#endif - if (readConfigLength()) - { - readConfig(); - _activateConfig(); - } -} - -void SetPowerSavingMode(bool state) -{ - // disable the lights ;) - powerSavingMode = state; - -#if MF_SEGMENT_SUPPORT == 1 - PowerSaveLedSegment(state); -#endif -#ifdef DEBUG - if (state) - cmdMessenger.sendCmd(kStatus, F("On")); - else - cmdMessenger.sendCmd(kStatus, F("Off")); +#if MF_SERVO_SUPPORT == 1 + lastServoUpdate = millis() + 8; #endif - //PowerSaveOutputs(state); -} - -void updatePowerSaving() -{ - if (!powerSavingMode && ((millis() - lastCommand) > (POWER_SAVING_TIME * 1000))) - { - // enable power saving - SetPowerSavingMode(true); - } - else if (powerSavingMode && ((millis() - lastCommand) < (POWER_SAVING_TIME * 1000))) - { - // disable power saving - SetPowerSavingMode(false); - } } +// ************************************************************ // Loop function +// ************************************************************ void loop() { // Process incoming serial data, and perform callbacks cmdMessenger.feedinSerialData(); updatePowerSaving(); - // if config has been reset - // and still is not activated + // if config has been reset and still is not activated // do not perform updates // to prevent mangling input for config (shared buffers) - if (!configActivated) - return; - - readButtons(); - readEncoder(); - -#if MF_INPUT_SHIFTER_SUPPORT == 1 - readInputShifters(); -#endif - -#if MF_ANALOG_SUPPORT == 1 - readAnalog(); -#endif - - // segments do not need update + if (getStatusConfig()) + { + if (millis() - lastButtonUpdate >= MF_BUTTON_DEBOUNCE_MS) + { + lastButtonUpdate = millis(); + Button::read(); + } + if (millis() - lastEncoderUpdate >= MF_ENCODER_DEBOUNCE_MS) + { + lastEncoderUpdate = millis(); + Encoder::read(); + } #if MF_STEPPER_SUPPORT == 1 - updateSteppers(); + Stepper::update(); #endif - #if MF_SERVO_SUPPORT == 1 - updateServos(); -#endif -} - -//// OUTPUT ///// -void AddOutput(uint8_t pin = 1, char const *name = "Output") -{ - if (outputsRegistered == MAX_OUTPUTS) - return; - if (!FitInMemory(sizeof(MFOutput))) - { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Output does not fit in Memory")); - return; - } - outputs[outputsRegistered] = new (allocateMemory(sizeof(MFOutput))) MFOutput(pin); - outputsRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added output")); -#endif -} - -void ClearOutputs() -{ - outputsRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared outputs")); -#endif -} - -//// BUTTONS ///// -void AddButton(uint8_t pin = 1, char const *name = "Button") -{ - if (buttonsRegistered == MAX_BUTTONS) - return; - if (!FitInMemory(sizeof(MFButton))) - { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Button does not fit in Memory")); - return; - } - buttons[buttonsRegistered] = new (allocateMemory(sizeof(MFButton))) MFButton(pin, name); - buttonsRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added button ") /* + name */); -#endif -} - -void ClearButtons() -{ - buttonsRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared buttons")); -#endif -} - -//// ENCODERS ///// -void AddEncoder(uint8_t pin1 = 1, uint8_t pin2 = 2, uint8_t encoder_type = 0, char const *name = "Encoder") -{ - if (encodersRegistered == MAX_ENCODERS) - return; - - if (!FitInMemory(sizeof(MFEncoder))) - { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Encoders does not fit in Memory")); - return; - } - encoders[encodersRegistered] = new (allocateMemory(sizeof(MFEncoder))) MFEncoder; - encoders[encodersRegistered]->attach(pin1, pin2, encoder_type, name); - encodersRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added encoder")); + if (millis() - lastServoUpdate >= MF_SERVO_DELAY_MS) + { + lastServoUpdate = millis(); + Servos::update(); + } #endif -} - -void ClearEncoders() -{ - encodersRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared encoders")); +#if MF_ANALOG_SUPPORT == 1 + if (millis() - lastAnalogRead >= MF_ANALOGREAD_DELAY_MS) + { + lastAnalogRead = millis(); + Analog::read(); + } + if (millis() - lastAnalogAverage >= MF_ANALOGAVERAGE_DELAY_MS) + { + lastAnalogAverage = millis(); + Analog::readAverage(); + } #endif -} - #if MF_INPUT_SHIFTER_SUPPORT == 1 -//// INPUT SHIFT REGISTER ///// -void AddInputShifter(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name = "Shifter") -{ - if (inputShiftersRegistered == MAX_INPUT_SHIFTERS) - return; - if (!FitInMemory(sizeof(MFInputShifter))) - { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("InputShifter does not fit in Memory")); - return; - } - inputShifters[inputShiftersRegistered] = new (allocateMemory(sizeof(MFInputShifter))) MFInputShifter; - inputShifters[inputShiftersRegistered]->attach(latchPin, clockPin, dataPin, modules, name); - inputShifters[inputShiftersRegistered]->clear(); - inputShiftersRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added input shifter")); -#endif -} - -void ClearInputShifters() -{ - for (int i = 0; i < inputShiftersRegistered; i++) - { - inputShifters[i]->detach(); - } - inputShiftersRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared input shifter")); -#endif -} -#endif - -//// OUTPUTS ///// - -#if MF_SEGMENT_SUPPORT == 1 -//// SEGMENTS ///// -void AddLedSegment(int dataPin, int csPin, int clkPin, int numDevices, int brightness) -{ - if (ledSegmentsRegistered == MAX_LEDSEGMENTS) - return; - - ledSegments[ledSegmentsRegistered].attach(dataPin, csPin, clkPin, numDevices, brightness); // lc is our object - ledSegmentsRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added Led Segment")); -#endif -} - -void ClearLedSegments() -{ - for (int i = 0; i != ledSegmentsRegistered; i++) - { - ledSegments[i].detach(); - } - ledSegmentsRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared segments")); -#endif -} - -void PowerSaveLedSegment(bool state) -{ - for (int i = 0; i != ledSegmentsRegistered; ++i) - { - ledSegments[i].powerSavingMode(state); - } - - for (int i = 0; i != outputsRegistered; ++i) - { - outputs[i]->powerSavingMode(state); - } -} -#endif - -#if MF_STEPPER_SUPPORT == 1 -//// STEPPER //// -void AddStepper(int pin1, int pin2, int pin3, int pin4, int btnPin1) -{ - if (steppersRegistered == MAX_STEPPERS) - return; - steppers[steppersRegistered].attach(pin1, pin2, pin3, pin4, btnPin1); - steppers[steppersRegistered].setMaxSpeed(STEPPER_SPEED); - steppers[steppersRegistered].setAcceleration(STEPPER_ACCEL); - // autoreset is not released yet - if (btnPin1 > 0) - { - // this triggers the auto reset if we need to reset - steppers[steppersRegistered].reset(); - } - - // all set - steppersRegistered++; - -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added stepper")); + if (millis() - lastInputShifterUpdate >= MF_ENCODER_DEBOUNCE_MS) + { + lastInputShifterUpdate = millis(); + InputShifter::read(); + } #endif -} - -void ClearSteppers() -{ - for (int i = 0; i != steppersRegistered; i++) - { - steppers[i].detach(); + // lcds, outputs, outputshifters, segments do not need update } - steppersRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared steppers")); -#endif -} -#endif - -#if MF_SERVO_SUPPORT == 1 -//// SERVOS ///// -void AddServo(int pin) -{ - if (servosRegistered == MAX_MFSERVOS) - return; - servos[servosRegistered].attach(pin, true); - servosRegistered++; -} - -void ClearServos() -{ - for (int i = 0; i != servosRegistered; i++) - { - servos[i].detach(); - } - servosRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared servos")); -#endif -} -#endif - -#if MF_LCD_SUPPORT == 1 -//// LCD Display ///// -void AddLcdDisplay(uint8_t address = 0x24, uint8_t cols = 16, uint8_t lines = 2) -{ - if (lcd_12cRegistered == MAX_MFLCD_I2C) - return; - lcd_I2C[lcd_12cRegistered].attach(address, cols, lines); - lcd_12cRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added lcdDisplay")); -#endif -} - -void ClearLcdDisplays() -{ - for (int i = 0; i != lcd_12cRegistered; i++) - { - lcd_I2C[i].detach(); - } - lcd_12cRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared lcdDisplays")); -#endif -} -#endif - -#if MF_ANALOG_SUPPORT == 1 - -void AddAnalog(uint8_t pin = 1, char const *name = "AnalogInput", uint8_t sensitivity = 3) -{ - if (analogRegistered == MAX_ANALOG_INPUTS) - return; - if (!FitInMemory(sizeof(MFAnalog))) - { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("AnalogIn does not fit in Memory")); - return; - } - analog[analogRegistered] = new (allocateMemory(sizeof(MFAnalog))) MFAnalog(pin, name, sensitivity); - analogRegistered++; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added analog device ")); -#endif -} - -void ClearAnalog() -{ - analogRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared analog devices")); -#endif -} - -#endif - -#if MF_OUTPUT_SHIFTER_SUPPORT == 1 -//// SHIFT REGISTER ///// -void AddShifter(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules) -{ - if (outputShifterRegistered == MAX_OUTPUT_SHIFTERS) - return; - if (!FitInMemory(sizeof(MFOutputShifter))) - { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("OutputShifter does not fit in Memory")); - return; - } - outputShifters[outputShifterRegistered] = new (allocateMemory(sizeof(MFOutputShifter))) MFOutputShifter; - outputShifters[outputShifterRegistered]->attach(latchPin, clockPin, dataPin, modules); - outputShifters[outputShifterRegistered]->clear(); - outputShifterRegistered++; - -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Added Output Shifter")); -#endif -} - -void ClearOutputShifters() -{ - for (int i = 0; i != outputShifterRegistered; i++) - { - outputShifters[i]->detach(); - } - - outputShifterRegistered = 0; -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Cleared Output Shifter")); -#endif -} -#endif - -//// EVENT HANDLER ///// -void handlerOnButton(uint8_t eventId, uint8_t pin, const char *name) -{ - cmdMessenger.sendCmdStart(kButtonChange); - cmdMessenger.sendCmdArg(name); - cmdMessenger.sendCmdArg(eventId); - cmdMessenger.sendCmdEnd(); -}; - -//// EVENT HANDLER ///// -void handlerOnEncoder(uint8_t eventId, uint8_t pin, const char *name) -{ - cmdMessenger.sendCmdStart(kEncoderChange); - cmdMessenger.sendCmdArg(name); - cmdMessenger.sendCmdArg(eventId); - cmdMessenger.sendCmdEnd(); -}; - -#if MF_INPUT_SHIFTER_SUPPORT == 1 -//// EVENT HANDLER ///// -void handlerInputShifterOnChange(uint8_t eventId, uint8_t pin, const char *name) -{ - cmdMessenger.sendCmdStart(kInputShifterChange); - cmdMessenger.sendCmdArg(name); - cmdMessenger.sendCmdArg(pin); - cmdMessenger.sendCmdArg(eventId); - cmdMessenger.sendCmdEnd(); -}; -#endif - -//// EVENT HANDLER ///// -void handlerOnAnalogChange(int value, uint8_t pin, const char *name) -{ - cmdMessenger.sendCmdStart(kAnalogChange); - cmdMessenger.sendCmdArg(name); - cmdMessenger.sendCmdArg(value); - cmdMessenger.sendCmdEnd(); -}; - -/** - ** config stuff - **/ -void OnSetConfig() -{ -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Setting config start")); -#endif - lastCommand = millis(); - char *cfg = cmdMessenger.readStringArg(); - uint8_t cfgLen = strlen(cfg); - - if (configLength + cfgLen + 1 < MEM_LEN_CONFIG) - { - MFeeprom.write_block(MEM_OFFSET_CONFIG + configLength, cfg, cfgLen+1); // save the received config string including the terminatung NULL (+1) to EEPROM - configLength += cfgLen; - cmdMessenger.sendCmd(kStatus, configLength); - } - else - cmdMessenger.sendCmd(kStatus, -1); // last successfull saving block is already NULL terminated, nothing more todo -#ifdef DEBUG - cmdMessenger.sendCmd(kStatus, F("Setting config end")); -#endif -} - -void resetConfig() -{ - ClearButtons(); - ClearEncoders(); - ClearOutputs(); - -#if MF_SEGMENT_SUPPORT == 1 - ClearLedSegments(); -#endif - -#if MF_SERVO_SUPPORT == 1 - ClearServos(); -#endif - -#if MF_STEPPER_SUPPORT == 1 - ClearSteppers(); -#endif - -#if MF_LCD_SUPPORT == 1 - ClearLcdDisplays(); -#endif - -#if MF_ANALOG_SUPPORT == 1 - ClearAnalog(); -#endif - -#if MF_OUTPUT_SHIFTER_SUPPORT == 1 - ClearOutputShifters(); -#endif - -#if MF_INPUT_SHIFTER_SUPPORT == 1 - ClearInputShifters(); -#endif - - ClearMemory(); - configLength = 0; - configActivated = false; -} - -void OnResetConfig() -{ - resetConfig(); - cmdMessenger.sendCmd(kStatus, F("OK")); -} - -void OnSaveConfig() -{ - cmdMessenger.sendCmd(kConfigSaved, F("OK")); -} - -void OnActivateConfig() -{ - readConfig(); - _activateConfig(); -} - -void _activateConfig() -{ - configActivated = true; - cmdMessenger.sendCmd(kConfigActivated, F("OK")); -} - -// reads an ascii value which is '.' terminated from EEPROM and returns it's value -uint8_t readUintFromEEPROM (volatile uint16_t *addreeprom) { - char params[4] = {0}; // max 3 (255) digits NULL terminated - uint8_t counter = 0; - do - { - params[counter++] = MFeeprom.read_char((*addreeprom)++); // read character from eeprom and locate next buffer and eeprom location - } - while ( params[counter-1] != '.' && counter < sizeof(params)); // reads until limiter '.' and for safety reason not more then size of params[] - params[counter-1] = 0x00; // replace '.' by NULL to terminate the string - return atoi(params); -} - -// reads a string from EEPROM at given address which is ':' terminated and saves it in the nameBuffer -// once the nameBuffer is not needed anymore, just read until the ":" termination -> see function below -bool readNameFromEEPROM(uint16_t *addreeprom, char* buffer, uint16_t *addrbuffer) { - char temp = 0; - do - { - temp = MFeeprom.read_char((*addreeprom)++); // read the first character - buffer[(*addrbuffer)++] = temp; // save character and locate next buffer position - if (*addrbuffer >= MEMLEN_CONFIG_BUFFER) { // nameBuffer will be exceeded - return false; // abort copying from EEPROM to nameBuffer - } - } while (temp != ':'); // reads until limiter ':' and locates the next free buffer position - buffer[(*addrbuffer)-1] = 0x00; // replace ':' by NULL, terminates the string - return true; -} - -// reads the EEPRROM until end of command which ':' terminated -bool readEndCommandFromEEPROM(uint16_t *addreeprom) { - char temp = 0; - uint16_t length = MFeeprom.get_length(); - do - { - temp = MFeeprom.read_char((*addreeprom)++); - if (*addreeprom > length) // abort if EEPROM size will be exceeded - return false; - } - while (temp != ':'); // reads until limiter ':' - return true; -} - -void readConfig() -{ - if (configLength == 0) // do nothing if no config is available - return; - uint16_t addreeprom = MEM_OFFSET_CONFIG; // define first memory location where config is saved in EEPROM - uint16_t addrbuffer = 0; // and start with first memory location from nameBuffer - char params[6] = ""; - char command = readUintFromEEPROM(&addreeprom); // read the first value from EEPROM, it's a device definition - bool copy_success = true; // will be set to false if copying input names to nameBuffer exceeds array dimensions - // not required anymore when pins instead of names are transferred to the UI - - if (command == 0) // just to be sure, configLength should also be 0 - return; - - do // go through the EEPROM until it is NULL terminated - { - switch (command) - { - case kTypeButton: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin number - AddButton(params[0], &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name - copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location - break; - - case kTypeOutput: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin number - AddOutput(params[0]); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; - -#if MF_SEGMENT_SUPPORT == 1 - case kTypeLedSegment: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin Data number - params[1] = readUintFromEEPROM(&addreeprom); // get the Pin CS number - params[2] = readUintFromEEPROM(&addreeprom); // get the Pin CLK number - params[3] = readUintFromEEPROM(&addreeprom); // get the brightness - params[4] = readUintFromEEPROM(&addreeprom); // get the number of modules - AddLedSegment(params[0], params[1], params[2], params[4], params[3]); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; -#endif - -#if MF_STEPPER_SUPPORT == 1 - case kTypeStepperDeprecated: - // this is for backwards compatibility - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number - params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number - params[2] = readUintFromEEPROM(&addreeprom); // get the Pin3 number - params[3] = readUintFromEEPROM(&addreeprom); // get the Pin4 number - params[4] = readUintFromEEPROM(&addreeprom); // get the Button number - AddStepper(params[0], params[1], params[2], params[3], 0); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; -#endif - -#if MF_STEPPER_SUPPORT == 1 - case kTypeStepper: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number - params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number - params[2] = readUintFromEEPROM(&addreeprom); // get the Pin3 number - params[3] = readUintFromEEPROM(&addreeprom); // get the Pin4 number - params[4] = readUintFromEEPROM(&addreeprom); // get the Button number - AddStepper(params[0], params[1], params[2], params[3], params[4]); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; -#endif - -#if MF_SERVO_SUPPORT == 1 - case kTypeServo: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin number - AddServo(params[0]); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; -#endif - - case kTypeEncoderSingleDetent: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number - params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number - AddEncoder(params[0], params[1], 0, &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name - copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to it and get the next free memory location - break; - - case kTypeEncoder: - params[0] = readUintFromEEPROM(&addreeprom); // get the Pin1 number - params[1] = readUintFromEEPROM(&addreeprom); // get the Pin2 number - params[2] = readUintFromEEPROM(&addreeprom); // get the type - AddEncoder(params[0], params[1], params[2], &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name - copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location - break; - -#if MF_LCD_SUPPORT == 1 - case kTypeLcdDisplayI2C: - params[0] = readUintFromEEPROM(&addreeprom); // get the address - params[1] = readUintFromEEPROM(&addreeprom); // get the columns - params[2] = readUintFromEEPROM(&addreeprom); // get the lines - AddLcdDisplay(params[0], params[1], params[2]); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; -#endif - -#if MF_ANALOG_SUPPORT == 1 - case kTypeAnalogInput: - params[0] = readUintFromEEPROM(&addreeprom); // get the pin number - params[1] = readUintFromEEPROM(&addreeprom); // get the sensitivity - AddAnalog(params[0], &nameBuffer[addrbuffer], params[1]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name - copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location - break; -#endif - -#if MF_OUTPUT_SHIFTER_SUPPORT == 1 - case kShiftRegister: - params[0] = readUintFromEEPROM(&addreeprom); // get the latch Pin - params[1] = readUintFromEEPROM(&addreeprom); // get the clock Pin - params[2] = readUintFromEEPROM(&addreeprom); // get the data Pin - params[3] = readUintFromEEPROM(&addreeprom); // get the number of daisy chained modules - AddShifter(params[0], params[1], params[2], params[3]); - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - break; -#endif - -#if MF_INPUT_SHIFTER_SUPPORT == 1 - case kTypeInputShifter: - params[0] = readUintFromEEPROM(&addreeprom); // get the latch Pin - params[1] = readUintFromEEPROM(&addreeprom); // get the clock Pin - params[2] = readUintFromEEPROM(&addreeprom); // get the data Pin - params[3] = readUintFromEEPROM(&addreeprom); // get the number of daisy chained modules - AddInputShifter(params[0], params[1], params[2], params[3], &nameBuffer[addrbuffer]); - copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location - break; -#endif - - default: - copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name - } - command = readUintFromEEPROM(&addreeprom); - } while (command && copy_success); - if (!copy_success) { // too much/long names for input devices - nameBuffer[MEMLEN_CONFIG_BUFFER-1] = 0x00; // terminate the last copied (part of) string with 0x00 - } -} - -// Called when a received command has no attached function -void OnUnknownCommand() -{ - lastCommand = millis(); - cmdMessenger.sendCmd(kStatus, F("n/a")); -} - -void OnGetInfo() -{ - lastCommand = millis(); - cmdMessenger.sendCmdStart(kInfo); - cmdMessenger.sendCmdArg(F(MOBIFLIGHT_TYPE)); - cmdMessenger.sendCmdArg(name); - cmdMessenger.sendCmdArg(serial); - cmdMessenger.sendCmdArg(F(VERSION)); - cmdMessenger.sendCmdEnd(); -} - -void OnGetConfig() -{ - lastCommand = millis(); - cmdMessenger.sendCmdStart(kInfo); - if (configLength > 0) - { - cmdMessenger.sendCmdArg(MFeeprom.read_char(MEM_OFFSET_CONFIG)); - for (uint16_t i = 1; i < configLength; i++) - { - cmdMessenger.sendArg(MFeeprom.read_char(MEM_OFFSET_CONFIG + i)); - } - } - cmdMessenger.sendCmdEnd(); -} - -// Callback function that sets led on or off -void OnSetPin() -{ - // Read led state argument, interpret string as boolean - int pin = cmdMessenger.readInt16Arg(); - int state = cmdMessenger.readInt16Arg(); - // Set led - analogWrite(pin, state); // why does the UI sends the pin number and not the x.th output number like other devices? -// outputs[pin]->set(state); - lastCommand = millis(); -} - -#if MF_SEGMENT_SUPPORT == 1 -void OnInitModule() -{ - int module = cmdMessenger.readInt16Arg(); - int subModule = cmdMessenger.readInt16Arg(); - int brightness = cmdMessenger.readInt16Arg(); - ledSegments[module].setBrightness(subModule, brightness); - lastCommand = millis(); -} - -void OnSetModule() -{ - int module = cmdMessenger.readInt16Arg(); - int subModule = cmdMessenger.readInt16Arg(); - char *value = cmdMessenger.readStringArg(); - uint8_t points = (uint8_t)cmdMessenger.readInt16Arg(); - uint8_t mask = (uint8_t)cmdMessenger.readInt16Arg(); - ledSegments[module].display(subModule, value, points, mask); - lastCommand = millis(); -} - -void OnSetModuleBrightness() -{ - int module = cmdMessenger.readInt16Arg(); - int subModule = cmdMessenger.readInt16Arg(); - int brightness = cmdMessenger.readInt16Arg(); - ledSegments[module].setBrightness(subModule, brightness); - lastCommand = millis(); -} - -#endif - -#if MF_OUTPUT_SHIFTER_SUPPORT == 1 -void OnInitOutputShifter() -{ - int module = cmdMessenger.readInt16Arg(); - outputShifters[module]->clear(); - lastCommand = millis(); -} - -void OnSetOutputShifterPins() -{ - - int module = cmdMessenger.readInt16Arg(); - char *pins = cmdMessenger.readStringArg(); - int value = cmdMessenger.readInt16Arg(); - outputShifters[module]->setPins(pins, value); - lastCommand = millis(); -} -#endif - -#if MF_INPUT_SHIFTER_SUPPORT == 1 -void OnInitInputShiftRegister() -{ - int module = cmdMessenger.readInt16Arg(); - inputShifters[module]->clear(); - lastCommand = millis(); -} -#endif - -#if MF_STEPPER_SUPPORT == 1 -void OnSetStepper() -{ - int stepper = cmdMessenger.readInt16Arg(); - long newPos = cmdMessenger.readInt32Arg(); - - if (stepper >= steppersRegistered) - return; - steppers[stepper].moveTo(newPos); - lastCommand = millis(); -} - -void OnResetStepper() -{ - int stepper = cmdMessenger.readInt16Arg(); - - if (stepper >= steppersRegistered) - return; - steppers[stepper].reset(); - lastCommand = millis(); -} - -void OnSetZeroStepper() -{ - int stepper = cmdMessenger.readInt16Arg(); - - if (stepper >= steppersRegistered) - return; - steppers[stepper].setZero(); - lastCommand = millis(); -} - -void updateSteppers() -{ - for (int i = 0; i != steppersRegistered; i++) - { - steppers[i].update(); - } -} -#endif - -#if MF_SERVO_SUPPORT == 1 -void OnSetServo() -{ - int servo = cmdMessenger.readInt16Arg(); - int newValue = cmdMessenger.readInt16Arg(); - if (servo >= servosRegistered) - return; - servos[servo].moveTo(newValue); - lastCommand = millis(); -} - -void updateServos() -{ - if (millis() - lastServoUpdate <= MF_SERVO_DELAY_MS) - return; - lastServoUpdate = millis(); - - for (int i = 0; i != servosRegistered; i++) - { - servos[i].update(); - } -} -#endif - -#if MF_LCD_SUPPORT == 1 -void OnSetLcdDisplayI2C() -{ - int address = cmdMessenger.readInt16Arg(); - char *output = cmdMessenger.readStringArg(); - lcd_I2C[address].display(output); - lastCommand = millis(); -} -#endif - -void readButtons() -{ - if (millis() - lastButtonUpdate < MF_BUTTON_DEBOUNCE_MS) - return; - lastButtonUpdate = millis(); - for (int i = 0; i != buttonsRegistered; i++) - { - buttons[i]->update(); - } -} - -void readEncoder() -{ - if (millis() - lastEncoderUpdate < 1) - return; - lastEncoderUpdate = millis(); - for (int i = 0; i != encodersRegistered; i++) - { - encoders[i]->update(); - } -} - -#if MF_INPUT_SHIFTER_SUPPORT == 1 -void readInputShifters() -{ - if (millis() - lastInputShifterUpdate <= MF_BUTTON_DEBOUNCE_MS) - return; - lastInputShifterUpdate = millis(); - - for (int i = 0; i != inputShiftersRegistered; i++) - { - inputShifters[i]->update(); - } -} -#endif - -#if MF_ANALOG_SUPPORT == 1 -void readAnalog() -{ - if (millis() - lastAnalogAverage > MF_ANALOGAVERAGE_DELAY_MS - 1) - { - for (int i = 0; i != analogRegistered; i++) - { - analog[i]->readBuffer(); - } - lastAnalogAverage = millis(); - } - if (millis() - lastAnalogRead < MF_ANALOGREAD_DELAY_MS) - return; - lastAnalogRead = millis(); - for (int i = 0; i != analogRegistered; i++) - { - analog[i]->update(); - } -} -#endif - -void OnGenNewSerial() -{ - generateSerial(true); -// cmdMessenger.sendCmdStart(kInfo); -// cmdMessenger.sendCmdArg(serial); -// cmdMessenger.sendCmdEnd(); - cmdMessenger.sendCmd(kInfo,serial); -} - -void OnSetName() -{ - char *cfg = cmdMessenger.readStringArg(); - memcpy(name, cfg, MEM_LEN_NAME); - _storeName(); - cmdMessenger.sendCmdStart(kStatus); - cmdMessenger.sendCmdArg(name); - cmdMessenger.sendCmdEnd(); -} - -void _storeName() -{ - char prefix[] = "#"; - MFeeprom.write_block(MEM_OFFSET_NAME, prefix, 1); - MFeeprom.write_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1); -} - -void _restoreName() -{ - char testHasName[1] = ""; - MFeeprom.read_block(MEM_OFFSET_NAME, testHasName, 1); - if (testHasName[0] != '#') - return; - - MFeeprom.read_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1); -} - -void OnTrigger() -{ - // Trigger all button release events first... - for (int i = 0; i != buttonsRegistered; i++) - { - buttons[i]->triggerOnRelease(); - } - // ... then trigger all the press events - for (int i = 0; i != buttonsRegistered; i++) - { - buttons[i]->triggerOnPress(); - } - -// Retrigger all the input shifters. This automatically sends -// the release events first followed by press events. -#if MF_INPUT_SHIFTER_SUPPORT == 1 - for (int i = 0; i != inputShiftersRegistered; i++) - { - inputShifters[i]->retrigger(); - } -#endif } From 8f7e1436ace43413641b45d3abe800a598c1ab6e Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 8 Feb 2022 14:16:00 +0100 Subject: [PATCH 02/12] minor clean up --- src/MF_Stepper/MFStepper.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/MF_Stepper/MFStepper.cpp b/src/MF_Stepper/MFStepper.cpp index c74dacb9..918c5669 100644 --- a/src/MF_Stepper/MFStepper.cpp +++ b/src/MF_Stepper/MFStepper.cpp @@ -17,9 +17,7 @@ void MFStepper::attach(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, u if (!FitInMemory(sizeof(AccelStepper))) { // Error Message to Connector - cmdMessenger.sendCmdStart(kStatus); - cmdMessenger.sendCmdArg(F("Stepper does not fit in Memory")); - cmdMessenger.sendCmdEnd(); + cmdMessenger.sendCmd(kStatus, F("MFStepper does not fit in Memory")); return; } if (pin2 == pin4 && pin1 == pin3) // if pin1/2 are identical to pin3/4 @@ -39,6 +37,7 @@ void MFStepper::attach(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, u _initialized = true; _resetting = false; } + void MFStepper::detach() { _initialized = false; From 141fa5c095e51f6efba82dcd85055af657efbb7b Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 8 Feb 2022 15:33:27 +0100 Subject: [PATCH 03/12] lowercase for file commandmessenger.h --- include/{CommandMessenger.h => commandmessenger.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename include/{CommandMessenger.h => commandmessenger.h} (100%) diff --git a/include/CommandMessenger.h b/include/commandmessenger.h similarity index 100% rename from include/CommandMessenger.h rename to include/commandmessenger.h From c90b6e554df101591f54d25198e0205a7d2afcbc Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 8 Feb 2022 16:16:25 +0100 Subject: [PATCH 04/12] note capitel letters --- src/{MF_LCDDIsplay => MF_LCDDIsplays}/LCDDIsplay.h | 0 src/{MF_LCDDIsplay => MF_LCDDIsplays}/LCDDisplay.cpp | 0 src/{MF_LCDDIsplay => MF_LCDDIsplays}/MFLCDDisplay.cpp | 0 src/{MF_LCDDIsplay => MF_LCDDIsplays}/MFLCDDisplay.h | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/{MF_LCDDIsplay => MF_LCDDIsplays}/LCDDIsplay.h (100%) rename src/{MF_LCDDIsplay => MF_LCDDIsplays}/LCDDisplay.cpp (100%) rename src/{MF_LCDDIsplay => MF_LCDDIsplays}/MFLCDDisplay.cpp (100%) rename src/{MF_LCDDIsplay => MF_LCDDIsplays}/MFLCDDisplay.h (100%) diff --git a/src/MF_LCDDIsplay/LCDDIsplay.h b/src/MF_LCDDIsplays/LCDDIsplay.h similarity index 100% rename from src/MF_LCDDIsplay/LCDDIsplay.h rename to src/MF_LCDDIsplays/LCDDIsplay.h diff --git a/src/MF_LCDDIsplay/LCDDisplay.cpp b/src/MF_LCDDIsplays/LCDDisplay.cpp similarity index 100% rename from src/MF_LCDDIsplay/LCDDisplay.cpp rename to src/MF_LCDDIsplays/LCDDisplay.cpp diff --git a/src/MF_LCDDIsplay/MFLCDDisplay.cpp b/src/MF_LCDDIsplays/MFLCDDisplay.cpp similarity index 100% rename from src/MF_LCDDIsplay/MFLCDDisplay.cpp rename to src/MF_LCDDIsplays/MFLCDDisplay.cpp diff --git a/src/MF_LCDDIsplay/MFLCDDisplay.h b/src/MF_LCDDIsplays/MFLCDDisplay.h similarity index 100% rename from src/MF_LCDDIsplay/MFLCDDisplay.h rename to src/MF_LCDDIsplays/MFLCDDisplay.h From bfb0ab54b21d21256703f10f82c340b2df14f2c7 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 8 Feb 2022 16:17:00 +0100 Subject: [PATCH 05/12] note capitel letters --- src/{MF_LCDDIsplays => MF_LCDDisplay}/LCDDIsplay.h | 0 src/{MF_LCDDIsplays => MF_LCDDisplay}/LCDDisplay.cpp | 0 src/{MF_LCDDIsplays => MF_LCDDisplay}/MFLCDDisplay.cpp | 0 src/{MF_LCDDIsplays => MF_LCDDisplay}/MFLCDDisplay.h | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/{MF_LCDDIsplays => MF_LCDDisplay}/LCDDIsplay.h (100%) rename src/{MF_LCDDIsplays => MF_LCDDisplay}/LCDDisplay.cpp (100%) rename src/{MF_LCDDIsplays => MF_LCDDisplay}/MFLCDDisplay.cpp (100%) rename src/{MF_LCDDIsplays => MF_LCDDisplay}/MFLCDDisplay.h (100%) diff --git a/src/MF_LCDDIsplays/LCDDIsplay.h b/src/MF_LCDDisplay/LCDDIsplay.h similarity index 100% rename from src/MF_LCDDIsplays/LCDDIsplay.h rename to src/MF_LCDDisplay/LCDDIsplay.h diff --git a/src/MF_LCDDIsplays/LCDDisplay.cpp b/src/MF_LCDDisplay/LCDDisplay.cpp similarity index 100% rename from src/MF_LCDDIsplays/LCDDisplay.cpp rename to src/MF_LCDDisplay/LCDDisplay.cpp diff --git a/src/MF_LCDDIsplays/MFLCDDisplay.cpp b/src/MF_LCDDisplay/MFLCDDisplay.cpp similarity index 100% rename from src/MF_LCDDIsplays/MFLCDDisplay.cpp rename to src/MF_LCDDisplay/MFLCDDisplay.cpp diff --git a/src/MF_LCDDIsplays/MFLCDDisplay.h b/src/MF_LCDDisplay/MFLCDDisplay.h similarity index 100% rename from src/MF_LCDDIsplays/MFLCDDisplay.h rename to src/MF_LCDDisplay/MFLCDDisplay.h From 8af77da64577813f7a3d9c1df7d339589c2eb14e Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 8 Feb 2022 16:24:42 +0100 Subject: [PATCH 06/12] note capitel letters --- src/MF_LCDDisplay/{LCDDIsplay.h => LCDDisplay.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/MF_LCDDisplay/{LCDDIsplay.h => LCDDisplay.h} (100%) diff --git a/src/MF_LCDDisplay/LCDDIsplay.h b/src/MF_LCDDisplay/LCDDisplay.h similarity index 100% rename from src/MF_LCDDisplay/LCDDIsplay.h rename to src/MF_LCDDisplay/LCDDisplay.h From f9e75ed40a291bf52cfccd051d1ab91302567e20 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 10 Feb 2022 13:11:32 +0100 Subject: [PATCH 07/12] avoid exceeding buffer size if device is cleared --- platformio.ini | 18 ++++++++++++++++++ src/MF_InputShifter/InputShifter.cpp | 2 +- src/MF_LCDDisplay/LCDDisplay.cpp | 2 +- src/MF_Segment/LedSegment.cpp | 2 +- src/MF_Servo/Servos.cpp | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2745a67c..877a7a76 100644 --- a/platformio.ini +++ b/platformio.ini @@ -100,3 +100,21 @@ lib_deps = monitor_speed = 115200 extra_scripts = ${env.extra_scripts} + + +[env:mini] +platform = atmelavr +board = pro16MHzatmega328 +framework = arduino +build_flags = + ${env.build_flags} + -I./_Boards/Atmel/Board_Uno +src_filter = + ${env.src_filter} + +<../_Boards/Atmel> +lib_deps = + ${env.lib_deps} +monitor_speed = 115200 +extra_scripts = + ${env.extra_scripts} + diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp index 0a8192aa..64f345f3 100644 --- a/src/MF_InputShifter/InputShifter.cpp +++ b/src/MF_InputShifter/InputShifter.cpp @@ -44,7 +44,7 @@ void Clear() { for (int i = 0; i < inputShiftersRegistered; i++) { - inputShifters[inputShiftersRegistered]->detach(); + inputShifters[i]->detach(); } inputShiftersRegistered = 0; #ifdef DEBUG2CMDMESSENGER diff --git a/src/MF_LCDDisplay/LCDDisplay.cpp b/src/MF_LCDDisplay/LCDDisplay.cpp index af786022..29dcde7f 100644 --- a/src/MF_LCDDisplay/LCDDisplay.cpp +++ b/src/MF_LCDDisplay/LCDDisplay.cpp @@ -33,7 +33,7 @@ void Clear() { for (int i = 0; i != lcd_12cRegistered; i++) { - lcd_I2C[lcd_12cRegistered]->detach(); + lcd_I2C[i]->detach(); } lcd_12cRegistered = 0; #ifdef DEBUG2CMDMESSENGER diff --git a/src/MF_Segment/LedSegment.cpp b/src/MF_Segment/LedSegment.cpp index 5908ae9c..0d5f13c8 100644 --- a/src/MF_Segment/LedSegment.cpp +++ b/src/MF_Segment/LedSegment.cpp @@ -33,7 +33,7 @@ void Clear() { for (int i = 0; i != ledSegmentsRegistered; i++) { - ledSegments[ledSegmentsRegistered]->detach(); + ledSegments[i]->detach(); } ledSegmentsRegistered = 0; #ifdef DEBUG2CMDMESSENGER diff --git a/src/MF_Servo/Servos.cpp b/src/MF_Servo/Servos.cpp index f9066c1e..c6a63453 100644 --- a/src/MF_Servo/Servos.cpp +++ b/src/MF_Servo/Servos.cpp @@ -34,7 +34,7 @@ void Clear() { for (int i = 0; i != servosRegistered; i++) { - servos[servosRegistered]->detach(); + servos[i]->detach(); } servosRegistered = 0; #ifdef DEBUG2CMDMESSENGER From 8a59c2b3981acbfd0b91dfa9fb6193ec9c0189cf Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Sun, 20 Feb 2022 11:01:40 +0100 Subject: [PATCH 08/12] enable power saving for outputs acc. main branch --- src/mobiflight.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 90dbe0a8..a4412eb6 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -54,7 +54,7 @@ void SetPowerSavingMode(bool state) { // disable the lights ;) powerSavingMode = state; -// Output::PowerSave(state); // why is this uncommeted in the main branch?? + Output::PowerSave(state); #if MF_SEGMENT_SUPPORT == 1 LedSegment::PowerSave(state); #endif From f0d7a61706166b90076e7544853bcea7e5c0287e Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 24 Feb 2022 09:09:36 +0100 Subject: [PATCH 09/12] w/o Arduino mini --- platformio.ini | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/platformio.ini b/platformio.ini index 877a7a76..2745a67c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -100,21 +100,3 @@ lib_deps = monitor_speed = 115200 extra_scripts = ${env.extra_scripts} - - -[env:mini] -platform = atmelavr -board = pro16MHzatmega328 -framework = arduino -build_flags = - ${env.build_flags} - -I./_Boards/Atmel/Board_Uno -src_filter = - ${env.src_filter} - +<../_Boards/Atmel> -lib_deps = - ${env.lib_deps} -monitor_speed = 115200 -extra_scripts = - ${env.extra_scripts} - From f5dfdd5578a79903170529d4963209bd95e2617c Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 24 Feb 2022 09:16:25 +0100 Subject: [PATCH 10/12] consider lower/upper case for include files --- src/MF_Analog/Analog.cpp | 2 +- src/MF_Button/Button.cpp | 2 +- src/MF_Encoder/Encoder.cpp | 2 +- src/MF_InputShifter/InputShifter.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MF_Analog/Analog.cpp b/src/MF_Analog/Analog.cpp index 0f2ea519..45db7a25 100644 --- a/src/MF_Analog/Analog.cpp +++ b/src/MF_Analog/Analog.cpp @@ -3,7 +3,7 @@ #include "allocateMem.h" #include "mobiflight.h" #include "commandmessenger.h" -#include "analog.h" +#include "Analog.h" #include "MFBoards.h" #if MF_ANALOG_SUPPORT == 1 diff --git a/src/MF_Button/Button.cpp b/src/MF_Button/Button.cpp index 3b3d243a..2c309798 100644 --- a/src/MF_Button/Button.cpp +++ b/src/MF_Button/Button.cpp @@ -3,7 +3,7 @@ #include "allocateMem.h" #include "mobiflight.h" #include "commandmessenger.h" -#include "button.h" +#include "Button.h" #include "MFBoards.h" namespace Button diff --git a/src/MF_Encoder/Encoder.cpp b/src/MF_Encoder/Encoder.cpp index 8ea33a2a..9df33bd4 100644 --- a/src/MF_Encoder/Encoder.cpp +++ b/src/MF_Encoder/Encoder.cpp @@ -3,7 +3,7 @@ #include "allocateMem.h" #include "mobiflight.h" #include "commandmessenger.h" -#include "encoder.h" +#include "Encoder.h" #include "MFBoards.h" namespace Encoder diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp index 64f345f3..da9e1900 100644 --- a/src/MF_InputShifter/InputShifter.cpp +++ b/src/MF_InputShifter/InputShifter.cpp @@ -3,7 +3,7 @@ #include "allocateMem.h" #include "mobiflight.h" #include "commandmessenger.h" -#include "inputShifter.h" +#include "InputShifter.h" #include "MFBoards.h" namespace InputShifter From dd83c951d8a5f22ccd0a1e38776599a2f36528b5 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Mon, 28 Feb 2022 08:17:28 +0100 Subject: [PATCH 11/12] consider case sensitive characters in include file --- src/allocateMem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/allocateMem.cpp b/src/allocateMem.cpp index 3e7bfdbb..8eef2a20 100644 --- a/src/allocateMem.cpp +++ b/src/allocateMem.cpp @@ -2,7 +2,7 @@ #include "MFBoards.h" #include "mobiflight.h" #include "allocateMem.h" -#include "CommandMessenger.h" +#include "commandmessenger.h" char deviceBuffer[MF_MAX_DEVICEMEM] = {0}; uint16_t nextPointer = 0; From 5fd019186b8d811b3dd3ff193f0d76f511ef4ab7 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Mon, 28 Feb 2022 08:36:27 +0100 Subject: [PATCH 12/12] not for now used function added --- src/MF_InputShifter/InputShifter.cpp | 9 +++++++++ src/MF_InputShifter/InputShifter.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp index da9e1900..0db978e2 100644 --- a/src/MF_InputShifter/InputShifter.cpp +++ b/src/MF_InputShifter/InputShifter.cpp @@ -68,5 +68,14 @@ void OnTrigger() { inputShifters[i]->retrigger(); } + setLastCommandMillis(); } + +void OnInit() // not used anywhere!? +{ + int module = cmdMessenger.readInt16Arg(); + inputShifters[module]->clear(); + setLastCommandMillis(); +} + } // end of namespace InputShifter diff --git a/src/MF_InputShifter/InputShifter.h b/src/MF_InputShifter/InputShifter.h index a60ca136..40a62dcb 100644 --- a/src/MF_InputShifter/InputShifter.h +++ b/src/MF_InputShifter/InputShifter.h @@ -4,6 +4,7 @@ namespace InputShifter { void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name); void Clear(); +//void OnInit(); // this is defined but not used!? void read(); void OnTrigger(); }