-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
1627 lines (1396 loc) · 47.9 KB
/
Copy pathmain.cpp
File metadata and controls
1627 lines (1396 loc) · 47.9 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Arduino.h>
//////////////////////////////////////////////////
// Feature defines
// These are now expected to be set from platformio.ini, like so:
// build_flags =
// -D WEBSERVER=1
// #define SERIAL_DEBUG 1
// #define OLED_DISPLAY 1
// #define WEBSERVER 1
// #define OTA_UPDATE 1
// #define WEBSERVER_DEBUG 1
// #define WEBSERVER_REMOTE_CONTROL 1
#if !defined(MDNS_NAME)
#define MDNS_NAME "softub"
#endif
// Networking is only available on ESP32.
#if (defined(WEBSERVER) || defined(OTA_UPDATE)) && defined(ARDUINO_ARCH_ESP32)
#if !defined(WIFI_SSID) || !defined(WIFI_PASSWORD)
#error "Network credentials must be defined"
#endif
#define NETWORK 1
#endif
void network_start();
void network_service();
//////////////////////////////////////////////////
// board-specific defines
#if defined(ARDUINO_AVR_LEONARDO)
// The I/O for the panel must be connected to the Serial1 pins (0 and 1).
const int pin_pump = 2;
const int pin_temp[] = { A5, A4 };
// Make sure the pins shared with hardware SPI via the shield are high-impedance.
#define QUIESCE_PINS \
pinMode(13, INPUT); \
pinMode(11, INPUT);
const int OLED_RESET = 10;
const int OLED_DC = 9;
const int OLED_CS = 8;
// The shield has SCK/MOSI broken out from the ICSP header, so we're using hardware SPI.
#elif defined(ARDUINO_AVR_PROMICRO16)
// The I/O for the panel must be connected to the Serial1 pins (0 and 1).
const int pin_pump = 2;
const int pin_temp[] = { A0, A1 };
const int OLED_RESET = 5;
const int OLED_DC = 6;
const int OLED_CS = 7;
#elif defined(ARDUINO_ARDUCAM_IOTAI) || defined(ARDUINO_ESP32_DEV)
const int pin_pump = D2;
const int pin_temp[] = { S5, S4, S3 };
// Make sure the pins shared with hardware SPI via the shield are high-impedance.
// The spot for pin 13 is defined as "no connection" on this one
#define QUIESCE_PINS \
pinMode(D11, INPUT);
const int PANEL_TX = D4;
const int PANEL_RX = D5;
const int OLED_RESET = D10;
const int OLED_DC = D9;
const int OLED_CS = D8;
#define TSENS_OFFSET (-113)
#define TSENS_MULTIPLIER (4.0)
#elif defined(ARDUINO_WEMOS_D1_R32)
// SOME pins files for this board ID define the pins using their actual IO line numbers.
// The esp32doit-espduino board variant is one of those.
const int pin_pump = IO26;
const int pin_temp[] = { IO39, IO36 };
// The TX/RX pins on this board are shared with the USB serial interface, which is problematic.
// (Even if we're not using serial debug, the bootloader and other services will send data on these lines sometimes.)
// Use the pins that would be assigned to Serial2 by default.
// This means the shield pinout needed for this board will be different from the one for the Leonardo.
// I admit defeat.
const int PANEL_TX = IO17;
const int PANEL_RX = IO16;
const int OLED_RESET = IO5;
const int OLED_DC = IO13;
const int OLED_CS = IO12;
// From my testing of the WeMos board, it appears that the value in the tsens reg increases by 1 for every ~5 degrees farenheit.
// It is 122 at 75 degrees ambient.
#define TSENS_OFFSET (-107)
#define TSENS_MULTIPLIER (5.0)
#endif
//////////////////////////////////////////////////
// CPU-architecture-specific defines
#if defined(__AVR__)
// Watchdog timer support
#include <avr/wdt.h>
void watchdog_init() {
// Start with the watchdog timer disabled
wdt_disable();
}
void watchdog_start() {
wdt_enable(WDTO_2S);
}
void watchdog_reset() {
wdt_reset();
}
// All AVRs have 10-bit ADCs.
const int ADC_RESOLUTION = 1024;
// Figure out which AREF option to use, and what the AREF voltage is.
// Lower AREF means higher resolution within range, so we want the lowest voltage above what we expect to see from the sensors.
// Many AVRs are able to select an internal AREF of 2.56 volts, which is a good choice.
// Internal AREF is also likely more accurate/stable than VCC.
#if defined(INTERNAL2V56)
// If we're on an architecture that supports this option, use it.
const double ADC_AREF_VOLTAGE = 2.56;
#define ADC_AREF_OPTION INTERNAL2V56
#elif defined(INTERNAL2V5)
// This is apparently available on megaAVR?
const double ADC_AREF_VOLTAGE = 2.5;
#define ADC_AREF_OPTION INTERNAL2V5
#else
// The singular internal AREF varies by CPU type.
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega8__)
// Internal AREF is 2.56. Use it.
const double ADC_AREF_VOLTAGE = 2.56;
#define ADC_AREF_OPTION INTERNAL
#else
// internal AREF is too low (1.1v). Use the default VCC
const double ADC_AREF_VOLTAGE = 5.0;
#define ADC_AREF_OPTION DEFAULT
#endif
#endif
double readVcc() {
// Save previous ADMUX value
uint8_t previous = ADMUX;
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0) ;
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long adc = (high<<8) | low;
// Restore ADMUX
ADMUX = previous;
delay(2); // Wait for Vref to settle
return 1125.300 / adc; // Calculate Vcc (in V); 1125.300 = 1.1*1023
}
#elif defined(ARDUINO_ARCH_ESP32)
// Watchdog timer support
#include <esp_task_wdt.h>
void watchdog_init() {
// Start with the watchdog timer disabled
esp_task_wdt_init(15, false);
}
void watchdog_start() {
// Start the watchdog timer watching this task
esp_task_wdt_init(15, true);
esp_task_wdt_add(NULL);
}
void watchdog_reset() {
esp_task_wdt_reset();
}
// ESP32 has 12 bit ADCs
const int ADC_RESOLUTION = 4096;
// Ref: https://esp32.com/viewtopic.php?t=1053
// The ADC measures from 0 to 1.1v, moderated by the attenuation factor.
// The voltage attenuation defaults to ADC_11db, which scales it by a factor of 1/3.6.
// const double ADC_AREF_VOLTAGE = 1.1 * 3.6;
// Reducing the attenuation factor a bit gives us more usable resolution.
// ADC_0db provides no attenuation, so we measure 0 - 1.1v
// ADC_2_5db provides attenuation of 1/1.34, so we measure 0 - 1.47v
// ADC_6db provides an attenuation of 1/2 , so we measure 0 - 2.2v
// ADC_11db provides an attenuation of 1/3.6, so we measure 0 - 3.96v
// From comments in the adc.h header:
// Due to ADC characteristics, most accurate results are obtained within the following approximate voltage ranges:
// - 0dB attenuaton (ADC_ATTEN_DB_0) between 100 and 950mV
// - 2.5dB attenuation (ADC_ATTEN_DB_2_5) between 100 and 1250mV
// - 6dB attenuation (ADC_ATTEN_DB_6) between 150 to 1750mV
// - 11dB attenuation (ADC_ATTEN_DB_11) between 150 to 2450mV
// I don't want to use ADC_0db, as this is only accurage to 0.95v and maxes out at 1.1 = 110 degrees F, which we could see.
// It's possible that the temperature sensor in the pod would approach 125 degrees, which is the edge of the accurate range for 2.5dB.
// 6dB should be accurate up to 1.75v = 175 degrees F, which should give us ample headroom.
#define ADC_ATTENUATION_FACTOR ADC_6db
const double ADC_AREF_VOLTAGE = 1.1 * 2.0;
// Handy constants for interpreting esp_timer_get_time()
const int64_t millisecond = 1000l;
const int64_t second = 1000 * millisecond;
const int64_t minute = 60 * second;
const int64_t hour = 60 * minute;
const int64_t day = 24 * hour;
const int64_t year = 365 * day;
// Code for reading the built-in CPU temperature sensor.
#if defined(TSENS_OFFSET) && defined(TSENS_MULTIPLIER)
#define CPU_TEMP_AVAILABLE
extern "C" uint8_t read_tsens_register();
float cpu_temp()
{
uint8_t tsens = read_tsens_register();
// Assuming the TSENS reading is actually linear, this should convert it to Farenheit:
return (tsens + TSENS_OFFSET) * TSENS_MULTIPLIER;
}
#endif
#else
// no watchdog timer support
void watchdog_init() {}
void watchdog_start() {}
void watchdog_reset() {}
#endif
const double ADC_DIVISOR = ADC_AREF_VOLTAGE / double(ADC_RESOLUTION);
#ifdef SERIAL_DEBUG
#ifndef SERIAL_DEBUG_SPEED
#define SERIAL_DEBUG_SPEED 9600
#endif
void debug(const char *format, ...)
{
char string[256];
va_list arg;
va_start(arg, format);
vsnprintf(string, sizeof(string), format, arg);
Serial.println(string);
}
void debug(const String& string)
{
Serial.println(string);
}
void serial_debug_init()
{
// delay(3000);
Serial.begin(SERIAL_DEBUG_SPEED);
// while (!Serial);
debug("Serial initialized");
// delay(100);
}
#else
#define serial_debug_init(...)
#define debug(...)
#endif
#ifdef OLED_DISPLAY
#include <U8x8lib.h>
#if defined(OLED_MOSI)
// Use software SPI
// https://www.amazon.com/gp/product/B01N1LZT8L
U8X8_SH1106_128X64_NONAME_4W_SW_SPI oled(OLED_CLOCK, OLED_MOSI, OLED_CS, OLED_DC , OLED_RESET);
#else
// Use hardware SPI
// https://www.amazon.com/gp/product/B01N1LZT8L
U8X8_SH1106_128X64_NONAME_4W_HW_SPI oled(OLED_CS, OLED_DC , OLED_RESET);
#endif
// Other possibilities:
// U8X8_SSD1306_128X64_NONAME_4W_HW_SPI oled(OLED_CS, OLED_DC , OLED_RESET);
// U8X8_SSD1306_128X64_NONAME_2ND_4W_HW_SPI oled(OLED_CS, OLED_DC , OLED_RESET);
void start_oled()
{
oled.begin();
// oled.setFont(u8x8_font_amstrad_cpc_extended_f);
// oled.setFont(u8x8_font_pressstart2p_f);
oled.setFont(u8x8_font_pcsenior_f);
// oled.setFont(u8x8_font_pxplusibmcgathin_f);
// oled.setFont(u8x8_font_pxplusibmcga_f);
// oled.setFont(u8x8_font_pxplustandynewtv_f);
// oled.setFont(u8x8_font_8x13_1x2_f);
}
const int display_width = 16;
void print_oled(int line, const char *format, ...)
{
char string[display_width + 1];
va_list arg;
va_start(arg, format);
vsnprintf(string, sizeof(string), format, arg);
// pad the remainder of the string with spaces, so it clears any previous text on this line.
strlcat(string , " ", sizeof(string));
oled.drawString(0, line, string);
}
void print_oled(int line, const String& string)
{
// pad the remainder of the string with spaces, so it clears any previous text on this line.
String s = string + " ";
oled.drawString(0, line, s.substring(0, display_width).c_str());
}
#else // !OLED_DISPLAY
// Turn these into no-ops.
#define start_oled(...)
#define print_oled(...)
#endif
//////////////////////////////////////////////////
// run states
enum
{
// Just started up.
runstate_startup,
// Not running, displaying setpoint temperature.
runstate_idle,
// Pump has just started running, we don't have confidence in the temperature reading yet
runstate_finding_temp,
// Temperature is known to be low, we're trying to increase it.
runstate_heating,
// The pump was turned on manually. Run it for 10 minutes.
runstate_manual_pump,
// for testing
runstate_test,
// Go to this state if we detect a problem with the temperature sensors.
// It shuts down the pump and sets the display into a "help me" state.
// The user can reset to startup mode by holding the "light" and "jets" buttons for 5 seconds.
runstate_panic
};
int runstate = runstate_startup;
// Min and max allowed setpoints
const int temp_min = 50;
const int temp_max = 110;
// The time of the last transition. This may be used differently by different states.
uint32_t runstate_last_transition_millis = 0;
// The time of the last change to buttons pressed. This may be used differently by different states.
uint32_t buttons_last_transition_millis = 0;
// The last temperature reading we took
double last_temp = 0;
// The last valid temperature reading we took
double last_valid_temp = 0;
// Used to display temperature for a short time after the user adjusts it
bool temp_adjusted = false;
uint32_t temp_adjusted_millis = 0;
const uint32_t temp_adjusted_display_millis = 5 * 1000l;
// The amount of time to wait on startup before doing anything
const uint32_t startup_wait_seconds = 10;
// The amount of time we run the pump before believing the temperature reading
const uint32_t temp_settle_millis = 30 * 1000l; // 30 seconds
// The amount of time after stopping the pump when we no longer consider the temp valid.
const uint32_t temp_decay_millis = 60 * 1000l; // 60 seconds
// The amount of time in the idle state after which we should run to check the temperature.
const uint32_t idle_seconds = 15 * 60; // 15 minutes
// When the user turns on the pump manually, run it for this long.
const uint32_t manual_pump_seconds = 5 * 60; // 5 minutes
// The amount of time the user has to hold buttons to escape panic state
const uint32_t panic_wait_seconds = 5;
// If smoothed readings ever disagree by this many degrees f, panic.
const int panic_sensor_difference = 10;
// If the temperature reading ever the max set temperature plus 5 degrees f, panic.
const int panic_high_temp = temp_max + 5;
// Used to flash things in panic mode
bool panic_flash;
String panic_string;
/////////////////////////////////////////////////
// other globals
const int pin_temp_count = sizeof(pin_temp) / sizeof(pin_temp[0]);
// smooth the temperature sampling over this many samples, to filter out noise.
const int temp_sample_count = 128;
int temp_sample_pointer = 0;
int temp_samples[pin_temp_count][temp_sample_count];
// Start out the display bytes with a sane value.
uint8_t display_buffer[] = { 0x02, 0x00, 0x01, 0x00, 0x00, 0x01, 0xFF};
const int32_t display_bytes = sizeof(display_buffer) / sizeof(display_buffer[0]);
bool display_dirty = true;
// loop no faster than 16Hz
const uint32_t loop_microseconds = 1000000l / 16;
uint32_t buttons = 0;
uint32_t last_buttons = 0;
bool pump_running = false;
uint32_t pump_switch_millis = 0;
bool temp_valid = false;
int temp_setting = 100;
// stop heating at temp_setting + temp_setting_range,
double temp_setting_range = 0.5;
enum {
button_jets = 0x01,
button_light = 0x02,
button_up = 0x04,
button_down = 0x08,
};
String dtostr(double number, signed char width = 1, unsigned char prec = 1)
{
char buf[32];
dtostrf(number, width, prec, buf);
return String(buf);
}
void temp_adjust(int amount)
{
temp_setting += amount;
if (temp_setting > temp_max)
temp_setting = temp_max;
if (temp_setting < temp_min)
temp_setting = temp_min;
temp_adjusted = true;
temp_adjusted_millis = millis();
}
double adc_to_farenheit(double reading)
{
// Scale the reading to voltage
reading *= ADC_DIVISOR;
// The temperature sensors seem to be LM34s.
// (Linear, 750mv at 75 degrees F, slope 10mv/degree F)
// Basically, temperature in F is voltage * 100.
return (reading * 100);
}
void runstate_transition()
{
runstate_last_transition_millis = millis();
}
const char* state_name(int state)
{
const char *name = "UNKNOWN";
switch(state) {
case runstate_startup: name = "startup"; break;
case runstate_idle: name = "idle"; break;
case runstate_finding_temp: name = "finding temp"; break;
case runstate_heating: name = "heating"; break;
case runstate_manual_pump: name = "manual"; break;
case runstate_test: name = "test"; break;
case runstate_panic: name = "PANIC"; break;
}
return name;
}
void enter_state(int state)
{
runstate = state;
runstate_transition();
#ifdef OLED_DISPLAY
print_oled(0, "%s", state_name(state));
#endif
}
void panic()
{
if (runstate != runstate_panic) {
enter_state(runstate_panic);
}
}
void panic(const char *format, ...)
{
char string[1024];
va_list arg;
va_start(arg, format);
vsnprintf(string, sizeof(string), format, arg);
panic_string = String(string);
panic();
}
// Optionally return the lowest/highest readings in the buffer
double smoothed_sensor_reading(int sensor, int *lowest = NULL, int *highest = NULL)
{
if (sensor >= pin_temp_count) {
sensor = 0;
}
if (lowest != NULL) {
*lowest = INT_MAX;
}
if (highest != NULL) {
*highest = INT_MIN;
}
double result = 0;
for (int i = 0; i < temp_sample_count; i++) {
int sample = temp_samples[sensor][i];
result += sample;
if (lowest != NULL) {
*lowest = min(*lowest, sample);
}
if (highest != NULL) {
*highest = max(*highest, sample);
}
}
result /= temp_sample_count;
return result;
}
void read_temp_sensors()
{
// Advance the sample pointer in the ring buffer.
temp_sample_pointer++;
if (temp_sample_pointer >= temp_sample_count) {
temp_sample_pointer = 0;
}
double avg_reading = 0;
for (int i = 0; i < pin_temp_count; i++)
{
#ifdef ARDUINO_ARCH_ESP32
// Use the calibrated API to read actual voltage in millivolts
uint32_t mv = analogReadMilliVolts(pin_temp[i]);
// Convert it to our defined range.
// The entire right-hand multiplier here is just constant values, so it should be computed at compile-time.
int value = double(mv) * ((1.0 / ADC_AREF_VOLTAGE) * (ADC_RESOLUTION / 1000.0));
#else
int value = analogRead(pin_temp[i]);
#endif
// Save the current sample in the ring buffer
temp_samples[i][temp_sample_pointer] = value;
// Smooth the temperature sampling over temp_sample_count samples
double smoothed_value = smoothed_sensor_reading(i);
// The first two sensors are the water temperature sensors. Any further ones are auxiliary.
if (i < 2) {
avg_reading += smoothed_value;
}
#if defined(OLED_DISPLAY)
// dtostr(smoothed_value * ADC_DIVISOR, 1, 3);
print_oled(i + 1, "%s(%s/%d)"
, dtostr(adc_to_farenheit(smoothed_value)).c_str()
, dtostr(adc_to_farenheit(value)).c_str()
, value
);
#endif
}
// Calculate the average smoothed temp of the first two sensors and save it as the water temp.
avg_reading /= 2;
last_temp = adc_to_farenheit(avg_reading);
// If the smoothed readings from sensors 0 and 1 ever differ by more than panic_sensor_difference degrees, panic.
if (fabs(adc_to_farenheit(smoothed_sensor_reading(0)) - adc_to_farenheit(smoothed_sensor_reading(1))) > panic_sensor_difference)
{
panic("sensor readings diverged (%s vs %s)",
dtostr(adc_to_farenheit(smoothed_sensor_reading(0))).c_str(),
dtostr(adc_to_farenheit(smoothed_sensor_reading(1))).c_str());
}
// If calculated temperature is over our defined limit, panic.
if (last_temp > panic_high_temp)
{
panic("last_temp too high (%s > %d)",
dtostr(last_temp).c_str(),
panic_high_temp);
}
// Reset the watchdog timer.
watchdog_reset();
}
void read_buttons()
{
while (Serial1.available()) {
int raw = Serial1.read();
// The 4 button bits are replicated and inverted between the low and high nybbles.
// Check that they match, and extract just one copy.
if ((raw & 0x0F) == (((raw >> 4) & 0x0F) ^ 0x0F))
{
buttons = raw >> 4;
}
}
}
void display_update_checksum()
{
uint8_t sum = 0;
sum += display_buffer[1];
sum += display_buffer[2];
sum += display_buffer[3];
sum += display_buffer[4];
display_buffer[5] = sum;
}
// Values in digit places:
// 0 - 9 - digit
// 0x0a - blank
// 0x0b -- "P"
void display_set_digit(int digit, uint8_t value)
{
if ((digit >= 0) && (digit < 3))
{
if (display_buffer[2 + digit] != value)
{
display_buffer[2 + digit] = value;
display_dirty = true;
}
}
}
void display_set_digits(uint8_t a, uint8_t b, uint8_t c)
{
display_set_digit(0, a);
display_set_digit(1, b);
display_set_digit(2, c);
}
void display_set_bits(int byte, int mask, bool value)
{
if (value) {
if ((display_buffer[byte] & mask) == 0)
{
display_buffer[byte] |= mask;
display_dirty = true;
}
} else {
if ((display_buffer[byte] & mask) != 0)
{
display_buffer[byte] &= ~mask;
display_dirty = true;
}
}
}
void display_filter(bool on)
{
display_set_bits(1, 0x10, on);
}
void display_heat(bool on)
{
display_set_bits(1, 0x20, on);
}
void display_temperature(int temp)
{
display_set_digits(temp / 100, (temp / 10) % 10, temp % 10);
}
void display_panic()
{
display_temperature(adc_to_farenheit(smoothed_sensor_reading(panic_flash?0:1)));
display_heat(!panic_flash);
display_filter(panic_flash);
}
void display_panic_countdown(int countdown)
{
display_set_digits(0x0a, countdown, 0x0a);
display_filter(true);
display_heat(true);
}
void display_vcc()
{
#if defined(__AVR__)
#if defined(OLED_DISPLAY)
double vcc = readVcc();
// dtostr(smoothed_value, 1, 0);
print_oled(7, "VCC = %s", dtostr(vcc, 1, 3).c_str());
#endif
#endif
}
void display_send()
{
if (display_dirty)
{
display_update_checksum();
Serial1.write(display_buffer, display_bytes);
display_dirty = false;
}
}
void set_pump(bool running)
{
if (pump_running != running)
{
display_filter(running);
digitalWrite(pin_pump, running);
pump_running = running;
pump_switch_millis = millis();
}
}
void setup()
{
watchdog_init();
serial_debug_init();
// Make sure the pins shared with hardware SPI via the shield are high-impedance.
#ifdef QUIESCE_PINS
QUIESCE_PINS
#endif
// Communications with the control panel is 2400 baud ttl serial.
Serial1.begin(2400
#if defined(ARDUINO_ARCH_ESP32)
// Specify format and pins
, SERIAL_8N1
, PANEL_RX
, PANEL_TX
, false // invert?
#endif
);
#ifdef ADC_AREF_OPTION
// Set the AREF voltage for reading from the sensors.
analogReference(ADC_AREF_OPTION);
#endif
#ifdef ADC_ATTENUATION_FACTOR
analogSetAttenuation(ADC_ATTENUATION_FACTOR);
#endif
pinMode(pin_pump, OUTPUT);
// Make really sure the pump is not running.
digitalWrite(pin_pump, 0);
start_oled();
display_vcc();
network_start();
enter_state(runstate_startup);
}
void check_temp_validity()
{
// Temperature readings are meaningless if the pump isn't running.
// We also want to ignore temp readings when the pump was just turned on,
// and consider the temp valid for a short time after it turns off.
if (pump_running && !temp_valid)
{
if (millis() - pump_switch_millis > temp_settle_millis)
{
// The pump has been running long enough for temp to be valid.
temp_valid = true;
}
}
else if (!pump_running && temp_valid)
{
if (millis() - pump_switch_millis > temp_decay_millis)
{
// The pump has been off long enough that we should no longer consider the temp valid.
temp_valid = false;
}
}
// If the temp is currently valid, update the last valid temp
if (temp_valid) {
last_valid_temp = last_temp;
}
}
// Make this a global so it can be displayed by the debug web endpoint
uint32_t loop_time = 0;
void loop() {
uint32_t loop_start_micros = micros();
uint32_t loop_start_millis = millis();
read_temp_sensors();
read_buttons();
check_temp_validity();
bool jets_pushed = false;
bool lights_pushed = false;
if (last_buttons != buttons)
{
// One or more buttons changed.
buttons_last_transition_millis = loop_start_millis;
debug("Buttons changed to 0x%02x, buttons_last_transition_millis = %ld", int(buttons), buttons_last_transition_millis);
if (runstate != runstate_panic)
{
if (!(last_buttons & button_jets) && (buttons & button_jets))
{
// Jets button was pushed.
jets_pushed = true;
}
if (!(last_buttons & button_up) && (buttons & button_up))
{
// Up button was pushed.
temp_adjust(1);
}
if (!(last_buttons & button_down) && (buttons & button_down))
{
// Down button was pushed.
temp_adjust(-1);
}
if (!(last_buttons & button_light) && (buttons & button_light))
{
lights_pushed = true;
}
} else {
// When in panic state, the only thing we do is look for the jets and lights buttons to be held down together.
// Whenever the buttons change, reset the countdown timer.
runstate_transition();
}
last_buttons = buttons;
}
// Useful timing shortcuts for the state machine
uint32_t millis_since_last_transition = loop_start_millis - runstate_last_transition_millis;
uint32_t seconds_since_last_transition = millis_since_last_transition / 1000l;
uint32_t millis_since_button_change = loop_start_millis - buttons_last_transition_millis;
uint32_t seconds_since_button_change = millis_since_button_change / 1000l;
if (temp_adjusted)
{
uint32_t millis_since_temp_adjust = millis() - temp_adjusted_millis;
// See if the temp-adjusted display window has expired.
if (millis_since_temp_adjust > temp_adjusted_display_millis)
{
temp_adjusted = false;
}
else
{
if (((buttons == button_up) || (buttons == button_down)) &&
(millis_since_temp_adjust <= millis_since_button_change))
{
// If the user has been holding down the up or down button since the last temp adjustment,
// and the last temp adjustment was over the repeat time (1/2s for the first adjustment, 1/4s for the next 1.5 seconds, 1/10s thereafter),
// adjust again.
uint32_t repeat_time = 500;
if (millis_since_button_change > 2000) {
repeat_time = 100;
} else if (millis_since_button_change > 500) {
repeat_time = 250;
}
if (millis_since_temp_adjust >= repeat_time)
{
temp_adjust((buttons == button_up)?1:-1);
}
}
}
}
// Default to displaying the current temp if it's valid and not recently adjusted, or the set point otherwise.
if (temp_valid && !temp_adjusted) {
display_temperature(last_temp);
} else {
display_temperature(temp_setting);
}
// In most states, we want the heating light off.
display_heat(false);
// Pushing the lights button at any time immediately enters the test state.
// if (runstate != runstate_test && lights_pushed) {
// enter_state(runstate_test);
// lights_pushed = false;
// }
switch(runstate) {
case runstate_startup:
if (seconds_since_last_transition > startup_wait_seconds)
{
// Before starting the pump for the first time, enable the watchdog timer.
watchdog_start();
// While we may not have an _actual_ valid temp yet, we should at least have a smoothed version of the current sensor temp.
// Use this for reporting stats for now, so we don't return garbage.
last_valid_temp = last_temp;
// Turn on the pump and enter the "finding temp" state.
enter_state(runstate_finding_temp);
} else {
display_set_digits(0x0a, 0x00, 0x0a);
}
break;
case runstate_finding_temp:
if (jets_pushed) {
// From the finding temp state, allow the user to turn off the pump manually.
enter_state(runstate_idle);
} else if (temp_valid) {
// Go into the heating state. It will flip back to idle if no heating is needed.
enter_state(runstate_heating);
} else {
set_pump(true);
// indicate that we're still finding temp
display_set_digits(0x0a, 0x0b, 0x0a);
}
break;
case runstate_heating:
if (jets_pushed) {
// From the heating state, allow the user to turn off the pump manually.
enter_state(runstate_idle);
} else if (last_valid_temp > (temp_setting + temp_setting_range)) {
// We've reached the set point. Turn the pump off and go to idle.
enter_state(runstate_idle);
} else {
set_pump(true);
display_heat(true);
}
break;
case runstate_idle:
if (jets_pushed) {
// From the idle state, allow the user to turn on the pump manually.
enter_state(runstate_manual_pump);
} else if (seconds_since_last_transition > idle_seconds) {
// We've been idle long enough that we should do a temperature check.
enter_state(runstate_finding_temp);
} else if ((temp_adjusted) && ((temp_setting + temp_setting_range) > last_valid_temp)) {
// The user adjusted the temperature to above the last valid reading.
// Transition to the finding temperature state to figure out if we need to heat.
enter_state(runstate_finding_temp);
} else {
set_pump(false);
}
break;
case runstate_manual_pump:
if (jets_pushed) {
// From the manual state, allow the user to turn off the pump.
enter_state(runstate_idle);
} else if (seconds_since_last_transition > manual_pump_seconds) {
// Don't let the manual mode run for too long.
enter_state(runstate_finding_temp);
} else {
set_pump(true);
}
break;
case runstate_test:
{
if (lights_pushed)
{
// Exit the test state
enter_state(runstate_finding_temp);
print_oled(3, "");
} else {
print_oled(3, "012345678901234567890");
}
}
break;
case runstate_panic:
set_pump(false);