2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/core/automation.h"
|
|
|
|
#include "esphome/components/binary_sensor/binary_sensor.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace pn532 {
|
|
|
|
|
2020-10-31 23:55:48 +01:00
|
|
|
static const uint8_t PN532_COMMAND_VERSION_DATA = 0x02;
|
|
|
|
static const uint8_t PN532_COMMAND_SAMCONFIGURATION = 0x14;
|
|
|
|
static const uint8_t PN532_COMMAND_RFCONFIGURATION = 0x32;
|
|
|
|
static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
|
|
|
|
static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
class PN532BinarySensor;
|
|
|
|
class PN532Trigger;
|
|
|
|
|
2020-10-31 23:55:48 +01:00
|
|
|
class PN532 : public PollingComponent {
|
2019-04-17 12:06:00 +02:00
|
|
|
public:
|
|
|
|
void setup() override;
|
|
|
|
|
|
|
|
void dump_config() override;
|
|
|
|
|
|
|
|
void update() override;
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
|
|
|
|
void loop() override;
|
|
|
|
|
|
|
|
void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); }
|
|
|
|
void register_trigger(PN532Trigger *trig) { this->triggers_.push_back(trig); }
|
|
|
|
|
|
|
|
protected:
|
2020-10-31 23:55:48 +01:00
|
|
|
void turn_off_rf_();
|
|
|
|
bool write_command_(const std::vector<uint8_t> &data);
|
|
|
|
bool read_response_(uint8_t command, std::vector<uint8_t> &data);
|
2019-04-17 12:06:00 +02:00
|
|
|
bool read_ack_();
|
2020-10-31 23:55:48 +01:00
|
|
|
uint8_t read_response_length_();
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2020-10-31 23:55:48 +01:00
|
|
|
virtual bool write_data(const std::vector<uint8_t> &data) = 0;
|
|
|
|
virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
|
2020-06-10 04:23:25 +02:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
bool requested_read_{false};
|
|
|
|
std::vector<PN532BinarySensor *> binary_sensors_;
|
|
|
|
std::vector<PN532Trigger *> triggers_;
|
2020-10-31 23:55:48 +01:00
|
|
|
std::vector<uint8_t> current_uid_;
|
2019-04-17 12:06:00 +02:00
|
|
|
enum PN532Error {
|
|
|
|
NONE = 0,
|
|
|
|
WAKEUP_FAILED,
|
|
|
|
SAM_COMMAND_FAILED,
|
|
|
|
} error_code_{NONE};
|
|
|
|
};
|
|
|
|
|
|
|
|
class PN532BinarySensor : public binary_sensor::BinarySensor {
|
|
|
|
public:
|
2019-04-22 21:56:30 +02:00
|
|
|
void set_uid(const std::vector<uint8_t> &uid) { uid_ = uid; }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2020-10-31 23:55:48 +01:00
|
|
|
bool process(std::vector<uint8_t> &data);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
void on_scan_end() {
|
|
|
|
if (!this->found_) {
|
|
|
|
this->publish_state(false);
|
|
|
|
}
|
|
|
|
this->found_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<uint8_t> uid_;
|
|
|
|
bool found_{false};
|
|
|
|
};
|
|
|
|
|
|
|
|
class PN532Trigger : public Trigger<std::string> {
|
|
|
|
public:
|
2020-10-31 23:55:48 +01:00
|
|
|
void process(std::vector<uint8_t> &data);
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace pn532
|
|
|
|
} // namespace esphome
|