This commit is contained in:
J. Nick Koston 2023-06-05 14:39:46 -05:00
parent dbf676d195
commit 39e0aa4f8d
No known key found for this signature in database
3 changed files with 113 additions and 33 deletions

View File

@ -1,6 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome import pins, automation
DEPENDENCIES = ["preferences"]
@ -10,10 +11,40 @@ RATGDO = ratgdo_ns.class_("RATGDO", cg.Component)
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_TRIGGER_OPEN = "trigger_open_pin"
DEFAULT_TRIGGER_OPEN = 14 # D5 dry contact for opening door
CONF_TRIGGER_CLOSE = "trigger_close_pin"
DEFAULT_TRIGGER_CLOSE = 12 # D6 dry contact for closing door
CONF_TRIGGER_LIGHT = "trigger_light_pin"
DEFAULT_TRIGGER_LIGHT = 0 # D3 dry contact for triggering light (no discrete light commands, so toggle only)
CONF_STATUS_DOOR = "status_door_pin"
DEFAULT_STATUS_DOOR = 16 # D0 output door status, HIGH for open, LOW for closed
CONF_STATUS_OBST = "status_obst_pin"
DEFAULT_STATUS_OBST = 15 # D8 output for obstruction status, HIGH for obstructed, LOW for clear
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(
{
cv.GenerateID(): cv.declare_id(RATGDO),
cv.Optional(CONF_ROLLING_CODES, default=True): cv.boolean,
cv.Required(CONF_OUTPUT_GDO, default=DEFAULT_OUTPUT_GDO): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_TRIGGER_OPEN, default=DEFAULT_TRIGGER_OPEN): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_TRIGGER_CLOSE, default=DEFAULT_TRIGGER_CLOSE): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_TRIGGER_LIGHT, default=DEFAULT_TRIGGER_LIGHT): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_STATUS_DOOR, default=DEFAULT_STATUS_DOOR): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_STATUS_OBST, default=DEFAULT_STATUS_OBST): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_INPUT_RPM1, default=DEFAULT_INPUT_RPM1): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_INPUT_RPM2, default=DEFAULT_INPUT_RPM2): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_INPUT_OBST, default=DEFAULT_INPUT_OBST): pins.internal_gpio_input_pin_schema,
}
).extend(cv.COMPONENT_SCHEMA)
@ -22,6 +53,26 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(var.set_rolling_codes(config[CONF_ROLLING_CODES]))
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_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))
cg.add_library(
name="secplus",
repository="https://github.com/bdraco/secplus",

View File

@ -41,25 +41,35 @@ namespace ratgdo {
this->rollingCodeCounter = 0;
}
this->swSerial.begin(9600, SWSERIAL_8N2, -1, OUTPUT_GDO, true);
pinMode(TRIGGER_OPEN, INPUT_PULLUP);
pinMode(TRIGGER_CLOSE, INPUT_PULLUP);
pinMode(TRIGGER_LIGHT, INPUT_PULLUP);
pinMode(STATUS_DOOR, OUTPUT);
pinMode(STATUS_OBST, OUTPUT);
pinMode(INPUT_RPM1,
INPUT_PULLUP); // set to pullup to add support for reed switches
pinMode(INPUT_RPM2,
INPUT_PULLUP); // make sure pin doesn't float when using reed switch
// and fire interrupt by mistake
pinMode(INPUT_OBST, INPUT);
this->output_gdo_pin_->setup();
this->trigger_open_pin_->setup();
this->trigger_close_pin_->setup();
this->trigger_light_pin_->setup();
this->status_door_pin_->setup();
this->status_obst_pin_->setup();
this->input_rpm1_pin_->setup();
this->input_rpm2_pin_->setup();
this->input_obst_pin_->setup();
attachInterrupt(TRIGGER_OPEN, &RATGDOComponent::isrDoorOpen, CHANGE);
attachInterrupt(TRIGGER_CLOSE, &RATGDOComponent::isrDoorClose, CHANGE);
attachInterrupt(TRIGGER_LIGHT, &RATGDOComponent::isrLight, CHANGE);
attachInterrupt(INPUT_OBST, &RATGDOComponent::isrObstruction, CHANGE);
attachInterrupt(INPUT_RPM1, &RATGDOComponent::isrRPM1, RISING);
attachInterrupt(INPUT_RPM2, &RATGDOComponent::isrRPM2, RISING);
this->swSerial.begin(9600, SWSERIAL_8N2, -1, this->output_gdo_pin_->get_pin(), true);
this->trigger_open_pin_->set_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->trigger_close_pin_->set_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->trigger_light_pin_->set_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->status_door_pin_->set_mode(gpio::FLAG_OUTPUT);
this->status_obst_pin_->set_mode(gpio::FLAG_OUTPUT);
this->input_rpm1_pin_->set_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); // set to pullup to add support for reed switches
this->input_rpm2_pin_->set_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);// make sure pin doesn't float when using reed switch
// and fire interrupt by mistake
this->input_obst_pin_->set_mode(gpio::FLAG_INPUT);
thiis->trigger_open_pin_->attach_interrupt(RATGDOComponent::isrDoorOpen, this, gpio::INTERRUPT_ANY_EDGE);
this->trigger_close_pin_->attach_interrupt(RATGDOComponent::isrDoorClose, this, gpio::INTERRUPT_ANY_EDGE);
this->trigger_light_pin_->attach_interrupt(RATGDOComponent::isrLight, this, gpio::INTERRUPT_ANY_EDGE);
this->input_obst_pin_->attach_interrupt(RATGDOComponent::isrObstruction, this, gpio::INTERRUPT_ANY_EDGE);
this->input_rpm1_pin_->attach_interrupt(RATGDOComponent::isrRPM1, this, gpio::INTERRUPT_RISING_EDGE);
this->input_rpm2_pin_->attach_interrupt(RATGDOComponent::isrRPM2, this, gpio::INTERRUPT_RISING_EDGE);
if (this->useRollingCodes_) {
ESP_LOGD(TAG, "Syncing rolling code counter after reboot...");
@ -155,16 +165,16 @@ namespace ratgdo {
// This may need to be debounced, but so far in testing I haven't detected any
// bounces
if (!rotaryEncoderDetected) {
if (digitalRead(INPUT_RPM1) == LOW) {
if (!this->input_rpm1_pin_->digital_read()) {
if (doorState != "reed_closed") {
ESP_LOGD(TAG, "Reed switch closed");
this->doorState = "reed_closed";
digitalWrite(STATUS_DOOR, HIGH);
this->status_door_pin_->digital_write(true);
}
} else if (doorState != "reed_open") {
ESP_LOGD(TAG, "Reed switch open");
this->doorState = "reed_open";
digitalWrite(STATUS_DOOR, LOW);
this->status_door_pin_->digital_write(false);
}
}
// end reed switch handling
@ -201,14 +211,14 @@ namespace ratgdo {
if (this->doorState == "closing") {
this->doorState = "closed";
ESP_LOGD(TAG, "Closed");
digitalWrite(STATUS_DOOR, LOW);
this->status_door_pin_->digital_write(false);
}
// 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");
digitalWrite(STATUS_DOOR, HIGH);
this->status_door_pin_->digital_write(true);
}
}
@ -229,7 +239,7 @@ namespace ratgdo {
return;
if (strcmp(type, "openDoor") == 0) {
if (digitalRead(TRIGGER_OPEN) == LOW) {
if (!this->trigger_open_pin_->digital_read()) {
// save the time of the falling edge
lastOpenDoorTime = currentMillis;
} else if (currentMillis - lastOpenDoorTime > 500 && currentMillis - lastOpenDoorTime < 10000) {
@ -240,7 +250,7 @@ namespace ratgdo {
}
if (strcmp(type, "closeDoor") == 0) {
if (digitalRead(TRIGGER_CLOSE) == LOW) {
if (!this->trigger_close_pin_->digital_read()) {
// save the time of the falling edge
lastCloseDoorTime = currentMillis;
} else if (currentMillis - lastCloseDoorTime > 500 && currentMillis - lastCloseDoorTime < 10000) {
@ -251,7 +261,7 @@ namespace ratgdo {
}
if (strcmp(type, "toggleLight") == 0) {
if (digitalRead(TRIGGER_LIGHT) == LOW) {
if (!this->trigger_light_pin_->digital_read()) {
// save the time of the falling edge
lastToggleLightTime = currentMillis;
} else if (currentMillis - lastToggleLightTime > 500 && currentMillis - lastToggleLightTime < 10000) {
@ -306,7 +316,7 @@ namespace ratgdo {
// If the RPM1 state is different from the RPM2 state, then the door is
// opening
if (digitalRead(INPUT_RPM1)) {
if (this->input_rpm1_pin_->digital_read()) {
this->doorPositionCounter--;
} else {
this->doorPositionCounter++;
@ -338,7 +348,7 @@ namespace ratgdo {
/*************************** OBSTRUCTION DETECTION ***************************/
void IRAM_ATTR RATGDOComponent::isrObstruction()
{
if (digitalRead(INPUT_OBST)) {
if (this->input_obst_pin_->digital_read()) {
this->lastObstructionHigh = millis();
} else {
this->obstructionLowCount++;
@ -368,7 +378,7 @@ namespace ratgdo {
} else if (this->obstructionLowCount == 0) {
// if the line is high and the last high pulse was more than 70ms ago,
// then there is an obstruction present
if (digitalRead(INPUT_OBST) && currentMillis - this->lastObstructionHigh > 70) {
if ((this->input_obst_pin_->digital_read() && currentMillis - this->lastObstructionHigh > 70) {
obstructionDetected();
} else {
// asleep
@ -387,7 +397,7 @@ namespace ratgdo {
// Anything less than 100ms is a bounce and is ignored
if (interruptTime - lastInterruptTime > 250) {
this->doorIsObstructed = true;
digitalWrite(STATUS_OBST, HIGH);
this->status_obst_pin_->digital_write(true);
ESP_LOGD(TAG, "Obstruction Detected");
}
lastInterruptTime = interruptTime;
@ -397,7 +407,7 @@ namespace ratgdo {
{
if (this->doorIsObstructed) {
this->doorIsObstructed = false;
digitalWrite(STATUS_OBST, LOW);
this->status_obst_pin_->digital_write(false);
ESP_LOGD(TAG, "Obstruction Cleared");
}
}
@ -413,10 +423,10 @@ namespace ratgdo {
*/
void RATGDOComponent::transmit(const unsigned char * payload, unsigned int length)
{
digitalWrite(OUTPUT_GDO, HIGH); // pull the line high for 1305 micros so the
this->output_gdo_pin_->digital_write(true); // pull the line high for 1305 micros so the
// door opener responds to the message
delayMicroseconds(1305);
digitalWrite(OUTPUT_GDO, LOW); // bring the line low
this->output_gdo_pin_->digital_write(false); // bring the line low
delayMicroseconds(1260); // "LOW" pulse duration before the message start
this->swSerial.write(payload, length);

View File

@ -79,6 +79,16 @@ namespace ratgdo {
bool rpm1Pulsed = false; // did rpm1 get a pulse or not - eliminates an issue when the
// sensor is parked on a high pulse which fires rpm2 isr
void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_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
* *****************************************/
void set_rolling_codes(bool useRollingCodes);
@ -112,6 +122,15 @@ namespace ratgdo {
protected:
ESPPreferenceObject pref_;
bool useRollingCodes_;
InternalGPIOPin* output_gdo_pin;
InternalGPIOPin* trigger_open_pin;
InternalGPIOPin* trigger_close_pin;
InternalGPIOPin* trigger_light_pin;
InternalGPIOPin* status_door_pin;
InternalGPIOPin* status_obst_pin;
InternalGPIOPin* input_rpm1_pin;
InternalGPIOPin* input_rpm2_pin;
InternalGPIOPin* input_obst_pin;
}; // RATGDOComponent