2 Incheckningar

Upphovsman SHA1 Meddelande Datum
  Thomas Buck 82a56ea9dc Now works with two distributed units connected via i2c 3 år sedan
  Thomas Buck caff88085f Started adding ESP support 3 år sedan

+ 1
- 0
.gitignore Visa fil

@@ -1 +1,2 @@
1 1
 .pio
2
+include/wifi.h

+ 6
- 0
README.md Visa fil

@@ -0,0 +1,6 @@
1
+Do this on ESP platforms
2
+
3
+    echo '#define WIFI_SSID "..."' > include/wifi.h
4
+    echo '#define WIFI_PW "..."' >> include/wifi.h
5
+
6
+To be able to connect to WiFi.

+ 28
- 0
include/Functionality.h Visa fil

@@ -0,0 +1,28 @@
1
+#ifndef _FUNCTIONALITY_H_
2
+#define _FUNCTIONALITY_H_
3
+
4
+#ifdef FUNCTION_UI
5
+
6
+void ui_setup(void);
7
+void ui_run(void);
8
+
9
+#endif // FUNCTION_UI
10
+
11
+// ----------------------------------------------------------------------------
12
+
13
+#ifdef FUNCTION_CONTROL
14
+
15
+void control_setup(void);
16
+void control_begin(void);
17
+void control_run(void);
18
+
19
+#endif // FUNCTION_CONTROL
20
+
21
+// ----------------------------------------------------------------------------
22
+
23
+void blink_lcd(int n, int wait = 200);
24
+void write_to_all(const char *a, const char *b,
25
+                  const char *c, const char *d, int num_input);
26
+void backspace(void);
27
+
28
+#endif // _FUNCTIONALITY_H_

+ 4
- 0
include/Keymatrix.h Visa fil

@@ -1,6 +1,8 @@
1 1
 #ifndef _KEYMATRIX_H_
2 2
 #define _KEYMATRIX_H_
3 3
 
4
+#ifdef FUNCTION_UI
5
+
4 6
 #include <CircularBuffer.h>
5 7
 
6 8
 class Keymatrix {
@@ -52,4 +54,6 @@ private:
52 54
     CircularBuffer<Event *, 32> events;
53 55
 };
54 56
 
57
+#endif // FUNCTION_UI
58
+
55 59
 #endif // _KEYMATRIX_H_

+ 14
- 0
include/SerialLCD.h Visa fil

@@ -1,7 +1,15 @@
1 1
 #ifndef _SERIAL_LCD_H_
2 2
 #define _SERIAL_LCD_H_
3 3
 
4
+#ifdef FUNCTION_UI
5
+
6
+#if defined(PLATFORM_AVR)
4 7
 #include <SendOnlySoftwareSerial.h>
8
+#elif defined(PLATFORM_ESP)
9
+#include <SoftwareSerial.h>
10
+#else
11
+#error platform not supported
12
+#endif
5 13
 
6 14
 class SerialLCD {
7 15
 public:
@@ -22,7 +30,13 @@ public:
22 30
     void write(int line, int col, const char *text);
23 31
     
24 32
 private:
33
+#if defined(PLATFORM_AVR)
25 34
     SendOnlySoftwareSerial *lcd;
35
+#elif defined(PLATFORM_ESP)
36
+    SoftwareSerial *lcd;
37
+#endif
26 38
 };
27 39
 
40
+#endif // FUNCTION_UI
41
+
28 42
 #endif // _SERIAL_LCD_H_

+ 54
- 0
include/SimpleUpdater.h Visa fil

@@ -0,0 +1,54 @@
1
+/*
2
+ * SimpleUpdater.h
3
+ *
4
+ * ESP8266 / ESP32 Environmental Sensor
5
+ *
6
+ * ----------------------------------------------------------------------------
7
+ * "THE BEER-WARE LICENSE" (Revision 42):
8
+ * <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
9
+ * you can do whatever you want with this stuff. If we meet some day, and you
10
+ * think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
11
+ * ----------------------------------------------------------------------------
12
+ */
13
+
14
+#ifndef __ESP_SIMPLE_UPDATER__
15
+#define __ESP_SIMPLE_UPDATER__
16
+
17
+#if defined(ARDUINO_ARCH_ESP8266)
18
+#include <ESP8266WebServer.h>
19
+#include <ESP8266HTTPUpdateServer.h>
20
+#define UPDATE_WEB_SERVER ESP8266WebServer
21
+#elif defined(ARDUINO_ARCH_ESP32)
22
+#include <WebServer.h>
23
+#define UPDATE_WEB_SERVER WebServer
24
+#endif
25
+
26
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
27
+
28
+class SimpleUpdater {
29
+public:
30
+    SimpleUpdater(String _uri = String("/update"));
31
+    void setup(UPDATE_WEB_SERVER *_server);
32
+    
33
+private:
34
+
35
+#if defined(ARDUINO_ARCH_ESP8266)
36
+    
37
+    ESP8266HTTPUpdateServer updateServer;
38
+
39
+#elif defined(ARDUINO_ARCH_ESP32)
40
+    
41
+    void get(void);
42
+    void postResult(void);
43
+    void postUpload(void);
44
+    
45
+#endif
46
+    
47
+    String uri;
48
+    UPDATE_WEB_SERVER *server;
49
+};
50
+
51
+#endif
52
+
53
+#endif // __ESP_SIMPLE_UPDATER__
54
+

+ 11
- 0
include/WifiStuff.h Visa fil

@@ -0,0 +1,11 @@
1
+#ifndef _WIFI_STUFF_H_
2
+#define _WIFI_STUFF_H_
3
+
4
+#ifdef PLATFORM_ESP
5
+
6
+void wifi_setup();
7
+void wifi_run();
8
+
9
+#endif
10
+
11
+#endif // _WIFI_STUFF_H_

+ 13
- 0
include/config.h Visa fil

@@ -14,4 +14,17 @@
14 14
 #define AUTO_PUMP_RUNTIME 5
15 15
 #define MAX_AUTO_PLANT_RUNTIME 15
16 16
 
17
+// Sketch version
18
+#define FIRMWARE_VERSION "0.1"
19
+
20
+// all given in milliseconds
21
+#define SERVER_HANDLE_INTERVAL 10
22
+#define LED_BLINK_INTERVAL 500
23
+#define LED_INIT_BLINK_INTERVAL 500
24
+#define LED_CONNECT_BLINK_INTERVAL 250
25
+#define LED_ERROR_BLINK_INTERVAL 100
26
+
27
+#define OWN_I2C_ADDRESS 0x42
28
+#define I2C_BUF_SIZE 32
29
+
17 30
 #endif // _CONFIG_H_

