fix
This commit is contained in:
parent
b15063763a
commit
795ed02637
|
@ -14,72 +14,74 @@
|
|||
#include "ratgdo.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
|
||||
namespace esphome {
|
||||
namespace ratgdo {
|
||||
|
||||
static const char* const TAG = "ratgdo";
|
||||
static const int STARTUP_DELAY = 2000; // delay before enabling interrupts
|
||||
static const int STARTUP_DELAY = 2000; // delay before enabling interrupts
|
||||
|
||||
/*************************** DRY CONTACT CONTROL OF LIGHT & DOOR
|
||||
* ***************************/
|
||||
void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore *arg) {
|
||||
static unsigned long lastOpenDoorTime = 0;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
// Prevent ISR during the first 2 seconds after reboot
|
||||
if (currentMillis < STARTUP_DELAY)
|
||||
return;
|
||||
|
||||
if (!arg->trigger_open.digital_read()) {
|
||||
// save the time of the falling edge
|
||||
lastOpenDoorTime = currentMillis;
|
||||
} else if (currentMillis - lastOpenDoorTime > 500 && currentMillis - 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) {
|
||||
static unsigned long lastCloseDoorTime = 0;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
// Prevent ISR during the first 2 seconds after reboot
|
||||
if (currentMillis < STARTUP_DELAY)
|
||||
return;
|
||||
|
||||
if (!arg->trigger_close.digital_read()) {
|
||||
// save the time of the falling edge
|
||||
lastCloseDoorTime = currentMillis;
|
||||
} else if (currentMillis - lastCloseDoorTime > 500 && currentMillis - 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) {
|
||||
static unsigned long lastToggleLightTime = 0;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
// Prevent ISR during the first 2 seconds after reboot
|
||||
if (currentMillis < STARTUP_DELAY)
|
||||
return;
|
||||
|
||||
if (!arg->trigger_light.digital_read()) {
|
||||
// save the time of the falling edge
|
||||
lastToggleLightTime = currentMillis;
|
||||
} else if (currentMillis - lastToggleLightTime > 500 && currentMillis - lastToggleLightTime < 10000) {
|
||||
// now see if the rising edge was between 500ms and 10 seconds after the
|
||||
// falling edge
|
||||
arg->dryContactToggleLight = true;
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore *arg)
|
||||
void IRAM_ATTR HOT RATGDOStore::isrDoorOpen(RATGDOStore* arg)
|
||||
{
|
||||
if (arg->input_obst.digital_read()) {
|
||||
static unsigned long lastOpenDoorTime = 0;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
// Prevent ISR during the first 2 seconds after reboot
|
||||
if (currentMillis < STARTUP_DELAY)
|
||||
return;
|
||||
|
||||
if (!arg->trigger_open.digital_read()) {
|
||||
// save the time of the falling edge
|
||||
lastOpenDoorTime = currentMillis;
|
||||
} else if (currentMillis - lastOpenDoorTime > 500 && currentMillis - 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)
|
||||
{
|
||||
static unsigned long lastCloseDoorTime = 0;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
// Prevent ISR during the first 2 seconds after reboot
|
||||
if (currentMillis < STARTUP_DELAY)
|
||||
return;
|
||||
|
||||
if (!arg->trigger_close.digital_read()) {
|
||||
// save the time of the falling edge
|
||||
lastCloseDoorTime = currentMillis;
|
||||
} else if (currentMillis - lastCloseDoorTime > 500 && currentMillis - 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)
|
||||
{
|
||||
static unsigned long lastToggleLightTime = 0;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
// Prevent ISR during the first 2 seconds after reboot
|
||||
if (currentMillis < STARTUP_DELAY)
|
||||
return;
|
||||
|
||||
if (!arg->trigger_light.digital_read()) {
|
||||
// save the time of the falling edge
|
||||
lastToggleLightTime = currentMillis;
|
||||
} else if (currentMillis - lastToggleLightTime > 500 && currentMillis - lastToggleLightTime < 10000) {
|
||||
// now see if the rising edge was between 500ms and 10 seconds after the
|
||||
// falling edge
|
||||
arg->dryContactToggleLight = true;
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR HOT RATGDOStore::isrObstruction(RATGDOStore* arg)
|
||||
{
|
||||
if (arg->input_obst.digital_read()) {
|
||||
ESP_LOGD(TAG, "isrObstruction HIGH");
|
||||
arg->lastObstructionHigh = millis();
|
||||
} else {
|
||||
|
@ -89,10 +91,13 @@ namespace ratgdo {
|
|||
}
|
||||
|
||||
class RATGDOComponent : public Component, public UARTDevice {
|
||||
public:
|
||||
RATGDOComponent(UARTComponent *parent) : UARTDevice(parent) {}
|
||||
public:
|
||||
RATGDOComponent(UARTComponent* parent)
|
||||
: UARTDevice(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void setup()
|
||||
void setup() override
|
||||
{
|
||||
this->pref_ = global_preferences->make_preference<int>(734874333U);
|
||||
if (!this->pref_.load(&this->rollingCodeCounter)) {
|
||||
|
@ -123,8 +128,8 @@ namespace ratgdo {
|
|||
this->status_door_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
||||
this->status_obst_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
||||
|
||||
//this->output_gdo_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
||||
//this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
// this->output_gdo_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
||||
// this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
|
||||
|
||||
this->swSerial.begin(9600, SWSERIAL_8N1, this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin(), true);
|
||||
|
@ -137,20 +142,20 @@ 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();
|
||||
gdoStateLoop();
|
||||
dryContactLoop();
|
||||
statusUpdateLoop();
|
||||
//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 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;
|
||||
|
@ -168,17 +173,17 @@ namespace ratgdo {
|
|||
byte1 = (data >> 16) & 0xff;
|
||||
byte2 = (data >> 24) & 0xff;
|
||||
|
||||
if(cmd == 0x81){
|
||||
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){
|
||||
} else if (cmd == 0x281) {
|
||||
light ^= 1; // toggle bit
|
||||
}else if(cmd == 0x84){
|
||||
}else if(cmd == 0x285){
|
||||
} else if (cmd == 0x84) {
|
||||
} else if (cmd == 0x285) {
|
||||
motion = 1; // toggle bit
|
||||
}
|
||||
}
|
||||
|
@ -190,34 +195,34 @@ namespace ratgdo {
|
|||
uint64_t fixed = 0;
|
||||
uint32_t data = 0;
|
||||
|
||||
if(strcmp(command,"reboot1") == 0){
|
||||
if (strcmp(command, "reboot1") == 0) {
|
||||
fixed = 0x400000000;
|
||||
data = 0x0000618b;
|
||||
}else if(strcmp(command,"reboot2") == 0){
|
||||
} else if (strcmp(command, "reboot2") == 0) {
|
||||
fixed = 0;
|
||||
data = 0x01009080;
|
||||
}else if(strcmp(command,"reboot3") == 0){
|
||||
} else if (strcmp(command, "reboot3") == 0) {
|
||||
fixed = 0;
|
||||
data = 0x0000b1a0;
|
||||
}else if(strcmp(command,"reboot4") == 0){
|
||||
} else if (strcmp(command, "reboot4") == 0) {
|
||||
fixed = 0;
|
||||
data = 0x01009080;
|
||||
}else if(strcmp(command,"reboot5") == 0){
|
||||
} else if (strcmp(command, "reboot5") == 0) {
|
||||
fixed = 0x300000000;
|
||||
data = 0x00008092;
|
||||
}else if(strcmp(command,"reboot6") == 0){
|
||||
} else if (strcmp(command, "reboot6") == 0) {
|
||||
fixed = 0x300000000;
|
||||
data = 0x00008092;
|
||||
}else if(strcmp(command,"door1") == 0){
|
||||
} else if (strcmp(command, "door1") == 0) {
|
||||
fixed = 0x200000000;
|
||||
data = 0x01018280;
|
||||
}else if(strcmp(command,"door2") == 0){
|
||||
} else if (strcmp(command, "door2") == 0) {
|
||||
fixed = 0x200000000;
|
||||
data = 0x01009280;
|
||||
}else if(strcmp(command,"light") == 0){
|
||||
} else if (strcmp(command, "light") == 0) {
|
||||
fixed = 0x200000000;
|
||||
data = 0x00009281;
|
||||
}else if(strcmp(command,"lock") == 0){
|
||||
} else if (strcmp(command, "lock") == 0) {
|
||||
fixed = 0x0100000000;
|
||||
data = 0x0000728c;
|
||||
} else {
|
||||
|
@ -281,19 +286,19 @@ namespace ratgdo {
|
|||
// 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
|
||||
|
||||
// Every 50ms
|
||||
if(currentMillis - lastMillis > 50){
|
||||
if (currentMillis - lastMillis > 50) {
|
||||
// check to see if we got between 3 and 8 low pulses on the line
|
||||
if(this->store_.obstructionLowCount >= 3 && this->store_.obstructionLowCount <= 8){
|
||||
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 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){
|
||||
if (this->input_obst_pin_->digital_read() && currentMillis - this->store_.lastObstructionHigh > 70) {
|
||||
this->store_.obstructionState = 0;
|
||||
// obstructionDetected();
|
||||
}else{
|
||||
} else {
|
||||
// asleep
|
||||
}
|
||||
}
|
||||
|
@ -303,9 +308,10 @@ namespace ratgdo {
|
|||
}
|
||||
}
|
||||
|
||||
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());
|
||||
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;
|
||||
}
|
||||
uint8_t serData = this->swSerial.read();
|
||||
|
@ -314,7 +320,7 @@ namespace ratgdo {
|
|||
static bool reading = false;
|
||||
static uint16_t byteCount = 0;
|
||||
|
||||
if(!reading){
|
||||
if (!reading) {
|
||||
// shift serial byte onto msg start
|
||||
msgStart <<= 8;
|
||||
msgStart |= serData;
|
||||
|
@ -323,7 +329,7 @@ namespace ratgdo {
|
|||
msgStart &= 0x00FFFFFF;
|
||||
|
||||
// if we are at the start of a message, capture the next 16 bytes
|
||||
if(msgStart == 0x550100){
|
||||
if (msgStart == 0x550100) {
|
||||
byteCount = 3;
|
||||
rxRollingCode[0] = 0x55;
|
||||
rxRollingCode[1] = 0x01;
|
||||
|
@ -334,11 +340,11 @@ namespace ratgdo {
|
|||
}
|
||||
}
|
||||
|
||||
if(reading){
|
||||
if (reading) {
|
||||
this->rxRollingCode[byteCount] = serData;
|
||||
byteCount++;
|
||||
|
||||
if(byteCount == 19){
|
||||
if (byteCount == 19) {
|
||||
reading = false;
|
||||
msgStart = 0;
|
||||
byteCount = 0;
|
||||
|
@ -348,20 +354,24 @@ 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){
|
||||
if (this->store_.motionState == 1) {
|
||||
sendMotionStatus();
|
||||
this->store_.motionState = 0;
|
||||
}
|
||||
|
@ -372,42 +382,47 @@ 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);
|
||||
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);
|
||||
this->status_obst_pin_->digital_write(this->store_.obstructionState == 0);
|
||||
}
|
||||
|
||||
/************************* 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
|
||||
*/
|
||||
void transmit(const unsigned char * payload)
|
||||
* 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
|
||||
*/
|
||||
void transmit(const unsigned char* payload)
|
||||
{
|
||||
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
|
||||
delayMicroseconds(1305);
|
||||
this->output_gdo_pin_->digital_write(false); // bring the line low
|
||||
|
||||
|
@ -446,7 +461,7 @@ namespace ratgdo {
|
|||
|
||||
void 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]);
|
||||
return;
|
||||
}
|
||||
|
@ -455,17 +470,18 @@ namespace ratgdo {
|
|||
|
||||
void 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]);
|
||||
return;
|
||||
}
|
||||
toggleDoor();
|
||||
}
|
||||
|
||||
void stopDoor(){
|
||||
if(this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing"){
|
||||
void stopDoor()
|
||||
{
|
||||
if (this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing") {
|
||||
toggleDoor();
|
||||
}else{
|
||||
} else {
|
||||
Serial.print("The door is not moving.");
|
||||
}
|
||||
}
|
||||
|
@ -478,51 +494,57 @@ namespace ratgdo {
|
|||
getRollingCode("door2");
|
||||
transmit(this->txRollingCode);
|
||||
this->pref_.save(&this->rollingCodeCounter);
|
||||
|
||||
}
|
||||
|
||||
void lightOn(){
|
||||
if(this->lightStates[this->store_.lightState] == "on"){
|
||||
void lightOn()
|
||||
{
|
||||
if (this->lightStates[this->store_.lightState] == "on") {
|
||||
ESP_LOGD(TAG, "already on");
|
||||
}else{
|
||||
} else {
|
||||
toggleLight();
|
||||
}
|
||||
}
|
||||
|
||||
void lightOff(){
|
||||
if(this->lightStates[this->store_.lightState] == "off"){
|
||||
void lightOff()
|
||||
{
|
||||
if (this->lightStates[this->store_.lightState] == "off") {
|
||||
ESP_LOGD(TAG, "already off");
|
||||
}else{
|
||||
} else {
|
||||
toggleLight();
|
||||
}
|
||||
}
|
||||
|
||||
void toggleLight(){
|
||||
void toggleLight()
|
||||
{
|
||||
sendCommand("light");
|
||||
}
|
||||
|
||||
// Lock functions
|
||||
void lock(){
|
||||
if(this->lockStates[this->store_.lockState] == "locked"){
|
||||
void lock()
|
||||
{
|
||||
if (this->lockStates[this->store_.lockState] == "locked") {
|
||||
ESP_LOGD(TAG, "already locked");
|
||||
}else{
|
||||
} else {
|
||||
toggleLock();
|
||||
}
|
||||
}
|
||||
|
||||
void unlock(){
|
||||
if(this->lockStates[this->store_.lockState] == "unlocked"){
|
||||
void unlock()
|
||||
{
|
||||
if (this->lockStates[this->store_.lockState] == "unlocked") {
|
||||
ESP_LOGD(TAG, "already unlocked");
|
||||
}else{
|
||||
} else {
|
||||
toggleLock();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue