ESP_EEPROM
ESP_EEPROM.h
1 /*
2  ESP_EEPROM.h - improved esp8266 EEPROM emulation
3 
4  Copyright (c) 2018 James Watson. All rights reserved.
5 
6  Based on API defined for ESP8266 EEPROM library, part of standard
7  esp8266 core for Arduino environment by Ivan Grokhotkov.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #ifndef ESP_EEPROM_h
25 #define ESP_EEPROM_h
26 
27 #include <stddef.h>
28 #include <stdint.h>
29 
33 const size_t EEPROM_MIN_SIZE = 16;
34 
35 class EEPROMClass {
36 public:
37 
38  EEPROMClass(void);
39 
40  void begin(size_t size);
41  uint8_t read(int const address);
42  void write(int const address, uint8_t const val);
43  bool commit();
44  bool commitReset();
45  bool wipe();
46  int percentUsed();
47  void end();
48 
63  template<typename T>
64  T &get(int const address, T &v) {
65  if (_data && (address >= 0) && (address + sizeof(T) <= _size)) {
66  memcpy((uint8_t*) &v, _data + address, sizeof(T));
67  }
68  return v;
69  }
70 
87  template<typename T>
88  const T &put(int const address, const T &v) {
89  if (_data && (address >= 0) && (address + sizeof(T) <= _size)) {
90 
91  // only flag as dirty and copied if different - if already dirty, just get on with copy
92  if (_dirty
93  || memcmp(_data + address, (const uint8_t*) &v, sizeof(T))
94  != 0) {
95  _dirty = true;
96  memcpy(_data + address, (const uint8_t*) &v, sizeof(T));
97  }
98  }
99  return v;
100  }
101 
107  size_t length() {
108  return _size;
109  }
110 
111 private:
112  EEPROMClass(uint32_t sector);
113 
114  uint32_t _sector;
115  uint8_t* _data;
116  uint32_t _size;
117  uint16_t _bitmapSize;
118  uint8_t* _bitmap;
119  uint16_t _offset;
120  bool _dirty;
121 
122  uint16_t offsetFromBitmap();
123  int flagUsedOffset();
124  uint16_t computeBitmapSize(size_t size);
125 };
126 
127 #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
128 extern EEPROMClass EEPROM;
129 #endif
130 
131 #endif
132 
int percentUsed()
Returns the percentage of EEPROM flash memory area that has been used by copies of our EEPROM data...
Definition: ESP_EEPROM.cpp:234
const T & put(int const address, const T &v)
Write data to the EEPROM buffer.
Definition: ESP_EEPROM.h:88
EEPROMClass(void)
Create an instance of the EEPROM class based on the default EEPROM flash sector.
Definition: ESP_EEPROM.cpp:133
void write(int const address, uint8_t const val)
Write a byte of data to an address within the the library&#39;s EEPROM data buffer.
Definition: ESP_EEPROM.cpp:296
bool wipe()
Force an immediate erase of the flash sector - but nothing is written.
Definition: ESP_EEPROM.cpp:421
uint8_t read(int const address)
Read a byte of data from an offset in the buffered EEPROM data.
Definition: ESP_EEPROM.cpp:277
size_t length()
Get the size of the EEPROM buffer.
Definition: ESP_EEPROM.h:107
The ESP does not have a genuine EEPROM memory so this needs to be emulated using a segment of flash m...
Definition: ESP_EEPROM.h:35
bool commitReset()
Perform an erase of the flash sector before committing the data.
Definition: ESP_EEPROM.cpp:315
void begin(size_t size)
Initialise the EEPROM system, reading from flash if there appears to be suitable data already there...
Definition: ESP_EEPROM.cpp:159
void end()
Free up storage used by the library.
Definition: ESP_EEPROM.cpp:248
bool commit()
Write the EEPROM data to the flash memory.
Definition: ESP_EEPROM.cpp:339