From b6d82c8afe4a3a5cd02e3315b4b02b2647428ca8 Mon Sep 17 00:00:00 2001 From: Jan Philipp Ecker Date: Tue, 5 Aug 2025 17:44:59 +0200 Subject: [PATCH] Graphic rework of the NFC writing process Introduces the new graphics for the NFC writing process. Also fixes some minor display bugs. Hides the service status icons during boot time. Fixes bugs in NFC write process where mutliple parallel API calls a created. Fixes a bug where spoolman is updated if a location tag is written (which is not required or correct). --- html/rfid.js | 2 ++ src/api.cpp | 25 +++++++++-------- src/display.cpp | 51 +++++++++++++++++++--------------- src/main.cpp | 2 ++ src/main.h | 9 ++++++ src/nfc.cpp | 73 +++++++++++++++++++++++++++++++++---------------- src/nfc.h | 2 +- src/website.cpp | 3 +- src/wlan.cpp | 2 +- 9 files changed, 109 insertions(+), 60 deletions(-) create mode 100644 src/main.h diff --git a/html/rfid.js b/html/rfid.js index d3a54a5..52866ea 100644 --- a/html/rfid.js +++ b/html/rfid.js @@ -660,6 +660,7 @@ function writeNfcTag() { writeButton.textContent = "Writing"; socket.send(JSON.stringify({ type: 'writeNfcTag', + tagType: 'spool', payload: nfcData })); } else { @@ -684,6 +685,7 @@ function writeLocationNfcTag() { writeButton.textContent = "Writing"; socket.send(JSON.stringify({ type: 'writeNfcTag', + tagType: 'location', payload: nfcData })); } else { diff --git a/src/api.cpp b/src/api.cpp index 54c17bf..9bba672 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -135,7 +135,7 @@ void sendToApi(void *parameter) { //oledShowMessage("Remaining: " + String(remaining_weight) + "g"); if(!octoEnabled){ // TBD: Do not use Strings... - oledShowProgressBar(octoEnabled?5:4, octoEnabled?5:4, "Spool Tag", ("Done: " + String(remainingWeight) + " g remain").c_str()); + oledShowProgressBar(1, 1, "Spool Tag", ("Done: " + String(remainingWeight) + " g remain").c_str()); remainingWeight = 0; }else{ // ocoto is enabled, trigger octo update @@ -143,7 +143,10 @@ void sendToApi(void *parameter) { } break; case API_REQUEST_SPOOL_LOCATION_UPDATE: - oledShowProgressBar(octoEnabled?5:4, octoEnabled?5:4, "Loc. Tag", "Done!"); + oledShowProgressBar(1, 1, "Loc. Tag", "Done!"); + break; + case API_REQUEST_SPOOL_TAG_ID_UPDATE: + oledShowProgressBar(1, 1, "Write Tag", "Done!"); break; case API_REQUEST_OCTO_SPOOL_UPDATE: // TBD: Do not use Strings... @@ -159,22 +162,17 @@ void sendToApi(void *parameter) { } else { switch(requestType){ case API_REQUEST_SPOOL_WEIGHT_UPDATE: - oledShowProgressBar(octoEnabled?5:4, octoEnabled?5:4, "Failure!", "Spoolman update"); - break; case API_REQUEST_SPOOL_LOCATION_UPDATE: - oledShowProgressBar(octoEnabled?5:4, octoEnabled?5:4, "Failure!", "Spoolman update"); + case API_REQUEST_SPOOL_TAG_ID_UPDATE: + oledShowProgressBar(1, 1, "Failure!", "Spoolman update"); break; case API_REQUEST_OCTO_SPOOL_UPDATE: - oledShowProgressBar(octoEnabled?5:4, octoEnabled?5:4, "Failure!", "Octoprint update"); + oledShowProgressBar(1, 1, "Failure!", "Octoprint update"); break; case API_REQUEST_BAMBU_UPDATE: // TBD: rework error oledShowMessage("Spoolman update failed"); break; - case API_REQUEST_SPOOL_TAG_ID_UPDATE: - // TBD: rework error - oledShowMessage("Spoolman update failed"); - break; } Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode)); @@ -193,6 +191,8 @@ void sendToApi(void *parameter) { } bool updateSpoolTagId(String uidString, const char* payload) { + oledShowProgressBar(2, 3, "Write Tag", "Update Spoolman"); + JsonDocument doc; DeserializationError error = deserializeJson(doc, payload); @@ -223,7 +223,7 @@ bool updateSpoolTagId(String uidString, const char* payload) { Serial.print("Update Payload: "); Serial.println(updatePayload); - SendToApiParams* params = new SendToApiParams(); + SendToApiParams* params = new SendToApiParams(); if (params == nullptr) { Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren."); return false; @@ -246,7 +246,8 @@ bool updateSpoolTagId(String uidString, const char* payload) { updateDoc.clear(); // Update Spool weight - if (weight > 10) updateSpoolWeight(doc["sm_id"].as(), weight); + //TBD: how to handle this with spool and locatin tags? Also potential parallel access again + //if (weight > 10) updateSpoolWeight(doc["sm_id"].as(), weight); return true; } diff --git a/src/display.cpp b/src/display.cpp index 2fd91ef..0ccaa5f 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -2,6 +2,7 @@ #include "api.h" #include #include "icons.h" +#include "main.h" Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); @@ -15,7 +16,10 @@ void setupDisplay() { } display.setTextColor(WHITE); display.clearDisplay(); - display.display(); + + + oledShowTopRow(); + oledShowProgressBar(0, 7, DISPLAY_BOOT_TEXT, "Display init"); } void oledclearline() { @@ -171,35 +175,38 @@ void oledShowTopRow() { iconToggle = !iconToggle; - if(bambuDisabled == false) { - if (bambu_connected == 1) { - display.drawBitmap(50, 0, bitmap_bambu_on , 16, 16, WHITE); - } else { - if(iconToggle){ + // Do not show status indicators during boot + if(!booting){ + if(bambuDisabled == false) { + if (bambu_connected == 1) { display.drawBitmap(50, 0, bitmap_bambu_on , 16, 16, WHITE); - display.drawLine(50, 15, 66, 0, WHITE); - display.drawLine(51, 15, 67, 0, WHITE); + } else { + if(iconToggle){ + display.drawBitmap(50, 0, bitmap_bambu_on , 16, 16, WHITE); + display.drawLine(50, 15, 66, 0, WHITE); + display.drawLine(51, 15, 67, 0, WHITE); + } } } - } - if (spoolmanConnected) { - display.drawBitmap(80, 0, bitmap_spoolman_on , 16, 16, WHITE); - } else { - if(iconToggle){ + if (spoolmanConnected) { display.drawBitmap(80, 0, bitmap_spoolman_on , 16, 16, WHITE); - display.drawLine(80, 15, 96, 0, WHITE); - display.drawLine(81, 15, 97, 0, WHITE); + } else { + if(iconToggle){ + display.drawBitmap(80, 0, bitmap_spoolman_on , 16, 16, WHITE); + display.drawLine(80, 15, 96, 0, WHITE); + display.drawLine(81, 15, 97, 0, WHITE); + } } - } - if (wifiOn == 1) { - display.drawBitmap(107, 0, wifi_on , 16, 16, WHITE); - } else { - if(iconToggle){ + if (wifiOn == 1) { display.drawBitmap(107, 0, wifi_on , 16, 16, WHITE); - display.drawLine(107, 15, 123, 0, WHITE); - display.drawLine(108, 15, 124, 0, WHITE); + } else { + if(iconToggle){ + display.drawBitmap(107, 0, wifi_on , 16, 16, WHITE); + display.drawLine(107, 15, 123, 0, WHITE); + display.drawLine(108, 15, 124, 0, WHITE); + } } } diff --git a/src/main.cpp b/src/main.cpp index 422551a..f160e95 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ bool mainTaskWasPaused = 0; uint8_t scaleTareCounter = 0; bool touchSensorConnected = false; +bool booting = true; // ##### SETUP ##### void setup() { @@ -63,6 +64,7 @@ void setup() { bool panic = true; // Wenn true, löst ein WDT-Timeout einen System-Panik aus esp_task_wdt_init(10, panic); + booting = false; // Aktuellen Task (loopTask) zum Watchdog hinzufügen esp_task_wdt_add(NULL); } diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..27c479b --- /dev/null +++ b/src/main.h @@ -0,0 +1,9 @@ +#ifndef MAIN_H +#define MAIN_H + +#include + + +extern bool booting; + +#endif \ No newline at end of file diff --git a/src/nfc.cpp b/src/nfc.cpp index 35a56a1..1638328 100644 --- a/src/nfc.cpp +++ b/src/nfc.cpp @@ -21,6 +21,11 @@ String nfcJsonData = ""; bool tagProcessed = false; volatile bool pauseBambuMqttTask = false; +struct NfcWriteParameterType { + bool tagType; + char* payload; +}; + volatile nfcReaderStateType nfcReaderState = NFC_IDLE; // 0 = nicht gelesen // 1 = erfolgreich gelesen @@ -268,11 +273,11 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) { } void writeJsonToTag(void *parameter) { - const char* payload = (const char*)parameter; + NfcWriteParameterType* params = (NfcWriteParameterType*)parameter; // Gib die erstellte NDEF-Message aus Serial.println("Erstelle NDEF-Message..."); - Serial.println(payload); + Serial.println(params->payload); nfcReaderState = NFC_WRITING; vTaskSuspend(RfidReaderTask); @@ -282,17 +287,22 @@ void writeJsonToTag(void *parameter) { // aktualisieren der Website wenn sich der Status ändert sendNfcData(); vTaskDelay(100 / portTICK_PERIOD_MS); - oledShowMessage("Waiting for NFC-Tag"); - + Serial.println("CP 1"); // Wait 10sec for tag uint8_t success = 0; String uidString = ""; for (uint16_t i = 0; i < 20; i++) { + Serial.println("CP 2"); uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; - success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500); + // yield before potentially waiting for 400ms + yield(); + esp_task_wdt_reset(); + success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 400); if (success) { + Serial.println("CP 3.1"); for (uint8_t i = 0; i < uidLength; i++) { + //TBD: Rework to remove all the string operations uidString += String(uid[i], HEX); if (i < uidLength - 1) { uidString += ":"; // Optional: Trennzeichen hinzufügen @@ -300,9 +310,12 @@ void writeJsonToTag(void *parameter) { } foundNfcTag(nullptr, success); break; + }else{ + Serial.println("CP 3.2"); } - if (i == 0) oledShowMessage("Waiting for NFC-Tag"); + //TBD: Is this required? + //if (i == 0) oledShowMessage("Waiting for NFC-Tag"); yield(); esp_task_wdt_reset(); @@ -311,29 +324,37 @@ void writeJsonToTag(void *parameter) { if (success) { - oledShowIcon("transfer"); + oledShowProgressBar(1, 3, "Write Tag", "Writing"); + // Schreibe die NDEF-Message auf den Tag - success = ntag2xx_WriteNDEF(payload); + success = ntag2xx_WriteNDEF(params->payload); if (success) { Serial.println("NDEF-Message erfolgreich auf den Tag geschrieben"); //oledShowMessage("NFC-Tag written"); - oledShowIcon("success"); - vTaskDelay(1000 / portTICK_PERIOD_MS); + //vTaskDelay(1000 / portTICK_PERIOD_MS); nfcReaderState = NFC_WRITE_SUCCESS; // aktualisieren der Website wenn sich der Status ändert sendNfcData(); pauseBambuMqttTask = false; - if (updateSpoolTagId(uidString, payload)) { - uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - uint8_t uidLength; - oledShowIcon("success"); - while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500)) { - yield(); + if(params->tagType){ + // TBD: should this be simplified? + if (updateSpoolTagId(uidString, params->payload) && params->tagType) { + + }else{ + // Potentially handle errors } + }else{ + oledShowProgressBar(1, 1, "Write Tag", "Done!"); } - + uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID + uint8_t uidLength; + yield(); + esp_task_wdt_reset(); + while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 400)) { + yield(); + } vTaskResume(RfidReaderTask); vTaskDelay(500 / portTICK_PERIOD_MS); } @@ -348,7 +369,7 @@ void writeJsonToTag(void *parameter) { else { Serial.println("Fehler: Kein Tag zu schreiben gefunden."); - oledShowMessage("No NFC-Tag found"); + oledShowProgressBar(1, 1, "Failure!", "No tag found"); vTaskDelay(2000 / portTICK_PERIOD_MS); nfcReaderState = NFC_IDLE; } @@ -362,20 +383,26 @@ void writeJsonToTag(void *parameter) { vTaskDelete(NULL); } -void startWriteJsonToTag(const char* payload) { - char* payloadCopy = strdup(payload); +void startWriteJsonToTag(const bool isSpoolTag, const char* payload) { + NfcWriteParameterType* parameters = new NfcWriteParameterType(); + parameters->tagType = isSpoolTag; + parameters->payload = strdup(payload); // Task nicht mehrfach starten - if (nfcReaderState != NFC_WRITING) { + if (nfcReaderState == NFC_IDLE) { + oledShowProgressBar(0, 1, "Write Tag", "Place tag now"); // Erstelle die Task xTaskCreate( writeJsonToTag, // Task-Funktion "WriteJsonToTagTask", // Task-Name 5115, // Stackgröße in Bytes - (void*)payloadCopy, // Parameter + (void*)parameters, // Parameter rfidWriteTaskPrio, // Priorität NULL // Task-Handle (nicht benötigt) ); + }else{ + oledShowProgressBar(0, 1, "FAILURE", "NFC busy!"); + // TBD: Add proper error handling (website) } } @@ -396,7 +423,7 @@ void scanRfidTask(void * parameter) { foundNfcTag(nullptr, success); // As long as there is still a tag on the reader, do not try to read it again - if (success && nfcReaderState != NFC_READ_SUCCESS && nfcReaderState != NFC_READ_ERROR) + if (success && nfcReaderState == NFC_IDLE) { // Set the current tag as not processed tagProcessed = false; diff --git a/src/nfc.h b/src/nfc.h index cd36d83..2c3d5f3 100644 --- a/src/nfc.h +++ b/src/nfc.h @@ -15,7 +15,7 @@ typedef enum{ void startNfc(); void scanRfidTask(void * parameter); -void startWriteJsonToTag(const char* payload); +void startWriteJsonToTag(const bool isSpoolTag, const char* payload); extern TaskHandle_t RfidReaderTask; extern String nfcJsonData; diff --git a/src/website.cpp b/src/website.cpp index 4ae0966..7c40fc7 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -67,7 +67,8 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp // Versuche NFC-Daten zu schreiben String payloadString; serializeJson(doc["payload"], payloadString); - startWriteJsonToTag(payloadString.c_str()); + + startWriteJsonToTag((doc["tagType"] == "spool") ? true : false, payloadString.c_str()); } } diff --git a/src/wlan.cpp b/src/wlan.cpp index 8756d7b..92081bc 100644 --- a/src/wlan.cpp +++ b/src/wlan.cpp @@ -61,7 +61,7 @@ void initWiFi() { wm.setWiFiAutoReconnect(true); wm.setConnectTimeout(10); - oledShowProgressBar(0, 7, DISPLAY_BOOT_TEXT, "WiFi Setup"); + oledShowProgressBar(1, 7, DISPLAY_BOOT_TEXT, "WiFi init"); //bool res = wm.autoConnect("FilaMan"); // anonymous ap if(!wm.autoConnect("FilaMan")) {