EEPROM-Storage Library
The EEPROM Storage library provides the ability to access variables stored in EEPROM just as if they were stored in RAM.
 
Loading...
Searching...
No Matches
EEPROM-Base.h
Go to the documentation of this file.
1// Copyright © 2017-2025 Daniel Porrey. All Rights Reserved.
2//
3// This file is part of the EEPROM-Storage library.
4//
5// EEPROM-Storage library is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// EEPROM-Storage library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with EEPROM-Storage library. If not,
17// see http://www.gnu.org/licenses/.
18//
19#pragma once
20#ifndef EEPROM_BASE_H
21#define EEPROM_BASE_H
22
28//
29// Cross-compatable with Arduino, GNU C++ for tests, and Particle.
30//
31#if defined(ARDUINO) && ARDUINO >= 100
32 #include <Arduino.h>
33 #include <EEPROM.h>
34#elif defined(PARTICLE)
35 #include <Particle.h>
36#endif
37
38#include "EEPROM-Util.h"
39#include "EEPROM-Checksum.h"
40
52template <typename T>
54{
55 public:
60 EEPROMBase(const uint address)
61 {
62 this->_address = this->normalizeAddress(address);
63 }
64
70 EEPROMBase(const uint address, T defaultValue) : EEPROMBase(address)
71 {
72 this->_defaultValue = defaultValue;
73 }
74
80 operator T()
81 {
82 return this->get();
83 }
84
92 byte operator[] (const uint index)
93 {
94 uint address = this->normalizeAddress(this->getAddress() + index);
95 return EEPROM[address];
96 }
97
103 {
104 T oldValue = this->get();
105 this->set(oldValue + 1);
106 return oldValue;
107 }
108
114 {
115 return this->set(this->get() + 1);
116 }
117
123 {
124 T oldValue = this->get();
125 this->set(this->get() - 1);
126 return oldValue;
127 }
128
134 {
135 return this->set(this->get() - 1);
136 }
137
143 T operator += (T const& value)
144 {
145 return this->set(this->get() + value);
146 }
147
153 T operator -= (T const& value)
154 {
155 return this->set(this->get() - value);
156 }
157
163 T operator *= (T const& value)
164 {
165 return this->set(this->get() * value);
166 }
167
173 T operator /= (T const& value)
174 {
175 return this->set(this->get() / value);
176 }
177
183 T operator ^= (T const& value)
184 {
185 return this->set(this->get() ^ value);
186 }
187
193 T operator %= (T const& value)
194 {
195 return this->set(this->get() % value);
196 }
197
203 T operator &= (T const& value)
204 {
205 return this->set(this->get() & value);
206 }
207
213 T operator |= (T const& value)
214 {
215 return this->set(this->get() | value);
216 }
217
223 T operator <<= (T const& value)
224 {
225 return this->set(this->get() << value);
226 }
227
233 T operator >>= (T const& value)
234 {
235 return this->set(this->get() >> value);
236 }
237
242 virtual T get() const
243 {
244 //
245 // Use default initialization.
246 //
247 return T{};
248 }
249
255 virtual T set(T const& value)
256 {
257 return value;
258 }
259
264 T read() const
265 {
266 T returnValue;
267
268 //
269 // Check if the variable has been set or not
270 // by comparing the value to the not set value
271 // specified in the constructor.
272 //
273 if (this->isInitialized())
274 {
275 //
276 // Get the variable from EEPROM
277 // using the address this->_address.
278 //
279 EEPROM.get(this->_address, returnValue);
280 }
281 else
282 {
283 //
284 // Return the default value.
285 //
286 returnValue = this->_defaultValue;
287 }
288
289 return returnValue;
290 }
291
296 void write(T const& value) const
297 {
298 //
299 // Write the value to EEPROM using the put method.
300 // Put uses EEPROM.update() to perform the write
301 // so it will not rewrite the value if it didn't
302 // change.
303 //
304 EEPROM.put(this->_address, value);
305
306 //
307 // Write the checksum.
308 //
309 byte checksum = Checksum<T>::get(value);
310 EEPROMUtil.updateEEPROM(this->checksumAddress(), checksum);
311 }
312
318 bool isInitialized() const
319 {
320 return (this->checksum() == this->checksumByte());
321 }
322
328 uint length() const
329 {
330 //
331 // The extra byte is the checksum byte.
332 //
333 return this->size() + 1;
334 }
335
342 uint size() const
343 {
344 //
345 // The extra byte is the checksum byte.
346 //
347 return sizeof(T);
348 }
349
355 void unset(byte unsetValue = UNSET_VALUE)
356 {
357 for (uint i = 0; i < this->length(); i++)
358 {
359 uint address = this->normalizeAddress(this->_address + i);
360 EEPROMUtil.updateEEPROM(address, unsetValue);
361 }
362 }
363
368 uint checksumAddress() const
369 {
370 return this->normalizeAddress(this->_address + this->length() - 1);
371 }
372
377 byte checksumByte() const
378 {
379 return EEPROM.read(this->checksumAddress());
380 }
381
386 byte checksum() const
387 {
388 return Checksum<T>::getEEPROM(this->getAddress(), sizeof(T));
389 }
390
394 void copyTo(byte* data, uint length) const
395 {
396 for (uint i = 0; i < length; i++)
397 {
398 uint address = this->normalizeAddress(this->_address + i);
399 data[i] = EEPROM[address];
400 }
401 }
402
407 uint getAddress() const
408 {
409 return this->_address;
410 }
411
417 uint nextAddress() const
418 {
419 return this->normalizeAddress(this->getAddress() + this->length());
420 }
421
427 {
428 return this->_defaultValue;
429 }
430
431 protected:
436 uint _address = 0;
437
445
451 byte computeChecksum(T value);
452
458 uint normalizeAddress(uint address) const
459 {
460 return min(address, EEPROM.length() - 1);
461 }
462};
463#endif
This file contains the Checksum definition.
This file contains the EEPROMDisplayClass definition.
#define UNSET_VALUE
Defines the default value used when clearing the EEPROM memory.
Definition EEPROM-Vars.h:39
static byte get(byte *data, uint length)
Calculate the checksum of a byte array.
static byte getEEPROM(uint address, uint length)
Calculates the checksum from data in the EEPROM.
Base class to wrap an EEPROM variable.
Definition EEPROM-Base.h:54
uint checksumAddress() const
Gets the address of the checksum byte in EEPROM for this variable.
void write(T const &value) const
Write the value to the EEPROM using the address in this instance.
byte computeChecksum(T value)
Computes the checksum of the given value.
uint nextAddress() const
Gets the next EEPROM address after this variable.
T _defaultValue
The default value.
T operator|=(T const &value)
Bitwise OR assignment operator.
T read() const
Read the variable value from the EEPROM using the address in this variable.
uint _address
The address of this variable in the EEPROM.
T operator+=(T const &value)
Addition assignment operator.
bool isInitialized() const
Checks whether the EEPROM variable has been initialized.
uint size() const
Returns the number of EEPROM bytes used by T.
T operator/=(T const &value)
Division assignment operator.
virtual T set(T const &value)
Set the variable value.
T operator<<=(T const &value)
Bitwise left shift assignment operator.
uint normalizeAddress(uint address) const
Normalize the given EEPROM address to ensure it is within valid range.
T operator-=(T const &value)
Subtraction assignment operator.
T operator^=(T const &value)
Bitwise XOR assignment operator.
void copyTo(byte *data, uint length) const
Copy the EEPROM bytes of this instance to a byte array.
byte operator[](const uint index)
Treating the variable as a byte array, get the byte at position index where index is between 0 and le...
Definition EEPROM-Base.h:92
T operator++()
Prefix increment operator.
T operator>>=(T const &value)
Bitwise right shift assignment operator.
virtual T get() const
Get the variable value.
T operator--()
Postfix decrement operator.
EEPROMBase(const uint address)
Initialize an instance of EEPROMBase<T> with the specified address.
Definition EEPROM-Base.h:60
byte checksumByte() const
Gets the stored checksum byte.
void unset(byte unsetValue=UNSET_VALUE)
Unset the variable.
T getDefaultValue() const
Gets the default value for this instance.
T operator&=(T const &value)
Bitwise AND assignment operator.
T operator*=(T const &value)
Multiplication assignment operator.
byte checksum() const
Calculate the checksum of the data in the EEPROM for this instance.
uint length() const
Returns the number of EEPROM bytes used.
uint getAddress() const
Get the EEPROM address of the variable.
EEPROMBase(const uint address, T defaultValue)
Initialize an instance of EEPROMBase with the specified address and default value.
Definition EEPROM-Base.h:70
T operator%=(T const &value)
Modulus assignment operator.