Skip to content

Commit

Permalink
Get rid of fixed device numbers (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
elral authored Nov 4, 2023
1 parent 9bb8036 commit 974b9a2
Show file tree
Hide file tree
Showing 31 changed files with 325 additions and 366 deletions.
39 changes: 1 addition & 38 deletions _Boards/Atmel/Board_Mega/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
39 changes: 1 addition & 38 deletions _Boards/Atmel/Board_Nano/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
39 changes: 1 addition & 38 deletions _Boards/Atmel/Board_ProMicro/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
39 changes: 1 addition & 38 deletions _Boards/Atmel/Board_Uno/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
39 changes: 1 addition & 38 deletions _Boards/RaspberryPi/Pico/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
83 changes: 82 additions & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,101 @@ 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
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[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) {
Expand Down
Loading

0 comments on commit 974b9a2

Please sign in to comment.