|
@@ -0,0 +1,287 @@
|
|
1
|
+#include <LiquidCrystal.h>
|
|
2
|
+#include <JC_Button.h>
|
|
3
|
+#include <OneWire.h>
|
|
4
|
+#include <DallasTemperature.h>
|
|
5
|
+#include "Boiler.h"
|
|
6
|
+#include "Storage.h"
|
|
7
|
+#include "AppCore.h"
|
|
8
|
+
|
|
9
|
+#define HIBERNATE_DELAY 5000
|
|
10
|
+#define SENSORS_CHECK_INTERVAL 2000
|
|
11
|
+#define SENSORS_REQUEST_DELAY 750
|
|
12
|
+#define TEMP_TRIGGER 10
|
|
13
|
+#define LCD_CHAR_SENSOR 1
|
|
14
|
+
|
|
15
|
+#define PIN_ONEWIRE 12
|
|
16
|
+#define PIN_BTN_MODE 11
|
|
17
|
+#define PIN_BTN_MINUS 10
|
|
18
|
+#define PIN_BTN_PLUS 9
|
|
19
|
+#define PIN_LCD_LED 8
|
|
20
|
+#define PIN_LCD_RS 7
|
|
21
|
+#define PIN_LCD_ENABLE 6
|
|
22
|
+#define PIN_LCD_D0 5
|
|
23
|
+#define PIN_LCD_D1 4
|
|
24
|
+#define PIN_LCD_D2 3
|
|
25
|
+#define PIN_LCD_D3 2
|
|
26
|
+
|
|
27
|
+byte g_sensorChar[8] = {
|
|
28
|
+ B00100,
|
|
29
|
+ B01110,
|
|
30
|
+ B01110,
|
|
31
|
+ B01110,
|
|
32
|
+ B01110,
|
|
33
|
+ B11111,
|
|
34
|
+ B11111,
|
|
35
|
+ B01110,
|
|
36
|
+};
|
|
37
|
+
|
|
38
|
+Storage g_storage;
|
|
39
|
+
|
|
40
|
+OneWire g_oneWire(PIN_ONEWIRE);
|
|
41
|
+DallasTemperature g_sensors(&g_oneWire);
|
|
42
|
+DeviceAddress g_sensor1;
|
|
43
|
+
|
|
44
|
+Button g_btnMode(PIN_BTN_MODE);
|
|
45
|
+Button g_btnMinus(PIN_BTN_MINUS);
|
|
46
|
+Button g_btnPlus(PIN_BTN_PLUS);
|
|
47
|
+Button* g_buttons[3] = {&g_btnMode, &g_btnMinus, &g_btnPlus};
|
|
48
|
+#define BUTTONS_COUNT (sizeof(g_buttons) / sizeof(*g_buttons))
|
|
49
|
+
|
|
50
|
+LiquidCrystal g_lcd(PIN_LCD_RS, PIN_LCD_ENABLE, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
|
|
51
|
+
|
|
52
|
+UiStateEnum g_modeSequence[3] = {WaterSetting, HeaterSetting, Lighting};
|
|
53
|
+#define MODE_SEQUENCE_COUNT (sizeof(g_modeSequence) / sizeof(*g_modeSequence))
|
|
54
|
+
|
|
55
|
+AppCoreState g_appCoreState = {
|
|
56
|
+ .appState = {
|
|
57
|
+ .lastSensorRequestMs = 0,
|
|
58
|
+ .hasReadSensors = true,
|
|
59
|
+ .water = {
|
|
60
|
+ .current = TEMP_T_INVALID,
|
|
61
|
+ .setting = 0,
|
|
62
|
+ .isActive = false
|
|
63
|
+ },
|
|
64
|
+ .heater = {
|
|
65
|
+ .current = TEMP_T_INVALID,
|
|
66
|
+ .setting = 0,
|
|
67
|
+ .isActive = false
|
|
68
|
+ }
|
|
69
|
+ },
|
|
70
|
+ .uiState = {
|
|
71
|
+ .state = Lighting,
|
|
72
|
+ .lastOpMs = 0,
|
|
73
|
+ .modeSequenceIndex = MODE_SEQUENCE_COUNT - 1,
|
|
74
|
+ .isUpdateNeeded = true
|
|
75
|
+ }
|
|
76
|
+};
|
|
77
|
+
|
|
78
|
+void tmpToStr(char* out, temp_t temp)
|
|
79
|
+{
|
|
80
|
+ if (temp == TEMP_T_INVALID)
|
|
81
|
+ {
|
|
82
|
+ strcpy(out, " -- ");
|
|
83
|
+ }
|
|
84
|
+ else
|
|
85
|
+ {
|
|
86
|
+ char tmp[7];
|
|
87
|
+ dtostrf(temp / 2.0f, 2, 1, tmp);
|
|
88
|
+ auto len = 4 - strlen(tmp);
|
|
89
|
+ for (auto i = 0; i < len; ++i)
|
|
90
|
+ {
|
|
91
|
+ out[i] = ' ';
|
|
92
|
+ }
|
|
93
|
+ strcpy(&out[len], tmp);
|
|
94
|
+ }
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+void printStateLine(char prefix, const BoilerItemState& boilerItemState, bool isModifying, bool isActive, LiquidCrystal lcd)
|
|
98
|
+{
|
|
99
|
+ char curTmp[7], setTmp[7], tmp[17];
|
|
100
|
+ tmpToStr(curTmp, boilerItemState.current);
|
|
101
|
+ tmpToStr(setTmp, boilerItemState.setting);
|
|
102
|
+ int count = snprintf(tmp, sizeof(tmp), "%c: %s [%s]%c%c", prefix, curTmp, setTmp, isModifying ? '<' : ' ', isActive ? LCD_CHAR_SENSOR : ' ');
|
|
103
|
+ for (; count < 17; ++count)
|
|
104
|
+ {
|
|
105
|
+ tmp[count] = ' ';
|
|
106
|
+ }
|
|
107
|
+ tmp[count] = 0;
|
|
108
|
+ lcd.print(tmp);
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+void printState(const AppCoreState& appCoreState, LiquidCrystal lcd)
|
|
112
|
+{
|
|
113
|
+ Serial.println("Updating display");
|
|
114
|
+
|
|
115
|
+ lcd.setCursor(0, 0);
|
|
116
|
+ printStateLine('S', appCoreState.appState.water, appCoreState.uiState.state == WaterSetting, appCoreState.appState.water.isActive, lcd);
|
|
117
|
+ lcd.setCursor(0, 1);
|
|
118
|
+ printStateLine('C', appCoreState.appState.heater, appCoreState.uiState.state == HeaterSetting, appCoreState.appState.heater.isActive, lcd);
|
|
119
|
+}
|
|
120
|
+
|
|
121
|
+void setState(AppCoreState& boilerState, UiStateEnum state)
|
|
122
|
+{
|
|
123
|
+ char tmp[50];
|
|
124
|
+ snprintf(tmp, sizeof(tmp), "Changing state %i => %i", g_appCoreState.uiState.state, state);
|
|
125
|
+ Serial.println(tmp);
|
|
126
|
+
|
|
127
|
+ if (state == Lighting)
|
|
128
|
+ {
|
|
129
|
+ digitalWrite(PIN_LCD_LED, 1);
|
|
130
|
+ }
|
|
131
|
+ else if (state == Hibernate)
|
|
132
|
+ {
|
|
133
|
+ digitalWrite(PIN_LCD_LED, 0);
|
|
134
|
+ g_appCoreState.uiState.modeSequenceIndex = MODE_SEQUENCE_COUNT - 1;
|
|
135
|
+ }
|
|
136
|
+ g_appCoreState.uiState.state = state;
|
|
137
|
+ g_appCoreState.uiState.isUpdateNeeded = true;
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+void checkBoilerItem(BoilerItemState& boilerItemState, UiState uiState)
|
|
141
|
+{
|
|
142
|
+ if (!boilerItemState.isActive && boilerItemState.current != TEMP_T_INVALID && boilerItemState.current <= boilerItemState.setting - TEMP_TRIGGER)
|
|
143
|
+ {
|
|
144
|
+ boilerItemState.isActive = true;
|
|
145
|
+ uiState.isUpdateNeeded = true;
|
|
146
|
+ }
|
|
147
|
+ else if (boilerItemState.isActive && (boilerItemState.current == TEMP_T_INVALID || boilerItemState.current >= boilerItemState.setting))
|
|
148
|
+ {
|
|
149
|
+ boilerItemState.isActive = false;
|
|
150
|
+ uiState.isUpdateNeeded = true;
|
|
151
|
+ }
|
|
152
|
+}
|
|
153
|
+
|
|
154
|
+void AppCore::setup()
|
|
155
|
+{
|
|
156
|
+ g_sensors.begin();
|
|
157
|
+ g_sensors.getAddress(g_sensor1, 0);
|
|
158
|
+ g_sensors.setWaitForConversion(false);
|
|
159
|
+
|
|
160
|
+ for (unsigned i = 0; i < BUTTONS_COUNT; ++i)
|
|
161
|
+ {
|
|
162
|
+ g_buttons[i]->begin();
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ pinMode(PIN_LCD_LED, OUTPUT);
|
|
166
|
+ digitalWrite(PIN_LCD_LED, 1);
|
|
167
|
+
|
|
168
|
+ g_lcd.begin(16, 2);
|
|
169
|
+ g_lcd.createChar(LCD_CHAR_SENSOR, g_sensorChar);
|
|
170
|
+
|
|
171
|
+ Serial.begin(9600);
|
|
172
|
+
|
|
173
|
+ bool allButtonsPressed = true;
|
|
174
|
+ for (unsigned i = 0; i < BUTTONS_COUNT; ++i)
|
|
175
|
+ {
|
|
176
|
+ allButtonsPressed = allButtonsPressed && g_buttons[i]->isPressed();
|
|
177
|
+ }
|
|
178
|
+ if (!allButtonsPressed)
|
|
179
|
+ {
|
|
180
|
+ g_storage.load(g_appCoreState);
|
|
181
|
+ }
|
|
182
|
+ else
|
|
183
|
+ {
|
|
184
|
+ g_storage.save(g_appCoreState);
|
|
185
|
+ }
|
|
186
|
+}
|
|
187
|
+
|
|
188
|
+void AppCore::loop()
|
|
189
|
+{
|
|
190
|
+ const auto& currentMs = millis();
|
|
191
|
+
|
|
192
|
+ if (currentMs - g_appCoreState.appState.lastSensorRequestMs >= SENSORS_CHECK_INTERVAL)
|
|
193
|
+ {
|
|
194
|
+ g_appCoreState.appState.lastSensorRequestMs = currentMs;
|
|
195
|
+ g_appCoreState.appState.hasReadSensors = false;
|
|
196
|
+ g_sensors.requestTemperaturesByAddress(g_sensor1);
|
|
197
|
+ }
|
|
198
|
+ if (currentMs - g_appCoreState.appState.lastSensorRequestMs >= SENSORS_REQUEST_DELAY && !g_appCoreState.appState.hasReadSensors)
|
|
199
|
+ {
|
|
200
|
+ g_appCoreState.appState.hasReadSensors = true;
|
|
201
|
+ auto raw = g_sensors.getTempC(g_sensor1);
|
|
202
|
+ temp_t temp = TEMP_T_INVALID;
|
|
203
|
+ if (raw != DEVICE_DISCONNECTED_C)
|
|
204
|
+ {
|
|
205
|
+ temp = (temp_t) (raw * 2);
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ if (temp != g_appCoreState.appState.water.current)
|
|
209
|
+ {
|
|
210
|
+ g_appCoreState.appState.water.current = temp;
|
|
211
|
+ g_appCoreState.uiState.isUpdateNeeded = true;
|
|
212
|
+ }
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+ for (unsigned i = 0; i < BUTTONS_COUNT; ++i)
|
|
217
|
+ {
|
|
218
|
+ g_buttons[i]->read();
|
|
219
|
+ if (g_buttons[i]->isPressed())
|
|
220
|
+ {
|
|
221
|
+ g_appCoreState.uiState.lastOpMs = currentMs;
|
|
222
|
+ break;
|
|
223
|
+ }
|
|
224
|
+ }
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+ if (g_appCoreState.uiState.state == Hibernate)
|
|
228
|
+ {
|
|
229
|
+ for (unsigned i = 0; i < BUTTONS_COUNT; ++i)
|
|
230
|
+ {
|
|
231
|
+ if (g_buttons[i]->wasReleased())
|
|
232
|
+ {
|
|
233
|
+ setState(g_appCoreState, Lighting);
|
|
234
|
+ break;
|
|
235
|
+ }
|
|
236
|
+ }
|
|
237
|
+ }
|
|
238
|
+ else
|
|
239
|
+ {
|
|
240
|
+ if (currentMs - g_appCoreState.uiState.lastOpMs >= HIBERNATE_DELAY)
|
|
241
|
+ {
|
|
242
|
+ setState(g_appCoreState, Hibernate);
|
|
243
|
+ }
|
|
244
|
+ else
|
|
245
|
+ {
|
|
246
|
+ if (g_btnMode.wasReleased())
|
|
247
|
+ {
|
|
248
|
+ g_appCoreState.uiState.modeSequenceIndex = (g_appCoreState.uiState.modeSequenceIndex + 1) % MODE_SEQUENCE_COUNT;
|
|
249
|
+ setState(g_appCoreState, g_modeSequence[g_appCoreState.uiState.modeSequenceIndex]);
|
|
250
|
+ }
|
|
251
|
+ else if (g_btnMinus.wasReleased() || g_btnPlus.wasReleased())
|
|
252
|
+ {
|
|
253
|
+ BoilerItemState* itemState = NULL;
|
|
254
|
+ if (g_appCoreState.uiState.state == WaterSetting)
|
|
255
|
+ {
|
|
256
|
+ itemState = &g_appCoreState.appState.water;
|
|
257
|
+ }
|
|
258
|
+ else if (g_appCoreState.uiState.state == HeaterSetting)
|
|
259
|
+ {
|
|
260
|
+ itemState = &g_appCoreState.appState.heater;
|
|
261
|
+ }
|
|
262
|
+
|
|
263
|
+ if (itemState)
|
|
264
|
+ {
|
|
265
|
+ if (g_btnMinus.wasReleased())
|
|
266
|
+ {
|
|
267
|
+ --itemState->setting;
|
|
268
|
+ }
|
|
269
|
+ else if (g_btnPlus.wasReleased())
|
|
270
|
+ {
|
|
271
|
+ ++itemState->setting;
|
|
272
|
+ }
|
|
273
|
+ g_appCoreState.uiState.isUpdateNeeded = true;
|
|
274
|
+ }
|
|
275
|
+ }
|
|
276
|
+ }
|
|
277
|
+ }
|
|
278
|
+
|
|
279
|
+ checkBoilerItem(g_appCoreState.appState.water, g_appCoreState.uiState);
|
|
280
|
+ checkBoilerItem(g_appCoreState.appState.heater, g_appCoreState.uiState);
|
|
281
|
+
|
|
282
|
+ if (g_appCoreState.uiState.isUpdateNeeded)
|
|
283
|
+ {
|
|
284
|
+ printState(g_appCoreState, g_lcd);
|
|
285
|
+ g_appCoreState.uiState.isUpdateNeeded = false;
|
|
286
|
+ }
|
|
287
|
+}
|