esphome-ratgdo/components/ratgdo/ratgdo.h

141 lines
4.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 19:46:23 +00:00
#include "esphome/core/gpio.h"
2023-06-05 21:26:33 +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 23:24:05 +00:00
#define CODE_LENGTH 19
2023-06-05 17:59:55 +00:00
namespace esphome {
namespace ratgdo {
2023-06-05 21:26:28 +00:00
class RATGDOComponent;
struct RATGDOStore {
ISRInternalGPIOPin output_gdo;
2023-06-05 23:07:10 +00:00
ISRInternalGPIOPin input_gdo;
ISRInternalGPIOPin input_obst;
2023-06-05 21:26:28 +00:00
ISRInternalGPIOPin trigger_open;
ISRInternalGPIOPin trigger_close;
ISRInternalGPIOPin trigger_light;
2023-06-05 23:07:10 +00:00
2023-06-05 21:26:28 +00:00
ISRInternalGPIOPin status_door;
ISRInternalGPIOPin status_obst;
2023-06-05 22:44:24 +00:00
unsigned long lastOpenDoorTime { 0 };
unsigned long lastCloseDoorTime { 0 };
unsigned long lastToggleLightTime { 0 };
unsigned long lastPulse { 0 };
2023-06-05 21:26:33 +00:00
volatile int doorPositionCounter { 0 }; // calculate the door's movement and position
2023-06-05 22:44:24 +00:00
bool rpm1Pulsed { false }; // did rpm1 get a pulse or not - eliminates an issue when the
2023-06-05 23:07:10 +00:00
// sensor is parked on a high pulse which fires rpm2 isr
2023-06-05 21:26:28 +00:00
2023-06-05 22:44:24 +00:00
int obstructionLowCount = 0; // count obstruction low pulses
long lastObstructionHigh = 0; // count time between high pulses from the obst ISR
2023-06-05 21:26:28 +00:00
2023-06-05 23:22:59 +00:00
uint8_t obstructionState = 2;
uint8_t motionState = 0;
uint8_t lockState = 2;
uint8_t lightState = 2;
uint8_t doorState = 0;
2023-06-05 22:44:24 +00:00
bool dryContactDoorOpen { false };
bool dryContactDoorClose { false };
bool dryContactToggleLight { false };
2023-06-05 21:26:33 +00:00
static void IRAM_ATTR isrDoorOpen(RATGDOStore* arg);
static void IRAM_ATTR isrDoorClose(RATGDOStore* arg);
static void IRAM_ATTR isrLight(RATGDOStore* arg);
static void IRAM_ATTR isrObstruction(RATGDOStore* arg);
static void IRAM_ATTR isrRPM1(RATGDOStore* arg);
static void IRAM_ATTR isrRPM2(RATGDOStore* arg);
2023-06-05 21:26:28 +00:00
};
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;
2023-06-05 23:07:10 +00:00
uint8_t txRollingCode[CODE_LENGTH];
uint8_t rxRollingCode[CODE_LENGTH];
2023-06-05 23:22:59 +00:00
String doorStates[6] = {"unknown","open","closed","stopped","opening","closing"};
String lightStates[3] = {"off","on","unknown"};
String lockStates[3] = {"unlocked","locked","unknown"};
String motionStates[2] = {"clear","detected"};
String obstructionStates[3] = {"obstructed","clear","unknown"};
2023-06-05 18:56:03 +00:00
2023-06-05 19:40:53 +00:00
void set_output_gdo_pin(InternalGPIOPin* pin) { this->output_gdo_pin_ = pin; };
2023-06-05 23:19:52 +00:00
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; };
void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; };
2023-06-05 19:40:53 +00:00
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; };
2023-06-05 23:19:52 +00:00
2023-06-05 19:40:53 +00:00
void set_status_door_pin(InternalGPIOPin* pin) { this->status_door_pin_ = pin; };
void set_status_obst_pin(InternalGPIOPin* pin) { this->status_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 22:28:50 +00:00
void transmit(const uint8_t*);
2023-06-05 18:56:03 +00:00
void sync();
void openDoor();
void closeDoor();
void toggleLight();
2023-06-05 23:07:10 +00:00
void toggleDoor();
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
protected:
ESPPreferenceObject pref_;
2023-06-05 21:26:33 +00:00
bool useRollingCodes_ { true };
RATGDOStore store_ {};
2023-06-05 21:26:28 +00:00
2023-06-05 19:40:53 +00:00
InternalGPIOPin* output_gdo_pin_;
2023-06-05 23:07:10 +00:00
InternalGPIOPin* input_gdo_pin_;
InternalGPIOPin* input_obst_pin_;
2023-06-05 19:40:53 +00:00
InternalGPIOPin* trigger_open_pin_;
InternalGPIOPin* trigger_close_pin_;
InternalGPIOPin* trigger_light_pin_;
2023-06-05 23:07:10 +00:00
2023-06-05 19:40:53 +00:00
InternalGPIOPin* status_door_pin_;
InternalGPIOPin* status_obst_pin_;
2023-06-05 18:56:03 +00:00
}; // RATGDOComponent
2023-06-05 18:07:10 +00:00
} // namespace ratgdo
} // namespace esphome