@ -7,75 +7,81 @@
# define __EEP_H__
# include "Arduino.h"
# include <EEPROM .h>
# include <Preferences .h>
# ifdef ESP32
# include <nvs_flash.h>
# 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 ( ) ;
}
} ;