-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThermoTimer.ino
More file actions
391 lines (328 loc) · 11.7 KB
/
Copy pathThermoTimer.ino
File metadata and controls
391 lines (328 loc) · 11.7 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// M5Stack Core-Ink board with KMeterISO thermocouple sensor
#include <M5Unified.h>
#include <M5UnitKmeterISO.h>
#include <Preferences.h>
#include <WiFi.h>
#include <time.h>
// array of image pixels created using: https://javl.github.io/image2cpp/
extern "C" const unsigned char kitten_200x200_1bit[];
Preferences prefs;
struct Config
{
uint16_t target_temperature; // in C * 10
uint8_t last_temperature; // in C
uint32_t temp_constant_start_time; // unix timestamp
};
Config cfg;
// buttons:
// BtnA (Up): G37
// BtnB (Press): G38
// BtnC (Down): G39
// BtnPWR: G27
// BtnEXT: G5
// constants
constexpr esp_log_level_t log_level = ESP_LOG_ERROR; // ESP_LOG_DEBUG;
constexpr float kTargetStepC = 0.5f;
constexpr unsigned long sleep_normal_s = 60;
constexpr unsigned long sleep_finished_s = 10;
constexpr float kStableMarginC = 2.0f;
constexpr float kShutdownTempC = 40.0f;
constexpr uint32_t kShutdownDurationS = 10U * 60U; // 10 minutes
constexpr uint32_t kShutdownDurationSNoProbe = 3U * 60U; // 3 minutes without sensor
M5UnitKmeterISO kmeter;
static float target_temp_c = 0.0f;
static bool config_mode = false;
static bool config_change_armed = true;
static unsigned long last_display_update = 0;
M5Canvas canvas(&M5.Display);
// TODO: when USB is plugged in - do not sleep! (wake issue via button otherwise)
// seems USB-plugged-in can only be detected with extra hardware...
void pushCanvas()
{
canvas.pushSprite(0, 0);
M5.Display.display();
}
void drawStatusScreen(int bat_percent, float temp_c, float target_c, bool is_config_mode)
{
canvas.fillSprite(WHITE);
char buf[25];
snprintf(buf, sizeof(buf), "BAT: %d%%", bat_percent);
canvas.setTextDatum(middle_right);
canvas.drawString(buf, 200, 12, &fonts::FreeSansBold12pt7b);
canvas.setTextDatum(middle_right);
snprintf(buf, sizeof(buf), "%.1f", temp_c);
canvas.drawString(buf, 130, 100, &fonts::FreeSansBold24pt7b);
canvas.setTextDatum(middle_left);
snprintf(buf, sizeof(buf), "C");
canvas.drawString(buf, 154, 100, &fonts::FreeSansBold24pt7b);
canvas.fillCircle(147, 84, 6, BLACK);
canvas.fillCircle(147, 84, 3, WHITE);
canvas.setTextDatum(bottom_left);
if (is_config_mode)
{
snprintf(buf, sizeof(buf), "Neu:");
uint8_t startx = 70, starty = 160;
canvas.fillRect(startx, starty, 200-startx, 200-starty, BLACK);
canvas.fillRect(startx + 1, starty + 1, 200-startx-2, 200-starty-2, WHITE);
} else {
snprintf(buf, sizeof(buf), "Alarm:");
}
canvas.drawString(buf, 0, 197, &fonts::FreeSansBold12pt7b);
// DEBUG
// snprintf(buf, sizeof(buf), "V: %d", M5.Power.getBatteryVoltage());
// canvas.drawString(buf, 0, 150, &fonts::FreeSansBold12pt7b);
canvas.setTextDatum(bottom_right);
snprintf(buf, sizeof(buf), "%.1f", target_c);
canvas.drawString(buf, 160, 200, &fonts::FreeSansBold18pt7b);
canvas.setTextDatum(bottom_right);
snprintf(buf, sizeof(buf), "C");
canvas.drawString(buf, 200, 200, &fonts::FreeSansBold18pt7b);
canvas.fillCircle(169, 172, 5, BLACK);
canvas.fillCircle(169, 172, 2, WHITE);
pushCanvas();
}
void drawSleepingKitty()
{
canvas.fillSprite(WHITE);
constexpr int16_t kKittyWidth = 200;
constexpr int16_t kKittyHeight = 200;
int16_t x = (canvas.width() - kKittyWidth) / 2;
int16_t y = (canvas.height() - kKittyHeight) / 2;
canvas.drawBitmap(x, y, kitten_200x200_1bit, kKittyWidth, kKittyHeight, WHITE, BLACK);
pushCanvas();
}
void loadConfig()
{
prefs.begin("appcfg", true); // read-only
cfg.target_temperature = prefs.getUShort("t_temp", 940);
cfg.last_temperature = prefs.getUChar("t_prev", 0);
cfg.temp_constant_start_time = prefs.getULong("c_start", 0);
prefs.end();
}
void saveConfig(float target_temperature, float last_temperature, uint32_t temp_constant_start_time)
{
uint16_t calc_target_t = (uint16_t)(target_temperature * 10.0f);
uint8_t calc_last_t = round(last_temperature);
if (calc_target_t != cfg.target_temperature ||
calc_last_t != cfg.last_temperature ||
temp_constant_start_time != cfg.temp_constant_start_time)
{
M5_LOGI("saving NVM values: target=%u last=%u const_start=%lu",
calc_target_t,
calc_last_t,
static_cast<unsigned long>(temp_constant_start_time));
cfg.target_temperature = calc_target_t;
cfg.last_temperature = calc_last_t;
cfg.temp_constant_start_time = temp_constant_start_time;
prefs.begin("appcfg", false); // write mode
prefs.putUShort("t_temp", cfg.target_temperature);
prefs.putUChar("t_prev", cfg.last_temperature);
prefs.putULong("c_start", cfg.temp_constant_start_time);
prefs.end();
}
}
void setup()
{
M5.Log.setLogLevel(m5::log_target_serial, log_level);
auto m5cfg = M5.config();
m5cfg.serial_baudrate = 115200;
m5cfg.clear_display = false;
m5cfg.internal_imu = false;
m5cfg.internal_rtc = true;
m5cfg.internal_spk = true;
m5cfg.external_imu = false;
m5cfg.external_rtc = false;
m5cfg.disable_rtc_irq = true;
m5cfg.led_brightness = 200; // avoid M5.Power.setLed(200);
M5.begin(m5cfg); // 305 ms
WiFi.mode(WIFI_OFF);
btStop();
loadConfig();
target_temp_c = cfg.target_temperature / 10.0f;
if (target_temp_c < 10.0f || target_temp_c > 400.0f)
{
target_temp_c = 94.0f;
}
M5.Display.setEpdMode(epd_mode_t::epd_fastest);
M5.Display.setRotation(1);
canvas.setColorDepth(2);
canvas.createSprite(M5.Display.width(), M5.Display.height());
canvas.setTextDatum(middle_center);
canvas.setTextFont(&fonts::Orbitron_Light_24);
canvas.setTextSize(1);
canvas.setTextColor(BLACK);
auto pin_num_sda = M5.getPin(m5::pin_name_t::ex_i2c_sda);
auto pin_num_scl = M5.getPin(m5::pin_name_t::ex_i2c_scl);
Wire.begin(pin_num_sda, pin_num_scl, 400000U);
// M5_LOGI("Looking for KmeterISO ...");
int8_t tries = 10;
while (!kmeter.begin(&Wire, KMETER_DEFAULT_ADDR, pin_num_sda, pin_num_scl, 100000L))
{
M5_LOGD("kmeter.begin() failed, tries left: %d", tries);
delay(10);
if (--tries == 0)
{
M5_LOGE("KmeterISO not found");
break;
}
}
M5_LOGI("Init done.");
}
// Call this when bread is done
void playFinishedTune() {
M5.Speaker.setVolume(100);
struct Note { uint16_t freq; uint16_t dur; };
Note melody[] = {
{ 4500, 200 },
{ 4500, 100 },
{ 0, 500 },
{ 4500, 200 },
{ 4500, 100 },
{ 0, 500 },
{ 4500, 200 },
{ 4500, 100 },
{ 0, 500 }
};
for (auto &n : melody) {
if (n.freq == 0) {
delay(n.dur);
} else {
M5.Speaker.tone(n.freq, n.dur);
delay(n.dur + 20); // slight gap
}
}
}
void shutdown(void)
{
drawSleepingKitty();
M5.Power.powerOff();
}
void loop()
{
M5.update();
bool btnConfig_pressed = M5.BtnB.isPressed();
bool btnConfig_released = M5.BtnB.wasReleased();
bool incTarget = M5.BtnA.wasReleased();
bool decTarget = M5.BtnC.wasReleased();
bool btnExtPressed = M5.BtnEXT.isPressed();
// handle shutdown on button press
if (btnExtPressed)
{
M5_LOGD("Off Btn pressed");
drawSleepingKitty();
M5.Speaker.setVolume(80);
M5.Speaker.tone(2600, 120);
delay(130);
M5.Speaker.tone(2300, 140);
delay(150);
M5.Speaker.tone(2100, 180);
delay(200);
M5.Speaker.tone(2000, 240);
saveConfig(target_temp_c, 0, 0);
M5.Power.powerOff();
}
M5_LOGD("btnConfig_pressed: %d, btnConfig_released: %d, incTarget: %d, decTarget: %d, btnExtPressed: %d",
btnConfig_pressed, btnConfig_released, incTarget, decTarget, btnExtPressed);
// handle config mode button press
if (config_change_armed && btnConfig_pressed && config_mode == false)
{
config_mode = true;
M5.Speaker.setVolume(80);
M5.Speaker.tone(2500, 200);
M5_LOGD("Config Btn enabled");
config_change_armed = false;
}
if (config_change_armed && btnConfig_pressed && config_mode == true)
{
config_mode = false;
M5.Speaker.tone(2500, 200);
M5_LOGD("Config Btn disabled");
config_change_armed = false;
}
if (btnConfig_released)
{
config_change_armed = true;
M5_LOGD("Config Btn released - armed!");
}
// handle config change buttons
if (config_mode)
{
if (incTarget)
{
target_temp_c += kTargetStepC;
M5.Speaker.tone(4000, 100);
}
else if (decTarget)
{
target_temp_c -= kTargetStepC;
M5.Speaker.tone(3000, 100);
}
}
// M5.Display.clear();
int bat_percent = M5.Power.getBatteryLevel();
float temp_k = 0.0f;
uint8_t error_status = kmeter.getReadyStatus();
// M5_LOGI("kmeter.getReadyStatus(): %d", error_status);
if (error_status == 0)
{
temp_k = ((float)(kmeter.getCelsiusTempValue())) / 100;
// disabled averaging: there is no real benefit ...
// int32_t sample_sum = 0;
// for (int i = 0; i < 3; ++i) {
// sample_sum += kmeter.getCelsiusTempValue();
// delay(80);
// }
// temp_k = static_cast<float>(sample_sum) / (3.0f * 100.0f);
}
if (last_display_update == 0 || millis() - last_display_update > 500)
{
drawStatusScreen(bat_percent, temp_k, target_temp_c, config_mode);
M5_LOGD("after drawStatusScreen() readings");
last_display_update = millis();
if (!config_mode)
{
M5_LOGI("config and timeout checks");
// get RTC time as unix timestamp
m5::rtc_datetime_t rtc_datetime = M5.Rtc.getDateTime();
tm t = rtc_datetime.get_tm();
time_t unix_secs = mktime(&t);
// check if temp is stable: below 40° for at least 10 minutes in a margin of 2°C
float delta = abs(temp_k - cfg.last_temperature);
uint32_t new_constant_start_time = cfg.temp_constant_start_time;
uint8_t new_last_temp = cfg.last_temperature;
if (delta > kStableMarginC)
{
// not stable: save current temperature and timestamp
new_last_temp = static_cast<uint8_t>(round(temp_k));
new_constant_start_time = unix_secs;
}
else if (
(temp_k < kShutdownTempC &&
cfg.temp_constant_start_time != 0 &&
(unix_secs - cfg.temp_constant_start_time) >= kShutdownDurationS)
||
(static_cast<uint8_t>(round(temp_k)) == 0 &&
cfg.temp_constant_start_time != 0 &&
(unix_secs - cfg.temp_constant_start_time) >= kShutdownDurationSNoProbe))
{
// stable and below 40°C for at least 10 minutes: shutdown
saveConfig(target_temp_c, 0, 0);
M5_LOGI("going to shutdown (idle timeout)");
shutdown();
return;
}
// either not stable or waiting for shutdown timeout: save config
saveConfig(target_temp_c, static_cast<float>(new_last_temp), new_constant_start_time);
// beep if target is reached
if (temp_k >= target_temp_c)
{
playFinishedTune();
M5_LOGI("going to sleep 10s (finished)");
M5.Power.timerSleep(sleep_finished_s);
}
M5_LOGI("going to sleep 20s (normal measuring)");
M5.Power.timerSleep(sleep_normal_s);
}
}
delay(100);
}