Skip to content

Commit dd1ec99

Browse files
committed
Added additional examples to EEPROM lib
1 parent 46e810c commit dd1ec99

File tree

7 files changed

+455
-0
lines changed

7 files changed

+455
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***
2+
Written by Christopher Andrews.
3+
CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ).
4+
5+
A CRC is a simple way of checking whether data has changed or become corrupted.
6+
This example calculates a CRC value directly on the EEPROM values.
7+
The purpose of this example is to highlight how the EEPROM object can be used just like an array.
8+
***/
9+
10+
#include <Arduino.h>
11+
#include <EEPROM.h>
12+
13+
void setup(){
14+
15+
//Start serial
16+
Serial.begin(9600);
17+
18+
//Print length of data to run CRC on.
19+
Serial.print( "EEPROM length: " );
20+
Serial.println( EEPROM.length() );
21+
22+
//Print the result of calling eeprom_crc()
23+
Serial.print( "CRC32 of EEPROM data: 0x" );
24+
Serial.println( eeprom_crc(), HEX );
25+
Serial.print( "\n\nDone!" );
26+
}
27+
28+
void loop(){ /* Empty loop */ }
29+
30+
unsigned long eeprom_crc( void ){
31+
32+
const unsigned long crc_table[16] = {
33+
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
34+
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
35+
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
36+
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
37+
};
38+
39+
unsigned long crc = ~0L;
40+
41+
for( int index = 0 ; index < 32 ; ++index ){
42+
crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4);
43+
crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4);
44+
crc = ~crc;
45+
}
46+
return crc;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***
2+
eeprom_get example.
3+
4+
This shows how to use the EEPROM.get() method.
5+
6+
To pre-set the EEPROM data, run the example sketch eeprom_put.
7+
This sketch will run without it, however, the values shown
8+
will be shown from what ever is already on the EEPROM.
9+
10+
This may cause the serial object to print out a large string
11+
of garbage if there is no null character inside one of the strings
12+
loaded.
13+
14+
Written by Christopher Andrews 2015
15+
Released under MIT licence.
16+
***/
17+
18+
#include <EEPROM.h>
19+
20+
void setup(){
21+
22+
float f = 0.00f; //Variable to store data read from EEPROM.
23+
int eeAddress = 0; //Location of the IP address inside the class.
24+
25+
Serial.begin( 9600 );
26+
Serial.print( "Read float from EEPROM: " );
27+
28+
//Get the float data from the EEPROM at position 'eeAddress'
29+
EEPROM.get( eeAddress, f );
30+
Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
31+
32+
/***
33+
As get also returns a reference to 'f', you can use it inline.
34+
E.g: Serial.print( EEPROM.get( eeAddress, f ) );
35+
***/
36+
37+
/***
38+
Get can be used with custom structures too.
39+
I have separated this into an extra function.
40+
***/
41+
42+
secondTest(); //Run the next test.
43+
}
44+
45+
struct MyObject{
46+
float field1;
47+
byte field2;
48+
char name[10];
49+
};
50+
51+
void secondTest(){
52+
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
53+
54+
MyObject customVar; //Variable to store custom object read from EEPROM.
55+
EEPROM.get( eeAddress, customVar );
56+
57+
Serial.println( "Read custom object from EEPROM: " );
58+
Serial.println( customVar.field1 );
59+
Serial.println( customVar.field2 );
60+
Serial.println( customVar.name );
61+
}
62+
63+
void loop(){ /* Empty loop */ }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/***
2+
eeprom_iteration example.
3+
4+
A set of example snippets highlighting the
5+
simplest methods for traversing the EEPROM.
6+
7+
Running this sketch is not necessary, this is
8+
simply highlighting certain programming methods.
9+
10+
Written by Christopher Andrews 2015
11+
Released under MIT licence.
12+
***/
13+
14+
#include <EEPROM.h>
15+
16+
void setup() {
17+
18+
/***
19+
Iterate the EEPROM using a for loop.
20+
***/
21+
22+
for( int index = 0 ; index < EEPROM.length() ; index++ ){
23+
24+
//Add one to each cell in the EEPROM
25+
EEPROM[ index ] += 1;
26+
}
27+
28+
/***
29+
Iterate the EEPROM using a while loop.
30+
***/
31+
32+
int index = 0;
33+
34+
while( index < EEPROM.length() ){
35+
36+
//Add one to each cell in the EEPROM
37+
EEPROM[ index ] += 1;
38+
index++;
39+
}
40+
41+
/***
42+
Iterate the EEPROM using a do-while loop.
43+
***/
44+
45+
int idx = 0;
46+
47+
do{
48+
49+
//Add one to each cell in the EEPROM
50+
EEPROM[ index ] += 1;
51+
index++;
52+
}while( index < EEPROM.length() );
53+
54+
/***
55+
Iterate the EEPROM using a C++11 ranged for loop.
56+
57+
This version of the loop is best explained in the example 'eeprom_pointer'
58+
as this kind of iteration uses pointers rather than an index/integer.
59+
60+
!! Note: C++11 is not yet enabled by default in any IDE version.
61+
Unless you manually enable it, this sketch will not compile.
62+
You can comment the loop below to verify the non C++11 content.
63+
***/
64+
65+
for( auto cell : EEPROM ){
66+
67+
//Add one to each cell in the EEPROM
68+
cell += 1;
69+
}
70+
71+
} //End of setup function.
72+
73+
void loop(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/***
2+
eeprom_pointer example.
3+
4+
This example shows how the built-in EEPtr
5+
object can be used to manipulate the EEPROM
6+
using standard pointer arithmetic.
7+
8+
Running this sketch is not necessary, this is
9+
simply highlighting certain programming methods.
10+
11+
Written by Christopher Andrews 2015
12+
Released under MIT licence.
13+
***/
14+
15+
#include <EEPROM.h>
16+
17+
void setup() {
18+
19+
Serial.begin(9600);
20+
21+
/***
22+
In this example, we will iterate forward over the EEPROM,
23+
starting at the 10th cell (remember indices are zero based).
24+
***/
25+
26+
EEPtr ptr = 9;
27+
28+
//Rather than hard coding a length, we can use the provided .length() function.
29+
30+
while( ptr < EEPROM.length() ){
31+
32+
Serial.print( *ptr, HEX ); //Print out hex value of the EEPROM cell pointed to by 'ptr'
33+
Serial.print( ", " ); //Separate values with a comma.
34+
ptr++; //Move to next cell
35+
}
36+
37+
/***
38+
In this example, we will iterate backwards over the EEPROM,
39+
starting at the last cell.
40+
***/
41+
42+
ptr = EEPROM.length() - 1;
43+
44+
do{
45+
46+
Serial.print( *ptr, HEX );
47+
Serial.print( ", " );
48+
49+
}while( ptr-- ); //When the pointer reaches zero the loop will end as zero is considered 'false'.
50+
51+
52+
/***
53+
And just for clarity, the loop below is an equivalent implementation
54+
of the C++11 ranged for loop.
55+
***/
56+
57+
for( EEPtr ptr = EEPROM.begin() ; item != EEPROM.end() ; ++item ){
58+
Serial.print( *ptr, HEX );
59+
Serial.print( ", " );
60+
}
61+
62+
/***
63+
The actual C++11 version:
64+
65+
for( auto ptr : EEPROM ){
66+
Serial.print( *ptr, HEX );
67+
Serial.print( ", " );
68+
}
69+
***/
70+
71+
72+
}
73+
74+
void loop(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***
2+
eeprom_put example.
3+
4+
This shows how to use the EEPROM.put() method.
5+
Also, this sketch will pre-set the EEPROM data for the
6+
example sketch eeprom_get.
7+
8+
Note, unlike the single byte version EEPROM.write(),
9+
the put method will use update semantics. As in a byte
10+
will only be written to the EEPROM if the data is actually
11+
different.
12+
13+
Written by Christopher Andrews 2015
14+
Released under MIT licence.
15+
***/
16+
17+
#include <EEPROM.h>
18+
19+
void setup(){
20+
21+
Serial.begin(9600);
22+
23+
float f = 123.456f; //Variable to store in EEPROM.
24+
int eeAddress = 0; //Location we want the data to be put.
25+
26+
27+
//One simple call, with the address first and the object second.
28+
EEPROM.put( eeAddress, f );
29+
30+
Serial.println("Written float data type!");
31+
32+
/** Put is designed for use with custom structures also. **/
33+
34+
struct MyObject{
35+
float field1;
36+
byte field2;
37+
char name[10];
38+
};
39+
40+
//Data to store.
41+
MyObject customVar = {
42+
3.14f,
43+
65,
44+
"Working!"
45+
};
46+
47+
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
48+
49+
EEPROM.put( eeAddress, customVar );
50+
Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" );
51+
}
52+
53+
void loop(){ /* Empty loop */ }

0 commit comments

Comments
 (0)