From 974b9a214152d6a85a201e89653d05f81bc246bf Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Sat, 4 Nov 2023 07:41:44 +0100 Subject: [PATCH] Get rid of fixed device numbers (#253) --- _Boards/Atmel/Board_Mega/MFBoards.h | 39 +----------- _Boards/Atmel/Board_Nano/MFBoards.h | 39 +----------- _Boards/Atmel/Board_ProMicro/MFBoards.h | 39 +----------- _Boards/Atmel/Board_Uno/MFBoards.h | 39 +----------- _Boards/RaspberryPi/Pico/MFBoards.h | 39 +----------- src/Config.cpp | 83 ++++++++++++++++++++++++- src/MF_Analog/Analog.cpp | 27 ++++---- src/MF_Analog/Analog.h | 1 + src/MF_Button/Button.cpp | 31 +++++---- src/MF_Button/Button.h | 1 + src/MF_CustomDevice/CustomDevice.cpp | 48 +++++++------- src/MF_CustomDevice/CustomDevice.h | 1 + src/MF_DigInMux/DigInMux.cpp | 31 ++++----- src/MF_DigInMux/DigInMux.h | 1 + src/MF_Encoder/Encoder.cpp | 26 ++++---- src/MF_Encoder/Encoder.h | 1 + src/MF_InputShifter/InputShifter.cpp | 33 +++++----- src/MF_InputShifter/InputShifter.h | 1 + src/MF_LCDDisplay/LCDDisplay.cpp | 28 +++++---- src/MF_LCDDisplay/LCDDisplay.h | 1 + src/MF_Output/Output.cpp | 30 +++++---- src/MF_Output/Output.h | 1 + src/MF_OutputShifter/OutputShifter.cpp | 33 +++++----- src/MF_OutputShifter/OutputShifter.h | 1 + src/MF_Segment/LedSegment.cpp | 36 ++++++----- src/MF_Segment/LedSegment.h | 3 +- src/MF_Servo/Servos.cpp | 30 +++++---- src/MF_Servo/Servos.h | 1 + src/MF_Stepper/Stepper.cpp | 43 +++++++------ src/MF_Stepper/Stepper.h | 1 + src/config.h | 3 +- 31 files changed, 325 insertions(+), 366 deletions(-) diff --git a/_Boards/Atmel/Board_Mega/MFBoards.h b/_Boards/Atmel/Board_Mega/MFBoards.h index efd9d0e2..06a9660c 100644 --- a/_Boards/Atmel/Board_Mega/MFBoards.h +++ b/_Boards/Atmel/Board_Mega/MFBoards.h @@ -36,43 +36,6 @@ #define MF_DIGIN_MUX_SUPPORT 1 #endif -#ifndef MAX_OUTPUTS -#define MAX_OUTPUTS 40 -#endif -#ifndef MAX_BUTTONS -#define MAX_BUTTONS 68 -#endif -#ifndef MAX_LEDSEGMENTS -#define MAX_LEDSEGMENTS 4 -#endif -#ifndef MAX_ENCODERS -#define MAX_ENCODERS 20 -#endif -#ifndef MAX_STEPPERS -#define MAX_STEPPERS 10 -#endif -#ifndef MAX_MFSERVOS -#define MAX_MFSERVOS 10 -#endif -#ifndef MAX_MFLCD_I2C -#define MAX_MFLCD_I2C 2 -#endif -#ifndef MAX_ANALOG_INPUTS -#define MAX_ANALOG_INPUTS 16 -#endif -#ifndef MAX_OUTPUT_SHIFTERS -#define MAX_OUTPUT_SHIFTERS 4 -#endif -#ifndef MAX_INPUT_SHIFTERS -#define MAX_INPUT_SHIFTERS 4 -#endif -#ifndef MAX_DIGIN_MUX -#define MAX_DIGIN_MUX 4 -#endif -#ifndef MAX_CUSTOM_DEVICES -#define MAX_CUSTOM_DEVICES 5 -#endif - #ifndef MOBIFLIGHT_TYPE #define MOBIFLIGHT_TYPE "MobiFlight Mega" #endif @@ -89,7 +52,7 @@ #define MEMLEN_NAMES_BUFFER 1000 // max. size for configBuffer, contains only names from inputs #endif #ifndef MF_MAX_DEVICEMEM -#define MF_MAX_DEVICEMEM 1500 // max. memory size for devices +#define MF_MAX_DEVICEMEM 1600 // max. memory size for devices #endif #endif diff --git a/_Boards/Atmel/Board_Nano/MFBoards.h b/_Boards/Atmel/Board_Nano/MFBoards.h index 9da771c7..1a00ca6e 100644 --- a/_Boards/Atmel/Board_Nano/MFBoards.h +++ b/_Boards/Atmel/Board_Nano/MFBoards.h @@ -36,43 +36,6 @@ #define MF_DIGIN_MUX_SUPPORT 1 #endif -#ifndef MAX_OUTPUTS -#define MAX_OUTPUTS 18 -#endif -#ifndef MAX_BUTTONS -#define MAX_BUTTONS 18 -#endif -#ifndef MAX_LEDSEGMENTS -#define MAX_LEDSEGMENTS 4 -#endif -#ifndef MAX_ENCODERS -#define MAX_ENCODERS 9 -#endif -#ifndef MAX_STEPPERS -#define MAX_STEPPERS 2 -#endif -#ifndef MAX_MFSERVOS -#define MAX_MFSERVOS 2 -#endif -#ifndef MAX_MFLCD_I2C -#define MAX_MFLCD_I2C 2 -#endif -#ifndef MAX_ANALOG_INPUTS -#define MAX_ANALOG_INPUTS 8 -#endif -#ifndef MAX_OUTPUT_SHIFTERS -#define MAX_OUTPUT_SHIFTERS 2 -#endif -#ifndef MAX_INPUT_SHIFTERS -#define MAX_INPUT_SHIFTERS 2 -#endif -#ifndef MAX_DIGIN_MUX -#define MAX_DIGIN_MUX 3 -#endif -#ifndef MAX_CUSTOM_DEVICES -#define MAX_CUSTOM_DEVICES 2 -#endif - #ifndef MOBIFLIGHT_TYPE #define MOBIFLIGHT_TYPE "MobiFlight Nano" #endif @@ -89,7 +52,7 @@ #define MEMLEN_NAMES_BUFFER 220 // max. size for configBuffer, contains only names from inputs #endif #ifndef MF_MAX_DEVICEMEM -#define MF_MAX_DEVICEMEM 300 // max. memory size for devices +#define MF_MAX_DEVICEMEM 420 // max. memory size for devices #endif #endif diff --git a/_Boards/Atmel/Board_ProMicro/MFBoards.h b/_Boards/Atmel/Board_ProMicro/MFBoards.h index 828c6ad8..20f093a9 100644 --- a/_Boards/Atmel/Board_ProMicro/MFBoards.h +++ b/_Boards/Atmel/Board_ProMicro/MFBoards.h @@ -36,43 +36,6 @@ #define MF_DIGIN_MUX_SUPPORT 1 #endif -#ifndef MAX_OUTPUTS -#define MAX_OUTPUTS 18 -#endif -#ifndef MAX_BUTTONS -#define MAX_BUTTONS 18 -#endif -#ifndef MAX_LEDSEGMENTS -#define MAX_LEDSEGMENTS 4 -#endif -#ifndef MAX_ENCODERS -#define MAX_ENCODERS 9 -#endif -#ifndef MAX_STEPPERS -#define MAX_STEPPERS 3 -#endif -#ifndef MAX_MFSERVOS -#define MAX_MFSERVOS 3 -#endif -#ifndef MAX_MFLCD_I2C -#define MAX_MFLCD_I2C 2 -#endif -#ifndef MAX_ANALOG_INPUTS -#define MAX_ANALOG_INPUTS 9 -#endif -#ifndef MAX_OUTPUT_SHIFTERS -#define MAX_OUTPUT_SHIFTERS 2 -#endif -#ifndef MAX_INPUT_SHIFTERS -#define MAX_INPUT_SHIFTERS 2 -#endif -#ifndef MAX_DIGIN_MUX -#define MAX_DIGIN_MUX 3 -#endif -#ifndef MAX_CUSTOM_DEVICES -#define MAX_CUSTOM_DEVICES 2 -#endif - #ifndef MOBIFLIGHT_TYPE #define MOBIFLIGHT_TYPE "MobiFlight Micro" #endif @@ -89,7 +52,7 @@ #define MEMLEN_NAMES_BUFFER 350 // max. size for configBuffer, contains only names from inputs #endif #ifndef MF_MAX_DEVICEMEM -#define MF_MAX_DEVICEMEM 400 // max. memory size for devices +#define MF_MAX_DEVICEMEM 470 // max. memory size for devices #endif #endif diff --git a/_Boards/Atmel/Board_Uno/MFBoards.h b/_Boards/Atmel/Board_Uno/MFBoards.h index 442f64c1..a54e59e7 100644 --- a/_Boards/Atmel/Board_Uno/MFBoards.h +++ b/_Boards/Atmel/Board_Uno/MFBoards.h @@ -36,43 +36,6 @@ #define MF_DIGIN_MUX_SUPPORT 1 #endif -#ifndef MAX_OUTPUTS -#define MAX_OUTPUTS 18 -#endif -#ifndef MAX_BUTTONS -#define MAX_BUTTONS 18 -#endif -#ifndef MAX_LEDSEGMENTS -#define MAX_LEDSEGMENTS 4 -#endif -#ifndef MAX_ENCODERS -#define MAX_ENCODERS 9 -#endif -#ifndef MAX_STEPPERS -#define MAX_STEPPERS 2 -#endif -#ifndef MAX_MFSERVOS -#define MAX_MFSERVOS 2 -#endif -#ifndef MAX_MFLCD_I2C -#define MAX_MFLCD_I2C 2 -#endif -#ifndef MAX_ANALOG_INPUTS -#define MAX_ANALOG_INPUTS 6 -#endif -#ifndef MAX_OUTPUT_SHIFTERS -#define MAX_OUTPUT_SHIFTERS 2 -#endif -#ifndef MAX_INPUT_SHIFTERS -#define MAX_INPUT_SHIFTERS 2 -#endif -#ifndef MAX_DIGIN_MUX -#define MAX_DIGIN_MUX 3 -#endif -#ifndef MAX_CUSTOM_DEVICES -#define MAX_CUSTOM_DEVICES 2 -#endif - #ifndef MOBIFLIGHT_TYPE #define MOBIFLIGHT_TYPE "MobiFlight Uno" #endif @@ -89,7 +52,7 @@ #define MEMLEN_NAMES_BUFFER 220 // max. size for configBuffer, contains only names from inputs #endif #ifndef MF_MAX_DEVICEMEM -#define MF_MAX_DEVICEMEM 300 // max. memory size for devices +#define MF_MAX_DEVICEMEM 420 // max. memory size for devices #endif #endif diff --git a/_Boards/RaspberryPi/Pico/MFBoards.h b/_Boards/RaspberryPi/Pico/MFBoards.h index 38fc4e75..3da7ce93 100644 --- a/_Boards/RaspberryPi/Pico/MFBoards.h +++ b/_Boards/RaspberryPi/Pico/MFBoards.h @@ -30,43 +30,6 @@ #define MF_DIGIN_MUX_SUPPORT 1 #endif -#ifndef MAX_OUTPUTS -#define MAX_OUTPUTS 26 -#endif -#ifndef MAX_BUTTONS -#define MAX_BUTTONS 26 -#endif -#ifndef MAX_LEDSEGMENTS -#define MAX_LEDSEGMENTS 4 -#endif -#ifndef MAX_ENCODERS -#define MAX_ENCODERS 8 -#endif -#ifndef MAX_STEPPERS -#define MAX_STEPPERS 6 -#endif -#ifndef MAX_MFSERVOS -#define MAX_MFSERVOS 8 -#endif -#ifndef MAX_MFLCD_I2C -#define MAX_MFLCD_I2C 2 -#endif -#ifndef MAX_ANALOG_INPUTS -#define MAX_ANALOG_INPUTS 3 -#endif -#ifndef MAX_OUTPUT_SHIFTERS -#define MAX_OUTPUT_SHIFTERS 4 -#endif -#ifndef MAX_INPUT_SHIFTERS -#define MAX_INPUT_SHIFTERS 4 -#endif -#ifndef MAX_DIGIN_MUX -#define MAX_DIGIN_MUX 4 -#endif -#ifndef MAX_CUSTOM_DEVICES -#define MAX_CUSTOM_DEVICES 5 -#endif - #ifndef MOBIFLIGHT_TYPE #define MOBIFLIGHT_TYPE "MobiFlight RaspiPico" #endif @@ -83,7 +46,7 @@ #define MEMLEN_NAMES_BUFFER 1000 // max. size for configBuffer, contains only names from inputs #endif #ifndef MF_MAX_DEVICEMEM -#define MF_MAX_DEVICEMEM 2000 // max. memory size for devices +#define MF_MAX_DEVICEMEM 1000 // max. memory size for devices #endif #endif diff --git a/src/Config.cpp b/src/Config.cpp index 7ea80301..aab9b19a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -242,6 +242,85 @@ bool readEndCommandFromEEPROM(uint16_t *addreeprom, uint8_t delimiter) return true; } +void sendFailureMessage(const char *deviceName) +{ + cmdMessenger.sendCmdStart(kStatus); + cmdMessenger.sendCmdArg(deviceName); + cmdMessenger.sendCmdArg(F("does not fit in Memory")); + cmdMessenger.sendCmdEnd(); +} + +bool getArraysizes() +{ + if (configLength == 0) // do nothing if no config is available + return true; + uint16_t addreeprom = MEM_OFFSET_CONFIG; // define first memory location where config is saved in EEPROM + uint8_t device = 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 + uint8_t numberDevices[kTypeMax] = {0}; + + if (device == 0) // just to be sure, configLength should also be 0 + return true; + + // step through the EEPROM and calculate the number of devices for each type + do // step through the EEPROM until it is NULL terminated + { + numberDevices[device]++; + copy_success = readEndCommandFromEEPROM(&addreeprom, ':'); // check EEPROM until end of name + device = readUintFromEEPROM(&addreeprom); + } while (device && copy_success); + + if (!copy_success) { // too much/long names for input devices -> tbd how to handle this!! + cmdMessenger.sendCmd(kStatus, F("Failure, EEPROM size exceeded ")); + return false; + } + + // then call the function to allocate required memory for the arrays of each type + if (!Button::setupArray(numberDevices[kTypeButton])) + sendFailureMessage("Button"); + if (!Output::setupArray(numberDevices[kTypeOutput])) + sendFailureMessage("Output"); +#if MF_SEGMENT_SUPPORT == 1 + if (!LedSegment::setupArray(numberDevices[kTypeLedSegmentDeprecated]) + numberDevices[kTypeLedSegmentMulti]) + sendFailureMessage("7Segment"); +#endif +#if MF_STEPPER_SUPPORT == 1 + if (!Stepper::setupArray(numberDevices[kTypeStepper] + numberDevices[kTypeStepperDeprecated1] + numberDevices[kTypeStepperDeprecated2])) + sendFailureMessage("Stepper"); +#endif +#if MF_SERVO_SUPPORT == 1 + if (!Servos::setupArray(numberDevices[kTypeServo])) + sendFailureMessage("Servo"); +#endif + if (!Encoder::setupArray(numberDevices[kTypeEncoder] + numberDevices[kTypeEncoderSingleDetent])) + sendFailureMessage("Encoders"); +#if MF_LCD_SUPPORT == 1 + if (!LCDDisplay::setupArray(numberDevices[kTypeLcdDisplayI2C])) + sendFailureMessage("LCD"); +#endif +#if MF_ANALOG_SUPPORT == 1 + if (!Analog::setupArray(numberDevices[kTypeAnalogInput])) + sendFailureMessage("AnalogIn"); +#endif +#if MF_OUTPUT_SHIFTER_SUPPORT == 1 + if (!OutputShifter::setupArray(numberDevices[kTypeOutputShifter])) + sendFailureMessage("OutputShifter"); +#endif +#if MF_INPUT_SHIFTER_SUPPORT == 1 + if (!InputShifter::setupArray(numberDevices[kTypeInputShifter])) + sendFailureMessage("InputShifter"); +#endif +#if MF_DIGIN_MUX_SUPPORT == 1 + if (!DigInMux::setupArray(numberDevices[kTypeDigInMux])) + sendFailureMessage("DigInMux"); +#endif +#if MF_CUSTOMDEVICE_SUPPORT == 1 + if (!CustomDevice::setupArray(numberDevices[kTypeCustomDevice])) + sendFailureMessage("CustomDevice"); +#endif + return true; +} + void readConfig() { if (configLength == 0) // do nothing if no config is available @@ -249,13 +328,15 @@ void readConfig() 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[8] = ""; // buffer for reading parameters from EEPROM and sending to ::Add() function of device - char command = readUintFromEEPROM(&addreeprom); // read the first value from EEPROM, it's a device definition + uint8_t 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; + getArraysizes(); + do // go through the EEPROM until it is NULL terminated { switch (command) { diff --git a/src/MF_Analog/Analog.cpp b/src/MF_Analog/Analog.cpp index f4bd2efa..85531bb6 100644 --- a/src/MF_Analog/Analog.cpp +++ b/src/MF_Analog/Analog.cpp @@ -11,8 +11,9 @@ #if MF_ANALOG_SUPPORT == 1 namespace Analog { - MFAnalog *analog[MAX_ANALOG_INPUTS]; + MFAnalog *analog; uint8_t analogRegistered = 0; + uint8_t maxAnalogIn = 0; void handlerOnAnalogChange(int value, const char *name) { @@ -22,17 +23,21 @@ namespace Analog cmdMessenger.sendCmdEnd(); }; + bool setupArray(uint16_t count) + { + if (!FitInMemory(sizeof(MFAnalog) * count)) + return false; + analog = new (allocateMemory(sizeof(MFAnalog) * count)) MFAnalog; + maxAnalogIn = count; + return true; + } + void Add(uint8_t pin, char const *name, uint8_t sensitivity) { - if (analogRegistered == MAX_ANALOG_INPUTS) + if (analogRegistered == maxAnalogIn) 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] = MFAnalog(pin, name, sensitivity); MFAnalog::attachHandler(handlerOnAnalogChange); analogRegistered++; #ifdef DEBUG2CMDMESSENGER @@ -51,14 +56,14 @@ namespace Analog void read(void) { for (uint8_t i = 0; i < analogRegistered; i++) { - analog[i]->update(); + analog[i].update(); } } void readAverage(void) { for (uint8_t i = 0; i < analogRegistered; i++) { - analog[i]->readBuffer(); + analog[i].readBuffer(); } } @@ -66,7 +71,7 @@ namespace Analog { // Scan and transit current values for (uint8_t i = 0; i < analogRegistered; i++) { - analog[i]->retrigger(); + analog[i].retrigger(); } } diff --git a/src/MF_Analog/Analog.h b/src/MF_Analog/Analog.h index 61fb0f5f..4c4b7099 100644 --- a/src/MF_Analog/Analog.h +++ b/src/MF_Analog/Analog.h @@ -9,6 +9,7 @@ #include namespace Analog { + bool setupArray(uint16_t count); void Add(uint8_t pin, char const *name = "AnalogInput", uint8_t sensitivity = 3); void Clear(); void read(); diff --git a/src/MF_Button/Button.cpp b/src/MF_Button/Button.cpp index 6a4b6cfd..085798c7 100644 --- a/src/MF_Button/Button.cpp +++ b/src/MF_Button/Button.cpp @@ -10,8 +10,9 @@ namespace Button { - MFButton *buttons[MAX_BUTTONS]; + MFButton *buttons; uint8_t buttonsRegistered = 0; + uint8_t maxButtons = 0; void handlerOnButton(uint8_t eventId, const char *name) { @@ -21,17 +22,20 @@ namespace Button cmdMessenger.sendCmdEnd(); }; - void Add(uint8_t pin, char const *name) + bool setupArray(uint16_t count) { - if (buttonsRegistered == MAX_BUTTONS) - return; + if (!FitInMemory(sizeof(MFButton) * count)) + return false; + buttons = new (allocateMemory(sizeof(MFButton) * count)) MFButton; + maxButtons = count; + return true; + } - if (!FitInMemory(sizeof(MFButton))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Button does not fit in Memory")); + void Add(uint8_t pin, char const *name) + { + if (buttonsRegistered == maxButtons) return; - } - buttons[buttonsRegistered] = new (allocateMemory(sizeof(MFButton))) MFButton(pin, name); + buttons[buttonsRegistered] = MFButton(pin, name); MFButton::attachHandler(handlerOnButton); buttonsRegistered++; #ifdef DEBUG2CMDMESSENGER @@ -50,7 +54,8 @@ namespace Button void read(void) { for (uint8_t i = 0; i < buttonsRegistered; i++) { - buttons[i]->update(); + // buttons[i]->update(); + buttons[i].update(); } } @@ -58,11 +63,13 @@ namespace Button { // Trigger all button release events first... for (uint8_t i = 0; i < buttonsRegistered; i++) { - buttons[i]->triggerOnRelease(); + // buttons[i]->triggerOnRelease(); + buttons[i].triggerOnRelease(); } // ... then trigger all the press events for (uint8_t i = 0; i < buttonsRegistered; i++) { - buttons[i]->triggerOnPress(); + // buttons[i]->triggerOnPress(); + buttons[i].triggerOnPress(); } } } // namespace button diff --git a/src/MF_Button/Button.h b/src/MF_Button/Button.h index 1d52fb6a..5dd41673 100644 --- a/src/MF_Button/Button.h +++ b/src/MF_Button/Button.h @@ -10,6 +10,7 @@ namespace Button { + bool setupArray(uint16_t count); void Add(uint8_t pin, char const *name = "Button"); void Clear(); void read(); diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index ec74a5ed..bdd51aaa 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -1,9 +1,6 @@ -#include +#include "mobiflight.h" #include "CustomDevice.h" #include "MFCustomDevice.h" -#include "commandmessenger.h" -#include "MFBoards.h" -#include "allocateMem.h" /* ********************************************************************************** Normally nothing has to be changed in this file @@ -11,22 +8,27 @@ ********************************************************************************** */ namespace CustomDevice { - uint8_t CustomDeviceRegistered = 0; - MFCustomDevice *customDevice[MAX_CUSTOM_DEVICES]; + MFCustomDevice *customDevice; + uint8_t customDeviceRegistered = 0; + uint8_t maxCustomDevices = 0; + + bool setupArray(uint16_t count) + { + if (!FitInMemory(sizeof(MFCustomDevice) * count)) + return false; + customDevice = new (allocateMemory(sizeof(MFCustomDevice) * count)) MFCustomDevice(0,0,0); + maxCustomDevices = count; + return true; + } void Add(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig) { - if (CustomDeviceRegistered == MAX_CUSTOM_DEVICES) + if (customDeviceRegistered == maxCustomDevices) return; - if (!FitInMemory(sizeof(MFCustomDevice))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Custom Device does not fit in Memory")); - return; - } - customDevice[CustomDeviceRegistered] = new (allocateMemory(sizeof(MFCustomDevice))) MFCustomDevice(adrPin, adrType, adrConfig); - CustomDeviceRegistered++; + customDevice[customDeviceRegistered] = MFCustomDevice(adrPin, adrType, adrConfig); + customDeviceRegistered++; #ifdef DEBUG2CMDMESSENGER - cmdMessenger.sendCmd(kStatus, F("Added Stepper Setpoint")); + cmdMessenger.sendCmd(kStatus, F("Added CustomDevice")); #endif } @@ -37,12 +39,12 @@ namespace CustomDevice ********************************************************************************** */ void Clear() { - for (int i = 0; i != CustomDeviceRegistered; i++) { - customDevice[i]->detach(); + for (int i = 0; i != customDeviceRegistered; i++) { + customDevice[i].detach(); } - CustomDeviceRegistered = 0; + customDeviceRegistered = 0; #ifdef DEBUG2CMDMESSENGER - cmdMessenger.sendCmd(kStatus, F("Cleared Stepper Setpoint")); + cmdMessenger.sendCmd(kStatus, F("Cleared CustomDevice")); #endif } @@ -56,8 +58,8 @@ namespace CustomDevice ********************************************************************************** */ void update() { - for (int i = 0; i != CustomDeviceRegistered; i++) { - customDevice[i]->update(); + for (int i = 0; i != customDeviceRegistered; i++) { + customDevice[i].update(); } } @@ -73,12 +75,12 @@ namespace CustomDevice void OnSet() { int16_t device = cmdMessenger.readInt16Arg(); // get the device number - if (device >= CustomDeviceRegistered) // and do nothing if this device is not registered + if (device >= customDeviceRegistered) // and do nothing if this device is not registered return; int16_t messageID = cmdMessenger.readInt16Arg(); // get the messageID number char *output = cmdMessenger.readStringArg(); // get the pointer to the new raw string cmdMessenger.unescape(output); // and unescape the string if escape characters are used - customDevice[device]->set(messageID, output); // send the string to your custom device + customDevice[device].set(messageID, output); // send the string to your custom device setLastCommandMillis(); } diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index a7f4fbc3..637b0e35 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -2,6 +2,7 @@ namespace CustomDevice { + bool setupArray(uint16_t count); void Add(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig); void Clear(); void update(); diff --git a/src/MF_DigInMux/DigInMux.cpp b/src/MF_DigInMux/DigInMux.cpp index 69a1d61c..a838ed91 100644 --- a/src/MF_DigInMux/DigInMux.cpp +++ b/src/MF_DigInMux/DigInMux.cpp @@ -12,8 +12,9 @@ extern MFMuxDriver MUX; namespace DigInMux { - MFDigInMux *digInMux[MAX_DIGIN_MUX]; + MFDigInMux *digInMux; uint8_t digInMuxRegistered = 0; + uint8_t maxDigInMux = 0; void handlerOnDigInMux(uint8_t eventId, uint8_t channel, const char *name) { @@ -24,19 +25,21 @@ namespace DigInMux cmdMessenger.sendCmdEnd(); }; + bool setupArray(uint16_t count) + { + if (!FitInMemory(sizeof(MFDigInMux) * count)) + return false; + digInMux = new (allocateMemory(sizeof(MFDigInMux) * count)) MFDigInMux; + maxDigInMux = count; + return true; + } + void Add(uint8_t dataPin, uint8_t nRegs, char const *name) { - if (digInMuxRegistered == MAX_DIGIN_MUX) + if (digInMuxRegistered == maxDigInMux) return; - MFDigInMux *dip; - if (!FitInMemory(sizeof(MFDigInMux))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("DigInMux does not fit in Memory")); - return; - } - dip = new (allocateMemory(sizeof(MFDigInMux))) MFDigInMux(&MUX, name); - digInMux[digInMuxRegistered] = dip; - dip->attach(dataPin, (nRegs == 1), name); + digInMux[digInMuxRegistered] = MFDigInMux(&MUX, name); + digInMux[digInMuxRegistered].attach(dataPin, (nRegs == 1), name); MFDigInMux::attachHandler(handlerOnDigInMux); digInMuxRegistered++; @@ -48,7 +51,7 @@ namespace DigInMux void Clear() { for (uint8_t i = 0; i < digInMuxRegistered; i++) { - digInMux[i]->detach(); + digInMux[i].detach(); } digInMuxRegistered = 0; #ifdef DEBUG2CMDMESSENGER @@ -59,14 +62,14 @@ namespace DigInMux void read() { for (uint8_t i = 0; i < digInMuxRegistered; i++) { - digInMux[i]->update(); + digInMux[i].update(); } } void OnTrigger() { for (uint8_t i = 0; i < digInMuxRegistered; i++) { - digInMux[i]->retrigger(); + digInMux[i].retrigger(); } } } // namespace diff --git a/src/MF_DigInMux/DigInMux.h b/src/MF_DigInMux/DigInMux.h index 7a6624bd..11409d22 100644 --- a/src/MF_DigInMux/DigInMux.h +++ b/src/MF_DigInMux/DigInMux.h @@ -10,6 +10,7 @@ namespace DigInMux { + bool setupArray(uint16_t count); void Add(uint8_t dataPin, uint8_t nRegs, char const *name = "DigInMux"); void Clear(); void read(); diff --git a/src/MF_Encoder/Encoder.cpp b/src/MF_Encoder/Encoder.cpp index 5a34e25a..0d99c23d 100644 --- a/src/MF_Encoder/Encoder.cpp +++ b/src/MF_Encoder/Encoder.cpp @@ -10,8 +10,9 @@ namespace Encoder { - MFEncoder *encoders[MAX_ENCODERS]; + MFEncoder *encoders; uint8_t encodersRegistered = 0; + uint8_t maxEncoders = 0; void handlerOnEncoder(uint8_t eventId, const char *name) { @@ -21,18 +22,21 @@ namespace Encoder cmdMessenger.sendCmdEnd(); }; - void Add(uint8_t pin1, uint8_t pin2, uint8_t encoder_type, char const *name) + bool setupArray(uint16_t count) { - if (encodersRegistered == MAX_ENCODERS) - return; + if (!FitInMemory(sizeof(MFEncoder) * count)) + return false; + encoders = new (allocateMemory(sizeof(MFEncoder) * count)) MFEncoder; + maxEncoders = count; + return true; + } - if (!FitInMemory(sizeof(MFEncoder))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Encoders does not fit in Memory")); + void Add(uint8_t pin1, uint8_t pin2, uint8_t encoder_type, char const *name) + { + if (encodersRegistered == maxEncoders) return; - } - encoders[encodersRegistered] = new (allocateMemory(sizeof(MFEncoder))) MFEncoder; - encoders[encodersRegistered]->attach(pin1, pin2, encoder_type, name); + encoders[encodersRegistered] = MFEncoder(); + encoders[encodersRegistered].attach(pin1, pin2, encoder_type, name); MFEncoder::attachHandler(handlerOnEncoder); encodersRegistered++; #ifdef DEBUG2CMDMESSENGER @@ -51,7 +55,7 @@ namespace Encoder void read() { for (uint8_t i = 0; i < encodersRegistered; i++) { - encoders[i]->update(); + encoders[i].update(); } } } // namespace encoder diff --git a/src/MF_Encoder/Encoder.h b/src/MF_Encoder/Encoder.h index 20290802..52b54894 100644 --- a/src/MF_Encoder/Encoder.h +++ b/src/MF_Encoder/Encoder.h @@ -9,6 +9,7 @@ namespace Encoder { + bool setupArray(uint16_t count); void Add(uint8_t pin1 = 1, uint8_t pin2 = 2, uint8_t encoder_type = 0, char const *name = "Encoder"); void Clear(); void read(); diff --git a/src/MF_InputShifter/InputShifter.cpp b/src/MF_InputShifter/InputShifter.cpp index cedf9429..bdb1c82e 100644 --- a/src/MF_InputShifter/InputShifter.cpp +++ b/src/MF_InputShifter/InputShifter.cpp @@ -10,10 +10,11 @@ namespace InputShifter { - MFInputShifter *inputShifters[MAX_INPUT_SHIFTERS]; + MFInputShifter *inputShifters; uint8_t inputShiftersRegistered = 0; + uint8_t maxInputShiffter = 0; - void handlerInputShifterOnChange(uint8_t eventId, uint8_t pin, const char *name) + void handlerInputShifterOnChange(uint8_t eventId, uint8_t pin, const char *name) { cmdMessenger.sendCmdStart(kInputShifterChange); cmdMessenger.sendCmdArg(name); @@ -22,17 +23,21 @@ namespace InputShifter cmdMessenger.sendCmdEnd(); }; + bool setupArray(uint16_t count) + { + if (!FitInMemory(sizeof(MFInputShifter) * count)) + return false; + inputShifters = new (allocateMemory(sizeof(MFInputShifter) * count)) MFInputShifter; + maxInputShiffter = count; + return true; + } + void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name) { - if (inputShiftersRegistered == MAX_INPUT_SHIFTERS) + if (inputShiftersRegistered == maxInputShiffter) 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] = MFInputShifter(); + inputShifters[inputShiftersRegistered].attach(latchPin, clockPin, dataPin, modules, name); MFInputShifter::attachHandler(handlerInputShifterOnChange); inputShiftersRegistered++; #ifdef DEBUG2CMDMESSENGER @@ -43,7 +48,7 @@ namespace InputShifter void Clear() { for (uint8_t i = 0; i < inputShiftersRegistered; i++) { - inputShifters[i]->detach(); + inputShifters[i].detach(); } inputShiftersRegistered = 0; #ifdef DEBUG2CMDMESSENGER @@ -54,7 +59,7 @@ namespace InputShifter void read() { for (uint8_t i = 0; i < inputShiftersRegistered; i++) { - inputShifters[i]->update(); + inputShifters[i].update(); } } @@ -63,7 +68,7 @@ namespace InputShifter // Retrigger all the input shifters. This automatically sends // the release events first followed by press events. for (uint8_t i = 0; i < inputShiftersRegistered; i++) { - inputShifters[i]->retrigger(); + inputShifters[i].retrigger(); } setLastCommandMillis(); } @@ -71,7 +76,7 @@ namespace InputShifter void OnInit() // not used anywhere!? { int module = cmdMessenger.readInt16Arg(); - inputShifters[module]->clear(); + inputShifters[module].clear(); setLastCommandMillis(); } diff --git a/src/MF_InputShifter/InputShifter.h b/src/MF_InputShifter/InputShifter.h index b28a4e9c..c16e61c7 100644 --- a/src/MF_InputShifter/InputShifter.h +++ b/src/MF_InputShifter/InputShifter.h @@ -9,6 +9,7 @@ namespace InputShifter { + bool setupArray(uint16_t count); void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules, char const *name = "InputShifter"); void Clear(); // void OnInit(); // this is defined but not used!? diff --git a/src/MF_LCDDisplay/LCDDisplay.cpp b/src/MF_LCDDisplay/LCDDisplay.cpp index 3998c860..f7d462bd 100644 --- a/src/MF_LCDDisplay/LCDDisplay.cpp +++ b/src/MF_LCDDisplay/LCDDisplay.cpp @@ -10,21 +10,25 @@ namespace LCDDisplay { - MFLCDDisplay *lcd_I2C[MAX_MFLCD_I2C]; + MFLCDDisplay *lcd_I2C; uint8_t lcd_12cRegistered = 0; + uint8_t maxLCD_I2C = 0; - void Add(uint8_t address, uint8_t cols, uint8_t lines) + bool setupArray(uint16_t count) { - if (lcd_12cRegistered == MAX_MFLCD_I2C) - return; + if (!FitInMemory(sizeof(MFLCDDisplay) * count)) + return false; + lcd_I2C = new (allocateMemory(sizeof(MFLCDDisplay) * count)) MFLCDDisplay; + maxLCD_I2C = count; + return true; + } - if (!FitInMemory(sizeof(MFLCDDisplay))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("LCD does not fit in Memory!")); + void Add(uint8_t address, uint8_t cols, uint8_t lines) + { + if (lcd_12cRegistered == maxLCD_I2C) return; - } - lcd_I2C[lcd_12cRegistered] = new (allocateMemory(sizeof(MFLCDDisplay))) MFLCDDisplay; - lcd_I2C[lcd_12cRegistered]->attach(address, cols, lines); + lcd_I2C[lcd_12cRegistered] = MFLCDDisplay(); + lcd_I2C[lcd_12cRegistered].attach(address, cols, lines); lcd_12cRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added lcdDisplay")); @@ -34,7 +38,7 @@ namespace LCDDisplay void Clear() { for (uint8_t i = 0; i < lcd_12cRegistered; i++) { - lcd_I2C[i]->detach(); + lcd_I2C[i].detach(); } lcd_12cRegistered = 0; #ifdef DEBUG2CMDMESSENGER @@ -47,7 +51,7 @@ namespace LCDDisplay int address = cmdMessenger.readInt16Arg(); char *output = cmdMessenger.readStringArg(); cmdMessenger.unescape(output); - lcd_I2C[address]->display(output); + lcd_I2C[address].display(output); setLastCommandMillis(); } } // namespace diff --git a/src/MF_LCDDisplay/LCDDisplay.h b/src/MF_LCDDisplay/LCDDisplay.h index e27b7746..12bcf2db 100644 --- a/src/MF_LCDDisplay/LCDDisplay.h +++ b/src/MF_LCDDisplay/LCDDisplay.h @@ -9,6 +9,7 @@ namespace LCDDisplay { + bool setupArray(uint16_t count); void Add(uint8_t address = 0x24, uint8_t cols = 16, uint8_t lines = 2); void Clear(); void OnSet(); diff --git a/src/MF_Output/Output.cpp b/src/MF_Output/Output.cpp index fa66d41e..b0b47057 100644 --- a/src/MF_Output/Output.cpp +++ b/src/MF_Output/Output.cpp @@ -10,20 +10,24 @@ namespace Output { - MFOutput *outputs[MAX_OUTPUTS]; - uint8_t outputsRegistered = 0; + MFOutput *outputs; + uint8_t outputsRegistered = 0; + uint8_t maxOutputs = 0; - void Add(uint8_t pin) + bool setupArray(uint16_t count) { - if (outputsRegistered == MAX_OUTPUTS) - return; + if (!FitInMemory(sizeof(MFOutput) * count)) + return false; + outputs = new (allocateMemory(sizeof(MFOutput) * count)) MFOutput; + maxOutputs = count; + return true; + } - if (!FitInMemory(sizeof(MFOutput))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Output does not fit in Memory")); + void Add(uint8_t pin) + { + if (outputsRegistered == maxOutputs) return; - } - outputs[outputsRegistered] = new (allocateMemory(sizeof(MFOutput))) MFOutput(pin); + outputs[outputsRegistered] = MFOutput(pin); outputsRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added output")); @@ -41,18 +45,18 @@ namespace Output void OnSet() { // Read led state argument, interpret string as boolean - int pin = cmdMessenger.readInt16Arg(); + 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 + // output[pin].set(state); // once this is changed uncomment this setLastCommandMillis(); } void PowerSave(bool state) { for (uint8_t i = 0; i < outputsRegistered; ++i) { - outputs[i]->powerSavingMode(state); + outputs[i].powerSavingMode(state); } } } // namespace diff --git a/src/MF_Output/Output.h b/src/MF_Output/Output.h index e8d9243e..6cbc7f6f 100644 --- a/src/MF_Output/Output.h +++ b/src/MF_Output/Output.h @@ -9,6 +9,7 @@ namespace Output { + bool setupArray(uint16_t count); void Add(uint8_t pin = 1); void Clear(); void OnSet(); diff --git a/src/MF_OutputShifter/OutputShifter.cpp b/src/MF_OutputShifter/OutputShifter.cpp index 5955f884..c0911895 100644 --- a/src/MF_OutputShifter/OutputShifter.cpp +++ b/src/MF_OutputShifter/OutputShifter.cpp @@ -10,21 +10,26 @@ namespace OutputShifter { - MFOutputShifter *outputShifters[MAX_OUTPUT_SHIFTERS]; + MFOutputShifter *outputShifters; uint8_t outputShifterRegistered = 0; + uint8_t maxOutputShifter = 0; - void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules) + bool setupArray(uint16_t count) { - if (outputShifterRegistered == MAX_OUTPUT_SHIFTERS) - return; - if (!FitInMemory(sizeof(MFOutputShifter))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("OutputShifter does not fit in Memory")); + if (!FitInMemory(sizeof(MFOutputShifter) * count)) + return false; + outputShifters = new (allocateMemory(sizeof(MFOutputShifter) * count)) MFOutputShifter; + maxOutputShifter = count; + return true; + } + + void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules) + { + if (outputShifterRegistered == maxOutputShifter) return; - } - outputShifters[outputShifterRegistered] = new (allocateMemory(sizeof(MFOutputShifter))) MFOutputShifter; - outputShifters[outputShifterRegistered]->attach(latchPin, clockPin, dataPin, modules); - outputShifters[outputShifterRegistered]->clear(); + outputShifters[outputShifterRegistered] = MFOutputShifter(); + outputShifters[outputShifterRegistered].attach(latchPin, clockPin, dataPin, modules); + outputShifters[outputShifterRegistered].clear(); outputShifterRegistered++; #ifdef DEBUG2CMDMESSENGER @@ -35,7 +40,7 @@ namespace OutputShifter void Clear() { for (uint8_t i = 0; i < outputShifterRegistered; i++) { - outputShifters[i]->detach(); + outputShifters[i].detach(); } outputShifterRegistered = 0; @@ -47,7 +52,7 @@ namespace OutputShifter void OnInit() // not used anywhere!? { int module = cmdMessenger.readInt16Arg(); - outputShifters[module]->clear(); + outputShifters[module].clear(); setLastCommandMillis(); } @@ -57,7 +62,7 @@ namespace OutputShifter int module = cmdMessenger.readInt16Arg(); char *pins = cmdMessenger.readStringArg(); int value = cmdMessenger.readInt16Arg(); - outputShifters[module]->setPins(pins, value); + outputShifters[module].setPins(pins, value); setLastCommandMillis(); } } // namespace diff --git a/src/MF_OutputShifter/OutputShifter.h b/src/MF_OutputShifter/OutputShifter.h index 6c0a609c..de572f95 100644 --- a/src/MF_OutputShifter/OutputShifter.h +++ b/src/MF_OutputShifter/OutputShifter.h @@ -8,6 +8,7 @@ namespace OutputShifter { + bool setupArray(uint16_t count); void Add(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t modules); void Clear(); // void OnInit(); // this is defined but not used!? diff --git a/src/MF_Segment/LedSegment.cpp b/src/MF_Segment/LedSegment.cpp index c0c66f0b..3974f1ba 100644 --- a/src/MF_Segment/LedSegment.cpp +++ b/src/MF_Segment/LedSegment.cpp @@ -10,21 +10,25 @@ namespace LedSegment { - MFSegments *ledSegments[MAX_LEDSEGMENTS]; - uint8_t ledSegmentsRegistered = 0; + MFSegments *ledSegments; + uint8_t ledSegmentsRegistered = 0; + uint8_t ledSegmentsRegistereds = 0; - void Add(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t numDevices, uint8_t brightness) + bool setupArray(uint16_t count) { - if (ledSegmentsRegistered == MAX_LEDSEGMENTS) - return; + if (!FitInMemory(sizeof(MFSegments) * count)) + return false; + ledSegments = new (allocateMemory(sizeof(MFSegments) * count)) MFSegments; + ledSegmentsRegistereds = count; + return true; + } - if (!FitInMemory(sizeof(MFSegments))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("7Segment does not fit in Memory!")); + void Add(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t numDevices, uint8_t brightness) + { + if (ledSegmentsRegistered == ledSegmentsRegistereds) return; - } - ledSegments[ledSegmentsRegistered] = new (allocateMemory(sizeof(MFSegments))) MFSegments; - ledSegments[ledSegmentsRegistered]->attach(type, dataPin, csPin, clkPin, numDevices, brightness); // lc is our object + ledSegments[ledSegmentsRegistered] = MFSegments(); + ledSegments[ledSegmentsRegistered].attach(type, dataPin, csPin, clkPin, numDevices, brightness); // lc is our object ledSegmentsRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added Led Segment")); @@ -34,7 +38,7 @@ namespace LedSegment void Clear() { for (uint8_t i = 0; i < ledSegmentsRegistered; i++) { - ledSegments[i]->detach(); + ledSegments[i].detach(); } ledSegmentsRegistered = 0; #ifdef DEBUG2CMDMESSENGER @@ -45,7 +49,7 @@ namespace LedSegment void PowerSave(bool state) { for (uint8_t i = 0; i < ledSegmentsRegistered; ++i) { - ledSegments[i]->powerSavingMode(state); + ledSegments[i].powerSavingMode(state); } } @@ -54,7 +58,7 @@ namespace LedSegment int module = cmdMessenger.readInt16Arg(); int subModule = cmdMessenger.readInt16Arg(); int brightness = cmdMessenger.readInt16Arg(); - ledSegments[module]->setBrightness(subModule, brightness); + ledSegments[module].setBrightness(subModule, brightness); setLastCommandMillis(); } @@ -65,7 +69,7 @@ namespace LedSegment 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); + ledSegments[module].display(subModule, value, points, mask); setLastCommandMillis(); } @@ -74,7 +78,7 @@ namespace LedSegment int module = cmdMessenger.readInt16Arg(); int subModule = cmdMessenger.readInt16Arg(); int brightness = cmdMessenger.readInt16Arg(); - ledSegments[module]->setBrightness(subModule, brightness); + ledSegments[module].setBrightness(subModule, brightness); setLastCommandMillis(); } } // namespace diff --git a/src/MF_Segment/LedSegment.h b/src/MF_Segment/LedSegment.h index 68bcc4c6..b3af44b8 100644 --- a/src/MF_Segment/LedSegment.h +++ b/src/MF_Segment/LedSegment.h @@ -7,7 +7,7 @@ #pragma once namespace LedSegment -{ +{ enum { TYPE_MAX72XX = 0, TYPE_TM1637_4DIGITS = 0xFD, @@ -15,6 +15,7 @@ namespace LedSegment TYPE_UNDEFINED = 0xFF }; + bool setupArray(uint16_t count); void Add(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t numDevices, uint8_t brightness); void Clear(); void PowerSave(bool state); diff --git a/src/MF_Servo/Servos.cpp b/src/MF_Servo/Servos.cpp index dc05ea49..7122a537 100644 --- a/src/MF_Servo/Servos.cpp +++ b/src/MF_Servo/Servos.cpp @@ -10,21 +10,25 @@ namespace Servos { - MFServo *servos[MAX_MFSERVOS]; + MFServo *servos; uint8_t servosRegistered = 0; + uint8_t maxServos = 0; - void Add(uint8_t pin) + bool setupArray(uint16_t count) { - if (servosRegistered == MAX_MFSERVOS) - return; + if (!FitInMemory(sizeof(MFServo) * count)) + return false; + servos = new (allocateMemory(sizeof(MFServo) * count)) MFServo; + maxServos = count; + return true; + } - if (!FitInMemory(sizeof(MFServo))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Servo does not fit in Memory!")); + void Add(uint8_t pin) + { + if (servosRegistered == maxServos) return; - } - servos[servosRegistered] = new (allocateMemory(sizeof(MFServo))) MFServo; - servos[servosRegistered]->attach(pin, true); + servos[servosRegistered] = MFServo(); + servos[servosRegistered].attach(pin, true); servosRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added servos")); @@ -34,7 +38,7 @@ namespace Servos void Clear() { for (uint8_t i = 0; i < servosRegistered; i++) { - servos[i]->detach(); + servos[i].detach(); } servosRegistered = 0; #ifdef DEBUG2CMDMESSENGER @@ -48,14 +52,14 @@ namespace Servos int newValue = cmdMessenger.readInt16Arg(); if (servo >= servosRegistered) return; - servos[servo]->moveTo(newValue); + servos[servo].moveTo(newValue); setLastCommandMillis(); } void update() { for (uint8_t i = 0; i < servosRegistered; i++) { - servos[i]->update(); + servos[i].update(); } } } // namespace diff --git a/src/MF_Servo/Servos.h b/src/MF_Servo/Servos.h index 58dc6ccb..fc1de28b 100644 --- a/src/MF_Servo/Servos.h +++ b/src/MF_Servo/Servos.h @@ -8,6 +8,7 @@ namespace Servos { + bool setupArray(uint16_t count); void Add(uint8_t pin); void Clear(); void OnSet(); diff --git a/src/MF_Stepper/Stepper.cpp b/src/MF_Stepper/Stepper.cpp index 580d93fb..7ccdcdb3 100644 --- a/src/MF_Stepper/Stepper.cpp +++ b/src/MF_Stepper/Stepper.cpp @@ -10,26 +10,29 @@ namespace Stepper { - MFStepper *steppers[MAX_STEPPERS]; + MFStepper *steppers; uint8_t steppersRegistered = 0; + uint8_t maxSteppers = 0; - void Add(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t btnPin1, uint8_t mode, int8_t backlash, bool deactivateOutput) + bool setupArray(uint16_t count) { - if (steppersRegistered == MAX_STEPPERS) - return; + if (!FitInMemory(sizeof(MFStepper) * count)) + return false; + steppers = new (allocateMemory(sizeof(MFStepper) * count)) MFStepper; + maxSteppers = count; + return true; + } - if (!FitInMemory(sizeof(MFStepper))) { - // Error Message to Connector - cmdMessenger.sendCmd(kStatus, F("Stepper does not fit in Memory!")); + void Add(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t btnPin1, uint8_t mode, int8_t backlash, bool deactivateOutput) + { + if (steppersRegistered == maxSteppers) return; - } - - steppers[steppersRegistered] = new (allocateMemory(sizeof(MFStepper))) MFStepper; - steppers[steppersRegistered]->attach(pin1, pin2, pin3, pin4, btnPin1, mode, backlash, deactivateOutput); + steppers[steppersRegistered] = MFStepper(); + steppers[steppersRegistered].attach(pin1, pin2, pin3, pin4, btnPin1, mode, backlash, deactivateOutput); if (btnPin1 > 0) { // this triggers the auto reset if we need to reset - steppers[steppersRegistered]->reset(); + steppers[steppersRegistered].reset(); } // all set @@ -43,7 +46,7 @@ namespace Stepper void Clear() { for (uint8_t i = 0; i < steppersRegistered; i++) { - steppers[i]->detach(); + steppers[i].detach(); } steppersRegistered = 0; #ifdef DEBUG2CMDMESSENGER @@ -58,7 +61,7 @@ namespace Stepper if (stepper >= steppersRegistered) return; - steppers[stepper]->moveTo(newPos); + steppers[stepper].moveTo(newPos); setLastCommandMillis(); } @@ -68,7 +71,7 @@ namespace Stepper if (stepper >= steppersRegistered) return; - steppers[stepper]->reset(); + steppers[stepper].reset(); setLastCommandMillis(); } @@ -78,7 +81,7 @@ namespace Stepper if (stepper >= steppersRegistered) return; - steppers[stepper]->setZero(); + steppers[stepper].setZero(); setLastCommandMillis(); } @@ -90,21 +93,21 @@ namespace Stepper if (stepper >= steppersRegistered) return; - steppers[stepper]->setMaxSpeed(maxSpeed); - steppers[stepper]->setAcceleration(maxAccel); + steppers[stepper].setMaxSpeed(maxSpeed); + steppers[stepper].setAcceleration(maxAccel); } void update() { for (uint8_t i = 0; i < steppersRegistered; i++) { - steppers[i]->update(); + steppers[i].update(); } } void PowerSave(bool state) { for (uint8_t i = 0; i < steppersRegistered; ++i) { - steppers[i]->powerSavingMode(state); + steppers[i].powerSavingMode(state); } } diff --git a/src/MF_Stepper/Stepper.h b/src/MF_Stepper/Stepper.h index 455272c7..fc7e797d 100644 --- a/src/MF_Stepper/Stepper.h +++ b/src/MF_Stepper/Stepper.h @@ -8,6 +8,7 @@ namespace Stepper { + bool setupArray(uint16_t count); void Add(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t btnPin1, uint8_t mode = 0, int8_t backlash = 0, bool deactivateOutput = false); void Clear(); void OnSet(); diff --git a/src/config.h b/src/config.h index 064e658f..d0de7d82 100644 --- a/src/config.h +++ b/src/config.h @@ -24,7 +24,8 @@ enum { kTypeDigInMux, // 14 Digital input multiplexer support (example: 74HCT4067, 74HCT4051) kTypeStepper, // 15 new stepper type with settings for backlash and deactivate output kTypeLedSegmentMulti, // 16 new led segment with MAX7219 and TM1637 support - kTypeCustomDevice // 16 Custom Device + kTypeCustomDevice, // 17 Custom Device + kTypeMax // if new device types are added, this MUST be before this one! }; void loadConfig(void);