diff --git a/esp32/platformio.ini b/esp32/platformio.ini index 59ac1b8..0987016 100644 --- a/esp32/platformio.ini +++ b/esp32/platformio.ini @@ -19,4 +19,4 @@ lib_deps = build_flags = -Wall -Werror upload_protocol = espota -upload_port = 10.20.0.119 +upload_port = spaceapibox.local diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index fb2fd78..cafc686 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -25,6 +25,7 @@ #define I_E 10 #define I_DASH 11 #define E_RANGE 0 +#define E_OTA 1 uint8_t dot01 = 0b00000010; uint8_t digits01[] = { @@ -81,7 +82,7 @@ ESP32Timer ITimer0(0); char buf[1024]; volatile int8_t counter = 0; volatile bool space_open = false; -volatile bool edited = false, just_edited = false, request_update = false; +volatile bool open_edited = false, counter_edited = false, just_edited = false, request_update = false; volatile uint32_t last_edit = 0; void isr_open() { @@ -91,7 +92,7 @@ void isr_open() { } last = millis(); space_open = true; - edited = true; + open_edited = true; just_edited = true; last_edit = millis(); } @@ -107,7 +108,8 @@ void isr_close() { last = millis(); space_open = false; counter = 0; - edited = true; + open_edited = true; + counter_edited = true; just_edited = true; last_edit = millis(); } @@ -120,7 +122,7 @@ void isr_plus() { last = millis(); if (counter < 99) { counter++; - edited = true; + counter_edited = true; just_edited = true; last_edit = millis(); } @@ -134,7 +136,7 @@ void isr_minus() { last = millis(); if (counter > 0) { counter--; - edited = true; + counter_edited = true; just_edited = true; last_edit = millis(); } @@ -149,13 +151,22 @@ void setBrightness(uint8_t value) { analogWrite(DISP_PWM, 255-value); } +void showError(uint8_t error, uint8_t bright = 255) { + setBrightness(0); + shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[E_RANGE]); + shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_E] | dot10); + digitalWrite(DISP_CLK, HIGH); + digitalWrite(DISP_CLK, LOW); + setBrightness(bright); +} void showNumber(int8_t number, uint8_t bright = 255, bool show_dot01 = false, bool show_dot10 = false) { - setBrightness(0); if (number < -9 || number > 99) { - shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[E_RANGE]); - shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_E] | dot10); - } else if (number < 0) { + showError(E_RANGE); + return; + } + setBrightness(0); + if (number < 0) { shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[-number] | dot01 * show_dot01); shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_DASH] | dot10 * show_dot10); } else if (number < 10) { @@ -258,9 +269,15 @@ void setup() { Serial.println("\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + uint8_t percentage = progress / (total / 100); + Serial.printf("Progress: %u%%\r", percentage); + if (percentage > 99) { + percentage = 99; + } + showNumber(percentage, BRIGHTNESS_HIGH, true, true); }) .onError([](ota_error_t error) { + showError(E_OTA, BRIGHTNESS_HIGH); Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("Auth Failed"); @@ -273,11 +290,13 @@ void setup() { } else if (error == OTA_END_ERROR) { Serial.println("End Failed"); } - }); + }) + .setHostname("spaceapibox"); ArduinoOTA.begin(); ITimer0.attachInterruptInterval(60 * 1000 * 1000, isr_timer0); - edited = false; + open_edited = false; + counter_edited = false; request_update = true; draw(BRIGHTNESS_LOW); } @@ -287,10 +306,17 @@ void loop() { just_edited = false; draw(BRIGHTNESS_HIGH, true); } - if (edited && millis() - last_edit > 1000) { - edited = false; + if ((open_edited || counter_edited) && millis() - last_edit > 1000) { Serial.println("Edited!"); - snprintf(buf, 1024, "open value=%d\npeople_now_present value=%d\n", space_open ? 1 : 0, counter); + if (open_edited && counter_edited) { + snprintf(buf, 1024, "open value=%d\npeople_now_present value=%d\n", space_open ? 1 : 0, counter); + } else if (open_edited) { + snprintf(buf, 1024, "open value=%d\n", space_open ? 1 : 0); + } else if (counter_edited) { + snprintf(buf, 1024, "people_now_present value=%d\n", counter); + } + open_edited = false; + counter_edited = false; Serial.print("Sending update: "); Serial.println(buf);