This commit is contained in:
J. Nick Koston 2023-06-05 20:06:12 -05:00
parent a6125240b0
commit 1c09148ea1
No known key found for this signature in database
2 changed files with 444 additions and 450 deletions

View File

@ -13,8 +13,6 @@
#include "ratgdo.h" #include "ratgdo.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/component.h"
namespace esphome { namespace esphome {
namespace ratgdo { namespace ratgdo {
@ -92,10 +90,7 @@ namespace ratgdo {
} }
} }
class RATGDOComponent : public uart::UARTDevice, public Component { void RATGDOComponent::setup()
public:
void setup() override
{ {
this->pref_ = global_preferences->make_preference<int>(734874333U); this->pref_ = global_preferences->make_preference<int>(734874333U);
if (!this->pref_.load(&this->rollingCodeCounter)) { if (!this->pref_.load(&this->rollingCodeCounter)) {
@ -142,7 +137,7 @@ namespace ratgdo {
// reboot/sync to the opener on startup // reboot/sync to the opener on startup
} }
void loop() override void RATGDOComponent::loop()
{ {
ESP_LOGD(TAG, "loop rollingCodeCounter: %d", this->rollingCodeCounter); ESP_LOGD(TAG, "loop rollingCodeCounter: %d", this->rollingCodeCounter);
obstructionLoop(); obstructionLoop();
@ -152,7 +147,7 @@ namespace ratgdo {
// ESP_LOGD(TAG, "Door State: %s", this->doorState.c_str()); // ESP_LOGD(TAG, "Door State: %s", this->doorState.c_str());
} }
void readRollingCode(uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction) void RATGDOComponent::readRollingCode(uint8_t& door, uint8_t& light, uint8_t& lock, uint8_t& motion, uint8_t& obstruction)
{ {
uint32_t rolling = 0; uint32_t rolling = 0;
uint64_t fixed = 0; uint64_t fixed = 0;
@ -186,7 +181,7 @@ namespace ratgdo {
} }
} }
void getRollingCode(const char* command) void RATGDOComponent::getRollingCode(const char* command)
{ {
uint64_t id = 0x539; uint64_t id = 0x539;
@ -240,7 +235,7 @@ namespace ratgdo {
return; return;
} }
void printRollingCode() void RATGDOComponent::printRollingCode()
{ {
for (int i = 0; i < CODE_LENGTH; i++) { for (int i = 0; i < CODE_LENGTH; i++) {
if (this->txRollingCode[i] <= 0x0f) if (this->txRollingCode[i] <= 0x0f)
@ -250,7 +245,7 @@ namespace ratgdo {
} }
// handle changes to the dry contact state // handle changes to the dry contact state
void dryContactLoop() void RATGDOComponent::dryContactLoop()
{ {
if (this->store_.dryContactDoorOpen) { if (this->store_.dryContactDoorOpen) {
ESP_LOGD(TAG, "Dry Contact: open the door"); ESP_LOGD(TAG, "Dry Contact: open the door");
@ -272,7 +267,8 @@ namespace ratgdo {
} }
/*************************** OBSTRUCTION DETECTION ***************************/ /*************************** OBSTRUCTION DETECTION ***************************/
void obstructionLoop()
void RATGDOComponent::obstructionLoop()
{ {
long currentMillis = millis(); long currentMillis = millis();
static unsigned long lastMillis = 0; static unsigned long lastMillis = 0;
@ -306,7 +302,7 @@ namespace ratgdo {
} }
} }
void gdoStateLoop() void RATGDOComponent::gdoStateLoop()
{ {
if (!this->swSerial.available()) { if (!this->swSerial.available()) {
// ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin()); // ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin());
@ -352,7 +348,7 @@ namespace ratgdo {
} }
} }
void statusUpdateLoop() void RATGDOComponent::statusUpdateLoop()
{ {
// initialize to unknown // initialize to unknown
static uint8_t previousDoorState = 0; static uint8_t previousDoorState = 0;
@ -380,29 +376,29 @@ namespace ratgdo {
previousObstructionState = this->store_.obstructionState; previousObstructionState = this->store_.obstructionState;
} }
void sendDoorStatus() void RATGDOComponent::sendDoorStatus()
{ {
ESP_LOGD(TAG, "Door state %d", this->store_.doorState); ESP_LOGD(TAG, "Door state %d", this->store_.doorState);
this->status_door_pin_->digital_write(this->store_.doorState == 1); this->status_door_pin_->digital_write(this->store_.doorState == 1);
} }
void sendLightStatus() void RATGDOComponent::sendLightStatus()
{ {
ESP_LOGD(TAG, "Light state %d", this->store_.lightState); ESP_LOGD(TAG, "Light state %d", this->store_.lightState);
} }
void sendLockStatus() void RATGDOComponent::sendLockStatus()
{ {
ESP_LOGD(TAG, "Lock state %d", this->store_.lockState); ESP_LOGD(TAG, "Lock state %d", this->store_.lockState);
} }
void sendMotionStatus() void RATGDOComponent::sendMotionStatus()
{ {
ESP_LOGD(TAG, "Motion state %d", this->store_.motionState); ESP_LOGD(TAG, "Motion state %d", this->store_.motionState);
this->store_.motionState = 0; // reset motion state this->store_.motionState = 0; // reset motion state
} }
void sendObstructionStatus() void RATGDOComponent::sendObstructionStatus()
{ {
ESP_LOGD(TAG, "Obstruction state %d", this->store_.obstructionState); ESP_LOGD(TAG, "Obstruction state %d", this->store_.obstructionState);
this->status_obst_pin_->digital_write(this->store_.obstructionState == 0); this->status_obst_pin_->digital_write(this->store_.obstructionState == 0);
@ -417,7 +413,7 @@ namespace ratgdo {
* The opener requires a specific duration low/high pulse before it will accept * The opener requires a specific duration low/high pulse before it will accept
* a message * a message
*/ */
void transmit(const unsigned char* payload) void RATGDOComponent::transmit(const unsigned char* payload)
{ {
this->output_gdo_pin_->digital_write(true); // 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 // door opener responds to the message
@ -428,7 +424,7 @@ namespace ratgdo {
this->swSerial.write(payload, CODE_LENGTH); this->swSerial.write(payload, CODE_LENGTH);
} }
void sync() void RATGDOComponent::sync()
{ {
getRollingCode("reboot1"); getRollingCode("reboot1");
transmit(this->txRollingCode); transmit(this->txRollingCode);
@ -457,7 +453,7 @@ namespace ratgdo {
this->pref_.save(&this->rollingCodeCounter); this->pref_.save(&this->rollingCodeCounter);
} }
void openDoor() void RATGDOComponent::openDoor()
{ {
if (this->doorStates[this->store_.doorState] == "open" || doorStates[this->store_.doorState] == "opening") { if (this->doorStates[this->store_.doorState] == "open" || doorStates[this->store_.doorState] == "opening") {
ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]); ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]);
@ -466,7 +462,7 @@ namespace ratgdo {
toggleDoor(); toggleDoor();
} }
void closeDoor() void RATGDOComponent::closeDoor()
{ {
if (this->doorStates[this->store_.doorState] == "closed" || doorStates[this->store_.doorState] == "closing") { if (this->doorStates[this->store_.doorState] == "closed" || doorStates[this->store_.doorState] == "closing") {
ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]); ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]);
@ -475,7 +471,7 @@ namespace ratgdo {
toggleDoor(); toggleDoor();
} }
void stopDoor() void RATGDOComponent::stopDoor()
{ {
if (this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing") { if (this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing") {
toggleDoor(); toggleDoor();
@ -484,7 +480,7 @@ namespace ratgdo {
} }
} }
void toggleDoor() void RATGDOComponent::toggleDoor()
{ {
getRollingCode("door1"); getRollingCode("door1");
transmit(this->txRollingCode); transmit(this->txRollingCode);
@ -494,7 +490,7 @@ namespace ratgdo {
this->pref_.save(&this->rollingCodeCounter); this->pref_.save(&this->rollingCodeCounter);
} }
void lightOn() void RATGDOComponent::lightOn()
{ {
if (this->lightStates[this->store_.lightState] == "on") { if (this->lightStates[this->store_.lightState] == "on") {
ESP_LOGD(TAG, "already on"); ESP_LOGD(TAG, "already on");
@ -503,7 +499,7 @@ namespace ratgdo {
} }
} }
void lightOff() void RATGDOComponent::lightOff()
{ {
if (this->lightStates[this->store_.lightState] == "off") { if (this->lightStates[this->store_.lightState] == "off") {
ESP_LOGD(TAG, "already off"); ESP_LOGD(TAG, "already off");
@ -512,13 +508,13 @@ namespace ratgdo {
} }
} }
void toggleLight() void RATGDOComponent::toggleLight()
{ {
sendCommand("light"); sendCommand("light");
} }
// Lock functions // Lock functions
void lock() void RATGDOComponent::lock()
{ {
if (this->lockStates[this->store_.lockState] == "locked") { if (this->lockStates[this->store_.lockState] == "locked") {
ESP_LOGD(TAG, "already locked"); ESP_LOGD(TAG, "already locked");
@ -527,7 +523,7 @@ namespace ratgdo {
} }
} }
void unlock() void RATGDOComponent::unlock()
{ {
if (this->lockStates[this->store_.lockState] == "unlocked") { if (this->lockStates[this->store_.lockState] == "unlocked") {
ESP_LOGD(TAG, "already unlocked"); ESP_LOGD(TAG, "already unlocked");
@ -536,17 +532,17 @@ namespace ratgdo {
} }
} }
void toggleLock() void RATGDOComponent::toggleLock()
{ {
sendCommand("lock"); sendCommand("lock");
} }
void sendCommand(const char* command) void RATGDOComponent::sendCommand(const char* command)
{ {
getRollingCode(command); getRollingCode(command);
transmit(this->txRollingCode); transmit(this->txRollingCode);
this->pref_.save(&this->rollingCodeCounter); this->pref_.save(&this->rollingCodeCounter);
} }
}
} // namespace ratgdo } // namespace ratgdo
} // namespace esphome } // namespace esphome

View File

@ -12,12 +12,11 @@
************************************/ ************************************/
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/core/gpio.h"
#include "esphome/core/preferences.h"
#include "esphome/core/log.h"
#include "esphome/components/uart/uart.h" #include "esphome/components/uart/uart.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/gpio.h"
#include "esphome/core/log.h"
#include "esphome/core/preferences.h"
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
extern "C" { extern "C" {
@ -29,7 +28,6 @@ extern "C" {
namespace esphome { namespace esphome {
namespace ratgdo { namespace ratgdo {
struct RATGDOStore { struct RATGDOStore {
ISRInternalGPIOPin input_obst; ISRInternalGPIOPin input_obst;