+ 91
- 0
include/config_pins.h Visa fil

@@ -0,0 +1,91 @@
1
+#ifndef _CONFIG_PINS_H_
2
+#define _CONFIG_PINS_H_
3
+
4
+// ----------------------------------------------------------------------------
5
+// ----------------------------------------------------------------------------
6
+
7
+#ifdef PLATFORM_AVR
8
+
9
+#define BUILTIN_LED_PIN 13
10
+
11
+// ----------------------------------------------------------------------------
12
+
13
+#ifdef FUNCTION_UI
14
+
15
+#define SERIAL_LCD_TX_PIN 10
16
+
17
+#define KEYMATRIX_ROWS 4
18
+#define KEYMATRIX_COLS 3
19
+#define KEYMATRIX_ROW_PINS 5, 6, 7, 8
20
+#define KEYMATRIX_COL_PINS 2, 3, 4
21
+
22
+#endif // FUNCTION_UI
23
+
24
+// ----------------------------------------------------------------------------
25
+
26
+#ifdef FUNCTION_CONTROL
27
+
28
+#define VALVE_COUNT 5
29
+#define VALVE_PINS 10, 11, 12, 14, 15
30
+
31
+#define PUMP_COUNT 3
32
+#define PUMP_PINS 16, 17, 18
33
+
34
+#define SWITCH_COUNT 2
35
+#define SWITCH_PINS 19, 20
36
+
37
+#endif // FUNCTION_CONTROL
38
+
39
+#endif // PLATFORM_AVR
40
+
41
+// ----------------------------------------------------------------------------
42
+// ----------------------------------------------------------------------------
43
+
44
+#ifdef PLATFORM_ESP
45
+
46
+#define BUILTIN_LED_PIN 1
47
+
48
+// ----------------------------------------------------------------------------
49
+
50
+#ifdef FUNCTION_UI
51
+
52
+#error configuration not supported
53
+
54
+#endif // FUNCTION_UI
55
+
56
+// ----------------------------------------------------------------------------
57
+
58
+#ifdef FUNCTION_CONTROL
59
+
60
+#ifdef FUNCTION_UI
61
+
62
+#define VALVE_COUNT 5
63
+#define VALVE_PINS 9, 11, 12, 13, 14
64
+
65
+#define PUMP_COUNT 3
66
+#define PUMP_PINS 15, 16, 17
67
+
68
+#define SWITCH_COUNT 2
69
+#define SWITCH_PINS 18, 19
70
+
71
+#else
72
+
73
+#define VALVE_COUNT 5
74
+#define VALVE_PINS 9, 11, 12, 13, 14
75
+
76
+#define PUMP_COUNT 3
77
+#define PUMP_PINS 15, 16, 17
78
+
79
+#define SWITCH_COUNT 2
80
+#define SWITCH_PINS 18, 19
81
+
82
+#endif
83
+
84
+#endif // FUNCTION_CONTROL
85
+
86
+#endif // PLATFORM_ESP
87
+
88
+// ----------------------------------------------------------------------------
89
+// ----------------------------------------------------------------------------
90
+
91
+#endif // _CONFIG_PINS_H_

+ 60
- 4
platformio.ini Visa fil

@@ -9,15 +9,71 @@
9 9
 ; https://docs.platformio.org/page/projectconf.html
10 10
 
11 11
 [platformio]
12
-default_envs = leonardo
12
+default_envs = esp8266_main, esp32_main, arduino_ui, arduino_test, leonardo_main, leonardo_test
13 13
 
14
-[env:leonardo]
14
+[env:esp8266_main]
15
+platform = espressif8266
16
+board = esp01_1m
17
+framework = arduino
18
+build_flags = -D PLATFORM_ESP -D FUNCTION_CONTROL
19
+lib_deps =
20
+    Wire
21
+
22
+[env:esp32_main]
23
+platform = espressif32
24
+board = esp32dev
25
+framework = arduino
26
+build_flags = -D PLATFORM_ESP -D FUNCTION_CONTROL
27
+upload_protocol = esptool
28
+upload_port = /dev/ttyUSB1
29
+lib_deps =
30
+    Wire
31
+
32
+[env:arduino_ui]
33
+platform = atmelavr
34
+board = pro16MHzatmega328
35
+framework = arduino
36
+build_flags = -D PLATFORM_AVR -D FUNCTION_UI
37
+upload_port = /dev/ttyUSB1
38
+monitor_port = /dev/ttyUSB1
39
+monitor_speed = 115200
40
+lib_deps =
41
+    Wire
42
+    https://github.com/nickgammon/SendOnlySoftwareSerial
43
+    https://github.com/rlogiacco/CircularBuffer
44
+
45
+[env:arduino_test]
46
+platform = atmelavr
47
+board = pro16MHzatmega328
48
+framework = arduino
49
+build_flags = -D PLATFORM_AVR -D FUNCTION_CONTROL -D FUNCTION_UI
50
+upload_port = /dev/ttyUSB1
51
+monitor_port = /dev/ttyUSB1
52
+monitor_speed = 115200
53
+lib_deps =
54
+    https://github.com/nickgammon/SendOnlySoftwareSerial
55
+    https://github.com/rlogiacco/CircularBuffer
56
+
57
+[env:leonardo_main]
58
+platform = atmelavr
59
+board = leonardo
60
+framework = arduino
61
+build_flags = -D PLATFORM_AVR -D FUNCTION_CONTROL
62
+upload_port = /dev/ttyACM0
63
+monitor_port = /dev/ttyACM0
64
+monitor_speed = 115200
65
+lib_deps =
66
+    Wire
67
+    https://github.com/rlogiacco/CircularBuffer
68
+
69
+[env:leonardo_test]
15 70
 platform = atmelavr
16 71
 board = leonardo
17 72
 framework = arduino
73
+build_flags = -D PLATFORM_AVR -D FUNCTION_CONTROL -D FUNCTION_UI
18 74
 upload_port = /dev/ttyACM0
19 75
 monitor_port = /dev/ttyACM0
20 76
 monitor_speed = 115200
21 77
 lib_deps =
22
-  https://github.com/nickgammon/SendOnlySoftwareSerial
23
-  https://github.com/rlogiacco/CircularBuffer
78
+    https://github.com/nickgammon/SendOnlySoftwareSerial
79
+    https://github.com/rlogiacco/CircularBuffer

+ 495
- 0
src/Functionality.cpp Visa fil

