esphome-ratgdo/components/ratgdo/secplus1.h

117 lines
3.3 KiB
C
Raw Normal View History

#pragma once
#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial
#include "esphome/core/optional.h"
#include "ratgdo_state.h"
#include "protocol.h"
#include "callbacks.h"
#include "observable.h"
namespace esphome {
class Scheduler;
class InternalGPIOPin;
namespace ratgdo {
namespace secplus1 {
2024-01-09 16:42:11 +00:00
using namespace esphome::ratgdo::protocol;
static const uint8_t RX_LENGTH = 2;
typedef uint8_t RxPacket[RX_LENGTH];
2024-01-11 04:09:36 +00:00
static const uint8_t TX_LENGTH = 2;
typedef uint8_t TxPacket[TX_LENGTH];
2024-01-11 04:09:36 +00:00
static const TxPacket toggle_door = {0x30, 0x31};
static const TxPacket toggle_light = {0x32, 0x33};
static const TxPacket toggle_lock = {0x34, 0x35};
static const uint8_t secplus1_states[] = {0x35,0x35,0x35,0x35,0x33,0x33,0x53,0x53,0x38,0x3A,0x3A,0x3A,0x39,0x38,0x3A, 0x38,0x3A,0x39,0x3A};
ENUM(CommandType, uint16_t,
(TOGGLE_DOOR_REQ, 0x30),
(TOGGLE_DOOR_COMMIT, 0x31),
(TOGGLE_LIGHT_REQ, 0x32),
(TOGGLE_LIGHT_COMMIT, 0x33),
(TOGGLE_LOCK_REQ, 0x34),
(TOGGLE_LOCK_COMMIT, 0x35),
(DOOR_STATUS_0x37, 0x37),
(DOOR_STATUS, 0x38),
(OBSTRUCTION, 0x39),
(OTHER_STATUS, 0x3A),
(UNKNOWN, 0xFF),
)
struct Command {
CommandType type;
uint8_t value;
Command(): type(CommandType::UNKNOWN) {}
Command(CommandType type_, uint8_t value_ = 0) : type(type_), value(value_) {}
};
enum class WallPanelEmulationState {
WAITING,
RUNNING,
};
class Secplus1 : public Protocol {
public:
void setup(RATGDOComponent* ratgdo, Scheduler* scheduler, InternalGPIOPin* rx_pin, InternalGPIOPin* tx_pin);
void loop();
void dump_config();
void sync();
void light_action(LightAction action);
void lock_action(LockAction action);
void door_action(DoorAction action);
2024-01-09 16:42:11 +00:00
Result call(Args args);
protected:
2024-01-11 04:09:36 +00:00
void wall_panel_emulation(size_t index = 0);
optional<Command> read_command();
void handle_command(const Command& cmd);
void print_rx_packet(const RxPacket& packet) const;
void print_tx_packet(const TxPacket& packet) const;
optional<Command> decode_packet(const RxPacket& packet) const;
void transmit_packet(const TxPacket& packet, bool twice = true, uint32_t delay = 500);
2024-01-13 00:00:54 +00:00
void transmit_byte(uint32_t value, bool twice = false);
LightState light_state { LightState::UNKNOWN };
LockState lock_state { LockState::UNKNOWN };
DoorState door_state { DoorState::UNKNOWN };
DoorState prev_door_state { DoorState::UNKNOWN };
bool wall_panel_starting_ { false };
uint32_t wall_panel_emulation_start_ { 0 };
WallPanelEmulationState wall_panel_emulation_state_ { WallPanelEmulationState::WAITING };
bool is_0x37_panel_ { false };
bool request_lock_toggle_ { false };
// bool transmit_pending_ { false };
// uint32_t transmit_pending_start_ { 0 };
TxPacket tx_packet_;
uint32_t last_rx_ { 0 };
SoftwareSerial sw_serial_;
InternalGPIOPin* tx_pin_;
InternalGPIOPin* rx_pin_;
RATGDOComponent* ratgdo_;
Scheduler* scheduler_;
};
} // namespace secplus1
} // namespace ratgdo
} // namespace esphome