Skip to content

Commit

Permalink
Optimization of MFEEPROM class (MobiFlight#187)
Browse files Browse the repository at this point in the history
* Optimization of MFEEPROM class
Fixes MobiFlight#186

* block gets not partly written if size is exceeded

* move if condition before loop in read_block
  • Loading branch information
elral authored Aug 5, 2022
1 parent e93bd16 commit 5d1e90c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
24 changes: 15 additions & 9 deletions src/MF_Modules/MFEEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,47 @@
#include "MFEEPROM.h"
#include <EEPROM.h>

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
9 changes: 5 additions & 4 deletions src/MF_Modules/MFEEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/mobiflight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -145,6 +148,7 @@ void ResetBoard()
void setup()
{
Serial.begin(115200);
MFeeprom.init();
attachCommandCallbacks();
cmdMessenger.printLfCr();
ResetBoard();
Expand Down

0 comments on commit 5d1e90c

Please sign in to comment.