From 46e810cf0743a7fb29d41c02c5ab6c2d9e50685b Mon Sep 17 00:00:00 2001 From: Chris--A Date: Tue, 17 Mar 2015 17:13:47 +1000 Subject: Added new version of EEPROM library. --- libraries/EEPROM/EEPROM.cpp | 50 ------------- libraries/EEPROM/EEPROM.h | 139 +++++++++++++++++++++++++++++++++--- libraries/EEPROM/keywords.txt | 6 +- libraries/EEPROM/library.properties | 4 +- 4 files changed, 138 insertions(+), 61 deletions(-) delete mode 100644 libraries/EEPROM/EEPROM.cpp (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp deleted file mode 100644 index dfa1deb..0000000 --- a/libraries/EEPROM/EEPROM.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - EEPROM.cpp - EEPROM library - Copyright (c) 2006 David A. Mellis. 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 -*/ - -/****************************************************************************** - * Includes - ******************************************************************************/ - -#include -#include "Arduino.h" -#include "EEPROM.h" - -/****************************************************************************** - * Definitions - ******************************************************************************/ - -/****************************************************************************** - * Constructors - ******************************************************************************/ - -/****************************************************************************** - * User API - ******************************************************************************/ - -uint8_t EEPROMClass::read(int address) -{ - return eeprom_read_byte((unsigned char *) address); -} - -void EEPROMClass::write(int address, uint8_t value) -{ - eeprom_write_byte((unsigned char *) address, value); -} - -EEPROMClass EEPROM; diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index aa2b577..1b528f7 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,6 +1,7 @@ /* EEPROM.h - EEPROM library - Copyright (c) 2006 David A. Mellis. All right reserved. + Original Copyright (c) 2006 David A. Mellis. All right reserved. + New version by Christopher Andrews 2015. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -21,15 +22,137 @@ #define EEPROM_h #include +#include +#include -class EEPROMClass -{ - public: - uint8_t read(int); - void write(int, uint8_t); +/*** + 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. +***/ + +struct EERef{ + + 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; } + + //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; } + 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 &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 &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 &operator >>=( uint8_t in ) { return *this = **this >> in; } + + EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } + + /** Prefix increment/decrement **/ + EERef& operator++() { return *this += 1; } + EERef& operator--() { return *this -= 1; } + + /** Postfix increment/decrement **/ + uint8_t operator++ (int){ + uint8_t ret = **this; + return ++(*this), ret; + } + + uint8_t operator-- (int){ + uint8_t ret = **this; + return --(*this), ret; + } + + int index; //Index of current EEPROM cell. }; -extern EEPROMClass EEPROM; +/*** + 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 + increment/decrement operators. +***/ + +struct EEPtr{ + + EEPtr( const int index ) + : index( index ) {} + + operator const int() const { return index; } + EEPtr &operator=( int in ) { return index = in, *this; } + + + //Iterator functionality. + bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } + EERef operator*() { return( this->index ); } + + + /** Prefix increment/decrement **/ + EEPtr& operator++() { return ++index, *this; } + EEPtr& operator--() { return --index, *this; } + + + /** Postfix increment/decrement **/ + EEPtr operator++ (int){ + int ret = index; + return ++index, ret; + } -#endif + EEPtr operator-- (int){ + int ret = index; + return --index, ret; + } + + int index; //Index of current EEPROM cell. +}; + +/*** + 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 index ) { return( index ); } + 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; + } +}; + +extern EEPROMClass EEPROM; +#endif \ No newline at end of file diff --git a/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt index d3218fe..2cabc0b 100644 --- a/libraries/EEPROM/keywords.txt +++ b/libraries/EEPROM/keywords.txt @@ -1,5 +1,5 @@ ####################################### -# Syntax Coloring Map For Ultrasound +# Syntax Coloring Map For EEPROM ####################################### ####################################### @@ -7,11 +7,15 @@ ####################################### EEPROM KEYWORD1 +EERef KEYWORD1 +EEPtr KEYWORD2 ####################################### # Methods and Functions (KEYWORD2) ####################################### +update KEYWORD2 + ####################################### # Constants (LITERAL1) ####################################### diff --git a/libraries/EEPROM/library.properties b/libraries/EEPROM/library.properties index 796f7cb..955a0ae 100644 --- a/libraries/EEPROM/library.properties +++ b/libraries/EEPROM/library.properties @@ -1,6 +1,6 @@ name=EEPROM -version=1.0 -author=Arduino +version=2.0 +author=Arduino, Christopher Andrews maintainer=Arduino sentence=Enables reading and writing to the permanent board storage. For all Arduino boards BUT Arduino DUE. paragraph= -- cgit v1.2.3-18-g5258 From dd1ec9920b8fd6b445cdcc943f53333990b34428 Mon Sep 17 00:00:00 2001 From: Chris--A Date: Tue, 17 Mar 2015 17:17:08 +1000 Subject: Added additional examples to EEPROM lib --- .../EEPROM/examples/eeprom_crc/eeprom_crc.ino | 47 +++++++++++ .../EEPROM/examples/eeprom_get/eeprom_get.ino | 63 +++++++++++++++ .../examples/eeprom_iteration/eeprom_iteration.ino | 73 +++++++++++++++++ .../examples/eeprom_pointer/eeprom_pointer.ino | 74 +++++++++++++++++ .../EEPROM/examples/eeprom_put/eeprom_put.ino | 53 ++++++++++++ .../examples/eeprom_reference/eeprom_reference.ino | 93 ++++++++++++++++++++++ .../examples/eeprom_update/eeprom_update.ino | 52 ++++++++++++ 7 files changed, 455 insertions(+) create mode 100644 libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino create mode 100644 libraries/EEPROM/examples/eeprom_get/eeprom_get.ino create mode 100644 libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino create mode 100644 libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino create mode 100644 libraries/EEPROM/examples/eeprom_put/eeprom_put.ino create mode 100644 libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino create mode 100644 libraries/EEPROM/examples/eeprom_update/eeprom_update.ino (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino new file mode 100644 index 0000000..40b08bd --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -0,0 +1,47 @@ +/*** + Written by Christopher Andrews. + CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ). + + A CRC is a simple way of checking whether data has changed or become corrupted. + This example calculates a CRC value directly on the EEPROM values. + The purpose of this example is to highlight how the EEPROM object can be used just like an array. +***/ + +#include +#include + +void setup(){ + + //Start serial + Serial.begin(9600); + + //Print length of data to run CRC on. + Serial.print( "EEPROM length: " ); + Serial.println( EEPROM.length() ); + + //Print the result of calling eeprom_crc() + Serial.print( "CRC32 of EEPROM data: 0x" ); + Serial.println( eeprom_crc(), HEX ); + Serial.print( "\n\nDone!" ); +} + +void loop(){ /* Empty loop */ } + +unsigned long eeprom_crc( void ){ + + const unsigned long crc_table[16] = { + 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, + 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, + 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c + }; + + unsigned long crc = ~0L; + + for( int index = 0 ; index < 32 ; ++index ){ + crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4); + crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4); + crc = ~crc; + } + return crc; +} \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino new file mode 100644 index 0000000..58475fd --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -0,0 +1,63 @@ +/*** + eeprom_get example. + + This shows how to use the EEPROM.get() method. + + To pre-set the EEPROM data, run the example sketch eeprom_put. + This sketch will run without it, however, the values shown + will be shown from what ever is already on the EEPROM. + + This may cause the serial object to print out a large string + of garbage if there is no null character inside one of the strings + loaded. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup(){ + + float f = 0.00f; //Variable to store data read from EEPROM. + int eeAddress = 0; //Location of the IP address inside the class. + + Serial.begin( 9600 ); + Serial.print( "Read float from EEPROM: " ); + + //Get the float data from the EEPROM at position 'eeAddress' + EEPROM.get( eeAddress, f ); + Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. + + /*** + As get also returns a reference to 'f', you can use it inline. + E.g: Serial.print( EEPROM.get( eeAddress, f ) ); + ***/ + + /*** + Get can be used with custom structures too. + I have separated this into an extra function. + ***/ + + secondTest(); //Run the next test. +} + +struct MyObject{ + float field1; + byte field2; + char name[10]; +}; + +void secondTest(){ + int 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 ); + + Serial.println( "Read custom object from EEPROM: " ); + Serial.println( customVar.field1 ); + Serial.println( customVar.field2 ); + Serial.println( customVar.name ); +} + +void loop(){ /* Empty loop */ } \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino new file mode 100644 index 0000000..34071fc --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -0,0 +1,73 @@ +/*** + eeprom_iteration example. + + A set of example snippets highlighting the + simplest methods for traversing the EEPROM. + + Running this sketch is not necessary, this is + simply highlighting certain programming methods. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + + /*** + Iterate the EEPROM using a for loop. + ***/ + + for( int index = 0 ; index < EEPROM.length() ; index++ ){ + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + } + + /*** + Iterate the EEPROM using a while loop. + ***/ + + int index = 0; + + while( index < EEPROM.length() ){ + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + index++; + } + + /*** + Iterate the EEPROM using a do-while loop. + ***/ + + int idx = 0; + + do{ + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + index++; + }while( index < EEPROM.length() ); + + /*** + Iterate the EEPROM using a C++11 ranged for loop. + + This version of the loop is best explained in the example 'eeprom_pointer' + as this kind of iteration uses pointers rather than an index/integer. + + !! Note: C++11 is not yet enabled by default in any IDE version. + Unless you manually enable it, this sketch will not compile. + You can comment the loop below to verify the non C++11 content. + ***/ + + for( auto cell : EEPROM ){ + + //Add one to each cell in the EEPROM + cell += 1; + } + +} //End of setup function. + +void loop(){} \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino b/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino new file mode 100644 index 0000000..b56b681 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino @@ -0,0 +1,74 @@ +/*** + eeprom_pointer example. + + This example shows how the built-in EEPtr + object can be used to manipulate the EEPROM + using standard pointer arithmetic. + + Running this sketch is not necessary, this is + simply highlighting certain programming methods. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + + Serial.begin(9600); + + /*** + In this example, we will iterate forward over the EEPROM, + starting at the 10th cell (remember indices are zero based). + ***/ + + EEPtr ptr = 9; + + //Rather than hard coding a length, we can use the provided .length() function. + + while( ptr < EEPROM.length() ){ + + Serial.print( *ptr, HEX ); //Print out hex value of the EEPROM cell pointed to by 'ptr' + Serial.print( ", " ); //Separate values with a comma. + ptr++; //Move to next cell + } + + /*** + In this example, we will iterate backwards over the EEPROM, + starting at the last cell. + ***/ + + ptr = EEPROM.length() - 1; + + do{ + + Serial.print( *ptr, HEX ); + Serial.print( ", " ); + + }while( ptr-- ); //When the pointer reaches zero the loop will end as zero is considered 'false'. + + + /*** + And just for clarity, the loop below is an equivalent implementation + of the C++11 ranged for loop. + ***/ + + for( EEPtr ptr = EEPROM.begin() ; item != EEPROM.end() ; ++item ){ + Serial.print( *ptr, HEX ); + Serial.print( ", " ); + } + + /*** + The actual C++11 version: + + for( auto ptr : EEPROM ){ + Serial.print( *ptr, HEX ); + Serial.print( ", " ); + } + ***/ + + +} + +void loop(){} \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino new file mode 100644 index 0000000..7575768 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -0,0 +1,53 @@ +/*** + eeprom_put example. + + This shows how to use the EEPROM.put() method. + Also, this sketch will pre-set the EEPROM data for the + example sketch eeprom_get. + + Note, unlike the single byte version EEPROM.write(), + the put method will use update semantics. As in a byte + will only be written to the EEPROM if the data is actually + different. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup(){ + + Serial.begin(9600); + + float f = 123.456f; //Variable to store in EEPROM. + int eeAddress = 0; //Location we want the data to be put. + + + //One simple call, with the address first and the object second. + EEPROM.put( eeAddress, f ); + + Serial.println("Written float data type!"); + + /** Put is designed for use with custom structures also. **/ + + struct MyObject{ + float field1; + byte field2; + char name[10]; + }; + + //Data to store. + MyObject customVar = { + 3.14f, + 65, + "Working!" + }; + + eeAddress += sizeof(float); //Move address to the next byte after float 'f'. + + EEPROM.put( eeAddress, customVar ); + Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" ); +} + +void loop(){ /* Empty loop */ } \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino b/libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino new file mode 100644 index 0000000..bcf84d6 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino @@ -0,0 +1,93 @@ +/*** + eeprom_reference example. + + This example shows how to use the EEPROM + reference object EERef, which allows usage + similar to using a simple char (uint8_t in this case). + + Running this sketch is not necessary, this is + simply highlighting certain programming methods. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + + + /*** + To create a reference to an EEPROM cell, simply create an EERef variable. + To let it know which cell you want to reference, you can simply assign the + address when you create it. + ***/ + + EERef ref = 0; + + /*** + An equivalent way is by calling the constructor directly: + EERef ref( 0 ); + ***/ + + /** Using the reference **/ + + /*** + Updating cell data. + To prevent unnecessary wear on the EEPROM cells + this function will only write the data when it + is different to what is already stored. + ***/ + + ref.update( 44 ); //May write 44 if not present. + ref.update( 44 ); //This second call will not write anything. + + /*** + Assign values directly to the EEPROM cell. + + You can use any form of assignment that would otherwise be available + to a standard uint8_t: + + *= + /= + += + -= + ^= + %= + &= + |= + <<= + >>= + + ***/ + + ref = 4; /*** + Take care to notice, this changes the EEPROM cell data, it does not + change the index of the cell referenced by 'ref'. + + Only the initial declaration like 'EERef ref = 0;' will set the address. + Using an assignment anywhere else modifies the referenced cell. + To modify the referenced address after declaring your variable see below. + ***/ + + /*** + Changing the referenced object. + The class has a member named 'index' which is an integer you can modify. + ***/ + + ref.index++; //Move reference to the next cell. + + + /*** + Grouping of references. + + Using EERef objects you can create a contiguous array referencing + non-contiguous EEPROM cells. + ***/ + + EERef array[] = { 0, 20, 40, 60, 80 }; + + +} //End of setup function. + +void loop(){} \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino new file mode 100644 index 0000000..e0e18d8 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -0,0 +1,52 @@ +/*** + EEPROM Update method + + Stores values read from analog input 0 into the EEPROM. + These values will stay in the EEPROM when the board is + turned off and may be retrieved later by another sketch. + + If a value has not changed in the EEPROM, it is not overwritten + which would reduce the life span of the EEPROM unnecessarily. + + Released using MIT licence. + ***/ + +#include + +/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ +int addr = 0; + +void setup(){ /** EMpty setup **/ } + +void loop() +{ + /*** + need to divide by 4 because analog inputs range from + 0 to 1023 and each byte of the EEPROM can only hold a + value from 0 to 255. + ***/ + int val = analogRead(0) / 4; + + /*** + Update the particular EEPROM cell. + these values will remain there when the board is + turned off. + ***/ + EEPROM.update(addr, val); + + /*** + The function EEPROM.update(addr, val) is equivalent to the following: + + if( EEPROM.read(addr) != val ){ + EEPROM.write(addr, val); + } + ***/ + + + /** advance to the next address. there are 512 bytes in the EEPROM, so go back to 0 when we hit 512. **/ + addr = addr + 1; + if (addr == 512) + addr = 0; + + delay(100); +} -- cgit v1.2.3-18-g5258 From c9ec4eabdab373c756a624f42660acaf0ec6812d Mon Sep 17 00:00:00 2001 From: Chris--A Date: Wed, 18 Mar 2015 18:56:08 +1000 Subject: Updated EEPROM storage class To avoid having a .cpp just for an extern variable definition, `static` has been chosen over `extern`. As the `EEPROMClass` class simply wraps functionality located elsewhere, it is completely compiled away. Even though each translation unit which includes the header will get a copy with internal linkage, there is no associated overhead. More info [here](http://stackoverflow.com/questions/29098518/extern-variable-only-in-header-unexpectedly-working-why) --- libraries/EEPROM/EEPROM.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 1b528f7..ae2645e 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -154,5 +154,5 @@ struct EEPROMClass{ } }; -extern EEPROMClass EEPROM; +static EEPROMClass EEPROM; #endif \ No newline at end of file -- cgit v1.2.3-18-g5258 From fd4323f360885725c736c90745ee57dbca20e8a0 Mon Sep 17 00:00:00 2001 From: Chris--A Date: Thu, 19 Mar 2015 17:13:32 +1000 Subject: Small tweaks to EEPROM lib and examples. --- libraries/EEPROM/EEPROM.h | 30 +++++++--------------- .../examples/eeprom_iteration/eeprom_iteration.ino | 8 +++--- .../examples/eeprom_pointer/eeprom_pointer.ino | 2 +- 3 files changed, 14 insertions(+), 26 deletions(-) (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index ae2645e..cde75db 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -92,28 +92,16 @@ struct EEPtr{ operator const int() const { return index; } EEPtr &operator=( int in ) { return index = in, *this; } - //Iterator functionality. bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } - EERef operator*() { return( this->index ); } - + EERef operator*() { return index; } - /** Prefix increment/decrement **/ + /** Prefix & Postfix increment/decrement **/ EEPtr& operator++() { return ++index, *this; } EEPtr& operator--() { return --index, *this; } - - - /** Postfix increment/decrement **/ - EEPtr operator++ (int){ - int ret = index; - return ++index, ret; - } + EEPtr operator++ (int) { return index++; } + EEPtr operator-- (int) { return index--; } - EEPtr operator-- (int){ - int ret = index; - return --index, ret; - } - int index; //Index of current EEPROM cell. }; @@ -128,15 +116,15 @@ struct EEPtr{ struct EEPROMClass{ //Basic user access methods. - EERef operator[]( const int index ) { return( index ); } - uint8_t read( int idx ) { return (EERef( idx )); } + 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; } + 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 ){ diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino index 34071fc..a2d825c 100644 --- a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -42,14 +42,14 @@ void setup() { Iterate the EEPROM using a do-while loop. ***/ - int idx = 0; + int idx = 0; //Used 'idx' to avoid name conflict with 'index' above. do{ //Add one to each cell in the EEPROM - EEPROM[ index ] += 1; - index++; - }while( index < EEPROM.length() ); + EEPROM[ idx ] += 1; + idx++; + }while( idx < EEPROM.length() ); /*** Iterate the EEPROM using a C++11 ranged for loop. diff --git a/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino b/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino index b56b681..637cdb7 100644 --- a/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino +++ b/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino @@ -54,7 +54,7 @@ void setup() { of the C++11 ranged for loop. ***/ - for( EEPtr ptr = EEPROM.begin() ; item != EEPROM.end() ; ++item ){ + for( EEPtr ptr = EEPROM.begin() ; ptr != EEPROM.end() ; ++ptr ){ Serial.print( *ptr, HEX ); Serial.print( ", " ); } -- cgit v1.2.3-18-g5258 From 26577474efcb8874dad687467a8ba2f01678ff4c Mon Sep 17 00:00:00 2001 From: Chris--A Date: Fri, 20 Mar 2015 12:06:20 +1000 Subject: Updated EEPROM examples. Removed hard coded lengths, which were incorrect for standard Arduino's now. --- .../EEPROM/examples/eeprom_clear/eeprom_clear.ino | 24 ++++++++--- .../EEPROM/examples/eeprom_read/eeprom_read.ino | 28 +++++++++--- .../examples/eeprom_update/eeprom_update.ino | 23 ++++++++-- .../EEPROM/examples/eeprom_write/eeprom_write.ino | 50 +++++++++++++++------- 4 files changed, 94 insertions(+), 31 deletions(-) (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino index b18ff2c..49eb5fe 100644 --- a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino +++ b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino @@ -2,22 +2,34 @@ * EEPROM Clear * * Sets all of the bytes of the EEPROM to 0. + * Please see eeprom_iteration for a more in depth + * look at how to traverse the EEPROM. + * * This example code is in the public domain. - */ #include void setup() { - // write a 0 to all 512 bytes of the EEPROM - for (int i = 0; i < 512; i++) + + /*** + Iterate through each byte of the EEPROM storage. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ + + for ( int i = 0 ; i < EEPROM.length() ; i++ ) EEPROM.write(i, 0); // turn the LED on when we're done digitalWrite(13, HIGH); } -void loop() -{ -} +void loop(){ /** Empty loop. **/ } diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino index ebf79d6..8567ed7 100644 --- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -31,13 +31,27 @@ void loop() Serial.print(value, DEC); Serial.println(); - // advance to the next address of the EEPROM - address = address + 1; - - // there are only 512 bytes of EEPROM, from 0 to 511, so if we're - // on address 512, wrap around to address 0 - if (address == 512) - address = 0; + /*** + Advance to the next address, when at the end restart at the beginning. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ + addr = addr + 1; + if(addr == EEPROM.length()) + addr = 0; + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++addr &= EEPROM.length() - 1; + ***/ delay(500); } diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino index e0e18d8..dbf05ec 100644 --- a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -38,15 +38,32 @@ void loop() The function EEPROM.update(addr, val) is equivalent to the following: if( EEPROM.read(addr) != val ){ - EEPROM.write(addr, val); + EEPROM.write(addr, val); } ***/ - /** advance to the next address. there are 512 bytes in the EEPROM, so go back to 0 when we hit 512. **/ + /*** + Advance to the next address, when at the end restart at the beginning. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ addr = addr + 1; - if (addr == 512) + if(addr == EEPROM.length()) addr = 0; + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++addr &= EEPROM.length() - 1; + ***/ delay(100); } diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino index c047887..f07446c 100644 --- a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @@ -8,31 +8,51 @@ #include -// the current address in the EEPROM (i.e. which byte -// we're going to write to next) -int addr = 0; +/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ +int addr = 0; -void setup() -{ -} +void setup(){ /** Empty setup. **/} void loop() { - // need to divide by 4 because analog inputs range from - // 0 to 1023 and each byte of the EEPROM can only hold a - // value from 0 to 255. + /*** + Need to divide by 4 because analog inputs range from + 0 to 1023 and each byte of the EEPROM can only hold a + value from 0 to 255. + ***/ + int val = analogRead(0) / 4; - // write the value to the appropriate byte of the EEPROM. - // these values will remain there when the board is - // turned off. + /*** + Write the value to the appropriate byte of the EEPROM. + these values will remain there when the board is + turned off. + ***/ + EEPROM.write(addr, val); - // advance to the next address. there are 512 bytes in - // the EEPROM, so go back to 0 when we hit 512. + /*** + Advance to the next address, when at the end restart at the beginning. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ addr = addr + 1; - if (addr == 512) + if(addr == EEPROM.length()) addr = 0; + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++addr &= EEPROM.length() - 1; + ***/ + delay(100); } -- cgit v1.2.3-18-g5258 From 5da9792cd61b5ba9eed9fb80874edb52081d6232 Mon Sep 17 00:00:00 2001 From: Chris--A Date: Tue, 24 Mar 2015 13:58:01 +1000 Subject: Fixed EEPROM examples and added readme --- libraries/EEPROM/README.md | 139 +++++++++++++++++++++ .../EEPROM/examples/eeprom_crc/eeprom_crc.ino | 2 +- .../EEPROM/examples/eeprom_get/eeprom_get.ino | 2 +- .../examples/eeprom_iteration/eeprom_iteration.ino | 16 --- .../examples/eeprom_pointer/eeprom_pointer.ino | 74 ----------- .../EEPROM/examples/eeprom_put/eeprom_put.ino | 12 +- .../EEPROM/examples/eeprom_read/eeprom_read.ino | 8 +- .../examples/eeprom_reference/eeprom_reference.ino | 93 -------------- .../examples/eeprom_update/eeprom_update.ino | 18 +-- 9 files changed, 160 insertions(+), 204 deletions(-) create mode 100644 libraries/EEPROM/README.md delete mode 100644 libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino delete mode 100644 libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/README.md b/libraries/EEPROM/README.md new file mode 100644 index 0000000..a624136 --- /dev/null +++ b/libraries/EEPROM/README.md @@ -0,0 +1,139 @@ +## **EEPROM Library V2.0** for Arduino + +**Written by:** _Christopher Andrews_. + +### **What is the EEPROM library.** + +Th EEPROM library provides an easy to use interface to interact with the internal non-volatile storage found in AVR based Arduino boards. This library will work on many AVR devices like ATtiny and ATmega chips. + +### **How to use it** +The EEPROM library is included in your IDE download. To add its functionality to your sketch you'll need to reference the library header file. You do this by adding an include directive to the top of your sketch. + +```Arduino +#include + +void setup(){ + +} + +void loop(){ + +} + +``` + +The library provides a global variable named `EEPROM`, you use this variable to access the library functions. The methods provided in the EEPROM class are listed below. + +You can view all the examples [here](examples/). + +### **Library functions** + +#### **`EEPROM.read( address )`** [[_example_]](examples/eeprom_read/eeprom_read.ino) + +This function allows you to read a single byte of data from the eeprom. +Its only parameter is an `int` which should be set to the address you wish to read. + +The function returns an `unsigned char` containing the value read. + +#### **`EEPROM.write( address, value )`** [[_example_]](examples/eeprom_write/eeprom_write.ino) + +The `write()` method allows you to write a single byte of data to the EEPROM. +Two parameters are needed. The first is an `int` containing the address that is to be written, and the second is a the data to be written (`unsigned char`). + +This function does not return any value. + +#### **`EEPROM.update( address, value )`** [[_example_]](examples/eeprom_update/eeprom_update.ino) + +This function is similar to `EEPROM.write()` however this method will only write data if the cell contents pointed to by `address` is different to `value`. This method can help prevent unnecessary wear on the EEPROM cells. + +This function does not return any value. + +#### **`EEPROM.get( address, object )`** [[_example_]](examples/eeprom_get/eeprom_get.ino) + +This function will retrieve any object from the EEPROM. +Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to read. + +This function returns a reference to the `object` passed in. It does not need to be used and is only returned for conveience. + +#### **`EEPROM.put( address, object )`** [[_example_]](examples/eeprom_put/eeprom_put.ino) + +This function will write any object to the EEPROM. +Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to write. + +This function uses the _update_ method to write its data, and therefore only rewrites changed cells. + +This function returns a reference to the `object` passed in. It does not need to be used and is only returned for conveience. + +#### **Subscript operator: `EEPROM[address]`** [[_example_]](examples/eeprom_crc/eeprom_crc.ino) + +This operator allows using the identifier `EEPROM` like an array. +EEPROM cells can be read _and_ **_written_** directly using this method. + +This operator returns a reference to the EEPROM cell. + +```c++ +unsigned char val; + +//Read first EEPROM cell. +val = EEPROM[ 0 ]; + +//Write first EEPROM cell. +EEPROM[ 0 ] = val; + +//Compare contents +if( val == EEPROM[ 0 ] ){ + //Do something... +} +``` + +#### **`EEPROM.length()`** + +This function returns an `unsigned int` containing the number of cells in the EEPROM. + +--- + +### **Advanced features** + +This library uses a component based approach to provide its functionality. This means you can also use these components to design a customized approach. Two background classes are available for use: `EERef` & `EEPtr`. + +#### **`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. + +```C++ +EERef ref = EEPROM[ 10 ]; //Create a reference to 11th cell. + +ref = 4; //write to EEPROM cell. + +unsigned char val = ref; //Read referenced cell. +``` + +#### **`EEPtr` class** + +This object is a bidirectional pointer to EEPROM cells represented by `EERef` objects. +Just like a normal pointer type, this type can be dereferenced and repositioned using +increment/decrement operators. + +```C++ +EEPtr ptr = 10; //Create a pointer to 11th cell. + +*ptr = 4; //dereference and write to EEPROM cell. + +unsigned char val = *ptr; //dereference and read. + +ptr++; //Move to next EEPROM cell. +``` + +#### **`EEPROM.begin()`** + +This function returns an `EEPtr` pointing to the first cell in the EEPROM. +This is useful for STL objects, custom iteration and C++11 style ranged for loops. + +#### **`EEPROM.end()`** + +This function returns an `EEPtr` pointing at the location after the last EEPROM cell. +Used with `begin()` to provide custom iteration. + +**Note:** The `EEPtr` returned is invalid as it is out of range. Infact the hardware causes wrapping of the address (overflow) and `EEPROM.end()` actually references the first EEPROM cell. diff --git a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino index 40b08bd..a4baacc 100644 --- a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino +++ b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -38,7 +38,7 @@ unsigned long eeprom_crc( void ){ unsigned long crc = ~0L; - for( int index = 0 ; index < 32 ; ++index ){ + for( int index = 0 ; index < EEPROM.length() ; ++index ){ crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4); crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4); crc = ~crc; diff --git a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino index 58475fd..dcd8678 100644 --- a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino +++ b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -20,7 +20,7 @@ void setup(){ float f = 0.00f; //Variable to store data read from EEPROM. - int eeAddress = 0; //Location of the IP address inside the class. + int eeAddress = 0; //EEPROM address to start reading from Serial.begin( 9600 ); Serial.print( "Read float from EEPROM: " ); diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino index a2d825c..650c90a 100644 --- a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -51,22 +51,6 @@ void setup() { idx++; }while( idx < EEPROM.length() ); - /*** - Iterate the EEPROM using a C++11 ranged for loop. - - This version of the loop is best explained in the example 'eeprom_pointer' - as this kind of iteration uses pointers rather than an index/integer. - - !! Note: C++11 is not yet enabled by default in any IDE version. - Unless you manually enable it, this sketch will not compile. - You can comment the loop below to verify the non C++11 content. - ***/ - - for( auto cell : EEPROM ){ - - //Add one to each cell in the EEPROM - cell += 1; - } } //End of setup function. diff --git a/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino b/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino deleted file mode 100644 index 637cdb7..0000000 --- a/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino +++ /dev/null @@ -1,74 +0,0 @@ -/*** - eeprom_pointer example. - - This example shows how the built-in EEPtr - object can be used to manipulate the EEPROM - using standard pointer arithmetic. - - Running this sketch is not necessary, this is - simply highlighting certain programming methods. - - Written by Christopher Andrews 2015 - Released under MIT licence. -***/ - -#include - -void setup() { - - Serial.begin(9600); - - /*** - In this example, we will iterate forward over the EEPROM, - starting at the 10th cell (remember indices are zero based). - ***/ - - EEPtr ptr = 9; - - //Rather than hard coding a length, we can use the provided .length() function. - - while( ptr < EEPROM.length() ){ - - Serial.print( *ptr, HEX ); //Print out hex value of the EEPROM cell pointed to by 'ptr' - Serial.print( ", " ); //Separate values with a comma. - ptr++; //Move to next cell - } - - /*** - In this example, we will iterate backwards over the EEPROM, - starting at the last cell. - ***/ - - ptr = EEPROM.length() - 1; - - do{ - - Serial.print( *ptr, HEX ); - Serial.print( ", " ); - - }while( ptr-- ); //When the pointer reaches zero the loop will end as zero is considered 'false'. - - - /*** - And just for clarity, the loop below is an equivalent implementation - of the C++11 ranged for loop. - ***/ - - for( EEPtr ptr = EEPROM.begin() ; ptr != EEPROM.end() ; ++ptr ){ - Serial.print( *ptr, HEX ); - Serial.print( ", " ); - } - - /*** - The actual C++11 version: - - for( auto ptr : EEPROM ){ - Serial.print( *ptr, HEX ); - Serial.print( ", " ); - } - ***/ - - -} - -void loop(){} \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino index 7575768..e99b4bd 100644 --- a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -16,6 +16,12 @@ #include +struct MyObject{ + float field1; + byte field2; + char name[10]; +}; + void setup(){ Serial.begin(9600); @@ -31,12 +37,6 @@ void setup(){ /** Put is designed for use with custom structures also. **/ - struct MyObject{ - float field1; - byte field2; - char name[10]; - }; - //Data to store. MyObject customVar = { 3.14f, diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino index 8567ed7..68c4ffc 100644 --- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -42,15 +42,15 @@ void loop() Rather than hard-coding the length, you should use the pre-provided length function. This will make your code portable to all AVR processors. ***/ - addr = addr + 1; - if(addr == EEPROM.length()) - addr = 0; + address = address + 1; + if(address == EEPROM.length()) + address = 0; /*** As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an EEPROM address is also doable by a bitwise and of the length - 1. - ++addr &= EEPROM.length() - 1; + ++address &= EEPROM.length() - 1; ***/ delay(500); diff --git a/libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino b/libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino deleted file mode 100644 index bcf84d6..0000000 --- a/libraries/EEPROM/examples/eeprom_reference/eeprom_reference.ino +++ /dev/null @@ -1,93 +0,0 @@ -/*** - eeprom_reference example. - - This example shows how to use the EEPROM - reference object EERef, which allows usage - similar to using a simple char (uint8_t in this case). - - Running this sketch is not necessary, this is - simply highlighting certain programming methods. - - Written by Christopher Andrews 2015 - Released under MIT licence. -***/ - -#include - -void setup() { - - - /*** - To create a reference to an EEPROM cell, simply create an EERef variable. - To let it know which cell you want to reference, you can simply assign the - address when you create it. - ***/ - - EERef ref = 0; - - /*** - An equivalent way is by calling the constructor directly: - EERef ref( 0 ); - ***/ - - /** Using the reference **/ - - /*** - Updating cell data. - To prevent unnecessary wear on the EEPROM cells - this function will only write the data when it - is different to what is already stored. - ***/ - - ref.update( 44 ); //May write 44 if not present. - ref.update( 44 ); //This second call will not write anything. - - /*** - Assign values directly to the EEPROM cell. - - You can use any form of assignment that would otherwise be available - to a standard uint8_t: - - *= - /= - += - -= - ^= - %= - &= - |= - <<= - >>= - - ***/ - - ref = 4; /*** - Take care to notice, this changes the EEPROM cell data, it does not - change the index of the cell referenced by 'ref'. - - Only the initial declaration like 'EERef ref = 0;' will set the address. - Using an assignment anywhere else modifies the referenced cell. - To modify the referenced address after declaring your variable see below. - ***/ - - /*** - Changing the referenced object. - The class has a member named 'index' which is an integer you can modify. - ***/ - - ref.index++; //Move reference to the next cell. - - - /*** - Grouping of references. - - Using EERef objects you can create a contiguous array referencing - non-contiguous EEPROM cells. - ***/ - - EERef array[] = { 0, 20, 40, 60, 80 }; - - -} //End of setup function. - -void loop(){} \ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino index dbf05ec..831056f 100644 --- a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -14,7 +14,7 @@ #include /** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ -int addr = 0; +int address = 0; void setup(){ /** EMpty setup **/ } @@ -32,13 +32,13 @@ void loop() these values will remain there when the board is turned off. ***/ - EEPROM.update(addr, val); + EEPROM.update(address, val); /*** - The function EEPROM.update(addr, val) is equivalent to the following: + The function EEPROM.update(address, val) is equivalent to the following: - if( EEPROM.read(addr) != val ){ - EEPROM.write(addr, val); + if( EEPROM.read(address) != val ){ + EEPROM.write(address, val); } ***/ @@ -54,15 +54,15 @@ void loop() Rather than hard-coding the length, you should use the pre-provided length function. This will make your code portable to all AVR processors. ***/ - addr = addr + 1; - if(addr == EEPROM.length()) - addr = 0; + address = address + 1; + if(address == EEPROM.length()) + address = 0; /*** As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an EEPROM address is also doable by a bitwise and of the length - 1. - ++addr &= EEPROM.length() - 1; + ++address &= EEPROM.length() - 1; ***/ delay(100); -- cgit v1.2.3-18-g5258 From d8656b8c5249c9d06cd8ed96b2061759ab69b5bf Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 24 Mar 2015 10:20:00 +0100 Subject: EEPROM: examples: fix Serial for board Leonardo --- libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino | 5 ++++- libraries/EEPROM/examples/eeprom_get/eeprom_get.ino | 5 ++++- libraries/EEPROM/examples/eeprom_put/eeprom_put.ino | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) (limited to 'libraries/EEPROM') diff --git a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino index a4baacc..8461d56 100644 --- a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino +++ b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -14,7 +14,10 @@ void setup(){ //Start serial Serial.begin(9600); - + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + //Print length of data to run CRC on. Serial.print( "EEPROM length: " ); Serial.println( EEPROM.length() ); diff --git a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino index dcd8678..6620999 100644 --- a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino +++ b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -23,8 +23,11 @@ void setup(){ int eeAddress = 0; //EEPROM address to start reading from Serial.begin( 9600 ); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } Serial.print( "Read float from EEPROM: " ); - + //Get the float data from the EEPROM at position 'eeAddress' EEPROM.get( eeAddress, f ); Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino index e99b4bd..186cf95 100644 --- a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -25,7 +25,10 @@ struct MyObject{ void setup(){ Serial.begin(9600); - + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + float f = 123.456f; //Variable to store in EEPROM. int eeAddress = 0; //Location we want the data to be put. -- cgit v1.2.3-18-g5258