diff --git a/ESP32_AP-Flasher/include/leds.h b/ESP32_AP-Flasher/include/leds.h index 5f6e8762..747b9b43 100644 --- a/ESP32_AP-Flasher/include/leds.h +++ b/ESP32_AP-Flasher/include/leds.h @@ -1,11 +1,14 @@ #include -#ifdef OPENEPAPERLINK_PCB +#ifdef HAS_RGB_LED #include #endif void ledTask(void* parameter); -#ifdef OPENEPAPERLINK_PCB +#ifdef HAS_RGB_LED void shortBlink(CRGB cname); +void showColorPattern(CRGB colorone, CRGB colortwo, CRGB colorthree); +void rgbIdle(); +void addFadeColor(CRGB cname); #endif \ No newline at end of file diff --git a/ESP32_AP-Flasher/src/flasher.cpp b/ESP32_AP-Flasher/src/flasher.cpp index 65da0b08..f525e6c1 100644 --- a/ESP32_AP-Flasher/src/flasher.cpp +++ b/ESP32_AP-Flasher/src/flasher.cpp @@ -6,6 +6,7 @@ #include // #include +#include "leds.h" #include "settings.h" #include "time.h" #include "zbs_interface.h" @@ -335,6 +336,9 @@ bool flasher::writeFlash(uint8_t *flashbuffer, uint16_t size) { return false; flashWriteSuccess: if (c % 256 == 0) { +#ifdef HAS_RGB_LED + shortBlink(CRGB::Yellow); +#endif Serial.printf("\rNow flashing, %d/%d ", c, size); vTaskDelay(1 / portTICK_PERIOD_MS); } @@ -422,6 +426,9 @@ bool flasher::writeFlashFromPackOffset(fs::File *file, uint16_t length) { file->read(buf, length); length = 0; } +#ifdef HAS_RGB_LED + shortBlink(CRGB::Yellow); +#endif Serial.printf("\rFlashing, %d bytes left ", length); bool res = writeBlock256(offset, buf); offset += 256; @@ -432,6 +439,9 @@ bool flasher::writeFlashFromPackOffset(fs::File *file, uint16_t length) { vTaskDelay(1 / portTICK_PERIOD_MS); } Serial.printf("\nFlashing done\n"); + #ifdef HAS_RGB_LED + addFadeColor(CRGB::Green); + #endif return true; } @@ -506,7 +516,7 @@ bool doForcedAPFlash() { } // we're going to overwrite the contents of the tag, so if we haven't set the mac already, we can forget about it. We'll set the mac to the wifi mac - if(!f->getInfoBlockMac()){ + if (!f->getInfoBlockMac()) { f->readInfoBlock(); f->getMacFromWiFi(); f->prepareInfoBlock(); @@ -516,7 +526,7 @@ bool doForcedAPFlash() { fs::File readfile = LittleFS.open("/AP_force_flash.bin", "r"); bool res = f->writeFlashFromPackOffset(&readfile, readfile.size()); readfile.close(); - if(res) LittleFS.remove("/AP_force_flash.bin"); + if (res) LittleFS.remove("/AP_force_flash.bin"); f->zbs->reset(); delete f; return res; @@ -579,11 +589,12 @@ bool doAPUpdate(uint8_t type) { f->writeInfoBlock(); } bool res = f->writeFlashFromPack("/AP_FW_Pack.bin", f->tagtype); - if(res)f->zbs->reset(); + if (res) f->zbs->reset(); delete f; return res; } +#ifdef OPENEPAPERLINK_PCB // perform device flash, save mac, everything bool doTagFlash() { class flasher *f = new flasher(); @@ -623,4 +634,5 @@ bool doTagFlash() { delete f; return false; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/ESP32_AP-Flasher/src/leds.cpp b/ESP32_AP-Flasher/src/leds.cpp index 7f5c164a..8eca0861 100644 --- a/ESP32_AP-Flasher/src/leds.cpp +++ b/ESP32_AP-Flasher/src/leds.cpp @@ -6,7 +6,6 @@ #include "settings.h" - QueueHandle_t ledQueue; #ifdef HAS_RGB_LED @@ -16,15 +15,18 @@ struct ledInstructionRGB { CRGB ledColor; uint16_t fadeTime; uint16_t length; + bool reQueue = false; }; CRGB leds[1]; +volatile bool rgbQueueFlush = false; #endif struct ledInstruction { uint16_t value; uint16_t fadeTime; uint16_t length; + bool reQueue = false; }; const uint8_t PROGMEM gamma8[] = { @@ -73,11 +75,10 @@ const uint16_t gamma12[256] = { 3502, 3540, 3578, 3616, 3654, 3693, 3732, 3771, 3810, 3850, 3890, 3930, 3971, 4013, 4054, 4095}; - - #ifdef HAS_RGB_LED -void addToRGBQueue(struct ledInstructionRGB* rgb) { +void addToRGBQueue(struct ledInstructionRGB* rgb, bool requeue) { + rgb->reQueue = requeue; BaseType_t queuestatus = xQueueSend(rgbLedQueue, &rgb, 0); if (queuestatus == pdFALSE) { delete rgb; @@ -89,27 +90,67 @@ void addFadeColor(CRGB cname) { rgb->ledColor = cname; rgb->fadeTime = 750; rgb->length = 0; - addToRGBQueue(rgb); + addToRGBQueue(rgb, false); } - void shortBlink(CRGB cname) { struct ledInstructionRGB* rgb = new struct ledInstructionRGB; rgb->ledColor = CRGB::Black; rgb->fadeTime = 0; rgb->length = 3; - addToRGBQueue(rgb); + addToRGBQueue(rgb, false); rgb = new struct ledInstructionRGB; rgb->ledColor = cname; rgb->ledColor.maximizeBrightness(0x80); rgb->fadeTime = 0; rgb->length = 10; - addToRGBQueue(rgb); + addToRGBQueue(rgb, false); rgb = new struct ledInstructionRGB; rgb->ledColor = CRGB::Black; rgb->fadeTime = 0; rgb->length = 3; - addToRGBQueue(rgb); + addToRGBQueue(rgb, false); +} + +void flushRGBQueue() { + rgbQueueFlush = true; +} + +void rgbIdle() { + flushRGBQueue(); +} + +void showColorPattern(CRGB colorone, CRGB colortwo, CRGB colorthree) { + struct ledInstructionRGB* rgb = new struct ledInstructionRGB; + rgb->ledColor = CRGB::Black; + rgb->fadeTime = 0; + rgb->length = 600; + addToRGBQueue(rgb, true); + rgb = new struct ledInstructionRGB; + rgb->ledColor = colorone; + rgb->fadeTime = 0; + rgb->length = 120; + addToRGBQueue(rgb, true); + rgb = new struct ledInstructionRGB; + rgb->ledColor = CRGB::Black; + rgb->fadeTime = 0; + rgb->length = 200; + addToRGBQueue(rgb, true); + rgb = new struct ledInstructionRGB; + rgb->ledColor = colortwo; + rgb->fadeTime = 0; + rgb->length = 120; + addToRGBQueue(rgb, true); + rgb = new struct ledInstructionRGB; + rgb->ledColor = CRGB::Black; + rgb->fadeTime = 0; + rgb->length = 200; + addToRGBQueue(rgb, true); + rgb = new struct ledInstructionRGB; + rgb->ledColor = colorthree; + rgb->fadeTime = 0; + rgb->length = 120; + addToRGBQueue(rgb, true); } void showRGB() { @@ -144,7 +185,6 @@ void rgbIdleStep() { } #endif - void addToMonoQueue(struct ledInstruction* mono) { BaseType_t queuestatus = xQueueSend(ledQueue, &mono, 0); if (queuestatus == pdFALSE) { @@ -160,8 +200,6 @@ void addFadeMono(uint8_t value) { addToMonoQueue(mono); } - - void showMono(uint8_t brightness) { ledcWrite(7, gamma12[brightness]); } @@ -169,7 +207,6 @@ volatile uint16_t monoIdlePeriod = 900; uint8_t monoValue = 0; - void monoIdleStep() { static bool dirUp = true; static uint16_t step = 0; @@ -201,7 +238,7 @@ void ledTask(void* parameter) { rgbLedQueue = xQueueCreate(30, sizeof(struct ledInstructionRGB*)); struct ledInstructionRGB* rgb = nullptr; - // open with a nice RGB crossfade + // open with a nice RGB crossfade addFadeColor(CRGB::Red); addFadeColor(CRGB::Green); addFadeColor(CRGB::Blue); @@ -228,7 +265,6 @@ void ledTask(void* parameter) { uint8_t oldBrightness = 0; - uint16_t monoInstructionFadeTime = 0; while (1) { @@ -238,12 +274,27 @@ void ledTask(void* parameter) { // fetch a led instruction BaseType_t q = xQueueReceive(rgbLedQueue, &rgb, 1); if (q == pdTRUE) { - rgbInstructionFadeTime = rgb->fadeTime; - if (rgb->fadeTime <= 1) { - leds[0] = rgb->ledColor; - showRGB(); + if (rgb->reQueue && !rgbQueueFlush) { + // requeue this instruction at the end of the queue, caveman style. + struct ledInstructionRGB* requeue = new ledInstructionRGB; + requeue->fadeTime = rgb->fadeTime; + requeue->ledColor = rgb->ledColor; + requeue->length = rgb->length; + addToRGBQueue(requeue, true); + } + + if (rgbQueueFlush) { + delete rgb; + rgb=nullptr; + } else { + rgbInstructionFadeTime = rgb->fadeTime; + if (rgb->fadeTime <= 1) { + leds[0] = rgb->ledColor; + showRGB(); + } } } else { + rgbQueueFlush = false; // no commands, run idle led task rgbIdleStep(); } diff --git a/ESP32_AP-Flasher/src/main.cpp b/ESP32_AP-Flasher/src/main.cpp index 8c21590b..aff5dceb 100644 --- a/ESP32_AP-Flasher/src/main.cpp +++ b/ESP32_AP-Flasher/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include + #include "contentmanager.h" #include "flasher.h" #include "makeimage.h" @@ -38,6 +39,7 @@ void timeTask(void* parameter) { void setup() { #ifdef OPENEPAPERLINK_MINI_AP_PCB + // this allows us to view the booting process. After connecting to USB, you have 3 seconds to open a terminal on the COM port vTaskDelay(3000 / portTICK_PERIOD_MS); #endif Serial.begin(115200); @@ -70,27 +72,37 @@ void setup() { } #ifdef HAS_USB - xTaskCreate(usbFlasherTask, "flasher", 10000, NULL, configMAX_PRIORITIES - 10, NULL); + xTaskCreate(usbFlasherTask, "usbflasher", 10000, NULL, configMAX_PRIORITIES - 10, NULL); #endif + xTaskCreate(ledTask, "ledhandler", 5000, NULL, 2, NULL); + configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "0.nl.pool.ntp.org", "europe.pool.ntp.org", "time.nist.gov"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv +#ifdef HAS_RGB_LED + showColorPattern(CRGB::Aqua, CRGB::Green, CRGB::Blue); +#endif + initAPconfig(); + init_web(); init_udp(); +#ifdef HAS_RGB_LED + rgbIdle(); +#endif + loadDB("/current/tagDB.json"); xTaskCreate(APTask, "AP Process", 10000, NULL, 2, NULL); xTaskCreate(webSocketSendProcess, "ws", 5000, NULL, configMAX_PRIORITIES - 10, NULL); xTaskCreate(timeTask, "timed tasks", 10000, NULL, 2, NULL); - xTaskCreate(ledTask, "ledhandler", 5000, NULL, 3, NULL); } void loop() { vTaskDelay(10000 / portTICK_PERIOD_MS); - //performDeviceFlash(); + // performDeviceFlash(); while (1) { vTaskDelay(10000 / portTICK_PERIOD_MS); } diff --git a/ESP32_AP-Flasher/src/serialap.cpp b/ESP32_AP-Flasher/src/serialap.cpp index 03928df0..9b1d8950 100644 --- a/ESP32_AP-Flasher/src/serialap.cpp +++ b/ESP32_AP-Flasher/src/serialap.cpp @@ -6,6 +6,7 @@ #include "commstructs.h" #include "flasher.h" +#include "leds.h" #include "newproto.h" #include "powermgt.h" #include "settings.h" @@ -108,11 +109,11 @@ bool waitCmdReply() { return false; } - #if (AP_PROCESS_PORT == FLASHER_AP_PORT) #define AP_RESET_PIN FLASHER_AP_RESET #define AP_POWER_PIN FLASHER_AP_POWER #endif +#ifdef OPENEPAPERLINK_PCB #if (AP_PROCESS_PORT == FLASHER_EXT_PORT) #define AP_RESET_PIN FLASHER_EXT_RESET #define AP_POWER_PIN FLASHER_EXT_POWER @@ -121,6 +122,7 @@ bool waitCmdReply() { #define AP_RESET_PIN FLASHER_ALT_RESET #define AP_POWER_PIN FLASHER_ALT_POWER #endif +#endif // Reset the tag void APTagReset() { @@ -296,12 +298,21 @@ void rxCmdProcessor(void* parameter) { switch (rxcmd->type) { case RX_CMD_RQB: processBlockRequest((struct espBlockRequest*)rxcmd->data); +#ifdef HAS_RGB_LED + shortBlink(CRGB::Blue); +#endif break; case RX_CMD_ADR: processDataReq((struct espAvailDataReq*)rxcmd->data, true); +#ifdef HAS_RGB_LED + shortBlink(CRGB::Aqua); +#endif break; case RX_CMD_XFC: processXferComplete((struct espXferComplete*)rxcmd->data, true); +#ifdef HAS_RGB_LED + shortBlink(CRGB::Purple); +#endif break; case RX_CMD_XTO: processXferTimeout((struct espXferComplete*)rxcmd->data, true); @@ -551,17 +562,29 @@ void APTask(void* parameter) { #if (AP_PROCESS_PORT == FLASHER_AP_PORT) AP_SERIAL_PORT.begin(115200, SERIAL_8N1, FLASHER_AP_RXD, FLASHER_AP_TXD); #endif +#ifdef OPENEPAPERLINK_PCB #if (AP_PROCESS_PORT == FLASHER_EXT_PORT) AP_SERIAL_PORT.begin(115200, SERIAL_8N1, FLASHER_EXT_RXD, FLASHER_EXT_TXD); #endif #if (AP_PROCESS_PORT == FLASHER_ALTRADIO_PORT) AP_SERIAL_PORT.begin(115200, SERIAL_8N1, FLASHER_AP_RXD, FLASHER_AP_TXD); +#endif #endif vTaskDelay(3000 / portTICK_PERIOD_MS); - if (checkForcedAPFlash()){ + if (checkForcedAPFlash()) { doForcedAPFlash(); +#if (FLASHER_AP_POWER == -1) + // If we have no soft power control, we'll now hang. + Serial.printf("Please power-cycle your device\n"); +#ifdef HAS_RGB_LED + showColorPattern(CRGB::Aqua, CRGB::Aqua, CRGB::Red); +#endif + while (1) { + vTaskDelay(3000 / portTICK_PERIOD_MS); + } +#endif } if (bringAPOnline()) { @@ -576,6 +599,16 @@ void APTask(void* parameter) { apInfo.isOnline = false; apInfo.state = AP_STATE_FLASHING; if (doAPUpdate(apInfo.type)) { +#if (FLASHER_AP_POWER == -1) + // If we have no soft power control, we'll now hang. + Serial.printf("Please power-cycle your device\n"); +#ifdef HAS_RGB_LED + showColorPattern(CRGB::Aqua, CRGB::Aqua, CRGB::Red); +#endif + while (1) { + vTaskDelay(3000 / portTICK_PERIOD_MS); + } +#endif Serial.printf("Flash completed, let's try to boot the AP!\n"); if (bringAPOnline()) { // AP works @@ -600,6 +633,16 @@ void APTask(void* parameter) { Serial.println("Performing firmware flash in about 10 seconds\n"); vTaskDelay(10000 / portTICK_PERIOD_MS); if (doAPFlash()) { +#if (FLASHER_AP_POWER == -1) + // If we have no soft power control, we'll now hang. + Serial.printf("Please power-cycle your device\n"); +#ifdef HAS_RGB_LED + showColorPattern(CRGB::Aqua, CRGB::Aqua, CRGB::Red); +#endif + while (1) { + vTaskDelay(3000 / portTICK_PERIOD_MS); + } +#endif if (bringAPOnline()) { // AP works ShowAPInfo(); diff --git a/ESP32_AP-Flasher/src/web.cpp b/ESP32_AP-Flasher/src/web.cpp index 51d16795..8ee034e7 100644 --- a/ESP32_AP-Flasher/src/web.cpp +++ b/ESP32_AP-Flasher/src/web.cpp @@ -52,7 +52,7 @@ void webSocketSendProcess(void *parameter) { } void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { -#ifdef OPENEPAPERLINK_PCB +#ifdef HAS_RGB_LED shortBlink(CRGB::BlueViolet); #endif switch (type) {