Преглед на файлове

Started adding ESP support

Thomas Buck преди 3 години
родител
ревизия
caff88085f
променени са 12 файла, в които са добавени 569 реда и са изтрити 99 реда
  1. 1
    0
      .gitignore
  2. 6
    0
      README.md
  3. 10
    0
      include/SerialLCD.h
  4. 54
    0
      include/SimpleUpdater.h
  5. 7
    0
      include/WifiStuff.h
  6. 10
    0
      include/config.h
  7. 35
    2
      platformio.ini
  8. 5
    0
      src/SerialLCD.cpp
  9. 136
    0
      src/SimpleUpdater.cpp
  10. 3
    1
      src/Statemachine.cpp
  11. 192
    0
      src/WifiStuff.cpp
  12. 110
    96
      src/main.cpp

+ 1
- 0
.gitignore Целия файл

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

+ 6
- 0
README.md Целия файл

@@ -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.

+ 10
- 0
include/SerialLCD.h Целия файл

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

+ 54
- 0
include/SimpleUpdater.h Целия файл

@@ -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
+

+ 7
- 0
include/WifiStuff.h Целия файл

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

+ 10
- 0
include/config.h Целия файл

@@ -14,4 +14,14 @@
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 (2 * 1000)
23
+#define LED_INIT_BLINK_INTERVAL 500
24
+#define LED_CONNECT_BLINK_INTERVAL 250
25
+#define LED_ERROR_BLINK_INTERVAL 100
26
+
17 27
 #endif // _CONFIG_H_

+ 35
- 2
platformio.ini Целия файл

@@ -9,12 +9,45 @@
9 9
 ; https://docs.platformio.org/page/projectconf.html
10 10
 
11 11
 [platformio]
12
-default_envs = leonardo
12
+default_envs = esp32_main, arduino_ui
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
+    https://github.com/rlogiacco/CircularBuffer
21
+    https://github.com/plerup/espsoftwareserial
22
+
23
+[env:esp32_main]
24
+platform = espressif32
25
+board = esp32dev
26
+framework = arduino
27
+build_flags = -D PLATFORM_ESP -D FUNCTION_CONTROL
28
+upload_protocol = esptool
29
+upload_port = /dev/ttyUSB1
30
+lib_deps =
31
+    https://github.com/rlogiacco/CircularBuffer
32
+    https://github.com/plerup/espsoftwareserial
33
+
34
+[env:arduino_ui]
35
+platform = atmelavr
36
+board = pro16MHzatmega328
37
+framework = arduino
38
+build_flags = -D PLATFORM_AVR -D FUNCTION_UI
39
+upload_port = /dev/ttyUSB1
40
+monitor_port = /dev/ttyUSB1
41
+monitor_speed = 115200
42
+lib_deps =
43
+  https://github.com/nickgammon/SendOnlySoftwareSerial
44
+  https://github.com/rlogiacco/CircularBuffer
45
+
46
+[env:leonardo_ui]
15 47
 platform = atmelavr
16 48
 board = leonardo
17 49
 framework = arduino
50
+build_flags = -D PLATFORM_AVR -D FUNCTION_UI
18 51
 upload_port = /dev/ttyACM0
19 52
 monitor_port = /dev/ttyACM0
20 53
 monitor_speed = 115200

+ 5
- 0
src/SerialLCD.cpp Целия файл

@@ -4,7 +4,12 @@
4 4
 #define LCD_DELAY 3 // 3
5 5
 
6 6
 SerialLCD::SerialLCD(int tx_pin) {
7
+#if defined(PLATFORM_AVR)
7 8
     lcd = new SendOnlySoftwareSerial(tx_pin);
9
+#elif defined(PLATFORM_ESP)
10
+    lcd = new SoftwareSerial(tx_pin);
11
+#endif
12
+    
8 13
     lcd->begin(9600);
9 14
 }
10 15
 

+ 136
- 0
src/SimpleUpdater.cpp Целия файл

@@ -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 Целия файл

@@ -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.",

+ 192
- 0
src/WifiStuff.cpp Целия файл

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

+ 110
- 96
src/main.cpp Целия файл

@@ -1,10 +1,17 @@
1 1
 #include <Arduino.h>
2
+
2 3
 #include "Keymatrix.h"
3 4
 #include "SerialLCD.h"
4 5
 #include "Statemachine.h"
5 6
 #include "Plants.h"
7
+#include "WifiStuff.h"
6 8
 #include "config.h"
7 9
 
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
+
8 15
 SerialLCD lcd(9);
9 16
 
10 17
 Keymatrix keys(4, 3);
@@ -15,95 +22,12 @@ int valve_pins[5] = { 10, 11, 12, 13, 14 };
15 22
 int pump_pins[3] = { 15, 16, 17 };
16 23
 int switch_pins[2] = { 18, 19 };
17 24
 
25
+Statemachine sm(write_to_all, backspace);
26
+
18 27
 unsigned long last_input_time = 0;
19 28
 bool backlight_state = true;
20
-
21 29
 bool doing_multi_input = false;
22 30
 
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
-
101
-void backspace(void) {
102
-    lcd.write("\b");
103
-}
104
-
105
-Statemachine sm(write_to_all, backspace);
106
-
107 31
 void setup() {
108 32
     Serial.begin(115200);
109 33
     Serial.println("Initializing Giess-o-mat");
@@ -127,22 +51,19 @@ void setup() {
127 51
     lcd.clear();
128 52
 #endif
129 53
     
54
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
55
+    wifi_setup();
56
+#endif
57
+    
130 58
     Serial.println("Ready, starting main loop");
131 59
     sm.begin();
132 60
 }
133 61
 
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 62
 void loop() {
63
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
64
+    wifi_run();
65
+#endif
66
+
146 67
     keys.scan();
147 68
     while (keys.hasEvent()) {
148 69
         auto ke = keys.getEvent();
@@ -242,3 +163,96 @@ void loop() {
242 163
         lcd.setBacklight(0);
243 164
     }
244 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(" |");
212
+    
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(" |");
221
+    
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(" |");
230
+    
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
243
+}
244
+
245
+void backspace(void) {
246
+    lcd.write("\b");
247
+}
248
+
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);
257
+    }
258
+}

Loading…
Отказ
Запис