@@ -0,0 +1,495 @@
1
+#include <Arduino.h>
2
+
3
+#include "config.h"
4
+#include "config_pins.h"
5
+#include "Functionality.h"
6
+
7
+#ifdef FUNCTION_UI
8
+
9
+#ifndef FUNCTION_CONTROL
10
+
11
+#include <Wire.h>
12
+#include <CircularBuffer.h>
13
+
14
+CircularBuffer<int, I2C_BUF_SIZE> keybuffer;
15
+String linebuffer[4];
16
+
17
+#endif // ! FUNCTION_CONTROL
18
+
19
+#include "SerialLCD.h"
20
+#include "Keymatrix.h"
21
+
22
+SerialLCD lcd(SERIAL_LCD_TX_PIN);
23
+
24
+Keymatrix keys(KEYMATRIX_ROWS, KEYMATRIX_COLS);
25
+int keymatrix_pins[KEYMATRIX_ROWS + KEYMATRIX_COLS] = { KEYMATRIX_ROW_PINS, KEYMATRIX_COL_PINS };
26
+
27
+unsigned long last_input_time = 0;
28
+bool backlight_state = true;
29
+bool doing_multi_input = false;
30
+
31
+#endif // FUNCTION_UI
32
+
33
+#ifdef FUNCTION_CONTROL
34
+
35
+#ifndef FUNCTION_UI
36
+#include <Wire.h>
37
+#endif // ! FUNCTION_UI
38
+
39
+#include "Plants.h"
40
+#include "Statemachine.h"
41
+
42
+Plants plants(VALVE_COUNT, PUMP_COUNT, SWITCH_COUNT);
43
+int valve_pins[VALVE_COUNT] = { VALVE_PINS };
44
+int pump_pins[PUMP_COUNT] = { PUMP_PINS };
45
+int switch_pins[SWITCH_COUNT] = { SWITCH_PINS };
46
+
47
+Statemachine sm(write_to_all, backspace);
48
+
49
+#endif // FUNCTION_CONTROL
50
+
51
+// ----------------------------------------------------------------------------
52
+
53
+#ifdef FUNCTION_UI
54
+
55
+#ifndef FUNCTION_CONTROL
56
+
57
+void ui_i2c_request(void) {
58
+    if (keybuffer.isEmpty()) {
59
+        Wire.write(-4);
60
+        return;
61
+    }
62
+    
63
+    Serial.print("ui_i2c_request: ");
64
+    
65
+    while (!keybuffer.isEmpty()) {
66
+        int n = keybuffer.shift();
67
+        
68
+        // for some reason it seems as if we always get -1 here,
69
+        // so we cant send our input (-2 to 9) as is.
70
+        // so -4 is no-data, -3 is -1, and the rest as-is.
71
+        if (n == -1) {
72
+            n = -3;
73
+        }
74
+        
75
+        Serial.print(n);
76
+        Serial.print(", ");
77
+        
78
+        Wire.write(n);
79
+    }
80
+    
81
+    Serial.println();
82
+}
83
+
84
+void ui_i2c_receive(int count) {
85
+    char buff[I2C_BUF_SIZE];
86
+    for (int i = 0; i < I2C_BUF_SIZE; i++) {
87
+        buff[i] = 0;
88
+    }
89
+    
90
+    for (int i = 0; (i < count) && (Wire.available()); i++) {
91
+        buff[i] = Wire.read();
92
+    }
93
+    
94
+    if (count <= 0) {
95
+        Serial.println("ui_i2c_receive: count is 0");
96
+        return;
97
+    }
98
+    
99
+    if (buff[0] == 0x01) {
100
+        if (count < 3) {
101
+            Serial.println("ui_i2c_receive: blink lcd too short");
102
+            return;
103
+        }
104
+        
105
+        int n = buff[1];
106
+        int wait = buff[2] * 10;
107
+        
108
+        Serial.println("ui_i2c_receive: blink lcd command");
109
+        blink_lcd(n, wait);
110
+    } else if (buff[0] == 0x02) {
111
+        Serial.println("ui_i2c_receive: backspace command");
112
+        backspace();
113
+    } else if (buff[0] == 0x03) {
114
+        if (count < 3) {
115
+            Serial.println("ui_i2c_receive: display far too short");
116
+            return;
117
+        }
118
+        
119
+        int line = buff[1];
120
+        int len = buff[2];
121
+        String s;
122
+        for (int i = 0; i < len; i++) {
123
+            s += buff[3 + i];
124
+        }
125
+        
126
+        Serial.println("ui_i2c_receive: display command");
127
+        linebuffer[line] = s;
128
+    } else if (buff[0] == 0x04) {
129
+        if (count < 2) {
130
+            Serial.println("ui_i2c_receive: num input too short");
131
+            return;
132
+        }
133
+        
134
+        int8_t num_input = buff[1];
135
+        
136
+        Serial.println("ui_i2c_receive: num input");
137
+        write_to_all(linebuffer[0].c_str(), linebuffer[1].c_str(),
138
+                     linebuffer[2].c_str(), linebuffer[3].c_str(),
139
+                     num_input);
140
+    } else {
141
+        Serial.println("ui_i2c_receive: unknown command");
142
+        return;
143
+    }
144
+}
145
+
146
+#endif // ! FUNCTION_CONTROL
147
+
148
+void ui_setup(void) {
149
+    keys.setPins(keymatrix_pins);
150
+    
151
+    Serial.println("Setting up LCD, please wait");
152
+    delay(1000); // give LCD some time to boot
153
+    lcd.init();
154
+    
155
+#ifdef DEBUG_WAIT_FOR_SERIAL_CONN
156
+    lcd.write(0, "Waiting for serial");
157
+    lcd.write(1, "connection on debug");
158
+    lcd.write(2, "USB port...");
159
+    while (!Serial);
160
+    lcd.clear();
161
+#endif // DEBUG_WAIT_FOR_SERIAL_CONN
162
+    
163
+#ifndef FUNCTION_CONTROL
164
+    Serial.println("Initializing I2C Slave");
165
+    Wire.begin(OWN_I2C_ADDRESS);
166
+    Wire.onReceive(ui_i2c_receive);
167
+    Wire.onRequest(ui_i2c_request);
168
+    
169
+    String a = String("- Giess-o-mat V") + FIRMWARE_VERSION + String(" -");
170
+    String b = String("    Address 0x") + String(OWN_I2C_ADDRESS, HEX) + String("    ");
171
+    
172
+    lcd.write(0, a.c_str());
173
+    lcd.write(1, "    I2C UI Panel    ");
174
+    lcd.write(2, "Waiting for data....");
175
+    lcd.write(3, b.c_str());
176
+#endif // ! FUNCTION_CONTROL
177
+}
178
+
179
+void handle_input(int n) {
180
+#ifdef FUNCTION_CONTROL
181
+    
182
+    sm.input(n);
183
+    
184
+#else
185
+    
186
+    keybuffer.push(n);
187
+    
188
+#endif // FUNCTION_CONTROL
189
+}
190
+
191
+void input_keypad(void) {
192
+    keys.scan();
193
+    while (keys.hasEvent()) {
194
+        auto ke = keys.getEvent();
195
+        if (ke.getType() == Keymatrix::Event::button_down) {
196
+            last_input_time = millis();
197
+            if (!backlight_state) {
198
+                backlight_state = true;
199
+                lcd.setBacklight(255);
200
+                
201
+                // swallow input when used to activate light
202
+                continue;
203
+            }
204
+            
205
+            int n = ke.getNum();
206
+            Serial.print("Got keypad input: \"");
207
+            
208
+            if (n < 0) {
209
+                Serial.print((n == -1) ? '*' : '#');
210
+            } else {
211
+                Serial.print(n);
212
+                
213
+                if (doing_multi_input) {
214
+                    char s[2] = { (char)(n + '0'), '\0' };
215
+                    lcd.write(s);
216
+                }
217
+            }
218
+            
219
+            Serial.println("\"");
220
+            
221
+            blink_lcd(1, 100);
222
+            handle_input(n);
223
+        }
224
+    }
225
+}
226
+
227
+void input_serial(void) {
228
+#ifdef DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
229
+    if (Serial.available() > 0) {
230
+        last_input_time = millis();
231
+        if (!backlight_state) {
232
+            backlight_state = true;
233
+            lcd.setBacklight(255);
234
+        }
235
+        
236
+        int c = Serial.read();
237
+        if (c == '*') {
238
+            Serial.write(c);
239
+            Serial.write('\n');
240
+            
241
+            if (doing_multi_input) {
242
+                char s[2] = { (char)(c), '\0' };
243
+                lcd.write(s);
244
+            }
245
+            
246
+            handle_input(-1);
247
+        } else if  (c == '#') {
248
+            Serial.write(c);
249
+            Serial.write('\n');
250
+            
251
+            if (doing_multi_input) {
252
+                char s[2] = { (char)(c), '\0' };
253
+                lcd.write(s);
254
+            }
255
+            
256
+            handle_input(-2);
257
+        } else if  (c == '\n') {
258
+            Serial.write('#');
259
+            Serial.write('\n');
260
+            
261
+            if (doing_multi_input) {
262
+                char s[2] = { '#', '\0' };
263
+                lcd.write(s);
264
+            }
265
+            
266
+            handle_input(-2);
267
+        } else if (c == '\b') {
268
+            Serial.write(c);
269
+            handle_input(-1);
270
+        } else if ((c >= '0') && (c <= '9')) {
271
+            Serial.write(c);
272
+            if (!doing_multi_input) {
273
+                Serial.write('\n');
274
+            }
275
+            
276
+            if (doing_multi_input) {
277
+                char s[2] = { (char)(c), '\0' };
278
+                lcd.write(s);
279
+            }
280
+            
281
+            handle_input(c - '0');
282
+        }
283
+    }
284
+#endif // DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
285
+}
286
+
287
+void ui_run(void) {
288
+    input_keypad();
289
+    input_serial();
290
+    
291
+    if (backlight_state && (millis() >= (last_input_time + DISPLAY_BACKLIGHT_TIMEOUT))) {
292
+        backlight_state = false;
293
+        lcd.setBacklight(0);
294
+    }
295
+}
296
+
297
+void write_to_all(const char *a, const char *b,
298
+                  const char *c, const char *d, int num_input) {
299
+    lcd.clear();
300
+    
301
+    if (num_input >= 0) {
302
+        lcd.write(0, a);
303
+        if (num_input >= 1) {
304
+            lcd.write(1, b);
305
+        }
306
+        if (num_input >= 2) {
307
+            lcd.write(2, c);
308
+        }
309
+        if (num_input >= 3) {
310
+            lcd.write(3, d);
311
+        }
312
+        
313
+        lcd.cursor(3);
314
+        doing_multi_input = true;
315
+    } else {
316
+        lcd.write(0, a);
317
+        lcd.write(1, b);
318
+        lcd.write(2, c);
319
+        lcd.write(3, d);
320
+        
321
+        lcd.cursor(0);
322
+        doing_multi_input = false;
323
+    }
324
+    
325
+#ifdef DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
326
+    int la = strlen(a);
327
+    int lb = strlen(b);
328
+    int lc = strlen(c);
329
+    int ld = strlen(d);
330
+    
331
+    Serial.println();
332
+    Serial.println(" ----------------------");
333
+    
334
+    Serial.print("| ");
335
+    Serial.print(a);
336
+    if (la < 20) {
337
+        for (int i = 0; i < (20 - la); i++) {
338
+            Serial.print(' ');
339
+        }
340
+    }
341
+    Serial.println(" |");
342
+    
343
+    Serial.print("| ");
344
+    Serial.print(b);
345
+    if (lb < 20) {
346
+        for (int i = 0; i < (20 - lb); i++) {
347
+            Serial.print(' ');
348
+        }
349
+    }
350
+    Serial.println(" |");
351
+    
352
+    Serial.print("| ");
353
+    Serial.print(c);
354
+    if (lc < 20) {
355
+        for (int i = 0; i < (20 - lc); i++) {
356
+            Serial.print(' ');
357
+        }
358
+    }
359
+    Serial.println(" |");
360
+    
361
+    Serial.print("| ");
362
+    Serial.print(d);
363
+    if (ld < 20) {
364
+        for (int i = 0; i < (20 - ld); i++) {
365
+            Serial.print(' ');
366
+        }
367
+    }
368
+    Serial.println(" |");
369
+    
370
+    Serial.println(" ----------------------");
371
+    Serial.println("Please provide keypad input:");
372
+#endif // DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
373
+}
374
+
375
+void backspace(void) {
376
+    lcd.write("\b");
377
+}
378
+
379
+void blink_lcd(int n, int wait) {
380
+    for (int i = 0; i < n; i++) {
381
+        lcd.setBacklight(0);
382
+        delay(wait);
383
+        
384
+        lcd.setBacklight(255);
385
+        if (i < (n - 1))
386
+            delay(wait);
387
+    }
388
+}
389
+
390
+#endif // FUNCTION_UI
391
+
392
+// ----------------------------------------------------------------------------
393
+
394
+#ifdef FUNCTION_CONTROL
395
+
396
+void control_setup(void) {
397
+    plants.setValvePins(valve_pins);
398
+    plants.setPumpPins(pump_pins);
399
+    plants.setSwitchPins(switch_pins, true);
400
+    
401
+#ifndef FUNCTION_UI
402
+    
403
+    Serial.println("Initializing I2C Master");
404
+    Wire.begin();
405
+    
406
+#ifdef DEBUG_WAIT_FOR_SERIAL_CONN
407
+    while (!Serial);
408
+#endif // DEBUG_WAIT_FOR_SERIAL_CONN
409
+    
410
+#endif // ! FUNCTION_UI
411
+}
412
+
413
+void control_begin(void) {
414
+    sm.begin();
415
+}
416
+
417
+void control_run(void) {
418
+#ifndef FUNCTION_UI
419
+    
420
+    Wire.requestFrom(OWN_I2C_ADDRESS, I2C_BUF_SIZE);
421
+    while (Wire.available()) {
422
+        char c = Wire.read();
423
+        
424
+        // for some reason it seems as if we always get -1 here,
425
+        // so we cant send our input (-2 to 9) as is.
426
+        // so -4 is no-data, -3 is -1, and the rest as-is.
427
+        if ((c >= -3) && (c <= 9) && (c != -1)) {
428
+            if (c == -3) {
429
+                c = -1;
430
+            }
431
+        
432
+            Serial.print("control_run: got input '");
433
+            Serial.print((int)c);
434
+            Serial.println("'");
435
+        
436
+            sm.input(c);
437
+        }
438
+    }
439
+
440
+#endif // ! FUNCTION_UI
441
+    
442
+    sm.act();
443
+}
444
+
445
+#ifndef FUNCTION_UI
446
+
447
+void blink_lcd(int n, int wait) {
448
+    Serial.println("blink_lcd i2c");
449
+    
450
+    Wire.beginTransmission(OWN_I2C_ADDRESS);
451
+    Wire.write(0x01); // blink command
452
+    Wire.write(n); // count
453
+    Wire.write(wait / 10); // time
454
+    Wire.endTransmission();
455
+}
456
+
457
+void backspace(void) {
458
+    Serial.println("backspace i2c");
459
+    
460
+    Wire.beginTransmission(OWN_I2C_ADDRESS);
461
+    Wire.write(0x02); // backspace command
462
+    Wire.endTransmission();
463
+}
464
+
465
+void write_to_all(const char *a, const char *b,
466
+                  const char *c, const char *d, int num_input) {
467
+    const char *lines[4] = { a, b, c, d };
468
+    
469
+    Serial.println("write_to_all i2c");
470
+    
471
+    for (int i = 0; i < 4; i++) {
472
+        Wire.beginTransmission(OWN_I2C_ADDRESS);
473
+        Wire.write(0x03); // display command
474
+    
475
+        Wire.write(i);
476
+        
477
+        int l = strlen(lines[i]);
478
+        Wire.write(l);
479
+        
480
+        for (int n = 0; n < l; n++) {
481
+            Wire.write(lines[i][n]);
482
+        }
483
+    
484
+        Wire.endTransmission();
485
+    }
486
+    
487
+    Wire.beginTransmission(OWN_I2C_ADDRESS);
488
+    Wire.write(0x04); // display command
489
+    Wire.write((int8_t)num_input);
490
+    Wire.endTransmission();
491
+}
492
+
493
+#endif // ! FUNCTION_UI
494
+
495
+#endif // FUNCTION_CONTROL

