@@ -207,11 +207,16 @@ void NfcManager::authPrecomputeTask() {
207207 * @param readerDataManager Reference to the ReaderDataManager used to read and persist reader data.
208208 * @param nfcGpioPins Four GPIO pin numbers used to construct the PN532 SPI interface.
209209 * @param hkAuthPrecomputeEnabled If true, enables HomeKit authentication precompute behavior.
210+ * @param nfcFastPollingEnabled If true, shortens the delay between polling iterations.
210211 */
211- NfcManager::NfcManager (ReaderDataManager& readerDataManager, const std::array<uint8_t , 4 > &nfcGpioPins, bool hkAuthPrecomputeEnabled)
212+ NfcManager::NfcManager (ReaderDataManager& readerDataManager,
213+ const std::array<uint8_t , 4 > &nfcGpioPins,
214+ bool hkAuthPrecomputeEnabled,
215+ bool nfcFastPollingEnabled)
212216 : nfcGpioPins(nfcGpioPins),
213217 m_readerDataManager(readerDataManager),
214218 m_hkAuthPrecomputeEnabled(hkAuthPrecomputeEnabled),
219+ m_nfcFastPollingEnabled(nfcFastPollingEnabled),
215220 m_pollingTaskHandle(nullptr ),
216221 m_retryTaskHandle(nullptr ),
217222 m_ecpData({ 0x6A , 0x2 , 0xCB , 0x2 , 0x6 , 0x2 , 0x11 , 0x0 })
@@ -259,6 +264,7 @@ bool NfcManager::begin() {
259264 } else {
260265 ESP_LOGI (TAG , " Auth precompute disabled." );
261266 }
267+ ESP_LOGI (TAG , " NFC fast polling: %s" , m_nfcFastPollingEnabled ? " enabled" : " disabled" );
262268 ESP_LOGI (TAG , " Starting NFC polling task..." );
263269 xTaskCreateUniversal (pollingTaskEntry, " nfc_poll_task" , 8192 , this , 2 , &m_pollingTaskHandle, 1 );
264270 return true ;
@@ -385,6 +391,17 @@ void NfcManager::pollingTask() {
385391 vTaskSuspend (NULL );
386392 }
387393
394+ const uint16_t inCommunicateThruTimeoutMs = 50 ;
395+ const uint16_t passiveTargetTimeoutMs = 500 ;
396+ const TickType_t pollDelayTicks =
397+ pdMS_TO_TICKS (m_nfcFastPollingEnabled ? 5 : 100 );
398+
399+ ESP_LOGI (TAG ,
400+ " NFC poll tuning active: delay=%lu ms, thruTimeout=%u ms, passiveTimeout=%u ms" ,
401+ static_cast <unsigned long >(pollDelayTicks * portTICK_PERIOD_MS),
402+ static_cast <unsigned int >(inCommunicateThruTimeoutMs),
403+ static_cast <unsigned int >(passiveTargetTimeoutMs));
404+
388405 while (true ) {
389406 if (m_nfc->WriteRegister ({0x63 ,0x3d ,0x0 }) != pn532::SUCCESS ) {
390407 ESP_LOGE (TAG , " PN532 is unresponsive. Attempting to reconnect..." );
@@ -397,12 +414,12 @@ void NfcManager::pollingTask() {
397414 }
398415
399416 std::vector<uint8_t > res;
400- m_nfc->InCommunicateThru (std::vector<uint8_t >(m_ecpData.begin (), m_ecpData.end ()), res, 50 );
417+ m_nfc->InCommunicateThru (std::vector<uint8_t >(m_ecpData.begin (), m_ecpData.end ()), res, inCommunicateThruTimeoutMs );
401418
402419 std::vector<uint8_t > uid;
403420 std::array<uint8_t ,2 > sens_res;
404421 uint8_t sel_res;
405- pn532::Status passiveTargetFound = m_nfc->InListPassiveTarget (PN532_MIFARE_ISO14443A , uid, sens_res, sel_res, 500 );
422+ pn532::Status passiveTargetFound = m_nfc->InListPassiveTarget (PN532_MIFARE_ISO14443A , uid, sens_res, sel_res, passiveTargetTimeoutMs );
406423
407424 if (passiveTargetFound == pn532::SUCCESS ) {
408425 ESP_LOGI (TAG , " NFC tag detected!" );
@@ -416,7 +433,7 @@ void NfcManager::pollingTask() {
416433 m_nfc->setPassiveActivationRetries (0 );
417434 }
418435
419- vTaskDelay (pdMS_TO_TICKS ( 100 ) );
436+ vTaskDelay (pollDelayTicks );
420437 taskYIELD ();
421438 }
422439}
0 commit comments