From e1fda67378f724d745071f3cce1be4eac79b9bb3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 5 Jun 2023 18:19:52 -0500 Subject: [PATCH] fix --- components/ratgdo/__init__.py | 22 +++++--- components/ratgdo/ratgdo.cpp | 98 +++++++++++++++++++++++++++++++---- components/ratgdo/ratgdo.h | 39 ++------------ 3 files changed, 105 insertions(+), 54 deletions(-) diff --git a/components/ratgdo/__init__.py b/components/ratgdo/__init__.py index d275328..0385be6 100644 --- a/components/ratgdo/__init__.py +++ b/components/ratgdo/__init__.py @@ -14,6 +14,11 @@ CONF_ROLLING_CODES = "rolling_codes" CONF_OUTPUT_GDO = "output_gdo_pin" DEFAULT_OUTPUT_GDO = 2 # D4 ed control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini +CONF_INPUT_GDO = "input_gdo_pin" +DEFAULT_INPUT_GDO = 4 # D2 red control terminal / GarageDoorOpener (UART1 RX) pin is D2 on D1 Mini +CONF_INPUT_OBST = "input_obst_pin" +DEFAULT_INPUT_OBST = 13 # D7 black obstruction sensor terminal + CONF_TRIGGER_OPEN = "trigger_open_pin" DEFAULT_TRIGGER_OPEN = 14 # D5 dry contact for opening door CONF_TRIGGER_CLOSE = "trigger_close_pin" @@ -28,8 +33,6 @@ CONF_INPUT_RPM1 = "input_rpm1_pin" DEFAULT_INPUT_RPM1 = 5 # D1 RPM1 rotary encoder input OR reed switch if not soldering to the door opener logic board CONF_INPUT_RPM2 = "input_rpm2_pin" DEFAULT_INPUT_RPM2 = 4 # D2 RPM2 rotary encoder input OR not used if using reed switch -CONF_INPUT_OBST = "input_obst_pin" -DEFAULT_INPUT_OBST = 13 # D7 black obstruction sensor terminal CONFIG_SCHEMA = cv.Schema( @@ -37,6 +40,7 @@ CONFIG_SCHEMA = cv.Schema( cv.GenerateID(): cv.declare_id(RATGDO), cv.Optional(CONF_ROLLING_CODES, default=True): cv.boolean, cv.Optional(CONF_OUTPUT_GDO, default=DEFAULT_OUTPUT_GDO): pins.internal_gpio_input_pin_schema, + cv.Optional(CONF_INPUT_GDO, default=DEFAULT_INPUT_GDO): pins.internal_gpio_input_pin_schema, cv.Optional(CONF_TRIGGER_OPEN, default=DEFAULT_TRIGGER_OPEN): pins.internal_gpio_input_pin_schema, cv.Optional(CONF_TRIGGER_CLOSE, default=DEFAULT_TRIGGER_CLOSE): pins.internal_gpio_input_pin_schema, cv.Optional(CONF_TRIGGER_LIGHT, default=DEFAULT_TRIGGER_LIGHT): pins.internal_gpio_input_pin_schema, @@ -56,22 +60,24 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_OUTPUT_GDO]) cg.add(var.set_output_gdo_pin(pin)) + pin = await cg.gpio_pin_expression(config[CONF_INPUT_GDO]) + cg.add(var.set_input_gdo_pin(pin)) + pin = await cg.gpio_pin_expression(config[CONF_INPUT_OBST]) + cg.add(var.set_input_obst_pin(pin)) + pin = await cg.gpio_pin_expression(config[CONF_TRIGGER_OPEN]) cg.add(var.set_trigger_open_pin(pin)) pin = await cg.gpio_pin_expression(config[CONF_TRIGGER_CLOSE]) cg.add(var.set_trigger_close_pin(pin)) pin = await cg.gpio_pin_expression(config[CONF_TRIGGER_LIGHT]) cg.add(var.set_trigger_light_pin(pin)) + pin = await cg.gpio_pin_expression(config[CONF_STATUS_DOOR]) cg.add(var.set_status_door_pin(pin)) pin = await cg.gpio_pin_expression(config[CONF_STATUS_OBST]) cg.add(var.set_status_obst_pin(pin)) - pin = await cg.gpio_pin_expression(config[CONF_INPUT_RPM1]) - cg.add(var.set_input_rpm1_pin(pin)) - pin = await cg.gpio_pin_expression(config[CONF_INPUT_RPM2]) - cg.add(var.set_input_rpm2_pin(pin)) - pin = await cg.gpio_pin_expression(config[CONF_INPUT_OBST]) - cg.add(var.set_input_obst_pin(pin)) + + cg.add_library( name="secplus", diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 5732249..a4980a0 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -185,7 +185,7 @@ namespace ratgdo { this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT); - swSerial.begin(9600, SWSERIAL_8N1, this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin(), true); + this->swSerial.begin(9600, SWSERIAL_8N1, this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin(), true); 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); @@ -444,6 +444,49 @@ namespace ratgdo { } } + void RATGDOComponent::gdoStateLoop(){ + if(!this->swSerial.available()) return; + uint8_t serData = this->swSerial.read(); + + static uint32_t msgStart; + static bool reading = false; + static uint16_t byteCount = 0; + + if(!reading){ + // shift serial byte onto msg start + msgStart <<= 8; + msgStart |= serData; + + // truncate to 3 bytes + msgStart &= 0x00FFFFFF; + + // 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; + + reading = true; + return; + } + } + + if(reading){ + this->rxRollingCode[byteCount] = serData; + byteCount++; + + if(byteCount == 19){ + reading = false; + msgStart = 0; + byteCount = 0; + + readRollingCode(doorState, lightState, lockState, motionState, obstructionState); + } + } + } + + void RATGDOComponent::statusUpdateLoop(){ // initialize to unknown static uint8_t previousDoorState = 0; @@ -467,6 +510,31 @@ namespace ratgdo { previousObstructionState = this->store_.obstructionState; } + void RATGDOComponent::sendDoorStatus(){ + ESP_LOGD(TAG, "Door state %d", this->store_.doorState); + this->status_door_pin_->digital_write(this->store_.doorState == 1); + } + + void RATGDOComponent::sendLightStatus(){ + ESP_LOGD(TAG, "Light state %d", this->store_.lightState) + Serial.println(lightStates[lightState]); + } + + void RATGDOComponent::sendLockStatus(){ + ESP_LOGD(TAG, "Lock state %d", this->store_.lockState); + Serial.println(lockStates[lockState]); + } + + void RATGDOComponent::sendMotionStatus(){ + ESP_LOGD(TAG, "Motion state %d", this->store_.motionState); + motionState = 0; // reset motion state + } + + void RATGDOComponent::sendObstructionStatus(){ + ESP_LOGD(TAG, "Obstruction state %d", this->store_.obstructionState); + this->status_obst_pin_->digital_write(this->store_.obstructionState == 0); + } + void RATGDOComponent::obstructionDetected() { static unsigned long lastInterruptTime = 0; @@ -516,27 +584,27 @@ namespace ratgdo { getRollingCode("reboot1"); transmit(this->rollingCode); - delay(45); + delay(65); getRollingCode("reboot2"); transmit(this->rollingCode); - delay(45); + delay(65); getRollingCode("reboot3"); transmit(this->rollingCode); - delay(45); + delay(65); getRollingCode("reboot4"); transmit(this->rollingCode); - delay(45); + delay(65); getRollingCode("reboot5"); transmit(this->rollingCode); - delay(45); + delay(65); getRollingCode("reboot6"); transmit(this->rollingCode); - delay(45); + delay(65); this->pref_.save(&this->rollingCodeCounter); } @@ -544,13 +612,13 @@ namespace ratgdo { void RATGDOComponent::sendSyncCodes() { transmit(SYNC1); - delay(45); + delay(65); transmit(SYNC2); - delay(45); + delay(65); transmit(SYNC3); - delay(45); + delay(65); transmit(SYNC4); - delay(45); + delay(65); } void RATGDOComponent::openDoor() @@ -581,6 +649,14 @@ namespace ratgdo { toggleDoor(); } + void RATGDOComponent::stopDoor(){ + if(doorStates[doorState] == "opening" || doorStates[doorState] == "closing"){ + toggleDoor(); + }else{ + Serial.print("The door is not moving."); + } + } + void RATGDOComponent::toggleDoor() { if (this->useRollingCodes_) { diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 91a9d0a..fd8a70c 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -21,38 +21,6 @@ extern "C" { #include "secplus.h" } -static const uint8_t D0 = 16; -static const uint8_t D1 = 5; -static const uint8_t D2 = 4; -static const uint8_t D3 = 0; -static const uint8_t D4 = 2; -static const uint8_t D5 = 14; -static const uint8_t D6 = 12; -static const uint8_t D7 = 13; -static const uint8_t D8 = 15; -static const uint8_t D9 = 3; -static const uint8_t D10 = 1; - -#define CODE_LENGTH 19 // the length of each command sent to the door. -/********************************** PIN DEFINITIONS - * *****************************************/ -#define OUTPUT_GDO \ - D4 // red control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini -#define TRIGGER_OPEN D5 // dry contact for opening door -#define TRIGGER_CLOSE D6 // dry contact for closing door -#define TRIGGER_LIGHT \ - D3 // dry contact for triggering light (no discrete light commands, so toggle - // only) -#define STATUS_DOOR D0 // output door status, HIGH for open, LOW for closed -#define STATUS_OBST \ - D8 // output for obstruction status, HIGH for obstructed, LOW for clear -#define INPUT_RPM1 \ - D1 // RPM1 rotary encoder input OR reed switch if not soldering to the door - // opener logic board -#define INPUT_RPM2 \ - D2 // RPM2 rotary encoder input OR not used if using reed switch -#define INPUT_OBST D7 // black obstruction sensor terminal - namespace esphome { namespace ratgdo { @@ -109,14 +77,15 @@ namespace ratgdo { // [online|offline|opening|open|closing|closed|obstructed|clear|reed_open|reed_closed] void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; }; + void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; }; + void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }; + void set_trigger_open_pin(InternalGPIOPin* pin) { this->trigger_open_pin_ = pin; }; void set_trigger_close_pin(InternalGPIOPin* pin) { this->trigger_close_pin_ = pin; }; void set_trigger_light_pin(InternalGPIOPin* pin) { this->trigger_light_pin_ = pin; }; + void set_status_door_pin(InternalGPIOPin* pin) { this->status_door_pin_ = pin; }; void set_status_obst_pin(InternalGPIOPin* pin) { this->status_obst_pin_ = pin; }; - void set_input_rpm1_pin(InternalGPIOPin* pin) { this->input_rpm1_pin_ = pin; }; - void set_input_rpm2_pin(InternalGPIOPin* pin) { this->input_rpm2_pin_ = pin; }; - void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }; /********************************** FUNCTION DECLARATION * *****************************************/