-
Notifications
You must be signed in to change notification settings - Fork 69
/
CAT24I2CFlashSettingsStorage.cpp
104 lines (65 loc) · 2.1 KB
/
CAT24I2CFlashSettingsStorage.cpp
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
#include "./CAT24I2CFlashSettingsStorage.h"
CAT24I2CFlashSettingsStorage::CAT24I2CFlashSettingsStorage(uint8_t address, uint16_t offset) {
_address = address;
_offset = offset;
};
CAT24I2CFlashSettingsStorage::~CAT24I2CFlashSettingsStorage() {};
void CAT24I2CFlashSettingsStorage::init(TwoWire* wire) {
SettingsStorage::init(this);
_wire = wire;
reset();
};
RegisterIO& CAT24I2CFlashSettingsStorage::operator>>(uint8_t& valueToSet) {
readBytes(&valueToSet, 1);
return *this;
};
RegisterIO& CAT24I2CFlashSettingsStorage::operator>>(float& valueToSet) {
readBytes(&valueToSet, 4);
return *this;
};
RegisterIO& CAT24I2CFlashSettingsStorage::operator>>(uint32_t& valueToSet) {
readBytes(&valueToSet, 4);
return *this;
};
RegisterIO& CAT24I2CFlashSettingsStorage::operator<<(uint8_t value) {
writeBytes(&value, 1);
return *this;
};
RegisterIO& CAT24I2CFlashSettingsStorage::operator<<(float value) {
writeBytes(&value, 4);
return *this;
};
RegisterIO& CAT24I2CFlashSettingsStorage::operator<<(uint32_t value) {
writeBytes(&value, 4);
return *this;
};
void CAT24I2CFlashSettingsStorage::beforeSave() {
reset();
};
void CAT24I2CFlashSettingsStorage::beforeLoad() {
reset();
_wire->beginTransmission(_address);
_wire->write(_currptr >> 8);
_wire->write(_currptr & 0xFF);
_wire->endTransmission(false);
};
void CAT24I2CFlashSettingsStorage::reset() {
_currptr = _offset;
};
int CAT24I2CFlashSettingsStorage::readBytes(void* valueToSet, int numBytes) {
int read = _wire->requestFrom((uint8_t)_address, (uint8_t)numBytes, (uint8_t)true);
_currptr += read;
if (read==numBytes)
_wire->readBytes((uint8_t*)valueToSet, numBytes);
return read;
};
int CAT24I2CFlashSettingsStorage::writeBytes(void* value, int numBytes) {
_wire->beginTransmission(_address);
_wire->write(_currptr >> 8);
_wire->write(_currptr & 0xFF);
size_t written = _wire->write((uint8_t*)value, numBytes);
_wire->endTransmission(true);
_currptr += written;
delay(5); // write-cycle time
return written;
};