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"
|
|
|
|
#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";
|
|
|
|
/*** Static Codes ***/
|
2023-06-05 19:01:44 +00:00
|
|
|
static const unsigned char SYNC1[] = { 0x55, 0x01, 0x00, 0x61, 0x12, 0x49, 0x2c, 0x92, 0x5b, 0x24,
|
2023-06-05 18:56:03 +00:00
|
|
|
0x96, 0x86, 0x0b, 0x65, 0x96, 0xd9, 0x8f, 0x26, 0x4a };
|
2023-06-05 19:01:44 +00:00
|
|
|
static const unsigned char SYNC2[] = { 0x55, 0x01, 0x00, 0x08, 0x34, 0x93, 0x49, 0xb4, 0x92, 0x4d,
|
2023-06-05 18:56:03 +00:00
|
|
|
0x20, 0x26, 0x1b, 0x4d, 0xb4, 0xdb, 0xad, 0x76, 0x93 };
|
2023-06-05 19:01:44 +00:00
|
|
|
static const unsigned char SYNC3[] = { 0x55, 0x01, 0x00, 0x06, 0x1b, 0x2c, 0xbf, 0x4b, 0x6d, 0xb6,
|
2023-06-05 18:56:03 +00:00
|
|
|
0x4b, 0x18, 0x20, 0x92, 0x09, 0x20, 0xf2, 0x11, 0x2c };
|
2023-06-05 19:01:44 +00:00
|
|
|
static const unsigned char SYNC4[] = { 0x55, 0x01, 0x00, 0x95, 0x29, 0x36, 0x91, 0x29, 0x36, 0x9a,
|
2023-06-05 18:56:03 +00:00
|
|
|
0x69, 0x05, 0x2f, 0xbe, 0xdf, 0x6d, 0x16, 0xcb, 0xe7 };
|
|
|
|
|
2023-06-05 19:01:44 +00:00
|
|
|
static const unsigned char DOOR_CODE[] = { 0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe,
|
2023-06-05 18:56:03 +00:00
|
|
|
0xfc, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x36, 0xb3 };
|
|
|
|
|
2023-06-05 19:01:44 +00:00
|
|
|
static const unsigned char LIGHT_CODE[] = { 0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe,
|
2023-06-05 18:56:03 +00:00
|
|
|
0xff, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x76, 0xb1 };
|
|
|
|
|
2023-06-05 21:26:28 +00:00
|
|
|
|
2023-06-05 21:46:27 +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
|
|
|
|
* ***************************/
|
|
|
|
void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore *arg) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!arg->trigger_open.digital_read()) {
|
|
|
|
// save the time of the falling edge
|
|
|
|
arg->lastOpenDoorTime = currentMillis;
|
|
|
|
} else if (currentMillis - arg->lastOpenDoorTime > 500 && currentMillis - arg->lastOpenDoorTime < 10000) {
|
|
|
|
// now see if the rising edge was between 500ms and 10 seconds after the
|
|
|
|
// falling edge
|
|
|
|
arg->dryContactDoorOpen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR HOT RATGDOStore::isrDoorClose(RATGDOStore *arg) {
|
|
|
|
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-05 21:29:48 +00:00
|
|
|
if (!arg->trigger_close.digital_read()) {
|
2023-06-05 21:26:28 +00:00
|
|
|
// save the time of the falling edge
|
|
|
|
arg->lastCloseDoorTime = currentMillis;
|
|
|
|
} else if (currentMillis - arg->lastCloseDoorTime > 500 && currentMillis - arg->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) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!arg->trigger_light.digital_read()) {
|
|
|
|
// save the time of the falling edge
|
|
|
|
arg->lastToggleLightTime = currentMillis;
|
|
|
|
} else if (currentMillis - arg->lastToggleLightTime > 500 && currentMillis - arg->lastToggleLightTime < 10000) {
|
|
|
|
// now see if the rising edge was between 500ms and 10 seconds after the
|
|
|
|
// falling edge
|
|
|
|
arg->dryContactToggleLight = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire on RISING edge of RPM1
|
2023-06-05 22:46:42 +00:00
|
|
|
void IRAM_ATTR HOT RATGDOStore::isrRPM1(RATGDOStore *arg) {
|
|
|
|
arg->rpm1Pulsed = true;
|
|
|
|
ESP_LOGD(TAG, "isrRPM1 rpm1Pulsed");
|
|
|
|
}
|
2023-06-05 21:26:28 +00:00
|
|
|
|
|
|
|
// Fire on RISING edge of RPM2
|
|
|
|
// When RPM1 HIGH on RPM2 rising edge, door closing:
|
|
|
|
// RPM1: __|--|___
|
|
|
|
// RPM2: ___|--|__
|
|
|
|
|
|
|
|
// When RPM1 LOW on RPM2 rising edge, door opening:
|
|
|
|
// RPM1: ___|--|__
|
|
|
|
// RPM2: __|--|___
|
|
|
|
void IRAM_ATTR HOT RATGDOStore::isrRPM2(RATGDOStore *arg)
|
|
|
|
{
|
2023-06-05 22:23:58 +00:00
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
|
2023-06-05 21:26:28 +00:00
|
|
|
// The encoder updates faster than the ESP wants to process, so by sampling
|
|
|
|
// every 5ms we get a more reliable curve The counter is behind the actual
|
|
|
|
// pulse counter, but it doesn't matter since we only need a reliable linear
|
|
|
|
// counter to determine the door direction
|
2023-06-05 22:23:58 +00:00
|
|
|
if (currentMillis - arg->lastPulse < 5) {
|
2023-06-05 21:26:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In rare situations, the rotary encoder can be parked so that RPM2
|
|
|
|
// continuously fires this ISR. This causes the door counter to change value
|
|
|
|
// even though the door isn't moving To solve this, check to see if RPM1
|
|
|
|
// pulsed. If not, do nothing. If yes, reset the pulsed flag
|
|
|
|
if (arg->rpm1Pulsed) {
|
|
|
|
arg->rpm1Pulsed = false;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:23:58 +00:00
|
|
|
arg->lastPulse = currentMillis;
|
2023-06-05 21:26:28 +00:00
|
|
|
|
|
|
|
// If the RPM1 state is different from the RPM2 state, then the door is
|
|
|
|
// opening
|
|
|
|
if (arg->input_rpm1.digital_read()) {
|
2023-06-05 22:46:42 +00:00
|
|
|
ESP_LOGD(TAG, "isrRPM2 RPM1 HIGH");
|
2023-06-05 21:26:28 +00:00
|
|
|
arg->doorPositionCounter--;
|
|
|
|
} else {
|
2023-06-05 22:46:42 +00:00
|
|
|
ESP_LOGD(TAG, "isrRPM2 RPM1 LOW");
|
2023-06-05 21:26:28 +00:00
|
|
|
arg->doorPositionCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:30:46 +00:00
|
|
|
void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore *arg)
|
2023-06-05 21:26:28 +00:00
|
|
|
{
|
2023-06-05 21:31:03 +00:00
|
|
|
if (arg->input_obst.digital_read()) {
|
2023-06-05 22:47:01 +00:00
|
|
|
ESP_LOGD(TAG, "isrObstruction HIGH");
|
2023-06-05 21:26:28 +00:00
|
|
|
arg->lastObstructionHigh = millis();
|
|
|
|
} else {
|
2023-06-05 22:47:01 +00:00
|
|
|
ESP_LOGD(TAG, "isrObstruction LOW");
|
2023-06-05 21:30:46 +00:00
|
|
|
arg->obstructionLowCount++;
|
2023-06-05 21:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
void RATGDOComponent::setup()
|
|
|
|
{
|
|
|
|
this->pref_ = global_preferences->make_preference<int>(734874333U);
|
|
|
|
if (!this->pref_.load(&this->rollingCodeCounter)) {
|
|
|
|
this->rollingCodeCounter = 0;
|
|
|
|
}
|
|
|
|
|
2023-06-05 19:39:46 +00:00
|
|
|
this->output_gdo_pin_->setup();
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.output_gdo = this->output_gdo_pin_->to_isr();
|
2023-06-05 23:07:10 +00:00
|
|
|
this->input_gdo_pin_->setup();
|
|
|
|
this->store_.input_gdo = this->input_gdo_pin_->to_isr();
|
|
|
|
this->input_obst_pin_->setup();
|
|
|
|
this->store_.input_obst = this->input_obst_pin_->to_isr();
|
|
|
|
|
2023-06-05 19:39:46 +00:00
|
|
|
this->trigger_open_pin_->setup();
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.trigger_open = this->trigger_open_pin_->to_isr();
|
2023-06-05 19:39:46 +00:00
|
|
|
this->trigger_close_pin_->setup();
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.trigger_close = this->trigger_close_pin_->to_isr();
|
2023-06-05 19:39:46 +00:00
|
|
|
this->trigger_light_pin_->setup();
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.trigger_light = this->trigger_light_pin_->to_isr();
|
2023-06-05 23:07:10 +00:00
|
|
|
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_door_pin_->setup();
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.status_door = this->status_door_pin_->to_isr();
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_obst_pin_->setup();
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.status_obst = this->status_obst_pin_->to_isr();
|
2023-06-05 19:39:46 +00:00
|
|
|
|
|
|
|
|
2023-06-05 19:48:16 +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 22:38:53 +00:00
|
|
|
|
2023-06-05 19:48:16 +00:00
|
|
|
this->status_door_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
|
|
|
this->status_obst_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
2023-06-05 23:07:10 +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-05 19:48:16 +00:00
|
|
|
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
|
2023-06-05 18:56:03 +00:00
|
|
|
|
2023-06-05 23:07:10 +00:00
|
|
|
swSerial.begin(9600, SWSERIAL_8N1, this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin(), true);
|
2023-06-05 19:39:46 +00:00
|
|
|
|
2023-06-05 21:32:01 +00:00
|
|
|
this->trigger_open_pin_->attach_interrupt(RATGDOStore::isrDoorOpen, &this->store_, gpio::INTERRUPT_ANY_EDGE);
|
2023-06-05 21:28:48 +00:00
|
|
|
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 18:56:03 +00:00
|
|
|
|
|
|
|
if (this->useRollingCodes_) {
|
|
|
|
ESP_LOGD(TAG, "Syncing rolling code counter after reboot...");
|
|
|
|
sync(); // if rolling codes are being used (rolling code counter > 0), send
|
|
|
|
// reboot/sync to the opener on startup
|
|
|
|
} else {
|
|
|
|
ESP_LOGD(TAG, "Rolling codes are disabled.");
|
|
|
|
}
|
2023-06-05 18:26:26 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
void RATGDOComponent::loop()
|
|
|
|
{
|
|
|
|
obstructionLoop();
|
2023-06-05 23:07:10 +00:00
|
|
|
gdoStateLoop();
|
2023-06-05 18:56:03 +00:00
|
|
|
dryContactLoop();
|
2023-06-05 23:07:10 +00:00
|
|
|
statusUpdateLoop();
|
2023-06-05 22:08:12 +00:00
|
|
|
//ESP_LOGD(TAG, "Door State: %s", this->doorState.c_str());
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 18:56:03 +00:00
|
|
|
|
2023-06-05 23:07:10 +00:00
|
|
|
void RATGDOComponent::readRollingCode(uint8_t &door, uint8_t &light, uint8_t &lock, uint8_t &motion, uint8_t &obstruction){
|
|
|
|
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){
|
|
|
|
door = nibble;
|
|
|
|
light = (byte2 >> 1) & 1;
|
|
|
|
lock = byte2 & 1;
|
|
|
|
motion = 0; // when the status message is read, reset motion state to 0|clear
|
|
|
|
// obstruction = (byte1 >> 6) & 1; // unreliable due to the time it takes to register an obstruction
|
|
|
|
|
|
|
|
}else if(cmd == 0x281){
|
|
|
|
light ^= 1; // toggle bit
|
|
|
|
}else if(cmd == 0x84){
|
|
|
|
}else if(cmd == 0x285){
|
|
|
|
motion = 1; // toggle bit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:57:01 +00:00
|
|
|
void RATGDOComponent::getRollingCode(const char* command)
|
|
|
|
{
|
|
|
|
|
|
|
|
uint64_t id = 0x539;
|
|
|
|
uint64_t fixed = 0;
|
|
|
|
uint32_t data = 0;
|
|
|
|
|
|
|
|
if (strcmp(command, "reboot1") == 0) {
|
|
|
|
fixed = 0x400000000;
|
|
|
|
data = 0x0000618b;
|
|
|
|
} else if (strcmp(command, "reboot2") == 0) {
|
|
|
|
fixed = 0;
|
|
|
|
data = 0x01009080;
|
|
|
|
} else if (strcmp(command, "reboot3") == 0) {
|
|
|
|
fixed = 0;
|
|
|
|
data = 0x0000b1a0;
|
|
|
|
} else if (strcmp(command, "reboot4") == 0) {
|
|
|
|
fixed = 0;
|
|
|
|
data = 0x01009080;
|
|
|
|
} else if (strcmp(command, "reboot5") == 0) {
|
|
|
|
fixed = 0x300000000;
|
|
|
|
data = 0x00008092;
|
|
|
|
} else if (strcmp(command, "reboot6") == 0) {
|
|
|
|
fixed = 0x300000000;
|
|
|
|
data = 0x00008092;
|
|
|
|
} else if (strcmp(command, "door1") == 0) {
|
|
|
|
fixed = 0x200000000;
|
|
|
|
data = 0x01018280;
|
|
|
|
} else if (strcmp(command, "door2") == 0) {
|
|
|
|
fixed = 0x200000000;
|
|
|
|
data = 0x01009280;
|
|
|
|
} else if (strcmp(command, "light") == 0) {
|
|
|
|
fixed = 0x200000000;
|
|
|
|
data = 0x00009281;
|
|
|
|
} else {
|
|
|
|
ESP_LOGD(TAG, "ERROR: Invalid command");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed = fixed | id;
|
|
|
|
|
2023-06-05 23:07:10 +00:00
|
|
|
encode_wireline(this->rollingCodeCounter, fixed, data, this->txRollingCode);
|
2023-06-05 18:57:01 +00:00
|
|
|
|
|
|
|
printRollingCode();
|
|
|
|
|
|
|
|
if (strcmp(command, "door1") != 0) { // door2 is created with same counter and should always be called after door1
|
|
|
|
this->rollingCodeCounter = (this->rollingCodeCounter + 1) & 0xfffffff;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RATGDOComponent::printRollingCode()
|
|
|
|
{
|
2023-06-05 22:15:49 +00:00
|
|
|
for (int i = 0; i < CODE_LENGTH; i++) {
|
|
|
|
if (this->rollingCode[i] <= 0x0f)
|
|
|
|
ESP_LOGD(TAG, "0");
|
|
|
|
ESP_LOGD(TAG, "%x", this->rollingCode[i]);
|
|
|
|
}
|
2023-06-05 18:57:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
void RATGDOComponent::set_rolling_codes(bool useRollingCodes)
|
|
|
|
{
|
|
|
|
this->useRollingCodes_ = useRollingCodes;
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
/*************************** DETECTING THE DOOR STATE
|
|
|
|
* ***************************/
|
|
|
|
void RATGDOComponent::doorStateLoop()
|
|
|
|
{
|
|
|
|
static bool rotaryEncoderDetected = false;
|
|
|
|
static int lastDoorPositionCounter = 0;
|
|
|
|
static int lastDirectionChangeCounter = 0;
|
|
|
|
static int lastCounterMillis = 0;
|
|
|
|
|
|
|
|
// Handle reed switch
|
|
|
|
// This may need to be debounced, but so far in testing I haven't detected any
|
|
|
|
// bounces
|
|
|
|
if (!rotaryEncoderDetected) {
|
2023-06-05 19:39:46 +00:00
|
|
|
if (!this->input_rpm1_pin_->digital_read()) {
|
2023-06-05 21:26:28 +00:00
|
|
|
if (this->doorState != "reed_closed") {
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Reed switch closed");
|
|
|
|
this->doorState = "reed_closed";
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_door_pin_->digital_write(true);
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
2023-06-05 21:26:28 +00:00
|
|
|
} else if (this->doorState != "reed_open") {
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Reed switch open");
|
|
|
|
this->doorState = "reed_open";
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_door_pin_->digital_write(false);
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// end reed switch handling
|
|
|
|
|
2023-06-05 22:38:53 +00:00
|
|
|
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
// If the previous and the current state of the RPM2 Signal are different,
|
|
|
|
// that means there is a rotary encoder detected and the door is moving
|
2023-06-05 21:35:38 +00:00
|
|
|
if (this->store_.doorPositionCounter != lastDoorPositionCounter) {
|
2023-06-05 18:56:03 +00:00
|
|
|
rotaryEncoderDetected = true; // this disables the reed switch handler
|
|
|
|
lastCounterMillis = millis();
|
|
|
|
|
2023-06-05 21:35:59 +00:00
|
|
|
ESP_LOGD(TAG, "Door Position: %d", this->store_.doorPositionCounter);
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 5 pulses before updating to door opening status
|
2023-06-05 21:36:40 +00:00
|
|
|
if (this->store_.doorPositionCounter - lastDirectionChangeCounter > 5) {
|
2023-06-05 18:56:03 +00:00
|
|
|
if (this->doorState != "opening") {
|
|
|
|
ESP_LOGD(TAG, "Door Opening...");
|
|
|
|
}
|
2023-06-05 21:35:38 +00:00
|
|
|
lastDirectionChangeCounter = this->store_.doorPositionCounter;
|
2023-06-05 18:56:03 +00:00
|
|
|
this->doorState = "opening";
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:35:38 +00:00
|
|
|
if (lastDirectionChangeCounter - this->store_.doorPositionCounter > 5) {
|
2023-06-05 18:56:03 +00:00
|
|
|
if (this->doorState != "closing") {
|
|
|
|
ESP_LOGD(TAG, "Door Closing...");
|
|
|
|
}
|
2023-06-05 21:35:38 +00:00
|
|
|
lastDirectionChangeCounter = this->store_.doorPositionCounter;
|
2023-06-05 18:56:03 +00:00
|
|
|
this->doorState = "closing";
|
|
|
|
}
|
|
|
|
|
|
|
|
// 250 millis after the last rotary encoder pulse, the door is stopped
|
|
|
|
if (millis() - lastCounterMillis > 250) {
|
|
|
|
// if the door was closing, and is now stopped, then the door is closed
|
|
|
|
if (this->doorState == "closing") {
|
|
|
|
this->doorState = "closed";
|
|
|
|
ESP_LOGD(TAG, "Closed");
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_door_pin_->digital_write(false);
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the door was opening, and is now stopped, then the door is open
|
|
|
|
if (this->doorState == "opening") {
|
|
|
|
this->doorState = "open";
|
|
|
|
ESP_LOGD(TAG, "Open");
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_door_pin_->digital_write(true);
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:38:53 +00:00
|
|
|
ESP_LOGD(TAG, "Door State: %s, doorPositionCounter: %d rotaryEncoderDetected: %d", this->doorState.c_str(), this->store_.doorPositionCounter, rotaryEncoderDetected);
|
2023-06-05 21:35:38 +00:00
|
|
|
lastDoorPositionCounter = this->store_.doorPositionCounter;
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
// handle changes to the dry contact state
|
|
|
|
void RATGDOComponent::dryContactLoop()
|
|
|
|
{
|
2023-06-05 21:26:28 +00:00
|
|
|
if (this->store_.dryContactDoorOpen) {
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Dry Contact: open the door");
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.dryContactDoorOpen = false;
|
2023-06-05 18:56:03 +00:00
|
|
|
openDoor();
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:26:28 +00:00
|
|
|
if (this->store_.dryContactDoorClose) {
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Dry Contact: close the door");
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.dryContactDoorClose = false;
|
2023-06-05 18:56:03 +00:00
|
|
|
closeDoor();
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:26:28 +00:00
|
|
|
if (this->store_.dryContactToggleLight) {
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Dry Contact: toggle the light");
|
2023-06-05 21:26:28 +00:00
|
|
|
this->store_.dryContactToggleLight = false;
|
2023-06-05 18:56:03 +00:00
|
|
|
toggleLight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************** OBSTRUCTION DETECTION ***************************/
|
2023-06-05 21:26:28 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
void RATGDOComponent::obstructionLoop()
|
|
|
|
{
|
|
|
|
long currentMillis = millis();
|
|
|
|
static unsigned long lastMillis = 0;
|
|
|
|
|
2023-06-05 23:07:10 +00:00
|
|
|
// 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
|
2023-06-05 18:56:03 +00:00
|
|
|
|
2023-06-05 23:07:10 +00:00
|
|
|
// 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
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
// Every 50ms
|
2023-06-05 23:07:10 +00:00
|
|
|
if(currentMillis - lastMillis > 50){
|
2023-06-05 18:56:03 +00:00
|
|
|
// check to see if we got between 3 and 8 low pulses on the line
|
2023-06-05 23:07:10 +00:00
|
|
|
if(this->store_.obstructionLowCount >= 3 && this->store_.obstructionLowCount <= 8){
|
|
|
|
// obstructionCleared();
|
|
|
|
this->store_.obstructionState = 1;
|
|
|
|
|
|
|
|
// 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){
|
|
|
|
this->store_.obstructionState = 0;
|
|
|
|
// obstructionDetected();
|
|
|
|
}else{
|
2023-06-05 18:56:03 +00:00
|
|
|
// asleep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastMillis = currentMillis;
|
2023-06-05 21:37:57 +00:00
|
|
|
this->store_.obstructionLowCount = 0;
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
void RATGDOComponent::obstructionDetected()
|
|
|
|
{
|
|
|
|
static unsigned long lastInterruptTime = 0;
|
|
|
|
unsigned long interruptTime = millis();
|
|
|
|
// Anything less than 100ms is a bounce and is ignored
|
|
|
|
if (interruptTime - lastInterruptTime > 250) {
|
2023-06-05 21:38:21 +00:00
|
|
|
this->store_.doorIsObstructed = true;
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_obst_pin_->digital_write(true);
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Obstruction Detected");
|
|
|
|
}
|
|
|
|
lastInterruptTime = interruptTime;
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
void RATGDOComponent::obstructionCleared()
|
|
|
|
{
|
2023-06-05 21:38:57 +00:00
|
|
|
if (this->store_.doorIsObstructed) {
|
2023-06-05 21:38:21 +00:00
|
|
|
this->store_.doorIsObstructed = false;
|
2023-06-05 19:39:46 +00:00
|
|
|
this->status_obst_pin_->digital_write(false);
|
2023-06-05 18:56:03 +00:00
|
|
|
ESP_LOGD(TAG, "Obstruction Cleared");
|
|
|
|
}
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +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-05 22:28:50 +00:00
|
|
|
void RATGDOComponent::transmit(const unsigned char * payload)
|
2023-06-05 18:56:03 +00:00
|
|
|
{
|
2023-06-05 19:39:46 +00:00
|
|
|
this->output_gdo_pin_->digital_write(true); // pull the line high for 1305 micros so the
|
2023-06-05 18:56:03 +00:00
|
|
|
// door opener responds to the message
|
|
|
|
delayMicroseconds(1305);
|
2023-06-05 19:39:46 +00:00
|
|
|
this->output_gdo_pin_->digital_write(false); // bring the line low
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
delayMicroseconds(1260); // "LOW" pulse duration before the message start
|
2023-06-05 22:28:50 +00:00
|
|
|
this->swSerial.write(payload, CODE_LENGTH);
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
void RATGDOComponent::sync()
|
|
|
|
{
|
|
|
|
if (!this->useRollingCodes_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
getRollingCode("reboot1");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
delay(45);
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
getRollingCode("reboot2");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
delay(45);
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
getRollingCode("reboot3");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
delay(45);
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
getRollingCode("reboot4");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
delay(45);
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
getRollingCode("reboot5");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
delay(45);
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
getRollingCode("reboot6");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
delay(45);
|
2023-06-05 17:12:51 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
this->pref_.save(&this->rollingCodeCounter);
|
2023-06-05 17:13:01 +00:00
|
|
|
}
|
2023-06-05 18:56:03 +00:00
|
|
|
|
2023-06-05 19:04:41 +00:00
|
|
|
void RATGDOComponent::sendSyncCodes()
|
|
|
|
{
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(SYNC1);
|
2023-06-05 19:04:41 +00:00
|
|
|
delay(45);
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(SYNC2);
|
2023-06-05 19:04:41 +00:00
|
|
|
delay(45);
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(SYNC3);
|
2023-06-05 19:04:41 +00:00
|
|
|
delay(45);
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(SYNC4);
|
2023-06-05 19:04:41 +00:00
|
|
|
delay(45);
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
void RATGDOComponent::openDoor()
|
|
|
|
{
|
|
|
|
if (this->doorState == "open" || this->doorState == "opening") {
|
|
|
|
ESP_LOGD(TAG, "The door is already %s", doorState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->doorState = "opening"; // It takes a couple of pulses to detect
|
|
|
|
// opening/closing. by setting here, we can avoid
|
|
|
|
// bouncing from rapidly repeated commands
|
|
|
|
|
2023-06-05 22:32:04 +00:00
|
|
|
toggleDoor();
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RATGDOComponent::closeDoor()
|
|
|
|
{
|
|
|
|
if (this->doorState == "closed" || this->doorState == "closing") {
|
|
|
|
ESP_LOGD(TAG, "The door is already %s", this->doorState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->doorState = "closing"; // It takes a couple of pulses to detect
|
|
|
|
// opening/closing. by setting here, we can avoid
|
|
|
|
// bouncing from rapidly repeated commands
|
|
|
|
|
2023-06-05 22:32:04 +00:00
|
|
|
toggleDoor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RATGDOComponent::toggleDoor()
|
|
|
|
{
|
2023-06-05 18:56:03 +00:00
|
|
|
if (this->useRollingCodes_) {
|
|
|
|
getRollingCode("door1");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
delay(40);
|
|
|
|
|
|
|
|
getRollingCode("door2");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
this->pref_.save(&this->rollingCodeCounter);
|
|
|
|
} else {
|
2023-06-05 19:04:41 +00:00
|
|
|
sendSyncCodes();
|
2023-06-05 19:07:28 +00:00
|
|
|
ESP_LOGD(TAG, "door_code");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(DOOR_CODE);
|
2023-06-05 22:32:04 +00:00
|
|
|
}
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RATGDOComponent::toggleLight()
|
|
|
|
{
|
2023-06-05 21:38:57 +00:00
|
|
|
if (this->useRollingCodes_) {
|
2023-06-05 18:56:03 +00:00
|
|
|
getRollingCode("light");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(this->rollingCode);
|
2023-06-05 18:56:03 +00:00
|
|
|
this->pref_.save(&this->rollingCodeCounter);
|
|
|
|
} else {
|
2023-06-05 19:04:41 +00:00
|
|
|
sendSyncCodes();
|
2023-06-05 19:07:28 +00:00
|
|
|
ESP_LOGD(TAG, "light_code");
|
2023-06-05 22:28:50 +00:00
|
|
|
transmit(LIGHT_CODE);
|
2023-06-05 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-05 18:26:26 +00:00
|
|
|
|
2023-06-05 17:54:46 +00:00
|
|
|
} // namespace ratgdo
|
|
|
|
} // namespace esphome
|