|
29 | 29 | #include <avr/eeprom.h>
|
30 | 30 |
|
31 | 31 | // Boards with ATmega328, Duemilanove, Uno, UnoSMD, Lilypad - 1024 bytes (1 kilobyte)
|
32 |
| -// Boards with ATmega1280 or 2560, Arduino Mega series – 4096 bytes (4 kilobytes) |
33 |
| -// Boards with ATmega168, Lilypad, old Nano, Diecimila – 512 bytes (1/2 kilobyte) |
| 32 | +// Boards with ATmega1280 or 2560, Arduino Mega series – 4096 bytes (4 kilobytes) |
| 33 | +// Boards with ATmega168, Lilypad, old Nano, Diecimila – 512 bytes (1/2 kilobyte) |
34 | 34 |
|
35 | 35 | #define EEPROMSizeATmega168 512
|
36 | 36 | #define EEPROMSizeATmega328 1024
|
|
55 | 55 |
|
56 | 56 | class EEPROMClassEx
|
57 | 57 | {
|
58 |
| - |
| 58 | + |
59 | 59 | public:
|
60 |
| - EEPROMClassEx(); |
61 |
| - bool isReady(); |
62 |
| - int writtenBytes(); |
63 |
| - void setMemPool(int base, int memSize); |
64 |
| - void setMaxAllowedWrites(int allowedWrites); |
65 |
| - int getAddress(int noOfBytes); |
| 60 | + EEPROMClassEx(); |
| 61 | + bool isReady(); |
| 62 | + int writtenBytes(); |
| 63 | + void setMemPool(int base, int memSize); |
| 64 | + void setMaxAllowedWrites(int allowedWrites); |
| 65 | + int getAddress(int noOfBytes); |
66 | 66 |
|
67 |
| - uint8_t read(int); |
68 |
| - bool readBit(int, byte); |
69 |
| - uint8_t readByte(int); |
| 67 | + uint8_t read(int); |
| 68 | + bool readBit(int, byte); |
| 69 | + uint8_t readByte(int); |
70 | 70 | uint16_t readInt(int);
|
71 | 71 | uint32_t readLong(int);
|
72 |
| - float readFloat(int); |
73 |
| - double readDouble(int); |
74 |
| - |
| 72 | + float readFloat(int); |
| 73 | + double readDouble(int); |
| 74 | + |
75 | 75 | bool write(int, uint8_t);
|
76 |
| - bool writeBit(int , uint8_t, bool); |
77 |
| - bool writeByte(int, uint8_t); |
78 |
| - bool writeInt(int, uint16_t); |
79 |
| - bool writeLong(int, uint32_t); |
80 |
| - bool writeFloat(int, float); |
81 |
| - bool writeDouble(int, double); |
82 |
| - |
83 |
| - bool update(int, uint8_t); |
84 |
| - bool updateBit(int , uint8_t, bool); |
85 |
| - bool updateByte(int, uint8_t); |
86 |
| - bool updateInt(int, uint16_t); |
87 |
| - bool updateLong(int, uint32_t); |
88 |
| - bool updateFloat(int, float); |
89 |
| - bool updateDouble(int, double); |
90 |
| - |
91 |
| - |
| 76 | + bool writeBit(int , uint8_t, bool); |
| 77 | + bool writeByte(int, uint8_t); |
| 78 | + bool writeInt(int, uint16_t); |
| 79 | + bool writeLong(int, uint32_t); |
| 80 | + bool writeFloat(int, float); |
| 81 | + bool writeDouble(int, double); |
| 82 | + |
| 83 | + bool update(int, uint8_t); |
| 84 | + bool updateBit(int , uint8_t, bool); |
| 85 | + bool updateByte(int, uint8_t); |
| 86 | + bool updateInt(int, uint16_t); |
| 87 | + bool updateLong(int, uint32_t); |
| 88 | + bool updateFloat(int, float); |
| 89 | + bool updateDouble(int, double); |
| 90 | + |
| 91 | + |
92 | 92 | // Use template for other data formats
|
93 | 93 |
|
94 | 94 |
|
95 |
| - template <class T> int readBlock(int address, const T value[], int items) |
96 |
| - { |
97 |
| - if (!isWriteOk(address+items*sizeof(T))) return 0; |
98 |
| - unsigned int i; |
99 |
| - for (i = 0; i < (unsigned int)items; i++) |
100 |
| - readBlock<T>(address+(i*sizeof(T)),value[i]); |
101 |
| - return i; |
102 |
| - } |
103 |
| - |
104 |
| - template <class T> int readBlock(int address, const T& value) |
105 |
| - { |
106 |
| - eeprom_read_block((void*)&value, (const void*)address, sizeof(value)); |
107 |
| - return sizeof(value); |
108 |
| - } |
109 |
| - |
110 |
| - template <class T> int writeBlock(int address, const T value[], int items) |
111 |
| - { |
112 |
| - if (!isWriteOk(address+items*sizeof(T))) return 0; |
113 |
| - unsigned int i; |
114 |
| - for (i = 0; i < (unsigned int)items; i++) |
115 |
| - writeBlock<T>(address+(i*sizeof(T)),value[i]); |
116 |
| - return i; |
117 |
| - } |
118 |
| - |
119 |
| - template <class T> int writeBlock(int address, const T& value) |
120 |
| - { |
121 |
| - if (!isWriteOk(address+sizeof(value))) return 0; |
122 |
| - eeprom_write_block((void*)&value, (void*)address, sizeof(value)); |
123 |
| - return sizeof(value); |
124 |
| - } |
125 |
| - |
126 |
| - template <class T> int updateBlock(int address, const T value[], int items) |
127 |
| - { |
128 |
| - int writeCount=0; |
129 |
| - if (!isWriteOk(address+items*sizeof(T))) return 0; |
130 |
| - unsigned int i; |
131 |
| - for (i = 0; i < (unsigned int)items; i++) |
132 |
| - writeCount+= updateBlock<T>(address+(i*sizeof(T)),value[i]); |
133 |
| - return writeCount; |
134 |
| - } |
135 |
| - |
136 |
| - template <class T> int updateBlock(int address, const T& value) |
137 |
| - { |
138 |
| - int writeCount=0; |
139 |
| - if (!isWriteOk(address+sizeof(value))) return 0; |
140 |
| - const byte* bytePointer = (const byte*)(const void*)&value; |
141 |
| - for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) { |
142 |
| - if (read(address)!=*bytePointer) { |
143 |
| - write(address, *bytePointer); |
144 |
| - writeCount++; |
145 |
| - } |
146 |
| - address++; |
147 |
| - bytePointer++; |
148 |
| - } |
149 |
| - return writeCount; |
150 |
| - } |
151 |
| - |
152 |
| - |
153 |
| - |
| 95 | + template <class T> int readBlock(int address, const T value[], int items) |
| 96 | + { |
| 97 | + unsigned int i; |
| 98 | + for (i = 0; i < (unsigned int)items; i++) |
| 99 | + readBlock<T>(address+(i*sizeof(T)),value[i]); |
| 100 | + return i; |
| 101 | + } |
| 102 | + |
| 103 | + template <class T> int readBlock(int address, const T& value) |
| 104 | + { |
| 105 | + eeprom_read_block((void*)&value, (const void*)address, sizeof(value)); |
| 106 | + return sizeof(value); |
| 107 | + } |
| 108 | + |
| 109 | + template <class T> int writeBlock(int address, const T value[], int items) |
| 110 | + { |
| 111 | + if (!isWriteOk(address+items*sizeof(T))) return 0; |
| 112 | + unsigned int i; |
| 113 | + for (i = 0; i < (unsigned int)items; i++) |
| 114 | + writeBlock<T>(address+(i*sizeof(T)),value[i]); |
| 115 | + return i; |
| 116 | + } |
| 117 | + |
| 118 | + template <class T> int writeBlock(int address, const T& value) |
| 119 | + { |
| 120 | + if (!isWriteOk(address+sizeof(value))) return 0; |
| 121 | + eeprom_write_block((void*)&value, (void*)address, sizeof(value)); |
| 122 | + return sizeof(value); |
| 123 | + } |
| 124 | + |
| 125 | + template <class T> int updateBlock(int address, const T value[], int items) |
| 126 | + { |
| 127 | + int writeCount=0; |
| 128 | + if (!isWriteOk(address+items*sizeof(T))) return 0; |
| 129 | + unsigned int i; |
| 130 | + for (i = 0; i < (unsigned int)items; i++) |
| 131 | + writeCount+= updateBlock<T>(address+(i*sizeof(T)),value[i]); |
| 132 | + return writeCount; |
| 133 | + } |
| 134 | + |
| 135 | + template <class T> int updateBlock(int address, const T& value) |
| 136 | + { |
| 137 | + int writeCount=0; |
| 138 | + if (!isWriteOk(address+sizeof(value))) return 0; |
| 139 | + const byte* bytePointer = (const byte*)(const void*)&value; |
| 140 | + for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) { |
| 141 | + if (read(address)!=*bytePointer) { |
| 142 | + write(address, *bytePointer); |
| 143 | + writeCount++; |
| 144 | + } |
| 145 | + address++; |
| 146 | + bytePointer++; |
| 147 | + } |
| 148 | + return writeCount; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + |
154 | 153 | private:
|
155 |
| - //Private variables |
156 |
| - static int _base; |
157 |
| - static int _memSize; |
158 |
| - static int _nextAvailableaddress; |
159 |
| - static int _writeCounts; |
160 |
| - int _allowedWrites; |
161 |
| - bool checkWrite(int base,int noOfBytes); |
162 |
| - bool isWriteOk(int address); |
163 |
| - bool isReadOk(int address); |
| 154 | + //Private variables |
| 155 | + static int _base; |
| 156 | + static int _memSize; |
| 157 | + static int _nextAvailableaddress; |
| 158 | + static int _writeCounts; |
| 159 | + int _allowedWrites; |
| 160 | + bool checkWrite(int base,int noOfBytes); |
| 161 | + bool isWriteOk(int address); |
| 162 | + bool isReadOk(int address); |
164 | 163 | };
|
165 | 164 |
|
166 | 165 | extern EEPROMClassEx EEPROM;
|
|
0 commit comments