Files
esphome-dev/esphome/components/rf_bridge/rf_bridge.h
T
Andrew Zaborowski a62b6548d2 Make some Action methods protected
Apparently play()/stop() etc. are not meant to be called directly by
users of the class and if they're called directly that would not give
the expected result for the classes that have an empty play().

Make all methods except play_complex, stop_comples and is_running
protected.  While there also make RemoteTransmitterActionBase::encode
protected.
2020-05-01 12:44:30 +02:00

96 lines
2.7 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
namespace esphome {
namespace rf_bridge {
static const uint8_t RF_MESSAGE_SIZE = 9;
static const uint8_t RF_CODE_START = 0xAA;
static const uint8_t RF_CODE_ACK = 0xA0;
static const uint8_t RF_CODE_LEARN = 0xA1;
static const uint8_t RF_CODE_LEARN_KO = 0xA2;
static const uint8_t RF_CODE_LEARN_OK = 0xA3;
static const uint8_t RF_CODE_RFIN = 0xA4;
static const uint8_t RF_CODE_RFOUT = 0xA5;
static const uint8_t RF_CODE_SNIFFING_ON = 0xA6;
static const uint8_t RF_CODE_SNIFFING_OFF = 0xA7;
static const uint8_t RF_CODE_RFOUT_NEW = 0xA8;
static const uint8_t RF_CODE_LEARN_NEW = 0xA9;
static const uint8_t RF_CODE_LEARN_KO_NEW = 0xAA;
static const uint8_t RF_CODE_LEARN_OK_NEW = 0xAB;
static const uint8_t RF_CODE_RFOUT_BUCKET = 0xB0;
static const uint8_t RF_CODE_STOP = 0x55;
static const uint8_t RF_DEBOUNCE = 200;
struct RFBridgeData {
uint16_t sync;
uint16_t low;
uint16_t high;
uint32_t code;
};
class RFBridgeComponent : public uart::UARTDevice, public Component {
public:
void loop() override;
void dump_config() override;
void add_on_code_received_callback(std::function<void(RFBridgeData)> callback) {
this->callback_.add(std::move(callback));
}
void send_code(RFBridgeData data);
void learn();
protected:
void ack_();
void decode_();
unsigned long last_ = 0;
unsigned char uartbuf_[RF_MESSAGE_SIZE + 3] = {0};
unsigned char uartpos_ = 0;
CallbackManager<void(RFBridgeData)> callback_;
};
class RFBridgeReceivedCodeTrigger : public Trigger<RFBridgeData> {
public:
explicit RFBridgeReceivedCodeTrigger(RFBridgeComponent *parent) {
parent->add_on_code_received_callback([this](RFBridgeData data) { this->trigger(data); });
}
};
template<typename... Ts> class RFBridgeSendCodeAction : public Action<Ts...> {
public:
RFBridgeSendCodeAction(RFBridgeComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(uint16_t, sync)
TEMPLATABLE_VALUE(uint16_t, low)
TEMPLATABLE_VALUE(uint16_t, high)
TEMPLATABLE_VALUE(uint32_t, code)
protected:
void play_(Ts... x) {
RFBridgeData data{};
data.sync = this->sync_.value(x...);
data.low = this->low_.value(x...);
data.high = this->high_.value(x...);
data.code = this->code_.value(x...);
this->parent_->send_code(data);
}
RFBridgeComponent *parent_;
};
template<typename... Ts> class RFBridgeLearnAction : public Action<Ts...> {
public:
RFBridgeLearnAction(RFBridgeComponent *parent) : parent_(parent) {}
protected:
void play_(Ts... x) { this->parent_->learn(); }
RFBridgeComponent *parent_;
};
} // namespace rf_bridge
} // namespace esphome