diff --git a/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_arrays/eeprom_arrays.ino b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_arrays/eeprom_arrays.ino new file mode 100644 index 00000000000..79e7f9283dc --- /dev/null +++ b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_arrays/eeprom_arrays.ino @@ -0,0 +1,98 @@ +/*** + eeprom_arrays example. + This example sketch shows how to use the EEPROM library + to read and write arrays of one or more dimensions. + + For more fine control over what is written, see the example: + eeprom_memory_blocks + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + + Serial.begin( 9600 ); + + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo boards only. + } + + /*** + + Read and write a simple 1D array. + + ***/ + + int address = 0; //Address to first EEPROM cell. + + char text[] = "Testing of a c-string (char array) using put() and get()."; + + EEPROM.put( address, text ); //Write array data. + + memset( text, 0, sizeof(text) ); //Clear testing array. + + EEPROM.get( address, text ); //Read back array data. + + Serial.print( "char array retrieved from EEPROM: " ); + Serial.println( text ); + + /*** + + Read and write a multi-dimensional array. + + ***/ + + long numbers[3][4] = { + { 0xA00, 0xA01, 0xA02, 0xA03 }, + { 0xB00, 0xB01, 0xB02, 0xB03 }, + { 0xC00, 0xC01, 0xC02, 0xC03 } + }; + + address += sizeof( text ); //Move to end of array 'text'. + + EEPROM.put( address, numbers ); //Write array data. + + memset( numbers, 0, sizeof(numbers) ); //Clear testing array. + + EEPROM.get( address, numbers ); //Read back array data. + + Serial.println( "\n\nArray of long values retrieved from EEPROM: " ); + Serial.println( '{' ); + for( int i = 0 ; i < 3 ; ++i ){ + + Serial.print( " {" ); + + for( int j = 0 ; j < 4 ; ++j ){ + Serial.print( numbers[ i ][ j ], HEX ); + Serial.print( ',' ); + } + Serial.println( '}' ); + } + Serial.println( '}' ); + + /*** + + Read a single dimension out of a multi-dimensional array. + + ***/ + + long set[4] = {}; //Enough space for a single dimension. + + int offset = address + sizeof( numbers[0] ); // Offset the address by the size of one dimension in 'numbers'. + + EEPROM.get( offset, set ); //Read second dimension ( index: 1 ) + + Serial.print( "\n\nSingle dimension retrieved from EEPROM:\n {" ); + for( int i = 0 ; i < 4 ; ++i ){ + Serial.print( set[ i ], HEX ); + Serial.print( ',' ); + } + Serial.print( '}' ); +} + +void loop(){ + //Empty loop +} \ No newline at end of file diff --git a/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_bits/eeprom_bits.ino b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_bits/eeprom_bits.ino new file mode 100644 index 00000000000..ab7d0ea207d --- /dev/null +++ b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_bits/eeprom_bits.ino @@ -0,0 +1,66 @@ +/*** + eeprom_bits example. + + This example sketch is highlighting the usage + of EEPROM.readBit() and EEPROM.writeBit(). + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + + int address = 0; //Address to first EEPROM cell. + + delay( 2000 ); //Prevent touching the EEPROM through resets and uploading of sketch. + Serial.begin( 9600 ); + + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo boards only. + } + + /*** + Writing and reading individual bits from EEPROM bytes. + + There are two functions provided specifically for dealing with bits. + + EEPROM.readBit( address, bit index ); + EEPROM.writeBit( address, bit index, value ); + + address: The address of the EEPROM cell requested to read or write from. + bit index: A zero based index of the bit to write from 0 - 7 (Each cell is one byte of eight bits). + value: The value to set the bit to: a boolean either true or false (writeBit only). + + ***/ + + //Clear first cell so we can see the changes. + EEPROM.update( address, 0 ); + + //Print original value. + Serial.print( "Contents of cell 0: " ); + Serial.println( EEPROM.read( address ), HEX ); + delay( 500 ); + + //Set the fourth bit HIGH, or true + EEPROM.writeBit( address, 3, true ); //Parameters: cell index, bit index, value + + Serial.print( "Contents of cell 0 after setting bit: " ); + Serial.println( EEPROM.read( address ), HEX ); + delay( 500 ); + + Serial.print( "Value of fourth bit: " ); + + bool value = EEPROM.readBit( address, 3 ); + + if( value ){ + Serial.println( "true" ); + }else{ + Serial.println( "false" ); + } +} + +void loop(){ + //Empty loop +} \ No newline at end of file diff --git a/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino index 8b5121c8ce3..f10dd706ea8 100644 --- a/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino +++ b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino @@ -11,9 +11,10 @@ #include void setup() { + // initialize the LED pin as an output. pinMode(13, OUTPUT); - + /*** Iterate through each byte of the EEPROM storage. @@ -27,7 +28,14 @@ void setup() { ***/ for (int i = 0 ; i < EEPROM.length() ; i++) { - EEPROM.write(i, 0); + + /* + Use the update method to prevent un-necessary wear on already cleared cells. + The update method checks to see if the current cell is different before writing. + You could easily replace this with EEPROM.write(i, 0); or even EEPROM.put(i, 0); + */ + + EEPROM.update(i, 0); } // turn the LED on when we're done diff --git a/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino index a07cee7c797..35f55c5614f 100644 --- a/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino +++ b/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -17,10 +17,12 @@ #include +//EEPROM address to start reading from. This does not need to be global, but in this example, it is shared between two different functions. +int eeAddress = 0; + void setup() { float f = 0.00f; //Variable to store data read from EEPROM. - int eeAddress = 0; //EEPROM address to start reading from Serial.begin(9600); while (!Serial) { @@ -52,7 +54,7 @@ struct MyObject { }; void secondTest() { - int eeAddress = sizeof(float); //Move address to the next byte after float 'f'. + eeAddress += sizeof(float); //Move address to the next byte after float 'f'. MyObject customVar; //Variable to store custom object read from EEPROM. EEPROM.get(eeAddress, customVar); diff --git a/hardware/arduino/avr/libraries/EEPROM/examples/using_EEMEM/using_EEMEM.ino b/hardware/arduino/avr/libraries/EEPROM/examples/using_EEMEM/using_EEMEM.ino new file mode 100644 index 00000000000..7b49a708726 --- /dev/null +++ b/hardware/arduino/avr/libraries/EEPROM/examples/using_EEMEM/using_EEMEM.ino @@ -0,0 +1,90 @@ +/*** + Using the EEMEM data attribute. + Written By: Christopher Andrews + + Released using MIT licence. + ***/ + +#include +#include + +/*** + EEMEM is an attribute that can be used with static or global + variable declarations. + + What this does is tell the compiling system that the address for the + variable is to reside in the EEPROM memory space. It can also allow + assigning a default value, however the Arduino IDE has this particular + feature disabled. + + Even though the address is located in the EEPROM, C++ does not know + the difference between memory spaces (RAM/Flash/EEPROM) and still needs + to be accessed as usual using the EEPROM library. The advantage however, + is the management of addressing. Your variables will not overlap from + faulty indexing, and other libraries using EEMEM will not interfere with + your application. +***/ + +//Two global variables marked with EEMEM data attribute. +int value EEMEM; +float fraction EEMEM; + + +struct Configuration{ + unsigned long ip; + unsigned int timesRun; +}; + +//An instance of a structure using EEMEM. +Configuration eeConfig EEMEM; + + +void setup() { + + Serial.begin(9600); + + // Wait for serial port to connect. Needed for Leonardo only. + while (!Serial) {} + + /*** + Using the standard get() and put() EEPROM methods, all that is + needed is a temporary storage space. By taking the address of + your variable marked with EEMEM, you can easily retrieve its + contents. + ***/ + + // Read value from EEPROM using the address assigned automatically + // when the declaration is used with EEMEM. + int result = EEPROM.get(&value); + + float frac = EEPROM.get(&fraction); + + //Using the standard usage of EEPROM.put() allows you to write to a variable marked with EEMEM. + result = random(10); + EEPROM.put(&value, result); + + frac = 3.14f; + EEPROM.put(&fraction, frac); + EEPROM.put(&fraction, 3.14f); //You can also directly write literal values without needing temporary storage. + + /*** + Using a structure with EEMEM. + ***/ + + IPAddress ip(192, 168, 1, 1); + + //Save an IP Address. An IPAddress object can cast to a uint32_t + //or unsigned long. This gives a raw 4 byte value to save. + EEPROM.put(&eeConfig.ip, (unsigned long) ip); + + //Read the value and assign the result directly to the IPAddress + //object (It accepts unsigned long values as an input). + ip = EEPROM.get(&eeConfig.ip); + + Serial.print("IP address is: "); + Serial.println(ip); +} + +void loop() { + /** Empty Loop **/ +} \ No newline at end of file diff --git a/hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h b/hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h index cde75dba9ae..16bf2d735f9 100644 --- a/hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h +++ b/hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h @@ -1,7 +1,10 @@ /* EEPROM.h - EEPROM library Original Copyright (c) 2006 David A. Mellis. All right reserved. - New version by Christopher Andrews 2015. + Version 2.0 - 2.1 Copyright (c) 2015 Christopher Andrews. All right reserved. + + This library has been entirely re-written, and none of the original code has + been reused. The only original element left are the names 'EEPROM' & 'EEPROMClass'. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -18,16 +21,21 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef EEPROM_h -#define EEPROM_h +#if defined(__AVR__) && !defined(EEPROM_h) + #define EEPROM_h -#include +#ifndef Arduino_h //These includes are available through Arduino.h + #include + #include +#endif #include -#include + +struct EEPtr; //Forward declaration for EERef::opreator& +struct EEBit; //Forward declaration for EERef::opreator[] /*** EERef class. - + This object references an EEPROM cell. Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. @@ -35,13 +43,20 @@ struct EERef{ - EERef( const int index ) - : index( index ) {} - + template< typename T > EERef( T *ptr ) : index( (int) ptr ) {} + EERef( const int index ) : index( index ) {} + //Access/read members. uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); } - operator const uint8_t() const { return **this; } - + operator uint8_t() const { return **this; } + + EEPtr operator&() const; //Defined below EEPtr + + //Bit access members, defined below EEBit declaration. + EEBit operator[]( const int bidx ); + EEBit begin(); + EEBit end(); + //Assignment/write members. EERef &operator=( const EERef &ref ) { return *this = *ref; } EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; } @@ -55,48 +70,129 @@ struct EERef{ EERef &operator |=( uint8_t in ) { return *this = **this | in; } EERef &operator <<=( uint8_t in ) { return *this = **this << in; } EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } - + EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } - - /** Prefix increment/decrement **/ + + // Prefix increment/decrement EERef& operator++() { return *this += 1; } EERef& operator--() { return *this -= 1; } - - /** Postfix increment/decrement **/ - uint8_t operator++ (int){ + + // Postfix increment/decrement + uint8_t operator++ (int){ uint8_t ret = **this; return ++(*this), ret; } - uint8_t operator-- (int){ + uint8_t operator-- (int){ uint8_t ret = **this; return --(*this), ret; } - + int index; //Index of current EEPROM cell. }; + +/*** + EEBit class. + This object is a reference object similar to EERef, however it references + only a single bit. Its function mimics a bool type. +***/ + +struct EEBit{ + + //Constructor, use by passing in index of EEPROM byte, then index of bit to read. + EEBit( int index, uint8_t bidx ) + : ref( index ), mask( 0x01 << bidx ) {} + + //Modifier functions. + EEBit &setIndex( uint8_t bidx ) { return mask = (0x01 << bidx), *this; } + EEBit &set() { return *this = true; } + EEBit &clear() { return *this = false; } + + //Read/write functions. + operator bool() const { return ref & mask; } + EEBit &operator =( const EEBit © ) { return *this = ( const bool ) copy; } + + EEBit &operator =( const bool © ){ + if( copy ) ref |= mask; + else ref &= ~mask; + return *this; + } + + //Iterator functionality. + EEBit& operator*() { return *this; } + bool operator==( const EEBit &bit ) { return (mask == bit.mask) && (ref.index == bit.ref.index); } + bool operator!=( const EEBit &bit ) { return !(*this == bit); } + + //Prefix & Postfix increment/decrement + EEBit& operator++(){ + if( mask & 0x80 ){ + ++ref.index; + mask = 0x01; + }else{ + mask <<= 1; + } + return *this; + } + + EEBit& operator--(){ + if( mask & 0x01 ){ + --ref.index; + mask = 0x80; + }else{ + mask >>= 1; + } + return *this; + } + + EEBit operator++ (int) { + EEBit cpy = *this; + return ++(*this), cpy; + } + + EEBit operator-- (int) { + EEBit cpy = *this; + return --(*this), cpy; + } + + EERef ref; //Reference to EEPROM cell. + uint8_t mask; //Mask of bit to read/write. +}; + +//Deferred definition till EEBit becomes available. +inline EEBit EERef::operator[]( const int bidx ) { return EEBit( index, bidx ); } +inline EEBit EERef::begin() { return EEBit( index, 0 ); } +inline EEBit EERef::end() { return EEBit( index + 1, 0 ); } + + /*** EEPtr class. - + This object is a bidirectional pointer to EEPROM cells represented by EERef objects. - Just like a normal pointer type, this can be dereferenced and repositioned using + Just like a normal pointer type, this can be dereferenced and repositioned using increment/decrement operators. ***/ struct EEPtr{ - EEPtr( const int index ) - : index( index ) {} - - operator const int() const { return index; } + template< typename T > EEPtr( T *ptr ) : index( (int) ptr ) {} + EEPtr( const int index ) : index( index ) {} + + //Pointer read/write. + operator int() const { return index; } EEPtr &operator=( int in ) { return index = in, *this; } - + EERef operator[]( int idx ) const { return index + idx; } + //Iterator functionality. - bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } - EERef operator*() { return index; } - - /** Prefix & Postfix increment/decrement **/ + bool operator!=( const EEPtr &ptr ) const { return index != ptr.index; } + EEPtr& operator+=( int idx ) { return index += idx, *this; } + EEPtr& operator-=( int idx ) { return index -= idx, *this; } + + //Dreference & member access. + EERef operator*() const { return index; } + EERef *operator->() const { return (EERef*) this; } + + // Prefix & Postfix increment/decrement EEPtr& operator++() { return ++index, *this; } EEPtr& operator--() { return --index, *this; } EEPtr operator++ (int) { return index++; } @@ -105,41 +201,101 @@ struct EEPtr{ int index; //Index of current EEPROM cell. }; +inline EEPtr EERef::operator&() const { return index; } //Deferred definition till EEPtr becomes available. + /*** EEPROMClass class. - + This object represents the entire EEPROM space. It wraps the functionality of EEPtr and EERef into a basic interface. This class is also 100% backwards compatible with earlier Arduino core releases. ***/ -struct EEPROMClass{ - - //Basic user access methods. - EERef operator[]( const int idx ) { return idx; } - uint8_t read( int idx ) { return EERef( idx ); } - void write( int idx, uint8_t val ) { (EERef( idx )) = val; } - void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } - - //STL and C++11 iteration capability. - EEPtr begin() { return 0x00; } - EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. - uint16_t length() { return E2END + 1; } - - //Functionality to 'get' and 'put' objects to and from EEPROM. - template< typename T > T &get( int idx, T &t ){ - EEPtr e = idx; - uint8_t *ptr = (uint8_t*) &t; - for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; - return t; - } - - template< typename T > const T &put( int idx, const T &t ){ - EEPtr e = idx; - const uint8_t *ptr = (const uint8_t*) &t; - for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); - return t; - } +class EEPROMClass{ + protected: + + /*** + EEIterator interface. + This interface allows creating customized ranges within + the EEPROM. Essentially intended for use with ranged for + loops, or STL style iteration on subsections of the EEPROM. + ***/ + + struct EEIterator{ + EEIterator( EEPtr _start, int _length ) : start(_start), length(_length) {} + EEPtr begin() { return start; } + EEPtr end() { return start + length; } + EEPtr start; + int length; + }; + + public: + + //Basic user access methods. + EERef operator[]( EERef ref ) { return ref; } + EERef read( EERef ref ) { return ref; } + void write( EERef ref, uint8_t val ) { ref = val; } + void update( EERef ref, uint8_t val ) { ref.update( val ); } + + //STL and C++11 iteration capability. + EEPtr begin() { return 0x00; } + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length() { return E2END + 1; } + + //Extended iteration functionality (arbitrary regions). + //These can make serialized reading/writing easy. + template< typename T > EEIterator iterate( T *t ) { return EEIterator( t, sizeof(T) ); } + EEIterator iterate( EEPtr ptr, int length ) { return EEIterator( ptr, length ); } + + //Bit access methods. + EEBit readBit( EERef ref, uint8_t bidx ) { return ref[ bidx ]; } + void writeBit( EERef ref, uint8_t bidx, const bool val ) { ref[ bidx ] = val; } + + //A helper function for the builtin eeprom_is_ready macro. + bool ready() { return eeprom_is_ready(); } + + + /* + Functionality to 'get' and 'put' objects to and from EEPROM. + All put() functions use the update() method of writing to the EEPROM + */ + + //Generic get() function, for any type of data. + template< typename T > T &get( EEPtr ptr, T &t ){ + uint8_t *dest = (uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++ptr ) *dest++ = *ptr; + return t; + } + + //EEMEM helper: This function retrieves an object which uses the same type as the provided object. + template< typename T > T get( T &t ){ return get(&t); } + template< typename T > T get( T *t ){ + T result; + return get( t, result ); + } + + //Overload get() function to deal with the String class. + String &get( EEPtr ptr, String &t ){ + for( auto el : iterate(ptr, length() - ptr)){ + if(el) t += char(el); + else break; + } + return t; + } + + //Generic put() function, for any type of data. + template< typename T > const T &put( EEPtr ptr, const T &t ){ + const uint8_t *src = (const uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++ptr ) (*ptr).update( *src++ ); + return t; + } + + //Overload of put() function to deal with the String class. + const String &put( EEPtr ptr, const String &t ){ + uint16_t idx = 0; + for(auto el : iterate(ptr, t.length() + 1)) el.update(t[idx++]); //Read past length() as String::operator[] returns 0 on an out of bounds read (for null terminator). + return t; + } }; static EEPROMClass EEPROM;