+ 4
- 0
src/Keymatrix.cpp Visa fil

@@ -1,6 +1,8 @@
1 1
 #include <Arduino.h>
2 2
 #include "Keymatrix.h"
3 3
 
4
+#ifdef FUNCTION_UI
5
+
4 6
 //#define DEBUG_PRINT_MATRIX_STATE
5 7
 
6 8
 Keymatrix::Event::Event(EventType _type, int _row, int _col) {
@@ -155,3 +157,5 @@ Keymatrix::Event Keymatrix::getEvent(void) {
155 157
         return Keymatrix::Event(Event::no_event, -1, -1);
156 158
     }
157 159
 }
160
+
161
+#endif // FUNCTION_UI

+ 9
- 0
src/SerialLCD.cpp Visa fil

@@ -1,10 +1,17 @@
1 1
 #include <Arduino.h>
2 2
 #include "SerialLCD.h"
3 3
 
4
+#ifdef FUNCTION_UI
5
+
4 6
 #define LCD_DELAY 3 // 3
5 7
 
6 8
 SerialLCD::SerialLCD(int tx_pin) {
9
+#if defined(PLATFORM_AVR)
7 10
     lcd = new SendOnlySoftwareSerial(tx_pin);
11
+#elif defined(PLATFORM_ESP)
12
+    lcd = new SoftwareSerial(tx_pin);
13
+#endif
14
+    
8 15
     lcd->begin(9600);
9 16
 }
