forked from Arduino-CI/arduino_ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEPROM.h
64 lines (54 loc) · 1.43 KB
/
EEPROM.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
#pragma once
#include <cassert>
#include <inttypes.h>
#include <Godmode.h>
// Does the current board have EEPROM?
#ifndef EEPROM_SIZE
// In lieu of an "EEPROM.h not found" error for unsupported boards
#error "EEPROM library not available for your board"
#endif
class EEPROMClass {
private:
GodmodeState* state;
public:
// constructor
EEPROMClass() {
state = GODMODE();
}
// array subscript operator
uint8_t &operator[](const int index) {
assert(index < EEPROM_SIZE);
return state->eeprom[index];
}
uint8_t read(const int index) {
assert(index < EEPROM_SIZE);
return state->eeprom[index];
}
void write(const int index, const uint8_t value) {
assert(index < EEPROM_SIZE);
state->eeprom[index] = value;
}
void update(const int index, const uint8_t value) {
assert(index < EEPROM_SIZE);
state->eeprom[index] = value;
}
uint16_t length() { return EEPROM_SIZE; }
// read any object
template <typename T> T &get(const int index, T &object) {
uint8_t *ptr = (uint8_t *)&object;
for (int i = 0; i < sizeof(T); ++i) {
*ptr++ = read(index + i);
}
return object;
}
// write any object
template <typename T> const T &put(const int index, T &object) {
const uint8_t *ptr = (const uint8_t *)&object;
for (int i = 0; i < sizeof(T); ++i) {
write(index + i, *ptr++);
}
return object;
}
};
// global available in Godmode.cpp
extern EEPROMClass EEPROM;