feat: improve ota support

This commit is contained in:
s3lph 2024-12-22 03:01:20 +01:00
parent 58fbda6f59
commit 43ff8671a9
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 42 additions and 16 deletions

View file

@ -19,4 +19,4 @@ lib_deps =
build_flags = build_flags =
-Wall -Werror -Wall -Werror
upload_protocol = espota upload_protocol = espota
upload_port = 10.20.0.119 upload_port = spaceapibox.local

View file

@ -25,6 +25,7 @@
#define I_E 10 #define I_E 10
#define I_DASH 11 #define I_DASH 11
#define E_RANGE 0 #define E_RANGE 0
#define E_OTA 1
uint8_t dot01 = 0b00000010; uint8_t dot01 = 0b00000010;
uint8_t digits01[] = { uint8_t digits01[] = {
@ -81,7 +82,7 @@ ESP32Timer ITimer0(0);
char buf[1024]; char buf[1024];
volatile int8_t counter = 0; volatile int8_t counter = 0;
volatile bool space_open = false; 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; volatile uint32_t last_edit = 0;
void isr_open() { void isr_open() {
@ -91,7 +92,7 @@ void isr_open() {
} }
last = millis(); last = millis();
space_open = true; space_open = true;
edited = true; open_edited = true;
just_edited = true; just_edited = true;
last_edit = millis(); last_edit = millis();
} }
@ -107,7 +108,8 @@ void isr_close() {
last = millis(); last = millis();
space_open = false; space_open = false;
counter = 0; counter = 0;
edited = true; open_edited = true;
counter_edited = true;
just_edited = true; just_edited = true;
last_edit = millis(); last_edit = millis();
} }
@ -120,7 +122,7 @@ void isr_plus() {
last = millis(); last = millis();
if (counter < 99) { if (counter < 99) {
counter++; counter++;
edited = true; counter_edited = true;
just_edited = true; just_edited = true;
last_edit = millis(); last_edit = millis();
} }
@ -134,7 +136,7 @@ void isr_minus() {
last = millis(); last = millis();
if (counter > 0) { if (counter > 0) {
counter--; counter--;
edited = true; counter_edited = true;
just_edited = true; just_edited = true;
last_edit = millis(); last_edit = millis();
} }
@ -149,13 +151,22 @@ void setBrightness(uint8_t value) {
analogWrite(DISP_PWM, 255-value); analogWrite(DISP_PWM, 255-value);
} }
void showError(uint8_t error, uint8_t bright = 255) {
void showNumber(int8_t number, uint8_t bright = 255, bool show_dot01 = false, bool show_dot10 = false) {
setBrightness(0); setBrightness(0);
if (number < -9 || number > 99) {
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[E_RANGE]); shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits01[E_RANGE]);
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_E] | dot10); shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_E] | dot10);
} else if (number < 0) { 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) {
if (number < -9 || number > 99) {
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, digits01[-number] | dot01 * show_dot01);
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_DASH] | dot10 * show_dot10); shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[I_DASH] | dot10 * show_dot10);
} else if (number < 10) { } else if (number < 10) {
@ -258,9 +269,15 @@ void setup() {
Serial.println("\nEnd"); Serial.println("\nEnd");
}) })
.onProgress([](unsigned int progress, unsigned int total) { .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) { .onError([](ota_error_t error) {
showError(E_OTA, BRIGHTNESS_HIGH);
Serial.printf("Error[%u]: ", error); Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) { if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed"); Serial.println("Auth Failed");
@ -273,11 +290,13 @@ void setup() {
} else if (error == OTA_END_ERROR) { } else if (error == OTA_END_ERROR) {
Serial.println("End Failed"); Serial.println("End Failed");
} }
}); })
.setHostname("spaceapibox");
ArduinoOTA.begin(); ArduinoOTA.begin();
ITimer0.attachInterruptInterval(60 * 1000 * 1000, isr_timer0); ITimer0.attachInterruptInterval(60 * 1000 * 1000, isr_timer0);
edited = false; open_edited = false;
counter_edited = false;
request_update = true; request_update = true;
draw(BRIGHTNESS_LOW); draw(BRIGHTNESS_LOW);
} }
@ -287,10 +306,17 @@ void loop() {
just_edited = false; just_edited = false;
draw(BRIGHTNESS_HIGH, true); draw(BRIGHTNESS_HIGH, true);
} }
if (edited && millis() - last_edit > 1000) { if ((open_edited || counter_edited) && millis() - last_edit > 1000) {
edited = false;
Serial.println("Edited!"); Serial.println("Edited!");
if (open_edited && counter_edited) {
snprintf(buf, 1024, "open value=%d\npeople_now_present value=%d\n", space_open ? 1 : 0, counter); 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.print("Sending update: ");
Serial.println(buf); Serial.println(buf);