esphome-ratgdo/components/ratgdo/ratgdo.cpp

673 lines
22 KiB
C++
Raw Normal View History

2023-06-05 17:12:51 +00:00
/************************************
* Rage
* Against
* The
* Garage
* Door
* Opener
*
* Copyright (C) 2022 Paul Wieland
*
* GNU GENERAL PUBLIC LICENSE
************************************/
#include "ratgdo.h"
2023-06-07 14:51:22 +00:00
#include "ratgdo_child.h"
2023-06-07 15:07:15 +00:00
#include "ratgdo_state.h"
2023-06-07 14:51:22 +00:00
2023-06-05 17:12:51 +00:00
#include "esphome/core/log.h"
2023-06-05 17:13:01 +00:00
namespace esphome {
namespace ratgdo {
2023-06-05 18:56:03 +00:00
static const char* const TAG = "ratgdo";
2023-06-06 00:59:20 +00:00
static const int STARTUP_DELAY = 2000; // delay before enabling interrupts
2023-06-05 21:26:28 +00:00
/*************************** DRY CONTACT CONTROL OF LIGHT & DOOR
* ***************************/
2023-06-06 00:59:20 +00:00
void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore* arg)
{
static unsigned long lastOpenDoorTime = 0;
2023-06-06 00:04:06 +00:00
2023-06-05 21:26:28 +00:00
unsigned long currentMillis = millis();
// Prevent ISR during the first 2 seconds after reboot
2023-06-05 21:46:27 +00:00
if (currentMillis < STARTUP_DELAY)
2023-06-05 21:26:28 +00:00
return;
2023-06-06 00:59:20 +00:00
if (!arg->trigger_open.digital_read()) {
// save the time of the falling edge
lastOpenDoorTime = currentMillis;
} else if (currentMillis - lastOpenDoorTime > 500 && currentMillis - lastOpenDoorTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the
// falling edge
arg->dryContactDoorOpen = true;
}
}
2023-06-05 21:26:28 +00:00
2023-06-06 00:59:20 +00:00
void IRAM_ATTR HOT RATGDOStore::isrDoorClose(RATGDOStore* arg)
{
static unsigned long lastCloseDoorTime = 0;
2023-06-06 00:04:06 +00:00
2023-06-05 21:26:28 +00:00
unsigned long currentMillis = millis();
// Prevent ISR during the first 2 seconds after reboot
2023-06-05 21:46:27 +00:00
if (currentMillis < STARTUP_DELAY)
2023-06-05 21:26:28 +00:00
return;
2023-06-06 00:59:20 +00:00
if (!arg->trigger_close.digital_read()) {
// save the time of the falling edge
lastCloseDoorTime = currentMillis;
} else if (currentMillis - lastCloseDoorTime > 500 && currentMillis - lastCloseDoorTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the
// falling edge
arg->dryContactDoorClose = true;
}
}
void IRAM_ATTR HOT RATGDOStore::isrLight(RATGDOStore* arg)
{
static unsigned long lastToggleLightTime = 0;
2023-06-06 00:04:06 +00:00
2023-06-05 21:26:28 +00:00
unsigned long currentMillis = millis();
// Prevent ISR during the first 2 seconds after reboot
2023-06-05 21:46:27 +00:00
if (currentMillis < STARTUP_DELAY)
2023-06-05 21:26:28 +00:00
return;
2023-06-06 00:59:20 +00:00
if (!arg->trigger_light.digital_read()) {
// save the time of the falling edge
lastToggleLightTime = currentMillis;
} else if (currentMillis - lastToggleLightTime > 500 && currentMillis - lastToggleLightTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the
// falling edge
arg->dryContactToggleLight = true;
}
}
void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore* arg)
2023-06-05 21:26:28 +00:00
{
2023-06-06 00:59:20 +00:00
if (arg->input_obst.digital_read()) {
2023-06-05 21:26:28 +00:00
arg->lastObstructionHigh = millis();
} else {
2023-06-05 21:30:46 +00:00
arg->obstructionLowCount++;
2023-06-05 21:26:28 +00:00
}
}
2023-06-06 01:06:12 +00:00
void RATGDOComponent::setup()
{
2023-06-07 16:11:00 +00:00
this->pref_ = global_preferences->make_preference<int>(734874333U);
2023-06-06 01:06:12 +00:00
if (!this->pref_.load(&this->rollingCodeCounter)) {
this->rollingCodeCounter = 0;
}
2023-06-06 00:57:06 +00:00
2023-06-06 01:06:12 +00:00
this->output_gdo_pin_->setup();
this->input_gdo_pin_->setup();
this->input_obst_pin_->setup();
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
this->trigger_open_pin_->setup();
this->trigger_close_pin_->setup();
this->trigger_light_pin_->setup();
2023-06-05 23:07:10 +00:00
2023-06-06 01:06:12 +00:00
this->status_door_pin_->setup();
this->status_obst_pin_->setup();
2023-06-05 23:07:10 +00:00
2023-06-06 01:06:12 +00:00
this->store_.input_obst = this->input_obst_pin_->to_isr();
2023-06-05 19:39:46 +00:00
2023-06-06 01:06:12 +00:00
this->store_.trigger_open = this->trigger_open_pin_->to_isr();
this->store_.trigger_close = this->trigger_close_pin_->to_isr();
this->store_.trigger_light = this->trigger_light_pin_->to_isr();
2023-06-06 00:00:16 +00:00
2023-06-06 01:06:12 +00:00
this->trigger_open_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->trigger_close_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->trigger_light_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
2023-06-05 19:39:46 +00:00
2023-06-06 01:06:12 +00:00
this->status_door_pin_->pin_mode(gpio::FLAG_OUTPUT);
this->status_obst_pin_->pin_mode(gpio::FLAG_OUTPUT);
2023-06-05 22:38:53 +00:00
2023-06-06 01:29:06 +00:00
this->output_gdo_pin_->pin_mode(gpio::FLAG_OUTPUT);
this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
2023-06-06 01:06:12 +00:00
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
2023-06-05 23:07:10 +00:00
this->check_uart_settings(9600, 1, esphome::uart::UART_CONFIG_PARITY_NONE, 8);
2023-06-06 01:29:06 +00:00
2023-06-06 01:06:12 +00:00
this->trigger_open_pin_->attach_interrupt(RATGDOStore::isrDoorOpen, &this->store_, gpio::INTERRUPT_ANY_EDGE);
this->trigger_close_pin_->attach_interrupt(RATGDOStore::isrDoorClose, &this->store_, gpio::INTERRUPT_ANY_EDGE);
this->trigger_light_pin_->attach_interrupt(RATGDOStore::isrLight, &this->store_, gpio::INTERRUPT_ANY_EDGE);
this->input_obst_pin_->attach_interrupt(RATGDOStore::isrObstruction, &this->store_, gpio::INTERRUPT_ANY_EDGE);
2023-06-05 19:39:46 +00:00
2023-06-06 01:06:12 +00:00
ESP_LOGD(TAG, "Syncing rolling code counter after reboot...");
2023-06-06 01:57:12 +00:00
sync(); // reboot/sync to the opener on startup
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::loop()
{
obstructionLoop();
gdoStateLoop();
dryContactLoop();
statusUpdateLoop();
}
2023-06-05 18:26:26 +00:00
2023-06-06 01:37:29 +00:00
void RATGDOComponent::dump_config()
{
2023-06-06 01:37:27 +00:00
ESP_LOGCONFIG(TAG, "Setting up RATGDO...");
2023-06-06 02:36:52 +00:00
LOG_PIN(" Output GDO Pin: ", this->output_gdo_pin_);
LOG_PIN(" Input GDO Pin: ", this->input_gdo_pin_);
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
LOG_PIN(" Trigger Open Pin: ", this->trigger_open_pin_);
LOG_PIN(" Trigger Close Pin: ", this->trigger_close_pin_);
LOG_PIN(" Trigger Light Pin: ", this->trigger_light_pin_);
LOG_PIN(" Status Door Pin: ", this->status_door_pin_);
LOG_PIN(" Status Obstruction Pin: ", this->status_obst_pin_);
2023-06-07 22:19:48 +00:00
ESP_LOGCONFIG(TAG, " Rolling Code Counter: %d", this->rollingCodeCounter);
2023-06-06 01:37:27 +00:00
}
2023-06-08 02:40:07 +00:00
void RATGDOComponent::readRollingCode(uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction, uint8_t& motor)
2023-06-06 01:06:12 +00:00
{
uint32_t rolling = 0;
uint64_t fixed = 0;
uint32_t data = 0;
uint16_t cmd = 0;
uint8_t nibble = 0;
uint8_t byte1 = 0;
uint8_t byte2 = 0;
decode_wireline(this->rxRollingCode, &rolling, &fixed, &data);
cmd = ((fixed >> 24) & 0xf00) | (data & 0xff);
nibble = (data >> 8) & 0xf;
byte1 = (data >> 16) & 0xff;
byte2 = (data >> 24) & 0xff;
if (cmd == 0x81) {
2023-06-07 22:19:48 +00:00
2023-06-06 01:06:12 +00:00
door = nibble;
light = (byte2 >> 1) & 1;
lock = byte2 & 1;
motion = 0; // when the status message is read, reset motion state to 0|clear
2023-06-08 02:40:07 +00:00
motor = 0; // when the status message is read, reset motor state to 0|off
2023-06-06 01:06:12 +00:00
// obstruction = (byte1 >> 6) & 1; // unreliable due to the time it takes to register an obstruction
2023-06-07 22:19:48 +00:00
ESP_LOGD(TAG, "Door: %d Light: %d Lock: %d Motion: %d Obstruction: %d", door, light, lock, motion, obstruction);
2023-06-08 01:40:38 +00:00
if (this->forceUpdate_) {
this->forceUpdate_ = false;
2023-06-08 01:39:20 +00:00
this->previousDoorState = DoorState::DOOR_STATE_UNKNOWN;
this->previousLightState = LightState::LIGHT_STATE_UNKNOWN;
this->previousLockState = LockState::LOCK_STATE_UNKNOWN;
}
2023-06-06 01:06:12 +00:00
} else if (cmd == 0x281) {
light ^= 1; // toggle bit
2023-06-08 01:06:39 +00:00
ESP_LOGD(TAG, "Light: %d (toggle)", light);
2023-06-06 01:06:12 +00:00
} else if (cmd == 0x84) {
ESP_LOGD(TAG, "Unknown 0x84");
2023-06-08 02:40:07 +00:00
} else if (cmd == 0x284) {
motor = 1;
2023-06-08 01:15:09 +00:00
} else if (cmd == 0x280) {
2023-06-08 01:16:20 +00:00
ESP_LOGD(TAG, "Pressed: %s", byte1 == 1 ? "pressed" : "released");
2023-06-08 01:15:09 +00:00
} else if (cmd == 0x48c) {
2023-06-08 01:15:11 +00:00
ESP_LOGD(TAG, "Openings: %d", (byte1 << 8) | byte2);
2023-06-06 01:06:12 +00:00
} else if (cmd == 0x285) {
motion = 1; // toggle bit
2023-06-08 01:06:39 +00:00
ESP_LOGD(TAG, "Motion: %d (toggle)", motion);
2023-06-08 01:18:32 +00:00
} else {
ESP_LOGD(TAG, "Unknown command: %04x", cmd);
2023-06-06 00:57:06 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:56:03 +00:00
2023-06-07 14:40:45 +00:00
void RATGDOComponent::getRollingCode(Commands command)
2023-06-06 01:06:12 +00:00
{
2023-06-05 23:07:10 +00:00
2023-06-06 01:06:12 +00:00
uint64_t id = 0x539;
uint64_t fixed = 0;
uint32_t data = 0;
2023-06-07 14:40:45 +00:00
switch (command) {
case REBOOT1:
2023-06-06 01:06:12 +00:00
fixed = 0x400000000;
data = 0x0000618b;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case REBOOT2:
2023-06-06 01:06:12 +00:00
fixed = 0;
data = 0x01009080;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case REBOOT3:
2023-06-06 01:06:12 +00:00
fixed = 0;
data = 0x0000b1a0;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case REBOOT4:
2023-06-06 01:06:12 +00:00
fixed = 0;
data = 0x01009080;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case REBOOT5:
2023-06-06 01:06:12 +00:00
fixed = 0x300000000;
data = 0x00008092;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case REBOOT6:
2023-06-06 01:06:12 +00:00
fixed = 0x300000000;
data = 0x00008092;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case DOOR1:
2023-06-06 01:06:12 +00:00
fixed = 0x200000000;
data = 0x01018280;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case DOOR2:
2023-06-06 01:06:12 +00:00
fixed = 0x200000000;
data = 0x01009280;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case LIGHT:
2023-06-06 01:06:12 +00:00
fixed = 0x200000000;
data = 0x00009281;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
case LOCK:
2023-06-06 01:06:12 +00:00
fixed = 0x0100000000;
data = 0x0000728c;
2023-06-07 17:30:14 +00:00
break;
2023-06-07 14:40:45 +00:00
default:
2023-06-06 01:06:12 +00:00
ESP_LOGD(TAG, "ERROR: Invalid command");
return;
}
2023-06-05 18:57:01 +00:00
2023-06-07 21:10:33 +00:00
ESP_LOGD(TAG, "Command: %d rollingCodeCounter=%d", command, this->rollingCodeCounter);
2023-06-07 18:49:10 +00:00
2023-06-06 01:06:12 +00:00
fixed = fixed | id;
2023-06-05 18:57:01 +00:00
2023-06-06 01:06:12 +00:00
encode_wireline(this->rollingCodeCounter, fixed, data, this->txRollingCode);
2023-06-05 18:57:01 +00:00
2023-06-06 01:06:12 +00:00
printRollingCode();
2023-06-05 18:57:01 +00:00
2023-06-07 14:40:45 +00:00
if (command != Commands::DOOR1) { // door2 is created with same counter and should always be called after door1
2023-06-07 22:59:08 +00:00
incrementRollingCodeCounter();
2023-06-05 18:57:01 +00:00
}
2023-06-06 01:06:12 +00:00
return;
}
2023-06-05 18:57:01 +00:00
2023-06-07 23:10:02 +00:00
void RATGDOComponent::setRollingCodeCounter(uint32_t counter)
{
ESP_LOGD(TAG, "Set rolling code counter to %d", counter);
this->rollingCodeCounter = counter;
this->pref_.save(&this->rollingCodeCounter);
sendRollingCodeChanged();
}
2023-06-07 22:59:08 +00:00
void RATGDOComponent::incrementRollingCodeCounter()
{
ESP_LOGD(TAG, "Incrementing rolling code counter");
this->rollingCodeCounter = (this->rollingCodeCounter + 1) & 0xfffffff;
2023-06-07 23:10:02 +00:00
sendRollingCodeChanged();
}
void RATGDOComponent::sendRollingCodeChanged()
{
2023-06-07 22:59:08 +00:00
for (auto* child : this->children_) {
child->on_rolling_code_change(this->rollingCodeCounter);
2023-06-07 22:59:14 +00:00
}
2023-06-07 22:59:08 +00:00
}
2023-06-06 01:06:12 +00:00
void RATGDOComponent::printRollingCode()
{
2023-06-07 20:52:51 +00:00
ESP_LOGD(TAG, "Counter: %d Send code: [%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X]",
2023-06-06 02:57:26 +00:00
this->rollingCodeCounter,
2023-06-07 13:49:09 +00:00
this->txRollingCode[0],
this->txRollingCode[1],
this->txRollingCode[2],
this->txRollingCode[3],
this->txRollingCode[4],
this->txRollingCode[5],
this->txRollingCode[6],
this->txRollingCode[7],
this->txRollingCode[8],
this->txRollingCode[9],
this->txRollingCode[10],
this->txRollingCode[11],
this->txRollingCode[12],
this->txRollingCode[13],
this->txRollingCode[14],
this->txRollingCode[15],
this->txRollingCode[16],
this->txRollingCode[17],
this->txRollingCode[18]);
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:57:01 +00:00
2023-06-06 01:06:12 +00:00
// handle changes to the dry contact state
void RATGDOComponent::dryContactLoop()
{
if (this->store_.dryContactDoorOpen) {
ESP_LOGD(TAG, "Dry Contact: open the door");
this->store_.dryContactDoorOpen = false;
openDoor();
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
if (this->store_.dryContactDoorClose) {
ESP_LOGD(TAG, "Dry Contact: close the door");
this->store_.dryContactDoorClose = false;
closeDoor();
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
if (this->store_.dryContactToggleLight) {
ESP_LOGD(TAG, "Dry Contact: toggle the light");
this->store_.dryContactToggleLight = false;
toggleLight();
2023-06-05 18:56:03 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 21:26:28 +00:00
2023-06-06 01:06:12 +00:00
/*************************** OBSTRUCTION DETECTION ***************************/
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::obstructionLoop()
{
long currentMillis = millis();
static unsigned long lastMillis = 0;
// the obstruction sensor has 3 states: clear (HIGH with LOW pulse every 7ms), obstructed (HIGH), asleep (LOW)
// the transitions between awake and asleep are tricky because the voltage drops slowly when falling asleep
// and is high without pulses when waking up
// If at least 3 low pulses are counted within 50ms, the door is awake, not obstructed and we don't have to check anything else
// Every 50ms
if (currentMillis - lastMillis > 50) {
// check to see if we got between 3 and 8 low pulses on the line
if (this->store_.obstructionLowCount >= 3 && this->store_.obstructionLowCount <= 8) {
// obstructionCleared();
2023-06-08 00:20:54 +00:00
this->obstructionState = ObstructionState::OBSTRUCTION_STATE_CLEAR;
2023-06-06 01:06:12 +00:00
// if there have been no pulses the line is steady high or low
} else if (this->store_.obstructionLowCount == 0) {
// if the line is high and the last high pulse was more than 70ms ago, then there is an obstruction present
if (this->input_obst_pin_->digital_read() && currentMillis - this->store_.lastObstructionHigh > 70) {
2023-06-08 00:20:54 +00:00
this->obstructionState = ObstructionState::OBSTRUCTION_STATE_OBSTRUCTED;
2023-06-06 01:06:12 +00:00
// obstructionDetected();
} else {
// asleep
}
2023-06-06 00:57:06 +00:00
}
2023-06-06 01:06:12 +00:00
lastMillis = currentMillis;
this->store_.obstructionLowCount = 0;
2023-06-05 18:56:03 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::gdoStateLoop()
{
if (!this->available()) {
2023-06-06 02:54:17 +00:00
// ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin());
2023-06-06 01:06:12 +00:00
return;
}
uint8_t serData;
if (!this->read_byte(&serData)) {
ESP_LOGD(TAG, "Failed to read byte");
return;
}
2023-06-06 01:06:12 +00:00
static uint32_t msgStart;
static bool reading = false;
static uint16_t byteCount = 0;
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
if (!reading) {
// shift serial byte onto msg start
msgStart <<= 8;
msgStart |= serData;
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
// truncate to 3 bytes
msgStart &= 0x00FFFFFF;
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
// if we are at the start of a message, capture the next 16 bytes
if (msgStart == 0x550100) {
byteCount = 3;
rxRollingCode[0] = 0x55;
rxRollingCode[1] = 0x01;
rxRollingCode[2] = 0x00;
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
reading = true;
return;
2023-06-05 23:19:52 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
if (reading) {
this->rxRollingCode[byteCount] = serData;
byteCount++;
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
if (byteCount == 19) {
reading = false;
msgStart = 0;
byteCount = 0;
2023-06-05 23:19:52 +00:00
2023-06-08 02:40:07 +00:00
readRollingCode(this->doorState, this->lightState, this->lockState, this->motionState, this->obstructionState, this->motorState);
2023-06-05 23:19:52 +00:00
}
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::statusUpdateLoop()
{
2023-06-08 00:20:32 +00:00
if (this->doorState != this->previousDoorState)
2023-06-06 01:06:12 +00:00
sendDoorStatus();
2023-06-08 00:20:32 +00:00
if (this->lightState != this->previousLightState)
2023-06-06 01:06:12 +00:00
sendLightStatus();
2023-06-08 00:20:32 +00:00
if (this->lockState != this->previousLockState)
2023-06-06 01:06:12 +00:00
sendLockStatus();
2023-06-08 00:20:32 +00:00
if (this->obstructionState != this->previousObstructionState)
2023-06-06 01:06:12 +00:00
sendObstructionStatus();
2023-06-08 02:48:15 +00:00
if (this->motorState != this->previousMotorState) {
sendMotorStatus();
}
2023-06-08 00:20:32 +00:00
if (this->motionState == MotionState::MOTION_STATE_DETECTED) {
2023-06-06 01:06:12 +00:00
sendMotionStatus();
2023-06-08 00:20:32 +00:00
this->motionState = MotionState::MOTION_STATE_CLEAR;
2023-06-06 00:57:06 +00:00
}
2023-06-05 23:19:52 +00:00
2023-06-08 00:20:32 +00:00
this->previousDoorState = this->doorState;
this->previousLightState = this->lightState;
this->previousLockState = this->lockState;
this->previousObstructionState = this->obstructionState;
2023-06-08 02:50:07 +00:00
this->previousMotorState = this->motorState;
2023-06-07 23:59:49 +00:00
}
void RATGDOComponent::query()
{
2023-06-08 01:40:38 +00:00
this->forceUpdate_ = true;
2023-06-07 23:59:49 +00:00
sendCommandAndSaveCounter(Commands::REBOOT2);
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::sendDoorStatus()
{
2023-06-08 00:20:32 +00:00
DoorState val = static_cast<DoorState>(this->doorState);
2023-06-07 15:00:42 +00:00
ESP_LOGD(TAG, "Door state: %s", door_state_to_string(val));
2023-06-07 15:00:48 +00:00
for (auto* child : this->children_) {
2023-06-07 14:59:09 +00:00
child->on_door_state(val);
}
2023-06-08 00:20:32 +00:00
this->status_door_pin_->digital_write(this->doorState == 1);
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::sendLightStatus()
{
2023-06-08 00:20:32 +00:00
LightState val = static_cast<LightState>(this->lightState);
ESP_LOGD(TAG, "Light state %s (%d)", light_state_to_string(val), this->lightState);
2023-06-07 14:59:12 +00:00
for (auto* child : this->children_) {
2023-06-07 14:59:09 +00:00
child->on_light_state(val);
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::sendLockStatus()
{
2023-06-08 00:20:32 +00:00
LockState val = static_cast<LockState>(this->lockState);
2023-06-07 14:59:09 +00:00
ESP_LOGD(TAG, "Lock state %s", lock_state_to_string(val));
2023-06-07 14:59:12 +00:00
for (auto* child : this->children_) {
2023-06-07 14:59:09 +00:00
child->on_lock_state(val);
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::sendMotionStatus()
{
2023-06-08 00:20:32 +00:00
MotionState val = static_cast<MotionState>(this->motionState);
2023-06-07 14:59:09 +00:00
ESP_LOGD(TAG, "Motion state %s", motion_state_to_string(val));
2023-06-07 14:59:12 +00:00
for (auto* child : this->children_) {
2023-06-07 14:59:09 +00:00
child->on_motion_state(val);
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 17:12:51 +00:00
2023-06-08 02:40:07 +00:00
void RATGDOComponent::sendMotorStatus()
{
2023-06-08 02:43:16 +00:00
MotorState val = static_cast<MotorState>(this->motorState);
2023-06-08 02:40:07 +00:00
ESP_LOGD(TAG, "Motor state %s", motor_state_to_string(val));
for (auto* child : this->children_) {
child->on_motor_state(val);
}
}
2023-06-06 01:06:12 +00:00
void RATGDOComponent::sendObstructionStatus()
{
2023-06-08 00:20:32 +00:00
ObstructionState val = static_cast<ObstructionState>(this->obstructionState);
2023-06-07 14:59:09 +00:00
ESP_LOGD(TAG, "Obstruction state %s", obstruction_state_to_string(val));
2023-06-07 14:59:12 +00:00
for (auto* child : this->children_) {
2023-06-07 14:59:09 +00:00
child->on_obstruction_state(val);
}
2023-06-08 00:20:32 +00:00
this->status_obst_pin_->digital_write(this->obstructionState == 0);
2023-06-06 01:06:12 +00:00
}
2023-06-05 17:12:51 +00:00
2023-06-06 01:06:12 +00:00
/************************* DOOR COMMUNICATION *************************/
/*
* Transmit a message to the door opener over uart1
* The TX1 pin is controlling a transistor, so the logic is inverted
* A HIGH state on TX1 will pull the 12v line LOW
*
* The opener requires a specific duration low/high pulse before it will accept
* a message
*/
2023-06-07 14:40:45 +00:00
void RATGDOComponent::transmit(Commands command)
2023-06-06 01:06:12 +00:00
{
2023-06-06 02:26:35 +00:00
getRollingCode(command);
2023-06-06 01:06:12 +00:00
this->output_gdo_pin_->digital_write(true); // pull the line high for 1305 micros so the
// door opener responds to the message
delayMicroseconds(1305);
this->output_gdo_pin_->digital_write(false); // bring the line low
2023-06-05 17:12:51 +00:00
2023-06-06 01:06:12 +00:00
delayMicroseconds(1260); // "LOW" pulse duration before the message start
this->write_array(this->txRollingCode, CODE_LENGTH);
2023-06-06 01:06:12 +00:00
}
2023-06-05 17:12:51 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::sync()
{
2023-06-07 14:40:45 +00:00
transmit(Commands::REBOOT1);
2023-06-06 01:06:12 +00:00
delay(65);
2023-06-05 17:12:51 +00:00
2023-06-07 14:40:45 +00:00
transmit(Commands::REBOOT2);
2023-06-06 01:06:12 +00:00
delay(65);
2023-06-05 17:12:51 +00:00
2023-06-07 14:40:45 +00:00
transmit(Commands::REBOOT3);
2023-06-06 01:06:12 +00:00
delay(65);
2023-06-05 17:12:51 +00:00
2023-06-07 14:40:45 +00:00
transmit(Commands::REBOOT4);
2023-06-06 01:06:12 +00:00
delay(65);
2023-06-05 18:56:03 +00:00
2023-06-07 14:40:45 +00:00
transmit(Commands::REBOOT5);
2023-06-06 01:06:12 +00:00
delay(65);
2023-06-05 18:56:03 +00:00
2023-06-07 19:05:01 +00:00
sendCommandAndSaveCounter(Commands::REBOOT6);
2023-06-06 01:06:12 +00:00
delay(65);
}
2023-06-05 23:19:52 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::openDoor()
{
2023-06-08 00:20:32 +00:00
if (this->doorState == DoorState::DOOR_STATE_OPEN || this->doorState == DoorState::DOOR_STATE_OPENING) {
ESP_LOGD(TAG, "The door is already %s", door_state_to_string(static_cast<DoorState>(this->doorState)));
2023-06-06 01:06:12 +00:00
return;
2023-06-06 00:57:06 +00:00
}
2023-06-06 01:06:12 +00:00
toggleDoor();
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::closeDoor()
{
2023-06-08 00:20:32 +00:00
if (this->doorState == DoorState::DOOR_STATE_CLOSED || this->doorState == DoorState::DOOR_STATE_CLOSING) {
ESP_LOGD(TAG, "The door is already %s", door_state_to_string(static_cast<DoorState>(this->doorState)));
2023-06-06 01:06:12 +00:00
return;
2023-06-05 23:40:36 +00:00
}
2023-06-06 01:06:12 +00:00
toggleDoor();
}
2023-06-05 23:40:36 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::stopDoor()
{
2023-06-08 00:20:32 +00:00
if (this->doorState == DoorState::DOOR_STATE_OPENING || this->doorState == DoorState::DOOR_STATE_CLOSING) {
2023-06-06 01:06:12 +00:00
toggleDoor();
} else {
2023-06-06 01:30:29 +00:00
ESP_LOGD(TAG, "The door is not moving.");
2023-06-05 18:56:03 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::toggleDoor()
{
2023-06-07 14:40:45 +00:00
transmit(Commands::DOOR1);
2023-06-07 19:05:01 +00:00
delay(40);
sendCommandAndSaveCounter(Commands::DOOR2);
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:40:36 +00:00
2023-06-08 01:47:17 +00:00
bool RATGDOComponent::isLightOn()
2023-06-08 01:46:44 +00:00
{
return this->lightState == LightState::LIGHT_STATE_ON;
}
2023-06-06 01:06:12 +00:00
void RATGDOComponent::lightOn()
{
2023-06-08 00:20:32 +00:00
if (this->lightState == LightState::LIGHT_STATE_ON) {
2023-06-08 00:12:16 +00:00
ESP_LOGD(TAG, "The light is already on");
2023-06-06 01:06:12 +00:00
} else {
2023-06-08 01:12:15 +00:00
toggleLight();
// We don't always get the state back so be optimistic
this->previousLightState = this->lightState;
this->lightState = LightState::LIGHT_STATE_ON;
2023-06-05 23:36:08 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 23:36:08 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::lightOff()
{
2023-06-08 00:20:32 +00:00
if (this->lightState == LightState::LIGHT_STATE_OFF) {
2023-06-08 00:12:16 +00:00
ESP_LOGD(TAG, "The light is already off");
2023-06-06 01:06:12 +00:00
} else {
toggleLight();
// We don't always get the state back so be optimistic
this->previousLightState = this->lightState;
this->lightState = LightState::LIGHT_STATE_OFF;
2023-06-05 23:36:08 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:56:03 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::toggleLight()
{
2023-06-07 19:05:01 +00:00
sendCommandAndSaveCounter(Commands::LIGHT);
2023-06-06 01:06:12 +00:00
}
2023-06-06 00:21:59 +00:00
2023-06-06 01:06:12 +00:00
// Lock functions
void RATGDOComponent::lock()
{
2023-06-08 00:20:32 +00:00
if (this->lockState == LockState::LOCK_STATE_LOCKED) {
2023-06-06 01:06:12 +00:00
ESP_LOGD(TAG, "already locked");
} else {
toggleLock();
2023-06-06 00:57:06 +00:00
}
2023-06-06 01:06:12 +00:00
}
2023-06-05 18:26:26 +00:00
2023-06-06 01:06:12 +00:00
void RATGDOComponent::unlock()
{
2023-06-08 00:20:32 +00:00
if (this->lockState == LockState::LOCK_STATE_UNLOCKED) {
2023-06-06 01:06:12 +00:00
ESP_LOGD(TAG, "already unlocked");
} else {
toggleLock();
2023-06-06 00:57:06 +00:00
}
}
2023-06-06 01:06:12 +00:00
void RATGDOComponent::toggleLock()
{
2023-06-07 19:05:01 +00:00
sendCommandAndSaveCounter(Commands::LOCK);
2023-06-06 01:06:12 +00:00
}
2023-06-07 19:05:01 +00:00
void RATGDOComponent::sendCommandAndSaveCounter(Commands command)
2023-06-06 01:06:12 +00:00
{
2023-06-06 02:26:35 +00:00
transmit(command);
2023-06-06 01:06:12 +00:00
this->pref_.save(&this->rollingCodeCounter);
}
2023-06-07 14:47:27 +00:00
void RATGDOComponent::register_child(RATGDOClient* obj)
2023-06-07 14:45:49 +00:00
{
this->children_.push_back(obj);
obj->set_parent(this);
}
2023-06-05 17:54:46 +00:00
} // namespace ratgdo
} // namespace esphome