This commit is contained in:
J. Nick Koston 2023-06-05 13:56:03 -05:00
parent 0eb1d11cfb
commit 6d45245ede
No known key found for this signature in database
2 changed files with 557 additions and 550 deletions

View File

@ -35,7 +35,8 @@ static const byte DOOR_CODE[] = {0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb,
static const byte LIGHT_CODE[] = { 0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe,
0xff, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x76, 0xb1 };
void RATGDOComponent::setup() {
void RATGDOComponent::setup()
{
this->pref_ = global_preferences->make_preference<int>(734874333U);
if (!this->pref_.load(&this->rollingCodeCounter)) {
this->rollingCodeCounter = 0;
@ -70,19 +71,22 @@ void RATGDOComponent::setup() {
}
}
void RATGDOComponent::loop() {
void RATGDOComponent::loop()
{
obstructionLoop();
doorStateLoop();
dryContactLoop();
}
void RATGDOComponent::set_rolling_codes(bool useRollingCodes) {
void RATGDOComponent::set_rolling_codes(bool useRollingCodes)
{
this->useRollingCodes_ = useRollingCodes;
}
/*************************** DETECTING THE DOOR STATE
* ***************************/
void RATGDOComponent::doorStateLoop() {
void RATGDOComponent::doorStateLoop()
{
static bool rotaryEncoderDetected = false;
static int lastDoorPositionCounter = 0;
static int lastDirectionChangeCounter = 0;
@ -154,7 +158,8 @@ void RATGDOComponent::doorStateLoop() {
/*************************** DRY CONTACT CONTROL OF LIGHT & DOOR
* ***************************/
void IRAM_ATTR RATGDOComponent::isrDebounce(const char *type) {
void IRAM_ATTR RATGDOComponent::isrDebounce(const char* type)
{
static unsigned long lastOpenDoorTime = 0;
static unsigned long lastCloseDoorTime = 0;
static unsigned long lastToggleLightTime = 0;
@ -168,8 +173,7 @@ void IRAM_ATTR RATGDOComponent::isrDebounce(const char *type) {
if (digitalRead(TRIGGER_OPEN) == LOW) {
// save the time of the falling edge
lastOpenDoorTime = currentMillis;
} else if (currentMillis - lastOpenDoorTime > 500 &&
currentMillis - lastOpenDoorTime < 10000) {
} else if (currentMillis - lastOpenDoorTime > 500 && currentMillis - lastOpenDoorTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the
// falling edge
this->dryContactDoorOpen = true;
@ -180,8 +184,7 @@ void IRAM_ATTR RATGDOComponent::isrDebounce(const char *type) {
if (digitalRead(TRIGGER_CLOSE) == LOW) {
// save the time of the falling edge
lastCloseDoorTime = currentMillis;
} else if (currentMillis - lastCloseDoorTime > 500 &&
currentMillis - lastCloseDoorTime < 10000) {
} else if (currentMillis - lastCloseDoorTime > 500 && currentMillis - lastCloseDoorTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the
// falling edge
this->dryContactDoorClose = true;
@ -192,8 +195,7 @@ void IRAM_ATTR RATGDOComponent::isrDebounce(const char *type) {
if (digitalRead(TRIGGER_LIGHT) == LOW) {
// save the time of the falling edge
lastToggleLightTime = currentMillis;
} else if (currentMillis - lastToggleLightTime > 500 &&
currentMillis - lastToggleLightTime < 10000) {
} else if (currentMillis - lastToggleLightTime > 500 && currentMillis - lastToggleLightTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the
// falling edge
this->dryContactToggleLight = true;
@ -218,7 +220,8 @@ void IRAM_ATTR RATGDOComponent::isrRPM1() { this->rpm1Pulsed = true; }
// When RPM1 LOW on RPM2 rising edge, door opening:
// RPM1: ___|--|__
// RPM2: __|--|___
void IRAM_ATTR RATGDOComponent::isrRPM2() {
void IRAM_ATTR RATGDOComponent::isrRPM2()
{
// The encoder updates faster than the ESP wants to process, so by sampling
// every 5ms we get a more reliable curve The counter is behind the actual
// pulse counter, but it doesn't matter since we only need a reliable linear
@ -252,7 +255,8 @@ void IRAM_ATTR RATGDOComponent::isrRPM2() {
}
// handle changes to the dry contact state
void RATGDOComponent::dryContactLoop() {
void RATGDOComponent::dryContactLoop()
{
if (this->dryContactDoorOpen) {
ESP_LOGD(TAG, "Dry Contact: open the door");
this->dryContactDoorOpen = false;
@ -273,7 +277,8 @@ void RATGDOComponent::dryContactLoop() {
}
/*************************** OBSTRUCTION DETECTION ***************************/
void IRAM_ATTR RATGDOComponent::isrObstruction() {
void IRAM_ATTR RATGDOComponent::isrObstruction()
{
if (digitalRead(INPUT_OBST)) {
this->lastObstructionHigh = millis();
} else {
@ -281,7 +286,8 @@ void IRAM_ATTR RATGDOComponent::isrObstruction() {
}
}
void RATGDOComponent::obstructionLoop() {
void RATGDOComponent::obstructionLoop()
{
long currentMillis = millis();
static unsigned long lastMillis = 0;
@ -315,7 +321,8 @@ void RATGDOComponent::obstructionLoop() {
}
}
void RATGDOComponent::obstructionDetected() {
void RATGDOComponent::obstructionDetected()
{
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
// Anything less than 100ms is a bounce and is ignored
@ -327,7 +334,8 @@ void RATGDOComponent::obstructionDetected() {
lastInterruptTime = interruptTime;
}
void RATGDOComponent::obstructionCleared() {
void RATGDOComponent::obstructionCleared()
{
if (this->doorIsObstructed) {
this->doorIsObstructed = false;
digitalWrite(STATUS_OBST, LOW);
@ -344,7 +352,8 @@ void RATGDOComponent::obstructionCleared() {
* The opener requires a specific duration low/high pulse before it will accept
* a message
*/
void RATGDOComponent::transmit(byte *payload, unsigned int length) {
void RATGDOComponent::transmit(byte* payload, unsigned int length)
{
digitalWrite(OUTPUT_GDO, HIGH); // pull the line high for 1305 micros so the
// door opener responds to the message
delayMicroseconds(1305);
@ -354,7 +363,8 @@ void RATGDOComponent::transmit(byte *payload, unsigned int length) {
this->swSerial.write(payload, length);
}
void RATGDOComponent::sync() {
void RATGDOComponent::sync()
{
if (!this->useRollingCodes_)
return;
@ -385,7 +395,8 @@ void RATGDOComponent::sync() {
this->pref_.save(&this->rollingCodeCounter);
}
void RATGDOComponent::openDoor() {
void RATGDOComponent::openDoor()
{
if (this->doorState == "open" || this->doorState == "opening") {
ESP_LOGD(TAG, "The door is already %s", doorState);
return;
@ -417,7 +428,8 @@ void RATGDOComponent::openDoor() {
}
}
void RATGDOComponent::closeDoor() {
void RATGDOComponent::closeDoor()
{
if (this->doorState == "closed" || this->doorState == "closing") {
ESP_LOGD(TAG, "The door is already %s", this->doorState);
return;
@ -449,7 +461,8 @@ void RATGDOComponent::closeDoor() {
}
}
void RATGDOComponent::toggleLight() {
void RATGDOComponent::toggleLight()
{
if (this->useRollingCodes) {
getRollingCode("light");
transmit(this->rollingCode, CODE_LENGTH);
@ -466,8 +479,8 @@ void RATGDOComponent::toggleLight() {
}
}
void RATGDOComponent::getRollingCode(const char *command){
void RATGDOComponent::getRollingCode(const char* command)
{
uint64_t id = 0x539;
uint64_t fixed = 0;
@ -517,9 +530,11 @@ void RATGDOComponent::getRollingCode(const char *command){
return;
}
void RATGDOComponent::printRollingCode(){
void RATGDOComponent::printRollingCode()
{
for (int i = 0; i < CODE_LENGTH; i++) {
if(this->rollingCode[i] <= 0x0f) ESP_LOGD(TAG, "0");
if (this->rollingCode[i] <= 0x0f)
ESP_LOGD(TAG, "0");
ESP_LOGD(TAG, "%x", this->rollingCode[i]);
}
}

View File

@ -15,7 +15,6 @@
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"
#include "SoftwareSerial.h"
extern "C" {
#include "secplus.h"
@ -33,7 +32,6 @@ 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
* *****************************************/
@ -66,13 +64,11 @@ class RATGDOComponent : public Component {
unsigned int rollingCodeCounter;
SoftwareSerial swSerial;
byte rollingCode[CODE_LENGTH];
String doorState =
"unknown"; // will be
String doorState = "unknown"; // will be
// [online|offline|opening|open|closing|closed|obstructed|clear|reed_open|reed_closed]
unsigned int obstructionLowCount = 0; // count obstruction low pulses
unsigned long lastObstructionHigh =
0; // count time between high pulses from the obst ISR
unsigned long lastObstructionHigh = 0; // count time between high pulses from the obst ISR
bool useRollingCodes = true; // use rolling codes or not
bool doorIsObstructed = false;
@ -80,9 +76,8 @@ class RATGDOComponent : public Component {
bool dryContactDoorClose = false;
bool dryContactToggleLight = false;
int doorPositionCounter = 0; // calculate the door's movement and position
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
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
/********************************** FUNCTION DECLARATION
* *****************************************/
@ -112,13 +107,10 @@ class RATGDOComponent : public Component {
void IRAM_ATTR isrRPM1();
void IRAM_ATTR isrRPM2();
protected:
ESPPreferenceObject pref_;
bool useRollingCodes_;
}; // RATGDOComponent
} // namespace ratgdo