1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-29 08:32:26 +01:00

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

View File

@@ -12,6 +12,14 @@ namespace pn532_i2c {
static const char *const TAG = "pn532_i2c";
bool PN532I2C::is_read_ready() {
uint8_t ready;
if (!this->read_bytes_raw(&ready, 1)) {
return false;
}
return ready == 0x01;
}
bool PN532I2C::write_data(const std::vector<uint8_t> &data) {
return this->write(data.data(), data.size()) == i2c::ERROR_OK;
}
@@ -19,19 +27,8 @@ bool PN532I2C::write_data(const std::vector<uint8_t> &data) {
bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
delay(1);
std::vector<uint8_t> ready;
ready.resize(1);
uint32_t start_time = millis();
while (true) {
if (this->read_bytes_raw(ready.data(), 1)) {
if (ready[0] == 0x01)
break;
}
if (millis() - start_time > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
return false;
}
if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
return false;
}
data.resize(len + 1);

View File

@@ -14,6 +14,7 @@ class PN532I2C : public pn532::PN532, public i2c::I2CDevice {
void dump_config() override;
protected:
bool is_read_ready() override;
bool write_data(const std::vector<uint8_t> &data) override;
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
bool read_response(uint8_t command, std::vector<uint8_t> &data) override;