diff --git a/src/RfidPn5180.cpp b/src/RfidPn5180.cpp index bb9a96b..ac5df33 100644 --- a/src/RfidPn5180.cpp +++ b/src/RfidPn5180.cpp @@ -8,6 +8,7 @@ #include "Queues.h" #include "System.h" #include "Port.h" +#include "AudioPlayer.h" #ifdef RFID_READER_TYPE_PN5180 #include @@ -52,6 +53,12 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; static PN5180ISO15693 nfc15693(RFID_CS, RFID_BUSY, RFID_RST); static uint32_t lastTimeDetected14443 = 0; static uint32_t lastTimeDetected15693 = 0; + #ifdef PAUSE_WHEN_RFID_REMOVED + static byte lastValidcardId[cardIdSize]; + static bool cardAppliedCurrentRun = false; + static bool cardAppliedLastRun = false; + bool sameCardReapplied = false; + #endif static uint8_t stateMachine = RFID_PN5180_STATE_INIT; static byte cardId[cardIdSize], lastCardId[cardIdSize]; uint8_t uid[10]; @@ -83,13 +90,18 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; cardReceived = true; stateMachine = RFID_PN5180_NFC14443_STATE_ACTIVE; lastTimeDetected14443 = millis(); - //Serial.println("14443"); + #ifdef PAUSE_WHEN_RFID_REMOVED + cardAppliedCurrentRun = true; + #endif } else { // Reset to dummy-value if no card is there // Necessary to differentiate between "card is still applied" and "card is re-applied again after removal" // lastTimeDetected14443 is used to prevent "new card detection with old card" with single events where no card was detected if (!lastTimeDetected14443 || (millis() - lastTimeDetected14443 >= 400)) { lastTimeDetected14443 = 0; + #ifdef PAUSE_WHEN_RFID_REMOVED + cardAppliedCurrentRun = false; + #endif for (uint8_t i=0; i= 400)) { lastTimeDetected15693 = 0; + #ifdef PAUSE_WHEN_RFID_REMOVED + cardAppliedCurrentRun = false; + #endif for (uint8_t i=0; i pause + AudioPlayer_TrackControlToQueueSender(PAUSEPLAY); + } + cardAppliedLastRun = cardAppliedCurrentRun; + #endif + // send card to queue if (cardReceived) { memcpy(cardId, uid, cardIdSize); @@ -151,6 +175,12 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; memcpy(lastCardId, cardId, cardIdSize); + #ifdef PAUSE_WHEN_RFID_REMOVED + if (memcmp((const void *)lastValidcardId, (const void *)cardId, sizeof(cardId)) == 0) { + sameCardReapplied = true; + } + #endif + Log_Print((char *) FPSTR(rfidTagDetected), LOGLEVEL_NOTICE); snprintf(Log_Buffer, Log_BufferLength, "(%s) ID: ", (RFID_PN5180_NFC14443_STATE_ACTIVE == stateMachine) ? "ISO-14443" : "ISO-15693"); Log_Print(Log_Buffer, LOGLEVEL_NOTICE); @@ -166,7 +196,19 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; cardIdString += num; } - xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); + #ifdef PAUSE_WHEN_RFID_REMOVED + if (!sameCardReapplied) { // Don't allow to send card to queue if it's the same card again... + xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); + } else { + // If pause-button was pressed while card was not applied, playback could be active. If so: don't pause when card is reapplied again as the desired functionality would be reversed in this case. + if (gPlayProperties.pausePlay) { + AudioPlayer_TrackControlToQueueSender(PAUSEPLAY); // ... play/pause instead + } + } + memcpy(lastValidcardId, uid, cardIdSize); + #else + xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); // If PAUSE_WHEN_RFID_REMOVED isn't active, every card-apply leads to new playlist-generation + #endif } if (stateMachine == RFID_PN5180_NFC14443_STATE_ACTIVE) { // If 14443 is active, bypass 15693 as next check (performance) diff --git a/src/main.cpp b/src/main.cpp index f4c225f..d2f4d8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -184,7 +184,7 @@ void setup() { Serial.println(F(" | |___ ___) | | __/ | |_| | | | | | | | | (_) |")); Serial.println(F(" |_____| |____/ |_| \\__,_| |_| |_| |_| \\___/ ")); Serial.println(F(" Rfid-controlled musicplayer\n")); - Serial.println(F(" Rev 20210713-3\n")); + Serial.println(F(" Rev 20210714-1\n")); // print wake-up reason printWakeUpReason(); diff --git a/src/settings.h b/src/settings.h index e033924..a5c9682 100644 --- a/src/settings.h +++ b/src/settings.h @@ -42,6 +42,7 @@ #define BLUETOOTH_ENABLE // If enabled and bluetooth-mode is active, you can stream to your ESPuino via bluetooth (a2dp-sink). //#define IR_CONTROL_ENABLE // Enables remote control #define CACHED_PLAYLIST_ENABLE // Enables playlist-caching (infos: https://forum.espuino.de/t/neues-feature-cached-playlist/515) + #define PAUSE_WHEN_RFID_REMOVED // (Only PN5180) Playback starts when card is applied and pauses, when card is removed (https://forum.espuino.de/t/neues-feature-pausieren-wenn-rfid-karte-entfernt-wurde/541) //################## select SD card mode #############################