MakeBlock Drive Updated Updated library for MakeBlock Ranger

Loading...
Searching...
No Matches
EEPROM.h
1/*
2 EEPROM.h - EEPROM library
3 Original Copyright (c) 2006 David A. Mellis. All right reserved.
4 New version by Christopher Andrews 2015.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#ifndef EEPROM_h
22#define EEPROM_h
23
24#include <inttypes.h>
25#include <avr/eeprom.h>
26#include <avr/io.h>
27
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 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
62 EERef& operator++() { return *this += 1; }
63 EERef& operator--() { return *this -= 1; }
64
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.
77};
78
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 int() const { return index; }
93 EEPtr &operator=( int in ) { return index = in, *this; }
94
95 //Iterator functionality.
96 bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
97 EERef operator*() { return index; }
98
100 EEPtr& operator++() { return ++index, *this; }
101 EEPtr& operator--() { return --index, *this; }
102 EEPtr operator++ (int) { return index++; }
103 EEPtr operator-- (int) { return index--; }
104
105 int index; //Index of current EEPROM cell.
106};
107
108/***
109 EEPROMClass class.
110
111 This object represents the entire EEPROM space.
112 It wraps the functionality of EEPtr and EERef into a basic interface.
113 This class is also 100% backwards compatible with earlier Arduino core releases.
114***/
115
116 struct EEPROMClass{
117
118 //Basic user access methods.
119 EERef operator[]( const int idx ) { return idx; }
120 uint8_t read( int idx ) { return EERef( idx ); }
121 void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
122 void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
123
124 //STL and C++11 iteration capability.
125 EEPtr begin() { return 0x00; }
126 EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
127 uint16_t length() { return E2END + 1; }
128
129 //Functionality to 'get' and 'put' objects to and from EEPROM.
130 template< typename T > T &get( int idx, T &t ){
131 EEPtr e = idx;
132 uint8_t *ptr = (uint8_t*) &t;
133 for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
134 return t;
135 }
136
137 template< typename T > const T &put( int idx, const T &t ){
138 EEPtr e = idx;
139 const uint8_t *ptr = (const uint8_t*) &t;
140 for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ );
141 return t;
142 }
143};
144
145__attribute__((unused))
146 static EEPROMClass EEPROM;
147#endif
EEPROMClass
Definition EEPROM.h:116
EEPtr
Definition EEPROM.h:87
EEPtr::operator++
EEPtr & operator++()
Definition EEPROM.h:100
EERef
Definition EEPROM.h:36
EERef::operator++
EERef & operator++()
Definition EEPROM.h:62