Browse Source

Now works with two distributed units connected via i2c

Thomas Buck 3 years ago
parent
commit
82a56ea9dc
12 changed files with 718 additions and 260 deletions
  1. 28
    0
      include/Functionality.h
  2. 4
    0
      include/Keymatrix.h
  3. 4
    0
      include/SerialLCD.h
  4. 4
    0
      include/WifiStuff.h
  5. 4
    1
      include/config.h
  6. 91
    0
      include/config_pins.h
  7. 34
    11
      platformio.ini
  8. 495
    0
      src/Functionality.cpp
  9. 4
    0
      src/Keymatrix.cpp
  10. 4
    0
      src/SerialLCD.cpp
  11. 7
    14
      src/WifiStuff.cpp
  12. 39
    234
      src/main.cpp

+ 28
- 0
include/Functionality.h View File

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 View File

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

+ 4
- 0
include/SerialLCD.h View File

1
 #ifndef _SERIAL_LCD_H_
1
 #ifndef _SERIAL_LCD_H_
2
 #define _SERIAL_LCD_H_
2
 #define _SERIAL_LCD_H_
3
 
3
 
4
+#ifdef FUNCTION_UI
5
+
4
 #if defined(PLATFORM_AVR)
6
 #if defined(PLATFORM_AVR)
5
 #include <SendOnlySoftwareSerial.h>
7
 #include <SendOnlySoftwareSerial.h>
6
 #elif defined(PLATFORM_ESP)
8
 #elif defined(PLATFORM_ESP)
35
 #endif
37
 #endif
36
 };
38
 };
37
 
39
 
40
+#endif // FUNCTION_UI
41
+
38
 #endif // _SERIAL_LCD_H_
42
 #endif // _SERIAL_LCD_H_

+ 4
- 0
include/WifiStuff.h View File

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

+ 4
- 1
include/config.h View File

19
 
19
 
20
 // all given in milliseconds
20
 // all given in milliseconds
21
 #define SERVER_HANDLE_INTERVAL 10
21
 #define SERVER_HANDLE_INTERVAL 10
22
-#define LED_BLINK_INTERVAL (2 * 1000)
22
+#define LED_BLINK_INTERVAL 500
23
 #define LED_INIT_BLINK_INTERVAL 500
23
 #define LED_INIT_BLINK_INTERVAL 500
24
 #define LED_CONNECT_BLINK_INTERVAL 250
24
 #define LED_CONNECT_BLINK_INTERVAL 250
25
 #define LED_ERROR_BLINK_INTERVAL 100
25
 #define LED_ERROR_BLINK_INTERVAL 100
26
 
26
 
27
+#define OWN_I2C_ADDRESS 0x42
28
+#define I2C_BUF_SIZE 32
29
+
27
 #endif // _CONFIG_H_
30
 #endif // _CONFIG_H_

+ 91
- 0
include/config_pins.h View File

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_

+ 34
- 11
platformio.ini View File

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

+ 495
- 0
src/Functionality.cpp View File

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 View File

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

+ 4
- 0
src/SerialLCD.cpp View File

1
 #include <Arduino.h>
1
 #include <Arduino.h>
2
 #include "SerialLCD.h"
2
 #include "SerialLCD.h"
3
 
3
 
4
+#ifdef FUNCTION_UI
5
+
4
 #define LCD_DELAY 3 // 3
6
 #define LCD_DELAY 3 // 3
5
 
7
 
6
 SerialLCD::SerialLCD(int tx_pin) {
8
 SerialLCD::SerialLCD(int tx_pin) {
95
     position(line, col);
97
     position(line, col);
96
     write(text);
98
     write(text);
97
 }
99
 }
100
+
101
+#endif // FUNCTION_UI

+ 7
- 14
src/WifiStuff.cpp View File

1
 #include <Arduino.h>
1
 #include <Arduino.h>
2
 
2
 
3
+#ifdef PLATFORM_ESP
4
+
3
 #if defined(ARDUINO_ARCH_ESP8266)
5
 #if defined(ARDUINO_ARCH_ESP8266)
4
 
6
 
5
 #include <ESP8266WiFi.h>
7
 #include <ESP8266WiFi.h>
14
 
16
 
15
 #endif
17
 #endif
16
 
18
 
17
-#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
18
-
19
 #include "wifi.h"
19
 #include "wifi.h"
20
 #include "config.h"
20
 #include "config.h"
21
+#include "config_pins.h"
21
 #include "SimpleUpdater.h"
22
 #include "SimpleUpdater.h"
22
 #include "WifiStuff.h"
23
 #include "WifiStuff.h"
23
 
24
 
24
-#define BUILTIN_LED_PIN 1
25
-
26
 UPDATE_WEB_SERVER server(80);
25
 UPDATE_WEB_SERVER server(80);
27
 SimpleUpdater updater;
26
 SimpleUpdater updater;
28
 unsigned long last_server_handle_time = 0;
27
 unsigned long last_server_handle_time = 0;
29
-unsigned long last_led_blink_time = 0;
30
 
28
 
31
 String message_buffer_a;
29
 String message_buffer_a;
32
 String message_buffer_b;
30
 String message_buffer_b;
172
     if ((millis() - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
170
     if ((millis() - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
173
         last_server_handle_time = millis();
171
         last_server_handle_time = millis();
174
         server.handleClient();
172
         server.handleClient();
175
-#if defined(ARDUINO_ARCH_ESP8266)
173
+        
174
+#ifdef ARDUINO_ARCH_ESP8266
176
         MDNS.update();
175
         MDNS.update();
177
-#endif
178
-    }
179
-
180
-    // blink heartbeat LED
181
-    if ((millis() - last_led_blink_time) >= LED_BLINK_INTERVAL) {
182
-        last_led_blink_time = millis();
183
-        digitalWrite(BUILTIN_LED_PIN, !digitalRead(BUILTIN_LED_PIN));
176
+#endif // ARDUINO_ARCH_ESP8266
184
     }
177
     }
185
     
178
     
186
     // reset ESP every 6h to be safe
179
     // reset ESP every 6h to be safe
189
     }
182
     }
190
 }
183
 }
191
 
184
 
192
-#endif // defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
185
+#endif // PLATFORM_ESP

+ 39
- 234
src/main.cpp View File

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

Loading…
Cancel
Save