Pn532 non blocking scan (#5191)

This commit is contained in:
Alexander Dimitrov
2023-11-28 23:17:16 +02:00
committed by GitHub
parent 087733c2fd
commit 6424f831e2
6 changed files with 92 additions and 48 deletions
+57 -1
View File
@@ -127,8 +127,18 @@ void PN532::loop() {
if (!this->requested_read_)
return;
auto ready = this->read_ready_(false);
if (ready == WOULDBLOCK)
return;
bool success = false;
std::vector<uint8_t> read;
bool success = this->read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
if (ready == READY) {
success = this->read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
} else {
this->send_ack_(); // abort still running InListPassiveTarget
}
this->requested_read_ = false;
@@ -286,12 +296,58 @@ bool PN532::read_ack_() {
return matches;
}
void PN532::send_ack_() {
ESP_LOGV(TAG, "Sending ACK for abort");
this->write_data({0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00});
delay(10);
}
void PN532::send_nack_() {
ESP_LOGV(TAG, "Sending NACK for retransmit");
this->write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
delay(10);
}
enum PN532ReadReady PN532::read_ready_(bool block) {
if (this->rd_ready_ == READY) {
if (block) {
this->rd_start_time_ = 0;
this->rd_ready_ = WOULDBLOCK;
}
return READY;
}
if (!this->rd_start_time_) {
this->rd_start_time_ = millis();
}
while (true) {
if (this->is_read_ready()) {
this->rd_ready_ = READY;
break;
}
if (millis() - this->rd_start_time_ > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
this->rd_ready_ = TIMEOUT;
break;
}
if (!block) {
this->rd_ready_ = WOULDBLOCK;
break;
}
yield();
}
auto rdy = this->rd_ready_;
if (block || rdy == TIMEOUT) {
this->rd_start_time_ = 0;
this->rd_ready_ = WOULDBLOCK;
}
return rdy;
}
void PN532::turn_off_rf_() {
ESP_LOGV(TAG, "Turning RF field OFF");
this->write_command_({
+11
View File
@@ -20,6 +20,12 @@ static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
static const uint8_t PN532_COMMAND_POWERDOWN = 0x16;
enum PN532ReadReady {
WOULDBLOCK = 0,
TIMEOUT,
READY,
};
class PN532BinarySensor;
class PN532 : public PollingComponent {
@@ -54,8 +60,11 @@ class PN532 : public PollingComponent {
void turn_off_rf_();
bool write_command_(const std::vector<uint8_t> &data);
bool read_ack_();
void send_ack_();
void send_nack_();
enum PN532ReadReady read_ready_(bool block);
virtual bool is_read_ready() = 0;
virtual bool write_data(const std::vector<uint8_t> &data) = 0;
virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
virtual bool read_response(uint8_t command, std::vector<uint8_t> &data) = 0;
@@ -91,6 +100,8 @@ class PN532 : public PollingComponent {
std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
std::vector<uint8_t> current_uid_;
nfc::NdefMessage *next_task_message_to_write_;
uint32_t rd_start_time_{0};
enum PN532ReadReady rd_ready_ { WOULDBLOCK };
enum NfcTask {
READ = 0,
CLEAN,