From c6b88e685827783db87e1cf7fba3e9fcd58e3016 Mon Sep 17 00:00:00 2001 From: Sajani <102922919+Nini12345678901@users.noreply.github.com> Date: Sun, 2 Oct 2022 15:59:55 -0400 Subject: [PATCH] Update eep.h Revised eep.h for esp8266 by using a different library for the issue. Replaced EEPROM.h with Preferences library and changed the methods respective to Preferences.h for the write and read functions. The revised changes are than committed to the Preferences library. --- tools/esp8266/eep.h | 104 ++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/tools/esp8266/eep.h b/tools/esp8266/eep.h index 69aff7f5..500aa537 100644 --- a/tools/esp8266/eep.h +++ b/tools/esp8266/eep.h @@ -7,75 +7,81 @@ #define __EEP_H__ #include "Arduino.h" -#include +#include #ifdef ESP32 #include #endif +using namespace std; class eep { public: + + //initializing local variables to pass in for method parameters from Preferences.h + string nameFiller = ""; + bool onlyRead; + const char* label; + const char* keyVal; + uint8_t num; eep() { - + #ifdef ESP32 - if(!EEPROM.begin(4096)) { - nvs_flash_init(); - EEPROM.begin(4096); - } - #else - EEPROM.begin(4096); + Preferences.begin(nameFiller, onlyRead, label); #endif } + + //destructor ~eep() { - EEPROM.end(); + Preferences.end(); } + //following read methods initiated to read in incrementing the size and value void read(uint32_t addr, char *str, uint8_t length) { for(uint8_t i = 0; i < length; i ++) { - *(str++) = (char)EEPROM.read(addr++); + *(str++) = Preferences.putUChar(keyVal, num); } } void read(uint32_t addr, float *value) { uint8_t *p = (uint8_t*)value; for(uint8_t i = 0; i < 4; i ++) { - *(p++) = (uint8_t)EEPROM.read(addr++); + *(p++) = Preferences.getBytesLength(addr++); } } void read(uint32_t addr, bool *value) { uint8_t intVal = 0x00; - intVal = EEPROM.read(addr++); + intVal = (int)Preferences.getBytesLength(addr++); *value = (intVal == 0x01); } void read(uint32_t addr, uint8_t *value) { - *value = (EEPROM.read(addr++)); + *value = Preferences.getBytesLength(addr++); } void read(uint32_t addr, uint8_t data[], uint16_t length) { for(uint16_t i = 0; i < length; i ++) { - *(data++) = EEPROM.read(addr++); + *(data++) = Preferences.getBytesLength(addr++); } } void read(uint32_t addr, uint16_t *value) { - *value = (EEPROM.read(addr++) << 8); - *value |= (EEPROM.read(addr++)); + *value = (Preferences.getBytesLength(addr++) << 8); + *value |= (Preferences.getBytesLength(addr++)); } void read(uint32_t addr, uint16_t data[], uint16_t length) { for(uint16_t i = 0; i < length; i ++) { - *(data) = (EEPROM.read(addr++) << 8); - *(data++) |= (EEPROM.read(addr++)); + *(data) = (Preferences.getBytesLength(addr++) << 8); + *(data++) |= (Preferences.getBytesLength(addr++)); } } void read(uint32_t addr, uint32_t *value) { - *value = (EEPROM.read(addr++) << 24); - *value |= (EEPROM.read(addr++) << 16); - *value |= (EEPROM.read(addr++) << 8); - *value |= (EEPROM.read(addr++)); + *value = (Preferences.getBytesLength(addr++) << 24); + *value |= (Preferences.getBytesLength(addr++) << 16); + *value |= (Preferences.getBytesLength(addr++) << 8); + *value |= (Preferences.getBytesLength(addr++)); } void read(uint32_t addr, uint64_t *value) { @@ -84,77 +90,71 @@ class eep { uint32_t tmp; read(addr+4, &tmp); *value |= tmp; - /**value = (EEPROM.read(addr++) << 56); - *value |= (EEPROM.read(addr++) << 48); - *value |= (EEPROM.read(addr++) << 40); - *value |= (EEPROM.read(addr++) << 32); - *value |= (EEPROM.read(addr++) << 24); - *value |= (EEPROM.read(addr++) << 16); - *value |= (EEPROM.read(addr++) << 8); - *value |= (EEPROM.read(addr++));*/ } + //following write methods to write in incrementing the size and value void write(uint32_t addr, const char *str, uint8_t length) { for(uint8_t i = 0; i < length; i ++) { - EEPROM.write(addr++, str[i]); + Preferences.putString(addr++, str[i]); } } void write(uint32_t addr, uint8_t data[], uint16_t length) { for(uint16_t i = 0; i < length; i ++) { - EEPROM.write(addr++, data[i]); + Preferences.putInt(addr++, data[i]); } } void write(uint32_t addr, float value) { uint8_t *p = (uint8_t*)&value; for(uint8_t i = 0; i < 4; i ++) { - EEPROM.write(addr++, p[i]); + Preferences.putFloat(addr++, p[i]); } } void write(uint32_t addr, bool value) { uint8_t intVal = (value) ? 0x01 : 0x00; - EEPROM.write(addr++, intVal); + Preferences.putBool(addr++, intVal); } void write(uint32_t addr, uint8_t value) { - EEPROM.write(addr++, value); + Preferences.putUInt(addr++, value); } void write(uint32_t addr, uint16_t value) { - EEPROM.write(addr++, (value >> 8) & 0xff); - EEPROM.write(addr++, (value ) & 0xff); + Preferences.putUInt(addr++, (value >> 8) & 0xff); + Preferences.putUInt(addr++, (value ) & 0xff); } void write(uint32_t addr, uint16_t data[], uint16_t length) { for(uint16_t i = 0; i < length; i ++) { - EEPROM.write(addr++, (data[i] >> 8) & 0xff); - EEPROM.write(addr++, (data[i] ) & 0xff); + Preferences.putUInt(addr++, (data[i] >> 8) & 0xff); + Preferences.putUInt(addr++, (data[i] ) & 0xff); } } void write(uint32_t addr, uint32_t value) { - EEPROM.write(addr++, (value >> 24) & 0xff); - EEPROM.write(addr++, (value >> 16) & 0xff); - EEPROM.write(addr++, (value >> 8) & 0xff); - EEPROM.write(addr++, (value ) & 0xff); + Preferences.putUInt(addr++, (value >> 24) & 0xff); + Preferences.putUInt(addr++, (value >> 16) & 0xff); + Preferences.putUInt(addr++, (value >> 8) & 0xff); + Preferences.putUInt(addr++, (value ) & 0xff); } void write(uint32_t addr, uint64_t value) { - EEPROM.write(addr++, (value >> 56) & 0xff); - EEPROM.write(addr++, (value >> 48) & 0xff); - EEPROM.write(addr++, (value >> 40) & 0xff); - EEPROM.write(addr++, (value >> 32) & 0xff); - EEPROM.write(addr++, (value >> 24) & 0xff); - EEPROM.write(addr++, (value >> 16) & 0xff); - EEPROM.write(addr++, (value >> 8) & 0xff); - EEPROM.write(addr++, (value ) & 0xff); + Preferences.putUInt(addr++, (value >> 56) & 0xff); + Preferences.putUInt(addr++, (value >> 48) & 0xff); + Preferences.putUInt(addr++, (value >> 40) & 0xff); + Preferences.putUInt(addr++, (value >> 32) & 0xff); + Preferences.putUInt(addr++, (value >> 24) & 0xff); + Preferences.putUInt(addr++, (value >> 16) & 0xff); + Preferences.putUInt(addr++, (value >> 8) & 0xff); + Preferences.putUInt(addr++, (value ) & 0xff); } + //commit will be called to revise changes made to Preferences lib void commit(void) { - EEPROM.commit(); + Preferences.commit(); } };