forked from thijse/Arduino-EEPROMEx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EEPROMex.h
191 lines (165 loc) · 5.67 KB
/
EEPROMex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
EEPROMEx.h - Extended EEPROM library
Copyright (c) 2012 Thijs Elenbaas. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EEPROMEX_h
#define EEPROMEX_h
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <inttypes.h>
#include <avr/eeprom.h>
#define EEPROMSizeATmega168 512
#define EEPROMSizeATmega328 1024
#define EEPROMSizeATmega1280 4096
#define EEPROMSizeATmega32u4 1024
#define EEPROMSizeAT90USB1286 4096
#define EEPROMSizeMK20DX128 2048
#define EEPROMSizeMK20DX256 2048
#define EEPROMSizeATSAMD21G18 16384
#define EEPROMSizeUno EEPROMSizeATmega328
#define EEPROMSizeUnoSMD EEPROMSizeATmega328
#define EEPROMSizeLilypad EEPROMSizeATmega328
#define EEPROMSizeDuemilanove EEPROMSizeATmega328
#define EEPROMSizePro EEPROMSizeATmega328
#define EEPROMSizeFio EEPROMSizeATmega328
#define EEPROMSizeMega EEPROMSizeATmega1280
#define EEPROMSizeDiecimila EEPROMSizeATmega168
#define EEPROMSizeNano EEPROMSizeATmega168
#define EEPROMSizeTeensy2 EEPROMSizeATmega32u4
#define EEPROMSizeLeonardo EEPROMSizeATmega32u4
#define EEPROMSizeMicro EEPROMSizeATmega32u4
#define EEPROMSizeEsplora EEPROMSizeATmega32u4
#define EEPROMSizeYun EEPROMSizeATmega32u4
#define EEPROMSizeTre EEPROMSizeATmega32u4
#define EEPROMSizeZero EEPROMSizeATSAMD21G18
#define EEPROMSizeTeensy2pp EEPROMSizeAT90USB1286
#define EEPROMSizeTeensy3 EEPROMSizeMK20DX128
#define EEPROMSizeTeensy31 EEPROMSizeMK20DX256
class EEPROMClassEx
{
public:
EEPROMClassEx();
bool isReady();
int writtenBytes();
void setMemPool(int base, int memSize);
void setMaxAllowedWrites(int allowedWrites);
int getAddress(int noOfBytes);
uint8_t read(int);
bool readBit(int, byte);
uint8_t readByte(int);
uint16_t readInt(int);
uint32_t readLong(int);
float readFloat(int);
double readDouble(int);
bool write(int, uint8_t);
bool writeBit(int , uint8_t, bool);
bool writeByte(int, uint8_t);
bool writeInt(int, uint16_t);
bool writeLong(int, uint32_t);
bool writeFloat(int, float);
bool writeDouble(int, double);
bool update(int, uint8_t);
bool updateBit(int , uint8_t, bool);
bool updateByte(int, uint8_t);
bool updateInt(int, uint16_t);
bool updateLong(int, uint32_t);
bool updateFloat(int, float);
bool updateDouble(int, double);
// Use template for other data formats
/**
* Template function to read multiple items of any type of variable, such as structs
*/
template <class T> int readBlock(int address, const T value[], int items)
{
unsigned int i;
for (i = 0; i < (unsigned int)items; i++)
readBlock<T>(address+(i*sizeof(T)),value[i]);
return i;
}
/**
* Template function to read any type of variable, such as structs
*/
template <class T> int readBlock(int address, const T& value)
{
eeprom_read_block((void*)&value, (const void*)address, sizeof(value));
return sizeof(value);
}
/**
* Template function to write multiple items of any type of variable, such as structs
*/
template <class T> int writeBlock(int address, const T value[], int items)
{
if (!isWriteOk(address+items*sizeof(T))) return 0;
unsigned int i;
for (i = 0; i < (unsigned int)items; i++)
writeBlock<T>(address+(i*sizeof(T)),value[i]);
return i;
}
/**
* Template function to write any type of variable, such as structs
*/
template <class T> int writeBlock(int address, const T& value)
{
if (!isWriteOk(address+sizeof(value))) return 0;
eeprom_write_block((void*)&value, (void*)address, sizeof(value));
return sizeof(value);
}
/**
* Template function to update multiple items of any type of variable, such as structs
* The EEPROM will only be overwritten if different. This will reduce wear.
*/
template <class T> int updateBlock(int address, const T value[], int items)
{
int writeCount=0;
if (!isWriteOk(address+items*sizeof(T))) return 0;
unsigned int i;
for (i = 0; i < (unsigned int)items; i++)
writeCount+= updateBlock<T>(address+(i*sizeof(T)),value[i]);
return writeCount;
}
/**
* Template function to update any type of variable, such as structs
* The EEPROM will only be overwritten if different. This will reduce wear.
*/
template <class T> int updateBlock(int address, const T& value)
{
int writeCount=0;
if (!isWriteOk(address+sizeof(value))) return 0;
const byte* bytePointer = (const byte*)(const void*)&value;
for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) {
if (read(address)!=*bytePointer) {
write(address, *bytePointer);
writeCount++;
}
address++;
bytePointer++;
}
return writeCount;
}
private:
//Private variables
static int _base;
static int _memSize;
static int _nextAvailableaddress;
static int _writeCounts;
int _allowedWrites;
bool checkWrite(int base,int noOfBytes);
bool isWriteOk(int address);
bool isReadOk(int address);
};
extern EEPROMClassEx EEPROM;
#endif