From 5d1e90c821375ad134163542e1b5c957c75d1ba4 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Fri, 5 Aug 2022 10:19:14 +0200 Subject: [PATCH] Optimization of MFEEPROM class (#187) * Optimization of MFEEPROM class Fixes #186 * block gets not partly written if size is exceeded * move if condition before loop in read_block --- src/MF_Modules/MFEEPROM.cpp | 24 +++++++++++++++--------- src/MF_Modules/MFEEPROM.h | 9 +++++---- src/mobiflight.cpp | 4 ++++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 120b1f03..d1f1b4bc 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -8,41 +8,47 @@ #include "MFEEPROM.h" #include -MFEEPROM::MFEEPROM() +MFEEPROM::MFEEPROM() {} + +void MFEEPROM::init(void) { - eepromLength = EEPROM.length(); + _eepromLength = EEPROM.length(); } uint16_t MFEEPROM::get_length(void) { - return eepromLength; + return _eepromLength; } -void MFEEPROM::read_block(uint16_t adr, char data[], uint16_t len) +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; } -void MFEEPROM::write_block(uint16_t adr, char data[], uint16_t len) +bool MFEEPROM::write_block(uint16_t adr, char data[], uint16_t len) { - if (adr + len >= eepromLength) return; + 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) { - if (adr >= eepromLength) return 0; + if (adr >= _eepromLength) return 0; return EEPROM.read(adr); } -void MFEEPROM::write_byte(uint16_t adr, char data) +bool MFEEPROM::write_byte(uint16_t adr, char data) { - if (adr >= eepromLength) return; + if (adr >= _eepromLength) return false; EEPROM.put(adr, data); + return true; } // MFEEPROM.cpp \ No newline at end of file diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index b3d8193b..1e72c6eb 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -13,14 +13,15 @@ class MFEEPROM public: MFEEPROM(); + void init(void); uint16_t get_length(void); - void read_block(uint16_t addr, char data[], uint16_t len); - void write_block(uint16_t addr, char data[], uint16_t len); + 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); - void write_byte(uint16_t adr, char data); + bool write_byte(uint16_t adr, char data); private: - uint16_t eepromLength = 0; + uint16_t _eepromLength = 0; }; // MFEEPROM.h \ No newline at end of file diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 43bf085d..ec0ef6c4 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -8,6 +8,7 @@ #include "mobiflight.h" #include "Button.h" #include "Encoder.h" +#include "MFEEPROM.h" #if MF_ANALOG_SUPPORT == 1 #include "Analog.h" #endif @@ -69,6 +70,8 @@ typedef struct { lastUpdate_t lastUpdate; +extern MFEEPROM MFeeprom; + void initPollIntervals(void) { // Init Time Gap between Inputs, do not read at the same loop @@ -145,6 +148,7 @@ void ResetBoard() void setup() { Serial.begin(115200); + MFeeprom.init(); attachCommandCallbacks(); cmdMessenger.printLfCr(); ResetBoard();