Skip to content

Commit 46e810c

Browse files
committed
Added new version of EEPROM library.
1 parent 1da3da0 commit 46e810c

File tree

4 files changed

+138
-61
lines changed

4 files changed

+138
-61
lines changed

libraries/EEPROM/EEPROM.cpp

-50
This file was deleted.

libraries/EEPROM/EEPROM.h

+131-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
EEPROM.h - EEPROM library
3-
Copyright (c) 2006 David A. Mellis. All right reserved.
3+
Original Copyright (c) 2006 David A. Mellis. All right reserved.
4+
New version by Christopher Andrews 2015.
45
56
This library is free software; you can redistribute it and/or
67
modify it under the terms of the GNU Lesser General Public
@@ -21,15 +22,137 @@
2122
#define EEPROM_h
2223

2324
#include <inttypes.h>
25+
#include <avr/eeprom.h>
26+
#include <avr/io.h>
2427

25-
class EEPROMClass
26-
{
27-
public:
28-
uint8_t read(int);
29-
void write(int, uint8_t);
28+
/***
29+
EERef class.
30+
31+
This object references an EEPROM cell.
32+
Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
33+
This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
34+
***/
35+
36+
struct EERef{
37+
38+
EERef( const int index )
39+
: index( index ) {}
40+
41+
//Access/read members.
42+
uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); }
43+
operator const uint8_t() const { return **this; }
44+
45+
//Assignment/write members.
46+
EERef &operator=( const EERef &ref ) { return *this = *ref; }
47+
EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; }
48+
EERef &operator +=( uint8_t in ) { return *this = **this + in; }
49+
EERef &operator -=( uint8_t in ) { return *this = **this - in; }
50+
EERef &operator *=( uint8_t in ) { return *this = **this * in; }
51+
EERef &operator /=( uint8_t in ) { return *this = **this / in; }
52+
EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; }
53+
EERef &operator %=( uint8_t in ) { return *this = **this % in; }
54+
EERef &operator &=( uint8_t in ) { return *this = **this & in; }
55+
EERef &operator |=( uint8_t in ) { return *this = **this | in; }
56+
EERef &operator <<=( uint8_t in ) { return *this = **this << in; }
57+
EERef &operator >>=( uint8_t in ) { return *this = **this >> in; }
58+
59+
EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; }
60+
61+
/** Prefix increment/decrement **/
62+
EERef& operator++() { return *this += 1; }
63+
EERef& operator--() { return *this -= 1; }
64+
65+
/** Postfix increment/decrement **/
66+
uint8_t operator++ (int){
67+
uint8_t ret = **this;
68+
return ++(*this), ret;
69+
}
70+
71+
uint8_t operator-- (int){
72+
uint8_t ret = **this;
73+
return --(*this), ret;
74+
}
75+
76+
int index; //Index of current EEPROM cell.
3077
};
3178

32-
extern EEPROMClass EEPROM;
79+
/***
80+
EEPtr class.
81+
82+
This object is a bidirectional pointer to EEPROM cells represented by EERef objects.
83+
Just like a normal pointer type, this can be dereferenced and repositioned using
84+
increment/decrement operators.
85+
***/
86+
87+
struct EEPtr{
88+
89+
EEPtr( const int index )
90+
: index( index ) {}
91+
92+
operator const int() const { return index; }
93+
EEPtr &operator=( int in ) { return index = in, *this; }
94+
95+
96+
//Iterator functionality.
97+
bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
98+
EERef operator*() { return( this->index ); }
99+
100+
101+
/** Prefix increment/decrement **/
102+
EEPtr& operator++() { return ++index, *this; }
103+
EEPtr& operator--() { return --index, *this; }
104+
105+
106+
/** Postfix increment/decrement **/
107+
EEPtr operator++ (int){
108+
int ret = index;
109+
return ++index, ret;
110+
}
33111

34-
#endif
112+
EEPtr operator-- (int){
113+
int ret = index;
114+
return --index, ret;
115+
}
116+
117+
int index; //Index of current EEPROM cell.
118+
};
119+
120+
/***
121+
EEPROMClass class.
122+
123+
This object represents the entire EEPROM space.
124+
It wraps the functionality of EEPtr and EERef into a basic interface.
125+
This class is also 100% backwards compatible with earlier Arduino core releases.
126+
***/
127+
128+
struct EEPROMClass{
35129

130+
//Basic user access methods.
131+
EERef operator[]( const int index ) { return( index ); }
132+
uint8_t read( int idx ) { return (EERef( idx )); }
133+
void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
134+
void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
135+
136+
//STL and C++11 iteration capability.
137+
EEPtr begin() { return( 0x00 ); }
138+
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
139+
uint16_t length() { return E2END + 1; }
140+
141+
//Functionality to 'get' and 'put' objects to and from EEPROM.
142+
template< typename T > T &get( int idx, T &t ){
143+
EEPtr e = idx;
144+
uint8_t *ptr = (uint8_t*) &t;
145+
for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
146+
return t;
147+
}
148+
149+
template< typename T > const T &put( int idx, const T &t ){
150+
EEPtr e = idx;
151+
const uint8_t *ptr = (const uint8_t*) &t;
152+
for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ );
153+
return t;
154+
}
155+
};
156+
157+
extern EEPROMClass EEPROM;
158+
#endif

libraries/EEPROM/keywords.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#######################################
2-
# Syntax Coloring Map For Ultrasound
2+
# Syntax Coloring Map For EEPROM
33
#######################################
44

55
#######################################
66
# Datatypes (KEYWORD1)
77
#######################################
88

99
EEPROM KEYWORD1
10+
EERef KEYWORD1
11+
EEPtr KEYWORD2
1012

1113
#######################################
1214
# Methods and Functions (KEYWORD2)
1315
#######################################
1416

17+
update KEYWORD2
18+
1519
#######################################
1620
# Constants (LITERAL1)
1721
#######################################

libraries/EEPROM/library.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=EEPROM
2-
version=1.0
3-
author=Arduino
2+
version=2.0
3+
author=Arduino, Christopher Andrews
44
maintainer=Arduino <info@arduino.cc>
55
sentence=Enables reading and writing to the permanent board storage. For all Arduino boards BUT Arduino DUE.
66
paragraph=

0 commit comments

Comments
 (0)