10 17
 
@@ -90,3 +97,5 @@ void SerialLCD::write(int line, int col, const char *text) {
90 97
     position(line, col);
91 98
     write(text);
92 99
 }
100
+
101
+#endif // FUNCTION_UI

+ 136
- 0
src/SimpleUpdater.cpp Visa fil

@@ -0,0 +1,136 @@
1
+/*
2
+ * SimpleUpdater.cpp
3
+ *
4
+ * ESP8266 / ESP32 Environmental Sensor
5
+ *
6
+ * ----------------------------------------------------------------------------
7
+ * "THE BEER-WARE LICENSE" (Revision 42):
8
+ * <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
9
+ * you can do whatever you want with this stuff. If we meet some day, and you
10
+ * think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
11
+ * ----------------------------------------------------------------------------
12
+ */
13
+
14
+#if defined(ARDUINO_ARCH_ESP8266)
15
+#include <ESP8266HTTPUpdateServer.h>
16
+#elif defined(ARDUINO_ARCH_ESP32)
17
+#include <Update.h>
18
+#endif
19
+
20
+#include "SimpleUpdater.h"
21
+
22
+#if defined(ARDUINO_ARCH_ESP32)
23
+
24
+void SimpleUpdater::get(void) {
25
+    String uploadPage = F(
26
+        "<html><head>"
27
+        "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
28
+        "<title>SimpleUpdater ESP32</title>"
29
+        "</head><body>"
30
+        "<h1>SimpleUpdater</h1>"
31
+        "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
32
+        "<input type='file' name='update'>"
33
+        "<input type='submit' value='Update'>"
34
+        "</form>"
35
+        "<div id='prg'>progress: 0%</div>"
36
+        "<a href=\"/\">Back to Main Page</a>"
37
+        "<script>"
38
+        "$('form').submit(function(e){"
39
+        "e.preventDefault();"
40
+        "var form = $('#upload_form')[0];"
41
+        "var data = new FormData(form);"
42
+        " $.ajax({"
43
+        "url: '/update',"
44
+        "type: 'POST',"
45
+        "data: data,"
46
+        "contentType: false,"
47
+        "processData:false,"
48
+        "xhr: function() {"
49
+        "var xhr = new window.XMLHttpRequest();"
50
+        "xhr.upload.addEventListener('progress', function(evt) {"
51
+        "if (evt.lengthComputable) {"
52
+        "var per = evt.loaded / evt.total;"
53
+        "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
54
+        "}"
55
+        "}, false);"
56
+        "return xhr;"
57
+        "},"
58
+        "success:function(d, s) {"
59
+        "console.log('success!')" 
60
+        "},"
61
+        "error: function (a, b, c) {"
62
+        "}"
63
+        "});"
64
+        "});"
65
+        "</script>"
66
+        "</body></html>"
67
+    );
68
+    
69
+    server->send(200, "text/html", uploadPage);
70
+}
71
+
72
+void SimpleUpdater::postResult(void) {
73
+    server->sendHeader("Connection", "close");
74
+    server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
75
+    ESP.restart();
76
+}
77
+
78
+void SimpleUpdater::postUpload(void) {
79
+    HTTPUpload& upload = server->upload();
80
+    if (upload.status == UPLOAD_FILE_START) {
81
+        Serial.printf("Update: %s\n", upload.filename.c_str());
82
+        // start with max available size
83
+        if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
84
+            Update.printError(Serial);
85
+        }
86
+    } else if (upload.status == UPLOAD_FILE_WRITE) {
87
+        // flashing firmware to ESP
88
+        if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
89
+            Update.printError(Serial);
90
+        }
91
+    } else if (upload.status == UPLOAD_FILE_END) {
92
+        // true to set the size to the current progress
93
+        if (Update.end(true)) {
94
+            Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
95
+        } else {
96
+            Update.printError(Serial);
97
+        }
98
+    }
99
+}
100
+
101
+#endif
102
+
103
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
104
+
105
+void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) {
106
+    if (_server == NULL) {
107
+        return;
108
+    }
109
+    
110
+    server = _server;
111
+    
112
+#if defined(ARDUINO_ARCH_ESP8266)
113
+    
114
+    updateServer.setup(server);
115
+    
116
+#elif defined(ARDUINO_ARCH_ESP32)
117
+    
118
+    server->on(uri.c_str(), HTTP_POST, [this]() {
119
+        postResult();
120
+    }, [this]() {
121
+        postUpload();
122
+    });
123
+    
124
+    server->on(uri.c_str(), HTTP_GET, [this]() {
125
+        get();
126
+    });
127
+    
128
+#endif
129
+}
130
+
131
+SimpleUpdater::SimpleUpdater(String _uri) {
132
+    uri = _uri;
133
+    server = NULL;
134
+}
135
+
136
+#endif

