This commit is contained in:
J. Nick Koston 2023-06-05 19:59:20 -05:00
parent b15063763a
commit 795ed02637
No known key found for this signature in database
1 changed files with 160 additions and 138 deletions

View File

@ -14,7 +14,6 @@
#include "ratgdo.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ratgdo {
@ -23,7 +22,8 @@ namespace ratgdo {
/*************************** DRY CONTACT CONTROL OF LIGHT & DOOR
* ***************************/
void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore *arg) {
void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore* arg)
{
static unsigned long lastOpenDoorTime = 0;
unsigned long currentMillis = millis();
@ -41,7 +41,8 @@ namespace ratgdo {
}
}
void IRAM_ATTR HOT RATGDOStore::isrDoorClose(RATGDOStore *arg) {
void IRAM_ATTR HOT RATGDOStore::isrDoorClose(RATGDOStore* arg)
{
static unsigned long lastCloseDoorTime = 0;
unsigned long currentMillis = millis();
@ -59,7 +60,8 @@ namespace ratgdo {
}
}
void IRAM_ATTR HOT RATGDOStore::isrLight(RATGDOStore *arg) {
void IRAM_ATTR HOT RATGDOStore::isrLight(RATGDOStore* arg)
{
static unsigned long lastToggleLightTime = 0;
unsigned long currentMillis = millis();
@ -90,9 +92,12 @@ namespace ratgdo {
class RATGDOComponent : public Component, public UARTDevice {
public:
RATGDOComponent(UARTComponent *parent) : UARTDevice(parent) {}
RATGDOComponent(UARTComponent* parent)
: UARTDevice(parent)
{
}
void setup()
void setup() override
{
this->pref_ = global_preferences->make_preference<int>(734874333U);
if (!this->pref_.load(&this->rollingCodeCounter)) {
@ -137,10 +142,9 @@ namespace ratgdo {
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
}
void loop()
void loop() override
{
ESP_LOGD(TAG, "loop rollingCodeCounter: %d", this->rollingCodeCounter);
obstructionLoop();
@ -150,7 +154,8 @@ namespace ratgdo {
// 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 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;
@ -303,7 +308,8 @@ namespace ratgdo {
}
}
void gdoStateLoop(){
void gdoStateLoop()
{
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());
return;
@ -348,18 +354,22 @@ namespace ratgdo {
}
}
void statusUpdateLoop(){
void statusUpdateLoop()
{
// initialize to unknown
static uint8_t previousDoorState = 0;
static uint8_t previousLightState = 2;
static uint8_t previousLockState = 2;
static uint8_t previousObstructionState = 2;
if(this->store_.doorState != previousDoorState) sendDoorStatus();
if(this->store_.lightState != previousLightState) sendLightStatus();
if(this->store_.lockState != previousLockState) sendLockStatus();
if(this->store_.obstructionState != previousObstructionState) sendObstructionStatus();
if (this->store_.doorState != previousDoorState)
sendDoorStatus();
if (this->store_.lightState != previousLightState)
sendLightStatus();
if (this->store_.lockState != previousLockState)
sendLockStatus();
if (this->store_.obstructionState != previousObstructionState)
sendObstructionStatus();
if (this->store_.motionState == 1) {
sendMotionStatus();
@ -372,25 +382,30 @@ namespace ratgdo {
previousObstructionState = this->store_.obstructionState;
}
void sendDoorStatus(){
void sendDoorStatus()
{
ESP_LOGD(TAG, "Door state %d", this->store_.doorState);
this->status_door_pin_->digital_write(this->store_.doorState == 1);
}
void sendLightStatus(){
void sendLightStatus()
{
ESP_LOGD(TAG, "Light state %d", this->store_.lightState);
}
void sendLockStatus(){
void sendLockStatus()
{
ESP_LOGD(TAG, "Lock state %d", this->store_.lockState);
}
void sendMotionStatus(){
void sendMotionStatus()
{
ESP_LOGD(TAG, "Motion state %d", this->store_.motionState);
this->store_.motionState = 0; // reset motion state
}
void sendObstructionStatus(){
void sendObstructionStatus()
{
ESP_LOGD(TAG, "Obstruction state %d", this->store_.obstructionState);
this->status_obst_pin_->digital_write(this->store_.obstructionState == 0);
}
@ -462,7 +477,8 @@ namespace ratgdo {
toggleDoor();
}
void stopDoor(){
void stopDoor()
{
if (this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing") {
toggleDoor();
} else {
@ -478,10 +494,10 @@ namespace ratgdo {
getRollingCode("door2");
transmit(this->txRollingCode);
this->pref_.save(&this->rollingCodeCounter);
}
void lightOn(){
void lightOn()
{
if (this->lightStates[this->store_.lightState] == "on") {
ESP_LOGD(TAG, "already on");
} else {
@ -489,7 +505,8 @@ namespace ratgdo {
}
}
void lightOff(){
void lightOff()
{
if (this->lightStates[this->store_.lightState] == "off") {
ESP_LOGD(TAG, "already off");
} else {
@ -497,12 +514,14 @@ namespace ratgdo {
}
}
void toggleLight(){
void toggleLight()
{
sendCommand("light");
}
// Lock functions
void lock(){
void lock()
{
if (this->lockStates[this->store_.lockState] == "locked") {
ESP_LOGD(TAG, "already locked");
} else {
@ -510,7 +529,8 @@ namespace ratgdo {
}
}
void unlock(){
void unlock()
{
if (this->lockStates[this->store_.lockState] == "unlocked") {
ESP_LOGD(TAG, "already unlocked");
} else {
@ -518,11 +538,13 @@ namespace ratgdo {
}
}
void toggleLock(){
void toggleLock()
{
sendCommand("lock");
}
void sendCommand(const char* command){
void sendCommand(const char* command)
{
getRollingCode(command);
transmit(this->txRollingCode);
this->pref_.save(&this->rollingCodeCounter);