esphome-ratgdo/components/ratgdo/ratgdo.h

113 lines
3.7 KiB
C
Raw Normal View History

2023-06-05 17:12:51 +00:00
/************************************
* Rage
* Against
* The
* Garage
* Door
* Opener
*
* Copyright (C) 2022 Paul Wieland
*
* GNU GENERAL PUBLIC LICENSE
************************************/
2023-06-05 18:07:10 +00:00
#pragma once
2023-06-05 18:34:06 +00:00
#include "esphome/core/component.h"
2023-06-05 18:34:44 +00:00
#include "esphome/core/preferences.h"
2023-06-05 18:40:48 +00:00
#include <Arduino.h>
2023-06-05 18:07:10 +00:00
2023-06-05 17:12:51 +00:00
2023-06-05 17:52:42 +00:00
#include "SoftwareSerial.h"
2023-06-05 18:26:26 +00:00
extern "C" {
#include "secplus.h"
}
#define CODE_LENGTH 19 // the length of each command sent to the door.
2023-06-05 17:29:04 +00:00
/********************************** PIN DEFINITIONS
* *****************************************/
#define OUTPUT_GDO \
D4 // red control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini
#define TRIGGER_OPEN D5 // dry contact for opening door
2023-06-05 17:12:51 +00:00
#define TRIGGER_CLOSE D6 // dry contact for closing door
2023-06-05 17:29:04 +00:00
#define TRIGGER_LIGHT \
D3 // dry contact for triggering light (no discrete light commands, so toggle
// only)
2023-06-05 17:12:51 +00:00
#define STATUS_DOOR D0 // output door status, HIGH for open, LOW for closed
2023-06-05 17:29:04 +00:00
#define STATUS_OBST \
D8 // output for obstruction status, HIGH for obstructed, LOW for clear
#define INPUT_RPM1 \
D1 // RPM1 rotary encoder input OR reed switch if not soldering to the door
// opener logic board
#define INPUT_RPM2 \
D2 // RPM2 rotary encoder input OR not used if using reed switch
2023-06-05 17:12:51 +00:00
#define INPUT_OBST D7 // black obstruction sensor terminal
2023-06-05 18:33:28 +00:00
2023-06-05 17:59:55 +00:00
namespace esphome {
namespace ratgdo {
2023-06-05 18:07:10 +00:00
class RATGDOComponent : public Component {
public:
void setup() override;
void loop() override;
/********************************** GLOBAL VARS
* *****************************************/
unsigned int rollingCodeCounter;
2023-06-05 18:31:48 +00:00
SoftwareSerial swSerial;
2023-06-05 18:07:10 +00:00
byte rollingCode[CODE_LENGTH];
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
2023-06-05 18:26:26 +00:00
bool useRollingCodes = true; // use rolling codes or not
2023-06-05 18:07:10 +00:00
bool doorIsObstructed = false;
bool dryContactDoorOpen = false;
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
/********************************** FUNCTION DECLARATION
* *****************************************/
2023-06-05 18:34:44 +00:00
void set_rolling_codes(bool useRollingCodes);
2023-06-05 18:07:10 +00:00
void transmit(byte *payload, unsigned int length);
void sync();
void openDoor();
void closeDoor();
void toggleLight();
void obstructionLoop();
void obstructionDetected();
void obstructionCleared();
void sendDoorStatus();
void doorStateLoop();
void dryContactLoop();
/********************************** INTERRUPT SERVICE ROUTINES
* ***********************************/
void IRAM_ATTR isrDebounce(const char *type);
void IRAM_ATTR isrDoorOpen();
void IRAM_ATTR isrDoorClose();
void IRAM_ATTR isrLight();
void IRAM_ATTR isrObstruction();
void IRAM_ATTR isrRPM1();
void IRAM_ATTR isrRPM2();
2023-06-05 18:39:17 +00:00
2023-06-05 18:07:10 +00:00
2023-06-05 18:36:50 +00:00
protected:
ESPPreferenceObject pref_;
bool useRollingCodes_;
2023-06-05 18:29:54 +00:00
}; // RATGDOComponent
2023-06-05 18:07:10 +00:00
} // namespace ratgdo
} // namespace esphome