diff --git a/src/CommandMessenger.cpp b/src/CommandMessenger.cpp index 0d0dcd08..e7ad8b51 100644 --- a/src/CommandMessenger.cpp +++ b/src/CommandMessenger.cpp @@ -78,7 +78,7 @@ void attachCommandCallbacks() cmdMessenger.attach(kSetLcdDisplayI2C, LCDDisplay::OnSet); #endif -#if MF_OUTPUT_SHIFTER_SUPPORT +#if MF_OUTPUT_SHIFTER_SUPPORT == 1 cmdMessenger.attach(kSetShiftRegisterPins, OutputShifter::OnSet); #endif diff --git a/src/Config.cpp b/src/Config.cpp index 4d27dbff..a96e6ca6 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -76,16 +76,15 @@ bool readConfigLength() uint16_t addreeprom = MEM_OFFSET_CONFIG; uint16_t length = MFeeprom.get_length(); configLength = 0; - do { - temp = MFeeprom.read_char(addreeprom++); + + while (MFeeprom.read_byte(addreeprom++) != 0x00) { configLength++; if (addreeprom > length) // abort if EEPROM size will be exceeded { cmdMessenger.sendCmd(kStatus, F("Loading config failed")); // text or "-1" like config upload? return false; } - } while (temp != 0x00); // reads until NULL - configLength--; + } return true; } @@ -194,7 +193,7 @@ uint8_t readUintFromEEPROM(volatile uint16_t *addreeprom) char params[4] = {0}; // max 3 (255) digits NULL terminated uint8_t counter = 0; do { - params[counter++] = MFeeprom.read_char((*addreeprom)++); // read character from eeprom and locate next buffer and eeprom location + params[counter++] = MFeeprom.read_byte((*addreeprom)++); // read character from eeprom and locate next buffer and eeprom location } while (params[counter - 1] != '.' && counter < sizeof(params)); // reads until limiter '.' and for safety reason not more then size of params[] params[counter - 1] = 0x00; // replace '.' by NULL to terminate the string return atoi(params); @@ -206,7 +205,7 @@ bool readEndCommandFromEEPROM(uint16_t *addreeprom) char temp = 0; uint16_t length = MFeeprom.get_length(); do { - temp = MFeeprom.read_char((*addreeprom)++); + temp = MFeeprom.read_byte((*addreeprom)++); if (*addreeprom > length) // abort if EEPROM size will be exceeded return false; } while (temp != ':'); // reads until limiter ':' @@ -386,9 +385,9 @@ void OnGetConfig() setLastCommandMillis(); cmdMessenger.sendCmdStart(kInfo); if (configLength > 0) { - cmdMessenger.sendCmdArg(MFeeprom.read_char(MEM_OFFSET_CONFIG)); + cmdMessenger.sendCmdArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG)); for (uint16_t i = 1; i < configLength; i++) { - cmdMessenger.sendArg(MFeeprom.read_char(MEM_OFFSET_CONFIG + i)); + cmdMessenger.sendArg((char)MFeeprom.read_byte(MEM_OFFSET_CONFIG + i)); } } cmdMessenger.sendCmdEnd(); @@ -444,7 +443,7 @@ void storeName() void restoreName() { - if (MFeeprom.read_char(MEM_OFFSET_NAME) != '#') + if (MFeeprom.read_byte(MEM_OFFSET_NAME) != '#') return; MFeeprom.read_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1); diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index d1f1b4bc..4dddc60f 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -6,7 +6,6 @@ #include #include "MFEEPROM.h" -#include MFEEPROM::MFEEPROM() {} @@ -20,34 +19,16 @@ uint16_t MFEEPROM::get_length(void) return _eepromLength; } -bool MFEEPROM::read_block(uint16_t adr, char data[], uint16_t len) -{ - if (adr + len > _eepromLength) return false; - for (uint16_t i = 0; i < len; i++) { - data[i] = read_char(adr + i); - } - return true; -} - -bool MFEEPROM::write_block(uint16_t adr, char data[], uint16_t len) -{ - if (adr + len > _eepromLength) return false; - for (uint16_t i = 0; i < len; i++) { - EEPROM.put(adr + i, data[i]); - } - return true; -} - -char MFEEPROM::read_char(uint16_t adr) +uint8_t MFEEPROM::read_byte(uint16_t adr) { if (adr >= _eepromLength) return 0; return EEPROM.read(adr); } -bool MFEEPROM::write_byte(uint16_t adr, char data) +bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) { if (adr >= _eepromLength) return false; - EEPROM.put(adr, data); + EEPROM.write(adr, data); return true; } diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 1e72c6eb..064602fc 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -6,22 +6,56 @@ #pragma once -#include +#include class MFEEPROM { +private: + uint16_t _eepromLength = 0; public: MFEEPROM(); void init(void); uint16_t get_length(void); - bool read_block(uint16_t addr, char data[], uint16_t len); - bool write_block(uint16_t addr, char data[], uint16_t len); - char read_char(uint16_t adr); - bool write_byte(uint16_t adr, char data); + uint8_t read_byte(uint16_t adr); + bool write_byte(uint16_t adr, const uint8_t data); -private: - uint16_t _eepromLength = 0; + template + bool read_block(uint16_t adr, T &t) + { + if (adr + sizeof(T) > _eepromLength) return false; + EEPROM.get(adr, t); + return true; + } + + template + bool read_block(uint16_t adr, T &t, uint16_t len) + { + if (adr + len > _eepromLength) return false; + uint8_t *ptr = (uint8_t*) &t; + for (uint16_t i = 0; i < len; i++) { + *ptr++ = EEPROM.read(adr + i); + } + return true; + } + + template + const bool write_block(uint16_t adr, const T &t) + { + if (adr + sizeof(T) > _eepromLength) return false; + EEPROM.put(adr, t); + return true; + } + + template + const bool write_block(uint16_t adr, const T &t, uint16_t len) + { + if (adr + len > _eepromLength) return false; + for (uint16_t i = 0; i < len; i++) { + EEPROM.put(adr + i, t[i]); + } + return true; + } }; // MFEEPROM.h \ No newline at end of file