Improves NFC writing workaround and removes debug output

Improved version of the NFC writing workaround. The task is no longer suspended. There is now a suspend request and a suspend state variable that is used to communicate between the writing and the reading task. The reading is stopped gracefully to prevent resets during writing.
This commit is contained in:
Jan Philipp Ecker
2025-08-08 15:33:08 +02:00
parent b95497aec2
commit 89a5728cc0

View File

@@ -20,7 +20,8 @@ String lastSpoolId = "";
String nfcJsonData = ""; String nfcJsonData = "";
bool tagProcessed = false; bool tagProcessed = false;
volatile bool pauseBambuMqttTask = false; volatile bool pauseBambuMqttTask = false;
volatile bool suspendNfcReading = false; volatile bool nfcReadingTaskSuspendRequest = false;
volatile bool nfcReadingTaskSuspendState = false;
struct NfcWriteParameterType { struct NfcWriteParameterType {
bool tagType; bool tagType;
@@ -279,21 +280,21 @@ void writeJsonToTag(void *parameter) {
Serial.println(params->payload); Serial.println(params->payload);
nfcReaderState = NFC_WRITING; nfcReaderState = NFC_WRITING;
suspendNfcReading = true;
//vTaskSuspend(RfidReaderTask); // First request the reading task to be suspended and than wait until it responds
// make sure to wait 600ms, after that the reading task should be waiting nfcReadingTaskSuspendRequest = true;
vTaskDelay(1000 / portTICK_PERIOD_MS); while(nfcReadingTaskSuspendState == false){
vTaskDelay(100 / portTICK_PERIOD_MS);
}
//pauseBambuMqttTask = true; //pauseBambuMqttTask = true;
// aktualisieren der Website wenn sich der Status ändert // aktualisieren der Website wenn sich der Status ändert
sendNfcData(); sendNfcData();
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);
Serial.println("CP 1");
// Wait 10sec for tag // Wait 10sec for tag
uint8_t success = 0; uint8_t success = 0;
String uidString = ""; String uidString = "";
for (uint16_t i = 0; i < 20; i++) { 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 uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; uint8_t uidLength;
// yield before potentially waiting for 400ms // yield before potentially waiting for 400ms
@@ -301,7 +302,6 @@ void writeJsonToTag(void *parameter) {
esp_task_wdt_reset(); esp_task_wdt_reset();
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 400); success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 400);
if (success) { if (success) {
Serial.println("CP 3.1");
for (uint8_t i = 0; i < uidLength; i++) { for (uint8_t i = 0; i < uidLength; i++) {
//TBD: Rework to remove all the string operations //TBD: Rework to remove all the string operations
uidString += String(uid[i], HEX); uidString += String(uid[i], HEX);
@@ -311,8 +311,6 @@ void writeJsonToTag(void *parameter) {
} }
foundNfcTag(nullptr, success); foundNfcTag(nullptr, success);
break; break;
}else{
Serial.println("CP 3.2");
} }
yield(); yield();
@@ -375,8 +373,7 @@ void writeJsonToTag(void *parameter) {
sendWriteResult(nullptr, success); sendWriteResult(nullptr, success);
sendNfcData(); sendNfcData();
suspendNfcReading = false; nfcReadingTaskSuspendRequest = false;
//vTaskResume(RfidReaderTask);
pauseBambuMqttTask = false; pauseBambuMqttTask = false;
vTaskDelete(NULL); vTaskDelete(NULL);
@@ -409,8 +406,9 @@ void scanRfidTask(void * parameter) {
Serial.println("RFID Task gestartet"); Serial.println("RFID Task gestartet");
for(;;) { for(;;) {
// Wenn geschrieben wird Schleife aussetzen // Wenn geschrieben wird Schleife aussetzen
if (nfcReaderState != NFC_WRITING && !suspendNfcReading) if (nfcReaderState != NFC_WRITING && !nfcReadingTaskSuspendRequest)
{ {
nfcReadingTaskSuspendState = false;
yield(); yield();
uint8_t success; uint8_t success;
@@ -491,7 +489,7 @@ void scanRfidTask(void * parameter) {
} }
} }
if (!success && nfcReaderState != NFC_IDLE && !suspendNfcReading) if (!success && nfcReaderState != NFC_IDLE && !nfcReadingTaskSuspendRequest)
{ {
nfcReaderState = NFC_IDLE; nfcReaderState = NFC_IDLE;
//uidString = ""; //uidString = "";
@@ -506,7 +504,7 @@ void scanRfidTask(void * parameter) {
} }
else else
{ {
// TBD: Debug only: nfcReadingTaskSuspendState = true;
Serial.println("NFC Reading disabled"); Serial.println("NFC Reading disabled");
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }