1
1
/*
2
2
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.
4
5
5
6
This library is free software; you can redistribute it and/or
6
7
modify it under the terms of the GNU Lesser General Public
21
22
#define EEPROM_h
22
23
23
24
#include < inttypes.h>
25
+ #include < avr/eeprom.h>
26
+ #include < avr/io.h>
24
27
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.
30
77
};
31
78
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
+ }
33
111
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 {
35
129
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
0 commit comments