mirror of
https://github.com/esphome/esphome.git
synced 2025-09-19 03:32:20 +01:00
Add support for PN7150 (#5487)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
25
esphome/components/pn7150_i2c/__init__.py
Normal file
25
esphome/components/pn7150_i2c/__init__.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, pn7150
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
AUTO_LOAD = ["pn7150"]
|
||||
CODEOWNERS = ["@kbx81", "@jesserockz"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
pn7150_i2c_ns = cg.esphome_ns.namespace("pn7150_i2c")
|
||||
PN7150I2C = pn7150_i2c_ns.class_("PN7150I2C", pn7150.PN7150, i2c.I2CDevice)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
pn7150.PN7150_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(PN7150I2C),
|
||||
}
|
||||
).extend(i2c.i2c_device_schema(0x28))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await pn7150.setup_pn7150(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
49
esphome/components/pn7150_i2c/pn7150_i2c.cpp
Normal file
49
esphome/components/pn7150_i2c/pn7150_i2c.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "pn7150_i2c.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pn7150_i2c {
|
||||
|
||||
static const char *const TAG = "pn7150_i2c";
|
||||
|
||||
uint8_t PN7150I2C::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
|
||||
if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
|
||||
ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
|
||||
return nfc::STATUS_FAILED;
|
||||
}
|
||||
|
||||
rx.get_message().resize(nfc::NCI_PKT_HEADER_SIZE);
|
||||
if (!this->read_bytes_raw(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE)) {
|
||||
return nfc::STATUS_FAILED;
|
||||
}
|
||||
|
||||
uint8_t length = rx.get_payload_size();
|
||||
if (length > 0) {
|
||||
rx.get_message().resize(length + nfc::NCI_PKT_HEADER_SIZE);
|
||||
if (!this->read_bytes_raw(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length)) {
|
||||
return nfc::STATUS_FAILED;
|
||||
}
|
||||
}
|
||||
// semaphore to ensure transaction is complete before returning
|
||||
if (this->wait_for_irq_(pn7150::NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
|
||||
ESP_LOGW(TAG, "read_nfcc_() post-read timeout waiting for IRQ line to clear");
|
||||
return nfc::STATUS_FAILED;
|
||||
}
|
||||
return nfc::STATUS_OK;
|
||||
}
|
||||
|
||||
uint8_t PN7150I2C::write_nfcc(nfc::NciMessage &tx) {
|
||||
if (this->write(tx.encode().data(), tx.encode().size()) == i2c::ERROR_OK) {
|
||||
return nfc::STATUS_OK;
|
||||
}
|
||||
return nfc::STATUS_FAILED;
|
||||
}
|
||||
|
||||
void PN7150I2C::dump_config() {
|
||||
PN7150::dump_config();
|
||||
LOG_I2C_DEVICE(this);
|
||||
}
|
||||
|
||||
} // namespace pn7150_i2c
|
||||
} // namespace esphome
|
22
esphome/components/pn7150_i2c/pn7150_i2c.h
Normal file
22
esphome/components/pn7150_i2c/pn7150_i2c.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/pn7150/pn7150.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace pn7150_i2c {
|
||||
|
||||
class PN7150I2C : public pn7150::PN7150, public i2c::I2CDevice {
|
||||
public:
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override;
|
||||
uint8_t write_nfcc(nfc::NciMessage &tx) override;
|
||||
};
|
||||
|
||||
} // namespace pn7150_i2c
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user