Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config can be read additionally from Flash #8

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions Template/MFCustomDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#include "commandmessenger.h"
#include "allocateMem.h"
#include "MFEEPROM.h"
#ifdef HAS_CONFIG_IN_FLASH
#include "MFCustomDevicesConfig.h"
#else
const char CustomDeviceConfig[] PROGMEM = {};
#endif

extern MFEEPROM MFeeprom;

/* **********************************************************************************
Expand All @@ -22,18 +28,27 @@ extern MFEEPROM MFeeprom;
********************************************************************************** */
#define MEMLEN_STRING_BUFFER 40

// reads a string from EEPROM at given address which is '.' terminated and saves it to the buffer
bool MFCustomDevice::getStringFromEEPROM(uint16_t addreeprom, char *buffer)
// reads a string from EEPROM or Flash at given address which is '.' terminated and saves it to the buffer
bool MFCustomDevice::getStringFromMem(uint16_t addrMem, char *buffer, bool configFromFlash)
{
char temp = 0;
uint8_t counter = 0;
char temp = 0;
uint8_t counter = 0;
uint16_t length = MFeeprom.get_length();
do {
temp = MFeeprom.read_byte((addreeprom)++); // read the first character
buffer[counter++] = temp; // save character and locate next buffer position
if (counter >= MEMLEN_STRING_BUFFER) { // nameBuffer will be exceeded
return false; // abort copying to buffer
if (configFromFlash) {
temp = pgm_read_byte_near(CustomDeviceConfig + addrMem++);
if (addrMem > sizeof(CustomDeviceConfig))
return false;
} else {
temp = MFeeprom.read_byte(addrMem++);
if (addrMem > length)
return false;
}
buffer[counter++] = temp; // save character and locate next buffer position
if (counter >= MEMLEN_STRING_BUFFER) { // nameBuffer will be exceeded
return false; // abort copying to buffer
}
} while (temp != '.'); // reads until limiter '.' and locates the next free buffer position
} while (temp != '.'); // reads until limiter '.' and locates the next free buffer position
buffer[counter - 1] = 0x00; // replace '.' by NULL, terminates the string
return true;
}
Expand All @@ -45,14 +60,14 @@ MFCustomDevice::MFCustomDevice()

/* **********************************************************************************
Within the connector pins, a device name and a config string can be defined
These informations are stored in the EEPROM like for the other devices.
While reading the config from the EEPROM this function is called.
These informations are stored in the EEPROM or Flash like for the other devices.
While reading the config from the EEPROM or Flash this function is called.
It is the first function which will be called for the custom device.
If it fits into the memory buffer, the constructor for the customer device
will be called
********************************************************************************** */

void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig)
void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig, bool configFromFlash)
{
if (adrPin == 0) return;

Expand All @@ -65,11 +80,11 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi
uint8_t _pin1, _pin2, _pin3;

/* **********************************************************************************
Read the Type from the EEPROM, copy it into a buffer and evaluate it
Read the Type from the EEPROM or Flash, copy it into a buffer and evaluate it
The string get's NOT stored as this would need a lot of RAM, instead a variable
is used to store the type
********************************************************************************** */
getStringFromEEPROM(adrType, parameter);
getStringFromMem(adrType, parameter, configFromFlash);
if (strcmp(parameter, "MOBIFLIGHT_TEMPLATE") == 0)
_customType = MY_CUSTOM_DEVICE_1;
if (strcmp(parameter, "MOBIFLIGHT_TEMPLATE2") == 0)
Expand All @@ -85,10 +100,10 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi
return;
}
/* **********************************************************************************************
Read the pins from the EEPROM, copy them into a buffer
Read the pins from the EEPROM or Flash, copy them into a buffer
If you have set '"isI2C": true' in the device.json file, the first value is the I2C address
********************************************************************************************** */
getStringFromEEPROM(adrPin, parameter);
getStringFromMem(adrPin, parameter, configFromFlash);
/* **********************************************************************************************
Split the pins up into single pins. As the number of pins could be different between
multiple devices, it is done here.
Expand All @@ -101,9 +116,9 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi
_pin3 = atoi(params);

/* **********************************************************************************
Read the configuration from the EEPROM, copy it into a buffer.
Read the configuration from the EEPROM or Flash, copy it into a buffer.
********************************************************************************** */
getStringFromEEPROM(adrConfig, parameter);
getStringFromMem(adrConfig, parameter, configFromFlash);
/* **********************************************************************************
Split the config up into single parameter. As the number of parameters could be
different between multiple devices, it is done here.
Expand Down Expand Up @@ -142,10 +157,10 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi
}

/* **********************************************************************************************
Read the pins from the EEPROM, copy them into a buffer
Read the pins from the EEPROM or Flash, copy them into a buffer
If you have set '"isI2C": true' in the device.json file, the first value is the I2C address
********************************************************************************************** */
getStringFromEEPROM(adrPin, parameter);
getStringFromMem(adrPin, parameter, configFromFlash);
/* **********************************************************************************************
split the pins up into single pins, as the number of pins could be different between
multiple devices, it is done here
Expand All @@ -158,9 +173,9 @@ void MFCustomDevice::attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfi
_pin3 = atoi(params);

/* **********************************************************************************
Read the configuration from the EEPROM, copy it into a buffer.
Read the configuration from the EEPROM or Flash, copy it into a buffer.
********************************************************************************** */
getStringFromEEPROM(adrConfig, parameter);
getStringFromMem(adrConfig, parameter, configFromFlash);
/* **********************************************************************************
split the config up into single parameter. As the number of parameters could be
different between multiple devices, it is done here.
Expand Down
4 changes: 2 additions & 2 deletions Template/MFCustomDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class MFCustomDevice
{
public:
MFCustomDevice();
void attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig);
void attach(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig, bool configFromFlash = false);
void detach();
void update();
void set(int16_t messageID, char *setPoint);

private:
bool getStringFromEEPROM(uint16_t addreeprom, char *buffer);
bool getStringFromMem(uint16_t addreeprom, char *buffer, bool configFromFlash);
bool _initialized = false;
MyCustomClass *_mydevice;
uint8_t _pin1, _pin2, _pin3;
Expand Down
11 changes: 11 additions & 0 deletions Template/MFCustomDevicesConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

// Define your input custom devices and uncomment -DHAS_CONFIG_IN_FLASH
// in your MFCustomDevice_platformio.ini
const char CustomDeviceConfig[] PROGMEM =
{
"1.2.Button Flash:"
"8.3.4.0.Encoder Flash:"
"11.54.5.Analog Input Flash:"
"12.7.6.5.1.InputShifter Flash:"
"14.12.8.9.10.11.2.Multiplexer Flash:"};
1 change: 1 addition & 0 deletions Template/MyCustomDevice_platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build_flags =
-DMF_CUSTOMDEVICE_SUPPORT=1 ; Required for Custom Devices
;-DMF_CUSTOMDEVICE_HAS_UPDATE ; if the custom device needs to be updated, uncomment this. W/o the following define it will be done each loop()
;-DMF_CUSTOMDEVICE_POLL_MS=10 ; time in ms between updating custom device, uncomment this if custom device needs to be updated regulary
;-DHAS_CONFIG_IN_FLASH ; undefine this and add your configuration to MFCustomDevicesConfig.h to save the config in Flash !!Core FW version must be at least 2.5.2!!
-I./src/src/MF_CustomDevice ; don't change this one!
-I./Template ; Include files for your custom device, replace "Template" by your folder name
build_src_filter =
Expand Down
Loading