-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataStore.h
159 lines (128 loc) · 3.88 KB
/
DataStore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <EEPROM.h>
#ifndef DataStore_h
#define DataStore_h
class DataStore {
private:
const int ADDRESS_UP_HOURS = 0; // uint8_t -> 1 byte
const int ADDRESS_UP_MINUTES = 1;
const int ADDRESS_DOWN_HOURS = 2;
const int ADDRESS_DOWN_MINUTES = 3;
const int ADDRESS_UP_POSITION = 4; // long -> 4 bytes
const int ADDRESS_DOWN_POSITION = 8;
const int ADDRESS_CURRENT_POSITION = 12;
uint8_t upHours = 0;
uint8_t upMinutes = 0;
uint8_t downHours = 0;
uint8_t downMinutes = 0;
long upPosition = 0;
long downPosition = 0;
long currentPosition = 0;
void eepromWriteUint8(int address, uint8_t value) {
EEPROM.write(address, value);
}
uint8_t eepromReadUint8(long address) {
uint8_t value = EEPROM.read(address);
return value;
}
void eepromWriteLong(int address, long value) {
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long eepromReadLong(long address) {
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF)
+ ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
public:
DataStore(){
}
uint8_t getDownHours() const {
return downHours;
}
void setDownHours(uint8_t downHours = 0) {
eepromWriteUint8(ADDRESS_DOWN_HOURS, downHours);
this->downHours = downHours;
}
uint8_t getDownMinutes() const {
return downMinutes;
}
void setDownMinutes(uint8_t downMinutes = 0) {
eepromWriteUint8(ADDRESS_DOWN_MINUTES, downMinutes);
this->downMinutes = downMinutes;
}
uint8_t getUpHours() const {
return upHours;
}
void setUpHours(uint8_t upHours = 0) {
eepromWriteUint8(ADDRESS_UP_HOURS, upHours);
this->upHours = upHours;
}
uint8_t getUpMinutes() const {
return upMinutes;
}
void setUpMinutes(uint8_t upMinutes = 0) {
eepromWriteUint8(ADDRESS_UP_MINUTES, upMinutes);
this->upMinutes = upMinutes;
}
long getDownPosition() const {
return downPosition;
}
void setDownPosition(long downPosition = 0) {
eepromWriteLong(ADDRESS_DOWN_POSITION, downPosition);
this->downPosition = downPosition;
}
long getUpPosition() const {
return upPosition;
}
void setUpPosition(long upPosition = 0) {
eepromWriteLong(ADDRESS_UP_POSITION, upPosition);
this->upPosition = upPosition;
}
long getCurrentPosition() const {
return currentPosition;
}
void setCurrentPosition(long currentPosition = 0) {
eepromWriteLong(ADDRESS_CURRENT_POSITION, currentPosition);
this->currentPosition = currentPosition;
}
void reset(){
this->setUpHours(0);
this->setUpMinutes(0);
this->setDownHours(0);
this->setDownMinutes(0);
this->setUpPosition(0);
this->setDownPosition(0);
this->setCurrentPosition(0);
}
void init(){
this->upHours = eepromReadUint8(ADDRESS_UP_HOURS);
this->upMinutes = eepromReadUint8(ADDRESS_UP_MINUTES);
this->downHours = eepromReadUint8(ADDRESS_DOWN_HOURS);
this->downMinutes = eepromReadUint8(ADDRESS_DOWN_MINUTES);
this->upPosition = eepromReadLong(ADDRESS_UP_POSITION);
this->downPosition = eepromReadLong(ADDRESS_DOWN_POSITION);
this->currentPosition = eepromReadLong(ADDRESS_CURRENT_POSITION);
// Serial.println( this->upHours );
// Serial.println( this->upMinutes );
// Serial.println( this->downHours );
// Serial.println( this->downMinutes );
// Serial.println( this->upPosition );
// Serial.println( this->downPosition );
// Serial.println( this->currentPosition );
}
};
#endif