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:07:10 +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"
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
static const uint8_t D0 = 16;
|
|
|
|
static const uint8_t D1 = 5;
|
|
|
|
static const uint8_t D2 = 4;
|
|
|
|
static const uint8_t D3 = 0;
|
|
|
|
static const uint8_t D4 = 2;
|
|
|
|
static const uint8_t D5 = 14;
|
|
|
|
static const uint8_t D6 = 12;
|
|
|
|
static const uint8_t D7 = 13;
|
|
|
|
static const uint8_t D8 = 15;
|
|
|
|
static const uint8_t D9 = 3;
|
|
|
|
static const uint8_t D10 = 1;
|
2023-06-05 18:42:39 +00:00
|
|
|
|
2023-06-05 18:26:26 +00:00
|
|
|
#define CODE_LENGTH 19 // the length of each command sent to the door.
|
2023-06-05 17:29:04 +00:00
|
|
|
/********************************** PIN DEFINITIONS
|
|
|
|
* *****************************************/
|
2023-06-05 18:56:03 +00:00
|
|
|
#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 18:56:03 +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 18:56:03 +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:56:03 +00:00
|
|
|
class RATGDOComponent : public Component {
|
|
|
|
public:
|
|
|
|
void setup() override;
|
|
|
|
void loop() override;
|
|
|
|
/********************************** GLOBAL VARS
|
|
|
|
* *****************************************/
|
|
|
|
unsigned int rollingCodeCounter;
|
|
|
|
SoftwareSerial swSerial;
|
|
|
|
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
|
|
|
|
|
|
|
|
bool useRollingCodes = true; // use rolling codes or not
|
|
|
|
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
|
|
|
|
|
2023-06-05 19:40:53 +00:00
|
|
|
void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; };
|
|
|
|
void set_trigger_open_pin(InternalGPIOPin* pin) { this->trigger_open_pin_ = pin; };
|
|
|
|
void set_trigger_close_pin(InternalGPIOPin* pin) { this->trigger_close_pin_ = pin; };
|
|
|
|
void set_trigger_light_pin(InternalGPIOPin* pin) { this->trigger_light_pin_ = pin; };
|
|
|
|
void set_status_door_pin(InternalGPIOPin* pin) { this->status_door_pin_ = pin; };
|
|
|
|
void set_status_obst_pin(InternalGPIOPin* pin) { this->status_obst_pin_ = pin; };
|
|
|
|
void set_input_rpm1_pin(InternalGPIOPin* pin) { this->input_rpm1_pin_ = pin; };
|
|
|
|
void set_input_rpm2_pin(InternalGPIOPin* pin) { this->input_rpm2_pin_ = pin; };
|
|
|
|
void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; };
|
2023-06-05 19:39:46 +00:00
|
|
|
|
2023-06-05 18:56:03 +00:00
|
|
|
/********************************** FUNCTION DECLARATION
|
|
|
|
* *****************************************/
|
|
|
|
void set_rolling_codes(bool useRollingCodes);
|
2023-06-05 19:06:19 +00:00
|
|
|
void transmit(const unsigned char*, unsigned int);
|
2023-06-05 18:56:03 +00:00
|
|
|
void sync();
|
|
|
|
void openDoor();
|
|
|
|
void closeDoor();
|
|
|
|
void toggleLight();
|
2023-06-05 19:05:08 +00:00
|
|
|
void sendSyncCodes();
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
void obstructionLoop();
|
|
|
|
void obstructionDetected();
|
|
|
|
void obstructionCleared();
|
|
|
|
|
|
|
|
void sendDoorStatus();
|
|
|
|
void doorStateLoop();
|
|
|
|
void dryContactLoop();
|
2023-06-05 19:00:45 +00:00
|
|
|
void printRollingCode();
|
|
|
|
void getRollingCode(const char* command);
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
/********************************** 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();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ESPPreferenceObject pref_;
|
|
|
|
bool useRollingCodes_;
|
2023-06-05 19:40:53 +00:00
|
|
|
InternalGPIOPin* output_gdo_pin_;
|
|
|
|
InternalGPIOPin* trigger_open_pin_;
|
|
|
|
InternalGPIOPin* trigger_close_pin_;
|
|
|
|
InternalGPIOPin* trigger_light_pin_;
|
|
|
|
InternalGPIOPin* status_door_pin_;
|
|
|
|
InternalGPIOPin* status_obst_pin_;
|
|
|
|
InternalGPIOPin* input_rpm1_pin_;
|
|
|
|
InternalGPIOPin* input_rpm2_pin_;
|
|
|
|
InternalGPIOPin* input_obst_pin_;
|
2023-06-05 18:56:03 +00:00
|
|
|
|
|
|
|
}; // RATGDOComponent
|
2023-06-05 18:07:10 +00:00
|
|
|
|
|
|
|
} // namespace ratgdo
|
|
|
|
} // namespace esphome
|