forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEepromManager.cpp
177 lines (144 loc) · 4.62 KB
/
EepromManager.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2013 BrewPi/Elco Jacobs.
* Copyright 2013 Matthew McGowan
*
* This file is part of BrewPi.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BrewPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Brewpi.h"
#include <stddef.h>
#include "EepromManager.h"
#include "TempControl.h"
#include "EepromFormat.h"
#include "PiLink.h"
EepromManager eepromManager;
EepromAccess eepromAccess;
#define pointerOffset(x) offsetof(EepromFormat, x)
EepromManager::EepromManager()
{
eepromSizeCheck();
}
bool EepromManager::hasSettings()
{
uint8_t version = eepromAccess.readByte(pointerOffset(version));
return (version==EEPROM_FORMAT_VERSION);
}
void EepromManager::zapEeprom()
{
for (uint16_t offset=0; offset<EepromFormat::MAX_EEPROM_SIZE; offset++)
eepromAccess.writeByte(offset, 0xFF);
#ifdef ESP8266
eepromAccess.commit();
#endif
}
void EepromManager::initializeEeprom()
{
// clear all eeprom
// for (uint16_t offset=0; offset<EepromFormat::MAX_EEPROM_SIZE; offset++)
// eepromAccess.writeByte(offset, 0);
#ifdef ESP8266
eepromAccess.set_manual_commit(true);
#endif
zapEeprom();
deviceManager.setupUnconfiguredDevices();
// fetch the default values
tempControl.loadDefaultConstants();
tempControl.loadDefaultSettings();
// write the default constants
for (uint8_t c=0; c<EepromFormat::MAX_CHAMBERS; c++) {
eptr_t pv = pointerOffset(chambers)+(c*sizeof(ChamberBlock)) ;
tempControl.storeConstants(pv+offsetof(ChamberBlock, chamberSettings.cc));
pv += offsetof(ChamberBlock, beer)+offsetof(BeerBlock, cs);
for (uint8_t b=0; b<ChamberBlock::MAX_BEERS; b++) {
// logDeveloper(PSTR("EepromManager - saving settings for beer %d at %d"), b, (uint16_t)pv);
tempControl.storeSettings(pv);
pv += sizeof(BeerBlock); // advance to next beer
}
}
// set the version flag - so that storeDevice will work
eepromAccess.writeByte(0, EEPROM_FORMAT_VERSION);
saveDefaultDevices();
// set state to startup
tempControl.init();
#ifdef ESP8266
eepromAccess.set_manual_commit(false);
eepromAccess.commit();
#endif
}
uint8_t EepromManager::saveDefaultDevices()
{
return 0;
}
#define arraySize(x) (sizeof(x)/sizeof(x[0]))
bool EepromManager::applySettings()
{
if (!hasSettings())
return false;
// start from a clean state
deviceManager.setupUnconfiguredDevices();
logDebug("Applying settings");
// load the one chamber and one beer for now
eptr_t pv = pointerOffset(chambers);
tempControl.loadConstants(pv+offsetof(ChamberBlock, chamberSettings.cc));
tempControl.loadSettings(pv+offsetof(ChamberBlock, beer[0].cs));
logDebug("Applied settings");
DeviceConfig deviceConfig;
for (uint8_t index = 0; fetchDevice(deviceConfig, index); index++)
{
if (deviceManager.isDeviceValid(deviceConfig, deviceConfig, index))
deviceManager.installDevice(deviceConfig);
else {
clear((uint8_t*)&deviceConfig, sizeof(deviceConfig));
eepromManager.storeDevice(deviceConfig, index);
}
}
return true;
}
void EepromManager::storeTempConstantsAndSettings()
{
uint8_t chamber = 0;
eptr_t pv = pointerOffset(chambers);
pv += sizeof(ChamberBlock)*chamber;
tempControl.storeConstants(pv+offsetof(ChamberBlock, chamberSettings.cc));
storeTempSettings();
}
void EepromManager::storeTempSettings()
{
uint8_t chamber = 0;
eptr_t pv = pointerOffset(chambers);
pv += sizeof(ChamberBlock)*chamber;
// for now assume just one beer.
tempControl.storeSettings(pv+offsetof(ChamberBlock, beer[0].cs));
}
bool EepromManager::fetchDevice(DeviceConfig& config, uint8_t deviceIndex)
{
bool ok = (hasSettings() && deviceIndex<EepromFormat::MAX_DEVICES);
if (ok)
eepromAccess.readDeviceDefinition(config, pointerOffset(devices)+sizeof(DeviceConfig)*deviceIndex, sizeof(DeviceConfig));
return ok;
}
bool EepromManager::storeDevice(const DeviceConfig& config, uint8_t deviceIndex)
{
bool ok = (hasSettings() && deviceIndex<EepromFormat::MAX_DEVICES);
if (ok)
eepromAccess.writeDeviceDefinition(pointerOffset(devices)+sizeof(DeviceConfig)*deviceIndex, config, sizeof(DeviceConfig));
return ok;
}
void fill(int8_t* p, uint8_t size) {
while (size-->0) *p++ = -1;
}
void clear(uint8_t* p, uint8_t size) {
while (size-->0) *p++ = 0;
}