Skip to content

Commit

Permalink
Changes for MobiFlight#205 - Device names kept in EEPROM instead of R…
Browse files Browse the repository at this point in the history
…AM buffer
  • Loading branch information
GioCC committed Oct 10, 2022
1 parent 2595024 commit 6a9dcff
Show file tree
Hide file tree
Showing 28 changed files with 222 additions and 204 deletions.
8 changes: 8 additions & 0 deletions MF_firmware.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}
14 changes: 7 additions & 7 deletions _Boards/Atmel/Board_Mega/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
#define STEPPER_SPEED 400 // 300 already worked, 467, too?
#define STEPPER_ACCEL 800

#define MOBIFLIGHT_TYPE "MobiFlight Mega"
#define MOBIFLIGHT_SERIAL "1234567890"
#define MOBIFLIGHT_NAME "MobiFlight Mega"
#define EEPROM_SIZE 4096 // EEPROMSizeMega
#define MEMLEN_CONFIG 1496 // max. size for config which wil be stored in EEPROM
#define MEMLEN_NAMES_BUFFER 1000 // max. size for configBuffer, contains only names from inputs
#define MF_MAX_DEVICEMEM 1500 // max. memory size for devices
#define MOBIFLIGHT_TYPE "MobiFlight Mega"
#define MOBIFLIGHT_SERIAL "1234567890"
#define MOBIFLIGHT_NAME "MobiFlight Mega"
#define EEPROM_SIZE 4096 // EEPROMSizeMega
#define MEMLEN_CONFIG 1496 // max. size for config which wil be stored in EEPROM
// #define MEMLEN_NAMES_BUFFER 1000 // max. size for configBuffer, contains only names from inputs
#define MF_MAX_DEVICEMEM 1500 // max. memory size for devices

#define RANDOM_SEED_INPUT A0

Expand Down
16 changes: 8 additions & 8 deletions _Boards/Atmel/Board_ProMicro/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define MF_STEPPER_SUPPORT 1
#endif
#ifndef MF_SERVO_SUPPORT
#define MF_SERVO_SUPPORT 0
#define MF_SERVO_SUPPORT 0
#endif
#ifndef MF_ANALOG_SUPPORT
#define MF_ANALOG_SUPPORT 1
Expand Down Expand Up @@ -53,13 +53,13 @@
#define STEPPER_SPEED 400 // 300 already worked, 467, too?
#define STEPPER_ACCEL 800

#define MOBIFLIGHT_TYPE "MobiFlight Micro"
#define MOBIFLIGHT_SERIAL "0987654321"
#define MOBIFLIGHT_NAME "MobiFlight Micro"
#define EEPROM_SIZE 1024 // EEPROMSizeMicro
#define MEMLEN_CONFIG 440 // max. size for config which wil be stored in EEPROM
#define MEMLEN_NAMES_BUFFER 350 // max. size for configBuffer, contains only names from inputs
#define MF_MAX_DEVICEMEM 400 // max. memory size for devices
#define MOBIFLIGHT_TYPE "MobiFlight Micro"
#define MOBIFLIGHT_SERIAL "0987654321"
#define MOBIFLIGHT_NAME "MobiFlight Micro"
#define EEPROM_SIZE 1024 // EEPROMSizeMicro
#define MEMLEN_CONFIG 440 // max. size for config which wil be stored in EEPROM
// #define MEMLEN_NAMES_BUFFER 350 // max. size for configBuffer, contains only names from inputs
#define MF_MAX_DEVICEMEM 400 // max. memory size for devices

#define RANDOM_SEED_INPUT A0

Expand Down
24 changes: 12 additions & 12 deletions _Boards/Atmel/Board_Uno/MFBoards.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
#define MFBoardUno_h

#ifndef MF_SEGMENT_SUPPORT
#define MF_SEGMENT_SUPPORT 1
#define MF_SEGMENT_SUPPORT 1
#endif
#ifndef MF_LCD_SUPPORT
#define MF_LCD_SUPPORT 1
#define MF_LCD_SUPPORT 1
#endif
#ifndef MF_STEPPER_SUPPORT
#define MF_STEPPER_SUPPORT 1
#define MF_STEPPER_SUPPORT 1
#endif
#ifndef MF_SERVO_SUPPORT
#define MF_SERVO_SUPPORT 1
#define MF_SERVO_SUPPORT 1
#endif
#ifndef MF_ANALOG_SUPPORT
#define MF_ANALOG_SUPPORT 1
#define MF_ANALOG_SUPPORT 1
#endif
#ifndef MF_OUTPUT_SHIFTER_SUPPORT
#define MF_OUTPUT_SHIFTER_SUPPORT 1
Expand Down Expand Up @@ -52,13 +52,13 @@
#define STEPPER_SPEED 400 // 300 already worked, 467, too?
#define STEPPER_ACCEL 800

