add uart rfid text_sensor

This commit is contained in:
Samuel Sieb
2022-02-03 14:38:24 -08:00
parent f3b73cbf30
commit 073eb20d39
4 changed files with 82 additions and 0 deletions

View File

View File

@@ -0,0 +1,25 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import uart
from esphome.components import text_sensor
from esphome.const import CONF_ID, CONF_STATE
DEPENDENCIES = ['uart']
rfid_ns = cg.esphome_ns.namespace('rfid')
RFIDTextSensor = rfid_ns.class_('RFIDTextSensor', cg.Component,
text_sensor.TextSensor, uart.UARTDevice)
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(RFIDTextSensor),
}).extend(uart.UART_DEVICE_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await text_sensor.register_text_sensor(var, config)
await uart.register_uart_device(var, config)

View File

@@ -0,0 +1,35 @@
#include "rfid_text_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace rfid {
static const char *TAG = "rfid.text_sensor";
void RFIDTextSensor::loop() {
while (this->available()) {
uint8_t c;
this->read_byte(&c);
this->handle_char_(c);
}
}
void RFIDTextSensor::handle_char_(uint8_t c) {
if (c == 1) {
this->rx_message_.clear();
return;
}
if (c == 2) {
std::string s(this->rx_message_.begin(), this->rx_message_.end());
this->publish_state(s);
this->rx_message_.clear();
return;
}
if (rx_message_.size() < 20)
this->rx_message_.push_back(c);
}
void RFIDTextSensor::dump_config() { LOG_TEXT_SENSOR("", "RFID Sensor", this); }
} // namespace rfid
} // namespace esphome

View File

@@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace rfid {
class RFIDTextSensor : public Component, public text_sensor::TextSensor, public uart::UARTDevice {
public:
float get_setup_priority() const override { return setup_priority::LATE; }
void loop() override;
void dump_config() override;
protected:
void handle_char_(uint8_t c);
std::vector<uint8_t> rx_message_;
};
} // namespace rfid
} // namespace esphome