1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-11 07:42:26 +01:00

Pn532 upgrades (#1302)

* Move pn532 -> pn532_spi
Add pn532_i2c

* Update i2c address

* Always wait for ready byte before reading

* Generalise the pn532 a bit more so less code in i2c and spi implementations

* clang

* Add pn532_i2c to test1

* Try to get setup working

* Fixes

* More updates

* Command consts

* A few upgrades

* Change text back to include 'new'

* Fix data reading
This commit is contained in:
Jesse Hills
2020-11-01 11:55:48 +13:00
committed by GitHub
parent 22e1758d5b
commit 0059a6de46
12 changed files with 415 additions and 261 deletions

View File

@@ -0,0 +1,21 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, pn532
from esphome.const import CONF_ID
AUTO_LOAD = ['pn532']
CODEOWNERS = ['@OttoWinter', '@jesserockz']
DEPENDENCIES = ['i2c']
pn532_i2c_ns = cg.esphome_ns.namespace('pn532_i2c')
PN532I2C = pn532_i2c_ns.class_('PN532I2C', pn532.PN532, i2c.I2CDevice)
CONFIG_SCHEMA = cv.All(pn532.PN532_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(PN532I2C),
}).extend(i2c.i2c_device_schema(0x24)))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield pn532.setup_pn532(var, config)
yield i2c.register_i2c_device(var, config)

View File

@@ -0,0 +1,45 @@
#include "pn532_i2c.h"
#include "esphome/core/log.h"
// Based on:
// - https://cdn-shop.adafruit.com/datasheets/PN532C106_Application+Note_v1.2.pdf
// - https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf
// - https://www.nxp.com/docs/en/nxp/application-notes/153710.pdf
namespace esphome {
namespace pn532_i2c {
static const char *TAG = "pn532_i2c";
bool PN532I2C::write_data(const std::vector<uint8_t> &data) { return this->write_bytes_raw(data.data(), data.size()); }
bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
delay(5);
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;
}
}
data.resize(len + 1);
this->read_bytes_raw(data.data(), len + 1);
return true;
}
void PN532I2C::dump_config() {
PN532::dump_config();
LOG_I2C_DEVICE(this);
}
} // namespace pn532_i2c
} // namespace esphome

View File

@@ -0,0 +1,20 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/pn532/pn532.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace pn532_i2c {
class PN532I2C : public pn532::PN532, public i2c::I2CDevice {
public:
void dump_config() override;
protected:
bool write_data(const std::vector<uint8_t> &data) override;
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
};
} // namespace pn532_i2c
} // namespace esphome