+ 3
- 1
src/Statemachine.cpp Visa fil

@@ -464,7 +464,9 @@ void Statemachine::switch_to(States s) {
464 464
     state = s;
465 465
     
466 466
     if (s == init) {
467
-        print("- Giess-o-mat V0.1 -",
467
+        String a = String("- Giess-o-mat V") + FIRMWARE_VERSION + String(" -");
468
+        
469
+        print(a.c_str(),
468 470
               "Usage:  Enter number",
469 471
               "* Delete prev. digit",
470 472
               "# Execute input num.",

+ 185
- 0
src/WifiStuff.cpp Visa fil

@@ -0,0 +1,185 @@
1
+#include <Arduino.h>
2
+
3
+#ifdef PLATFORM_ESP
4
+
5
+#if defined(ARDUINO_ARCH_ESP8266)
6
+
7
+#include <ESP8266WiFi.h>
8
+#include <ESP8266WebServer.h>
9
+#include <ESP8266mDNS.h>
10
+
11
+#elif defined(ARDUINO_ARCH_ESP32)
12
+
13
+#include <WiFi.h>
14
+#include <WebServer.h>
15
+#include <ESPmDNS.h>
16
+
17
+#endif
18
+
19
+#include "wifi.h"
20
+#include "config.h"
21
+#include "config_pins.h"
22
+#include "SimpleUpdater.h"
23
+#include "WifiStuff.h"
24
+
25
+UPDATE_WEB_SERVER server(80);
26
+SimpleUpdater updater;
27
+unsigned long last_server_handle_time = 0;
28
+
29
+String message_buffer_a;
30
+String message_buffer_b;
31
+String message_buffer_c;
32
+String message_buffer_d;
33
+
34
+void wifi_set_message_buffer(String a, String b, String c, String d) {
35
+    message_buffer_a = a;
36
+    message_buffer_b = b;
37
+    message_buffer_c = c;
38
+    message_buffer_d = d;
39
+}
40
+
41
+void handleRoot() {
42
+    String message = F("<html><head>\n");
43
+    message += F("<title>Giess-o-mat</title>\n");
44
+    message += F("</head><body>\n");
45
+    message += F("<h1>Giess-o-mat</h1>\n");
46
+    
47
+    message += F("\n<pre>\n");
48
+    message += message_buffer_a + '\n';
49
+    message += message_buffer_b + '\n';
50
+    message += message_buffer_c + '\n';
51
+    message += message_buffer_d + '\n';
52
+    message += F("\n</pre>\n");
53
+    
54
+    message += F("\n<p>\n");
55
+    message += F("State: ");
56
+    // TODO
57
+    message += F("\n</p>\n");
58
+    
59
+    message += F("\n<p>\n");
60
+    message += F("Version: ");
61
+    message += FIRMWARE_VERSION;
62
+    message += F("\n<br>\n");
63
+    message += F("MAC: ");
64
+    message += WiFi.macAddress();
65
+    message += F("\n</p>\n");
66
+
67
+#if defined(ARDUINO_ARCH_ESP8266)
68
+    
69
+    message += F("\n<p>\n");
70
+    message += F("Reset reason: ");
71
+    message += ESP.getResetReason();
72
+    message += F("\n<br>\n");
73
+    message += F("Free heap: ");
74
+    message += String(ESP.getFreeHeap());
75
+    message += F(" (");
76
+    message += String(ESP.getHeapFragmentation());
77
+    message += F("% fragmentation)");
78
+    message += F("\n<br>\n");
79
+    message += F("Free sketch space: ");
80
+    message += String(ESP.getFreeSketchSpace());
81
+    message += F("\n<br>\n");
82
+    message += F("Flash chip real size: ");
83
+    message += String(ESP.getFlashChipRealSize());
84
+
85
+    if (ESP.getFlashChipSize() != ESP.getFlashChipRealSize()) {
86
+        message += F("\n<br>\n");
87
+        message += F("WARNING: sdk chip size (");
88
+        message += (ESP.getFlashChipSize());
89
+        message += F(") does not match!");
90
+    }
91
+    
92
+    message += F("\n</p>\n");
93
+    
94
+#elif defined(ARDUINO_ARCH_ESP32)
95
+
96
+    message += F("\n<p>\n");
97
+    message += F("Free heap: ");
98
+    message += String(ESP.getFreeHeap() / 1024.0);
99
+    message += F("k\n<br>\n");
100
+    message += F("Free sketch space: ");
101
+    message += String(ESP.getFreeSketchSpace() / 1024.0);
102
+    message += F("k\n<br>\n");
103
+    message += F("Flash chip size: ");
104
+    message += String(ESP.getFlashChipSize() / 1024.0);
105
+    message += F("k\n</p>\n");
106
+    
107
+#endif
108
+
109
+    message += F("<p>\n");
110
+    message += F("Try <a href=\"/update\">/update</a> for OTA firmware updates!\n");
111
+    message += F("</p>\n");
112
+    message += F("</body></html>\n");
113
+
114
+    server.send(200, "text/html", message);
115
+}
116
+
117
+void wifi_setup() {
118
+    // Build hostname string
119
+    String hostname = "giess-o-mat";
120
+
121
+#if defined(ARDUINO_ARCH_ESP8266)
122
+
123
+    // Connect to WiFi AP
124
+    WiFi.hostname(hostname);
125
+    WiFi.mode(WIFI_STA);
126
+    WiFi.begin(WIFI_SSID, WIFI_PW);
127
+    while (WiFi.status() != WL_CONNECTED) {
128
+        delay(LED_CONNECT_BLINK_INTERVAL);
129
+        digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
130
+    }
131
+    
132
+#elif defined(ARDUINO_ARCH_ESP32)
133
+
134
+    // Set hostname workaround
135
+    WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
136
+    WiFi.setHostname(hostname.c_str());
137
+    
138
+    // Workaround for WiFi connecting only every 2nd reset
139
+    // https://github.com/espressif/arduino-esp32/issues/2501#issuecomment-513602522
140
+    WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
141
+        if (info.disconnected.reason == 202) {
142
+            esp_sleep_enable_timer_wakeup(10);
143
+            esp_deep_sleep_start();
144
+            delay(100);
145
+        }
146
+    }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
147
+
148
+    // Connect to WiFi AP
149
+    WiFi.mode(WIFI_STA);
150
+    WiFi.begin(WIFI_SSID, WIFI_PW);
151
+    while (WiFi.status() != WL_CONNECTED) {
152
+        delay(LED_CONNECT_BLINK_INTERVAL);
153
+        digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
154
+    }
155
+    
156
+    // Set hostname workaround
157
+    WiFi.setHostname(hostname.c_str());
158
+
159
+#endif
160
+
161
+    // Setup HTTP Server
162
+    MDNS.begin(hostname.c_str());
163
+    updater.setup(&server);
164
+    server.on("/", handleRoot);
165
+    server.begin();
166
+    MDNS.addService("http", "tcp", 80);
167
+}
168
+
169
+void wifi_run() {
170
+    if ((millis() - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
171
+        last_server_handle_time = millis();
172
+        server.handleClient();
173
+        
174
+#ifdef ARDUINO_ARCH_ESP8266
175
+        MDNS.update();
176
+#endif // ARDUINO_ARCH_ESP8266
177
+    }
178
+    
179
+    // reset ESP every 6h to be safe
180
+    if (millis() >= (6 * 60 * 60 * 1000)) {
181
+        ESP.restart();
182
+    }
183
+}
184
+
185
+#endif // PLATFORM_ESP

+ 46
- 227
src/main.cpp Visa fil

@@ -1,244 +1,63 @@
1 1
 #include <Arduino.h>
2
-#include "Keymatrix.h"
3
-#include "SerialLCD.h"
4
-#include "Statemachine.h"
5
-#include "Plants.h"
6
-#include "config.h"
7
-
8
-SerialLCD lcd(9);
9
-
10
-Keymatrix keys(4, 3);
11
-int keymatrix_pins[4 + 3] = { 5, 6, 7, 8, 2, 3, 4 };
12
-
13
-Plants plants(5, 3, 2);
14
-int valve_pins[5] = { 10, 11, 12, 13, 14 };
15
-int pump_pins[3] = { 15, 16, 17 };
16
-int switch_pins[2] = { 18, 19 };
17
-
18
-unsigned long last_input_time = 0;
19
-bool backlight_state = true;
20
-
21
-bool doing_multi_input = false;
22
-
23
-void write_to_all(const char *a, const char *b,
24
-                  const char *c, const char *d, int num_input) {
25
-    lcd.clear();
26
-    
27
-    if (num_input >= 0) {
28
-        lcd.write(0, a);
29
-        if (num_input >= 1) {
30
-            lcd.write(1, b);
31
-        }
32
-        if (num_input >= 2) {
33
-            lcd.write(2, c);
34
-        }
35
-        if (num_input >= 3) {
36
-            lcd.write(3, d);
37
-        }
38
-        
39
-        lcd.cursor(3);
40
-        doing_multi_input = true;
41
-    } else {
42
-        lcd.write(0, a);
43
-        lcd.write(1, b);
44
-        lcd.write(2, c);
45
-        lcd.write(3, d);
46
-        
47
-        lcd.cursor(0);
48
-        doing_multi_input = false;
49
-    }
50
-    
51
-#ifdef DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
52
-    int la = strlen(a);
53
-    int lb = strlen(b);
54
-    int lc = strlen(c);
55
-    int ld = strlen(d);
56
-    
57
-    Serial.println();
58
-    Serial.println(" ----------------------");
59
-    
60
-    Serial.print("| ");
61
-    Serial.print(a);
62
-    if (la < 20) {
63
-        for (int i = 0; i < (20 - la); i++) {
64
-            Serial.print(' ');
65
-        }
66
-    }
67
-    Serial.println(" |");
68
-    
69
-    Serial.print("| ");
70
-    Serial.print(b);
71
-    if (lb < 20) {
72
-        for (int i = 0; i < (20 - lb); i++) {
73
-            Serial.print(' ');
74
-        }
75
-    }
76
-    Serial.println(" |");
77
-    
78
-    Serial.print("| ");
79
-    Serial.print(c);
80
-    if (lc < 20) {
81
-        for (int i = 0; i < (20 - lc); i++) {
82
-            Serial.print(' ');
83
-        }
84
-    }
85
-    Serial.println(" |");
86
-    
87
-    Serial.print("| ");
88
-    Serial.print(d);
89
-    if (ld < 20) {
90
-        for (int i = 0; i < (20 - ld); i++) {
91
-            Serial.print(' ');
92
-        }
93
-    }
94
-    Serial.println(" |");
95
-    
96
-    Serial.println(" ----------------------");
97
-    Serial.println("Please provide keypad input:");
98
-#endif
99
-}
100 2
 
101
-void backspace(void) {
102
-    lcd.write("\b");
103
-}
3
+#include "Functionality.h"
4
+#include "WifiStuff.h"
5
+#include "config.h"
6
+#include "config_pins.h"
104 7
 
105
-Statemachine sm(write_to_all, backspace);
8
+unsigned long last_led_blink_time = 0;
106 9
 
107 10
 void setup() {
11
+    pinMode(BUILTIN_LED_PIN, OUTPUT);
12
+    digitalWrite(BUILTIN_LED_PIN, HIGH);
13
+    
108 14
     Serial.begin(115200);
109 15
     Serial.println("Initializing Giess-o-mat");
110
-    
111
-    keys.setPins(keymatrix_pins);
112
-    plants.setValvePins(valve_pins);
113
-    plants.setPumpPins(pump_pins);
114
-    plants.setSwitchPins(switch_pins, true);
16
+    Serial.println("Version: " FIRMWARE_VERSION);
115 17
 
116
-    Serial.println("Setting up LCD, please wait");
117
-    delay(1000); // give LCD some time to boot
118
-    lcd.init();
119
-    
120
-#ifdef DEBUG_WAIT_FOR_SERIAL_CONN
121
-    lcd.write(0, "Waiting for serial");
122
-    lcd.write(1, "connection on debug");
123
-    lcd.write(2, "USB port...");
18
+#ifdef FUNCTION_UI
19
+    ui_setup();
20
+#endif // FUNCTION_UI
124 21
     
125
-    while (!Serial);
22
+#ifdef FUNCTION_CONTROL
23
+    control_setup();
24
+#endif // FUNCTION_CONTROL
126 25
     
127
-    lcd.clear();
128
-#endif
26
+#ifdef PLATFORM_ESP
27
+    wifi_setup();
28
+#endif // PLATFORM_ESP
129 29
     
130 30
     Serial.println("Ready, starting main loop");
131
-    sm.begin();
132
-}
133
-
134
-void blink_lcd(int n, int wait = 200) {
135
-    for (int i = 0; i < n; i++) {
136
-        lcd.setBacklight(0);
137
-        delay(wait);
138
-        
139
-        lcd.setBacklight(255);
140
-        if (i < (n - 1))
141
-            delay(wait);
142
-    }
143
-}
144
-
145
-void loop() {
146
-    keys.scan();
147
-    while (keys.hasEvent()) {
148
-        auto ke = keys.getEvent();
149
-        if (ke.getType() == Keymatrix::Event::button_down) {
150
-            last_input_time = millis();
151
-            if (!backlight_state) {
152
-                backlight_state = true;
153
-                lcd.setBacklight(255);
154
-                
155
-                // swallow input when used to activate light
156
-                continue;
157
-            }
158
-            
159
-            int n = ke.getNum();
160
-            Serial.print("Got keypad input: \"");
161
-            
162
-            if (n < 0) {
163
-                Serial.print((n == -1) ? '*' : '#');
164
-            } else {
165
-                Serial.print(n);
166
-                
167
-                if (doing_multi_input) {
168
-                    char s[2] = { (char)(n + '0'), '\0' };
169
-                    lcd.write(s);
170
-                }
171
-            }
172
-            
173
-            Serial.println("\"");
174
-            
175
-            blink_lcd(1, 100);
176
-            sm.input(n);
177
-        }
178
-    }
31
+    digitalWrite(BUILTIN_LED_PIN, LOW);
179 32
     
180
-#ifdef DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
181
-    if (Serial.available() > 0) {
182
-        last_input_time = millis();
183
-        if (!backlight_state) {
184
-            backlight_state = true;
185
-            lcd.setBacklight(255);
186
-        }
187
-        
188
-        int c = Serial.read();
189
-        if (c == '*') {
190
-            Serial.write(c);
191
-            Serial.write('\n');
192
-            
193
-            if (doing_multi_input) {
194
-                char s[2] = { (char)(c), '\0' };
195
-                lcd.write(s);
196
-            }
197
-            
198
-            sm.input(-1);
199
-        } else if  (c == '#') {
200
-            Serial.write(c);
201
-            Serial.write('\n');
202
-            
203
-            if (doing_multi_input) {
204
-                char s[2] = { (char)(c), '\0' };
205
-                lcd.write(s);
206
-            }
207
-            
208
-            sm.input(-2);
209
-        } else if  (c == '\n') {
210
-            Serial.write('#');
211
-            Serial.write('\n');
212
-            
213
-            if (doing_multi_input) {
214
-                char s[2] = { '#', '\0' };
215
-                lcd.write(s);
216
-            }
217
-            
218
-            sm.input(-2);
219
-        } else if (c == '\b') {
220
-            Serial.write(c);
221
-            sm.input(-1);
222
-        } else if ((c >= '0') && (c <= '9')) {
223
-            Serial.write(c);
224
-            if (!doing_multi_input) {
225
-                Serial.write('\n');
226
-            }
227
-            
228
-            if (doing_multi_input) {
229
-                char s[2] = { (char)(c), '\0' };
230
-                lcd.write(s);
231
-            }
232
-            
233
-            sm.input(c - '0');
234
-        }
235
-    }
236
-#endif
33
+#ifdef FUNCTION_CONTROL
237 34
     
238
-    sm.act();
35
+#ifndef FUNCTION_UI
36
+    // give ui unit some time to initialize
37
+    delay(3000);
38
+#endif // ! FUNCTION_UI
239 39
     
240
-    if (backlight_state && (millis() >= (last_input_time + DISPLAY_BACKLIGHT_TIMEOUT))) {
241
-        backlight_state = false;
242
-        lcd.setBacklight(0);
40
+    control_begin();
41
+    
42
+#endif // FUNCTION_CONTROL
43
+}
44
+
45
+void loop() {
46
+#ifdef PLATFORM_ESP
47
+    wifi_run();
48
+#endif // PLATFORM_ESP
49
+
50
+#ifdef FUNCTION_UI
51
+    ui_run();
52
+#endif // FUNCTION_UI
53
+    
54
+#ifdef FUNCTION_CONTROL
55
+    control_run();
56
+#endif // FUNCTION_CONTROL
57
+
58
+    // blink heartbeat LED
59
+    if ((millis() - last_led_blink_time) >= LED_BLINK_INTERVAL) {
60
+        last_led_blink_time = millis();
61
+        digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
243 62
     }
244 63
 }

Laddar…
Avbryt
Spara