Skip to content

Commit

Permalink
Merge branch 'main' into wo_NameBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
elral committed Dec 9, 2022
2 parents 145e4b8 + b1e0ddb commit 9895bd1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/CommandMessenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 8 additions & 9 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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 ':'
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 3 additions & 22 deletions src/MF_Modules/MFEEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <Arduino.h>
#include "MFEEPROM.h"
#include <EEPROM.h>

MFEEPROM::MFEEPROM() {}

Expand All @@ -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;
}

Expand Down
48 changes: 41 additions & 7 deletions src/MF_Modules/MFEEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,56 @@

#pragma once

#include <stdint.h>
#include <EEPROM.h>

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 <typename T>
bool read_block(uint16_t adr, T &t)
{
if (adr + sizeof(T) > _eepromLength) return false;
EEPROM.get(adr, t);
return true;
}

template <typename T>
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 <typename T>
const bool write_block(uint16_t adr, const T &t)
{
if (adr + sizeof(T) > _eepromLength) return false;
EEPROM.put(adr, t);
return true;
}

template <typename T>
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

0 comments on commit 9895bd1

Please sign in to comment.