EEPROMEx
1.0.0
EEPROMEX is an extension of the standard Arduino EEPROM library for reading and writing basic types, structs, strings, arrays and more.
|
00001 /* 00002 EEPROMEx.h - Extended EEPROM library 00003 Copyright (c) 2012 Thijs Elenbaas. All right reserved. 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 #ifndef EEPROMEX_h 00021 #define EEPROMEX_h 00022 00023 #if ARDUINO >= 100 00024 #include <Arduino.h> 00025 #else 00026 #include <WProgram.h> 00027 #endif 00028 #include <inttypes.h> 00029 #include <avr/eeprom.h> 00030 00031 00032 #define EEPROMSizeATmega168 512 00033 #define EEPROMSizeATmega328 1024 00034 #define EEPROMSizeATmega1280 4096 00035 #define EEPROMSizeATmega32u4 1024 00036 #define EEPROMSizeAT90USB1286 4096 00037 #define EEPROMSizeMK20DX128 2048 00038 #define EEPROMSizeMK20DX256 2048 00039 #define EEPROMSizeATSAMD21G18 16384 00040 00041 #define EEPROMSizeUno EEPROMSizeATmega328 00042 #define EEPROMSizeUnoSMD EEPROMSizeATmega328 00043 #define EEPROMSizeLilypad EEPROMSizeATmega328 00044 #define EEPROMSizeDuemilanove EEPROMSizeATmega328 00045 #define EEPROMSizePro EEPROMSizeATmega328 00046 #define EEPROMSizeFio EEPROMSizeATmega328 00047 #define EEPROMSizeMega EEPROMSizeATmega1280 00048 #define EEPROMSizeDiecimila EEPROMSizeATmega168 00049 #define EEPROMSizeNano EEPROMSizeATmega168 00050 #define EEPROMSizeTeensy2 EEPROMSizeATmega32u4 00051 #define EEPROMSizeLeonardo EEPROMSizeATmega32u4 00052 #define EEPROMSizeMicro EEPROMSizeATmega32u4 00053 #define EEPROMSizeEsplora EEPROMSizeATmega32u4 00054 #define EEPROMSizeYun EEPROMSizeATmega32u4 00055 #define EEPROMSizeTre EEPROMSizeATmega32u4 00056 #define EEPROMSizeZero EEPROMSizeATSAMD21G18 00057 #define EEPROMSizeTeensy2pp EEPROMSizeAT90USB1286 00058 #define EEPROMSizeTeensy3 EEPROMSizeMK20DX128 00059 #define EEPROMSizeTeensy31 EEPROMSizeMK20DX256 00060 class EEPROMClassEx 00061 { 00062 00063 public: 00064 EEPROMClassEx(); 00065 bool isReady(); 00066 int writtenBytes(); 00067 void setMemPool(int base, int memSize); 00068 void setMaxAllowedWrites(int allowedWrites); 00069 int getAddress(int noOfBytes); 00070 00071 uint8_t read(int); 00072 bool readBit(int, byte); 00073 uint8_t readByte(int); 00074 uint16_t readInt(int); 00075 uint32_t readLong(int); 00076 float readFloat(int); 00077 double readDouble(int); 00078 00079 bool write(int, uint8_t); 00080 bool writeBit(int , uint8_t, bool); 00081 bool writeByte(int, uint8_t); 00082 bool writeInt(int, uint16_t); 00083 bool writeLong(int, uint32_t); 00084 bool writeFloat(int, float); 00085 bool writeDouble(int, double); 00086 00087 bool update(int, uint8_t); 00088 bool updateBit(int , uint8_t, bool); 00089 bool updateByte(int, uint8_t); 00090 bool updateInt(int, uint16_t); 00091 bool updateLong(int, uint32_t); 00092 bool updateFloat(int, float); 00093 bool updateDouble(int, double); 00094 00095 00096 // Use template for other data formats 00097 00101 template <class T> int readBlock(int address, const T value[], int items) 00102 { 00103 unsigned int i; 00104 for (i = 0; i < (unsigned int)items; i++) 00105 readBlock<T>(address+(i*sizeof(T)),value[i]); 00106 return i; 00107 } 00108 00112 template <class T> int readBlock(int address, const T& value) 00113 { 00114 eeprom_read_block((void*)&value, (const void*)address, sizeof(value)); 00115 return sizeof(value); 00116 } 00117 00121 template <class T> int writeBlock(int address, const T value[], int items) 00122 { 00123 if (!isWriteOk(address+items*sizeof(T))) return 0; 00124 unsigned int i; 00125 for (i = 0; i < (unsigned int)items; i++) 00126 writeBlock<T>(address+(i*sizeof(T)),value[i]); 00127 return i; 00128 } 00129 00133 template <class T> int writeBlock(int address, const T& value) 00134 { 00135 if (!isWriteOk(address+sizeof(value))) return 0; 00136 eeprom_write_block((void*)&value, (void*)address, sizeof(value)); 00137 return sizeof(value); 00138 } 00139 00144 template <class T> int updateBlock(int address, const T value[], int items) 00145 { 00146 int writeCount=0; 00147 if (!isWriteOk(address+items*sizeof(T))) return 0; 00148 unsigned int i; 00149 for (i = 0; i < (unsigned int)items; i++) 00150 writeCount+= updateBlock<T>(address+(i*sizeof(T)),value[i]); 00151 return writeCount; 00152 } 00153 00158 template <class T> int updateBlock(int address, const T& value) 00159 { 00160 int writeCount=0; 00161 if (!isWriteOk(address+sizeof(value))) return 0; 00162 const byte* bytePointer = (const byte*)(const void*)&value; 00163 for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) { 00164 if (read(address)!=*bytePointer) { 00165 write(address, *bytePointer); 00166 writeCount++; 00167 } 00168 address++; 00169 bytePointer++; 00170 } 00171 return writeCount; 00172 } 00173 00174 00175 00176 private: 00177 //Private variables 00178 static int _base; 00179 static int _memSize; 00180 static int _nextAvailableaddress; 00181 static int _writeCounts; 00182 int _allowedWrites; 00183 bool checkWrite(int base,int noOfBytes); 00184 bool isWriteOk(int address); 00185 bool isReadOk(int address); 00186 }; 00187 00188 extern EEPROMClassEx EEPROM; 00189 00190 #endif 00191