diff --git a/Template/MFCustomDevice.cpp b/Template/MFCustomDevice.cpp index 733f556..849f391 100644 --- a/Template/MFCustomDevice.cpp +++ b/Template/MFCustomDevice.cpp @@ -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; /* ********************************************************************************** @@ -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; } @@ -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; @@ -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) @@ -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. @@ -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. @@ -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 @@ -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. diff --git a/Template/MFCustomDevice.h b/Template/MFCustomDevice.h index f6fb5d4..7ad66f4 100644 --- a/Template/MFCustomDevice.h +++ b/Template/MFCustomDevice.h @@ -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; diff --git a/Template/MFCustomDevicesConfig.h b/Template/MFCustomDevicesConfig.h new file mode 100644 index 0000000..a3a4cc8 --- /dev/null +++ b/Template/MFCustomDevicesConfig.h @@ -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:"}; diff --git a/Template/MyCustomDevice_platformio.ini b/Template/MyCustomDevice_platformio.ini index 9ba1df4..5a43206 100644 --- a/Template/MyCustomDevice_platformio.ini +++ b/Template/MyCustomDevice_platformio.ini @@ -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 =