#define MOBIFLIGHT_TYPE "MobiFlight Uno"
#define MOBIFLIGHT_SERIAL "0987654321"
#define MOBIFLIGHT_NAME "MobiFlight Uno"
#define EEPROM_SIZE 1024 // EEPROMSizeUno
#define MEMLEN_CONFIG 286 // max. size for config which wil be stored in EEPROM
#define MEMLEN_NAMES_BUFFER 220 // max. size for configBuffer, contains only names from inputs
#define MF_MAX_DEVICEMEM 300 // max. memory size for devices
#define MOBIFLIGHT_TYPE "MobiFlight Uno"
#define MOBIFLIGHT_SERIAL "0987654321"
#define MOBIFLIGHT_NAME "MobiFlight Uno"
#define EEPROM_SIZE 1024 // EEPROMSizeUno
#define MEMLEN_CONFIG 286 // max. size for config which wil be stored in EEPROM
// #define MEMLEN_NAMES_BUFFER 220 // max. size for configBuffer, contains only names from inputs
#define MF_MAX_DEVICEMEM 300 // max. memory size for devices

#define RANDOM_SEED_INPUT A0

Expand Down
134 changes: 68 additions & 66 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
#include "Output.h"

#if MF_ANALOG_SUPPORT == 1
#include "Analog.h"
#include "Analog.h"
#endif
#if MF_INPUT_SHIFTER_SUPPORT == 1
#include "InputShifter.h"
#include "InputShifter.h"
#endif
#if MF_SEGMENT_SUPPORT == 1
#include "LedSegment.h"
#include "LedSegment.h"
#endif
#if MF_STEPPER_SUPPORT == 1
#include "Stepper.h"
#include "Stepper.h"
#endif
#if MF_SERVO_SUPPORT == 1
#include "Servos.h"
#include "Servos.h"
#endif
#if MF_LCD_SUPPORT == 1
#include "LCDDisplay.h"
#include "LCDDisplay.h"
#endif
#if MF_OUTPUT_SHIFTER_SUPPORT == 1
#include "OutputShifter.h"
#include "OutputShifter.h"
#endif
#if MF_MUX_SUPPORT == 1
#include "MFMuxDriver.h"
Expand All @@ -49,6 +49,7 @@
#define STRINGIZER(arg) #arg
#define STR_VALUE(arg) STRINGIZER(arg)
#define VERSION STR_VALUE(BUILD_VERSION)

MFEEPROM MFeeprom;

#if MF_MUX_SUPPORT == 1
Expand All @@ -65,9 +66,11 @@ 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;
// There was an error here, nameBuffer[] had size MEM_LEN_CONFIG instead of MEMLEN_NAMES_BUFFER!

char devNameBuffer[MAX_DEVNAME_LEN + 1] = "";
uint16_t configLength = 0;
boolean configActivated = false;

void resetConfig();
void readConfig();
Expand Down Expand Up @@ -117,7 +120,7 @@ void OnSetConfig()
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
MFeeprom.write_block(MEM_OFFSET_CONFIG + configLength, cfg, cfgLen + 1); // save the received config string including the terminating NULL (+1) to EEPROM
configLength += cfgLen;
cmdMessenger.sendCmd(kStatus, configLength);
} else
Expand Down Expand Up @@ -170,17 +173,17 @@ void OnResetConfig()
void OnSaveConfig()
{
cmdMessenger.sendCmd(kConfigSaved, F("OK"));
// Uncomment the if{} part to reset and load the config via serial terminal for testing w/o the GUI
// 1: Type "13" to reset the config
// 2: Type "14" to get the config length
// 3: Type "16" to load the config
/*
if (readConfigLength())
{
readConfig();
_activateConfig();
}
*/
// Uncomment the if{} part to reset and load the config via serial terminal for testing w/o the GUI
// 1: Type "13" to reset the config
// 2: Type "14" to get the config length
// 3: Type "16" to load the config
/*
if (readConfigLength())
{
readConfig();
_activateConfig();
}
*/
}

void OnActivateConfig()
Expand All @@ -207,20 +210,17 @@ uint8_t readUintFromEEPROM(volatile uint16_t *addreeprom)
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)
// reads a string from EEPROM at given address which is ':' terminated and saves it in the device name buffer
char *readNameFromEEPROM(uint16_t addreeprom)
{
char temp = 0;
char ch = 0;
uint8_t pos = 0;
do {
temp = MFeeprom.read_char((*addreeprom)++); // read the first character
buffer[(*addrbuffer)++] = temp; // save character and locate next buffer position
if (*addrbuffer >= MEMLEN_NAMES_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;
ch = MFeeprom.read_char(addreeprom++);
devNameBuffer[pos++] = ch;
} while (ch != ':' && pos < MAX_DEVNAME_LEN);
devNameBuffer[pos++] = 0x00; // replace ':' by NULL, terminates the string
return devNameBuffer;
}

// reads the EEPRROM until end of command which ':' terminated
Expand All @@ -240,12 +240,12 @@ 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
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;
Expand All @@ -254,9 +254,10 @@ void readConfig()
{
switch (command) {
case kTypeButton:
params[0] = readUintFromEEPROM(&addreeprom); // 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
params[0] = readUintFromEEPROM(&addreeprom); // Pin number
Button::Add(params[0], addreeprom);
// copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to nameBuffer and set to next free memory location
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;

case kTypeOutput:
Expand Down Expand Up @@ -311,20 +312,20 @@ void readConfig()
#endif

case kTypeEncoderSingleDetent:
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // 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
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
Encoder::Add(params[0], params[1], 0, addreeprom); // 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);
break;

case kTypeEncoder:
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = readUintFromEEPROM(&addreeprom); // 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
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = readUintFromEEPROM(&addreeprom); // type
Encoder::Add(params[0], params[1], params[2], addreeprom); // 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);
break;

#if MF_LCD_SUPPORT == 1
Expand All @@ -339,11 +340,11 @@ void readConfig()

#if MF_ANALOG_SUPPORT == 1
case kTypeAnalogInput:
params[0] = readUintFromEEPROM(&addreeprom); // pin number
params[1] = readUintFromEEPROM(&addreeprom); // 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
params[0] = readUintFromEEPROM(&addreeprom); // pin number
params[1] = readUintFromEEPROM(&addreeprom); // sensitivity
Analog::Add(params[0], addreeprom, 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);
break;
#endif

Expand All @@ -364,9 +365,9 @@ void readConfig()
params[1] = readUintFromEEPROM(&addreeprom); // clock Pin
params[2] = readUintFromEEPROM(&addreeprom); // data Pin
params[3] = readUintFromEEPROM(&addreeprom); // 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
InputShifter::Add(params[0], params[1], params[2], params[3], addreeprom);
// 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);
break;
#endif

Expand All @@ -393,8 +394,9 @@ void readConfig()
params[4] = readUintFromEEPROM(&addreeprom); // Sel3 pin
MUX.attach(params[1], params[2], params[3], params[4]);
params[5] = readUintFromEEPROM(&addreeprom); // 8-bit registers (1-2)
DigInMux::Add(params[0], params[5], &nameBuffer[addrbuffer]);
copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer);
DigInMux::Add(params[0], params[5], addreeprom);
// copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer);
copy_success = readEndCommandFromEEPROM(&addreeprom);

// cmdMessenger.sendCmd(kDebug, F("Mux loaded"));
break;
Expand All @@ -405,8 +407,8 @@ void readConfig()
}
command = readUintFromEEPROM(&addreeprom);
} while (command && copy_success);
if (!copy_success) { // too much/long names for input devices
nameBuffer[MEMLEN_NAMES_BUFFER - 1] = 0x00; // terminate the last copied (part of) string with 0x00
if (!copy_success) { // too much/long names for input devices
// nameBuffer[MEMLEN_NAMES_BUFFER - 1] = 0x00; // terminate the last copied (part of) string with 0x00
cmdMessenger.sendCmd(kStatus, F("Failure on reading config"));
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/MF_Analog/Analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ namespace Analog
MFAnalog *analog[MAX_ANALOG_INPUTS];
uint8_t analogRegistered = 0;

void handlerOnAnalogChange(int value, uint8_t pin, const char *name)
void handlerOnAnalogChange(int value, uint8_t pin, uint16_t nameAddr)
{
const char *name = (nameAddr == 0xFFFF ? "AnalogInput" : readNameFromEEPROM(nameAddr));
cmdMessenger.sendCmdStart(kAnalogChange);
cmdMessenger.sendCmdArg(name);
cmdMessenger.sendCmdArg(value);
cmdMessenger.sendCmdEnd();
};

void Add(uint8_t pin, char const *name, uint8_t sensitivity)
void Add(uint8_t pin, uint16_t nameAddr, uint8_t sensitivity)
{
if (analogRegistered == MAX_ANALOG_INPUTS)
return;
Expand All @@ -32,7 +33,7 @@ namespace Analog
cmdMessenger.sendCmd(kStatus, F("AnalogIn does not fit in Memory"));
return;
}
analog[analogRegistered] = new (allocateMemory(sizeof(MFAnalog))) MFAnalog(pin, name, sensitivity);
analog[analogRegistered] = new (allocateMemory(sizeof(MFAnalog))) MFAnalog(pin, nameAddr, sensitivity);
MFAnalog::attachHandler(handlerOnAnalogChange);
analogRegistered++;
#ifdef DEBUG2CMDMESSENGER
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Analog/Analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdint.h>
namespace Analog
{
void Add(uint8_t pin, char const *name = "AnalogInput", uint8_t sensitivity = 3);
void Add(uint8_t pin, uint16_t nameAddr = 0xFFFF, uint8_t sensitivity = 3); // "AnalogInput"
void Clear();
void read();
void readAverage();
Expand Down
Loading

0 comments on commit 6a9dcff

Please sign in to comment.