This commit is contained in:
J. Nick Koston 2023-06-05 13:26:26 -05:00
parent 69cf8231a1
commit d9a1fba168
No known key found for this signature in database
6 changed files with 147 additions and 189 deletions

View File

@ -21,8 +21,8 @@ CONFIG_SCHEMA = cv.Schema(
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)
# rolling_codes = await cg.get_variable(config[CONF_ROLLING_CODES]) rolling_codes = await cg.get_variable(config[CONF_ROLLING_CODES])
# cg.add(var.set_microphone(mic)) cg.add(var.set_rolling_codes(rolling_codes))
cg.add_library( cg.add_library(
name="secplus", name="secplus",
repository="https://github.com/bdraco/secplus", repository="https://github.com/bdraco/secplus",

View File

@ -1,5 +0,0 @@
#include <Arduino.h>
#define CODE_LENGTH 19 // the length of each command sent to the door.
extern byte rollingCode[CODE_LENGTH];
extern unsigned int rollingCodeCounter;

View File

@ -12,8 +12,6 @@
************************************/ ************************************/
#include "ratgdo.h" #include "ratgdo.h"
#include "common.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome {
@ -22,6 +20,11 @@ namespace ratgdo {
static const char *const TAG = "ratgdo"; static const char *const TAG = "ratgdo";
void RATGDOComponent::setup() { void RATGDOComponent::setup() {
this->pref_ = global_preferences->make_preference<int>(this->get_object_id_hash());
if (!this->pref_.load(&this->rollingCodeCounter)) {
this->rollingCodeCounter = 0;
}
swSerial.begin(9600, SWSERIAL_8N2, -1, OUTPUT_GDO, true); swSerial.begin(9600, SWSERIAL_8N2, -1, OUTPUT_GDO, true);
pinMode(TRIGGER_OPEN, INPUT_PULLUP); pinMode(TRIGGER_OPEN, INPUT_PULLUP);
pinMode(TRIGGER_CLOSE, INPUT_PULLUP); pinMode(TRIGGER_CLOSE, INPUT_PULLUP);
@ -46,9 +49,7 @@ void RATGDOComponent::setup() {
readCounterFromFlash(); readCounterFromFlash();
if (useRollingCodes) { if (this->useRollingCodes_) {
// if(rollingCodeCounter == 0) rollingCodeCounter = 1;
ESP_LOGD(TAG, "Syncing rolling code counter after reboot..."); ESP_LOGD(TAG, "Syncing rolling code counter after reboot...");
sync(); // if rolling codes are being used (rolling code counter > 0), send sync(); // if rolling codes are being used (rolling code counter > 0), send
// reboot/sync to the opener on startup // reboot/sync to the opener on startup
@ -63,6 +64,10 @@ void RATGDOComponent::loop() {
dryContactLoop(); dryContactLoop();
} }
void RATGDOComponent::set_rolling_codes(bool useRollingCodes) {
this->useRollingCodes_ = useRollingCodes;
}
/*************************** DETECTING THE DOOR STATE /*************************** DETECTING THE DOOR STATE
* ***************************/ * ***************************/
void RATGDOComponent::doorStateLoop() { void RATGDOComponent::doorStateLoop() {
@ -78,12 +83,12 @@ void RATGDOComponent::doorStateLoop() {
if (digitalRead(INPUT_RPM1) == LOW) { if (digitalRead(INPUT_RPM1) == LOW) {
if (doorState != "reed_closed") { if (doorState != "reed_closed") {
ESP_LOGD(TAG, "Reed switch closed"); ESP_LOGD(TAG, "Reed switch closed");
doorState = "reed_closed"; this->doorState = "reed_closed";
digitalWrite(STATUS_DOOR, HIGH); digitalWrite(STATUS_DOOR, HIGH);
} }
} else if (doorState != "reed_open") { } else if (doorState != "reed_open") {
ESP_LOGD(TAG, "Reed switch open"); ESP_LOGD(TAG, "Reed switch open");
doorState = "reed_open"; this->doorState = "reed_open";
digitalWrite(STATUS_DOOR, LOW); digitalWrite(STATUS_DOOR, LOW);
} }
} }
@ -91,7 +96,7 @@ void RATGDOComponent::doorStateLoop() {
// If the previous and the current state of the RPM2 Signal are different, // If the previous and the current state of the RPM2 Signal are different,
// that means there is a rotary encoder detected and the door is moving // that means there is a rotary encoder detected and the door is moving
if (doorPositionCounter != lastDoorPositionCounter) { if (this->doorPositionCounter != lastDoorPositionCounter) {
rotaryEncoderDetected = true; // this disables the reed switch handler rotaryEncoderDetected = true; // this disables the reed switch handler
lastCounterMillis = millis(); lastCounterMillis = millis();
@ -100,33 +105,33 @@ void RATGDOComponent::doorStateLoop() {
// Wait 5 pulses before updating to door opening status // Wait 5 pulses before updating to door opening status
if (doorPositionCounter - lastDirectionChangeCounter > 5) { if (doorPositionCounter - lastDirectionChangeCounter > 5) {
if (doorState != "opening") { if (this->doorState != "opening") {
ESP_LOGD(TAG, "Door Opening..."); ESP_LOGD(TAG, "Door Opening...");
} }
lastDirectionChangeCounter = doorPositionCounter; lastDirectionChangeCounter = this->doorPositionCounter;
doorState = "opening"; this->doorState = "opening";
} }
if (lastDirectionChangeCounter - doorPositionCounter > 5) { if (this->lastDirectionChangeCounter - this->doorPositionCounter > 5) {
if (doorState != "closing") { if (this->doorState != "closing") {
ESP_LOGD(TAG, "Door Closing..."); ESP_LOGD(TAG, "Door Closing...");
} }
lastDirectionChangeCounter = doorPositionCounter; lastDirectionChangeCounter = this->doorPositionCounter;
doorState = "closing"; this->doorState = "closing";
} }
// 250 millis after the last rotary encoder pulse, the door is stopped // 250 millis after the last rotary encoder pulse, the door is stopped
if (millis() - lastCounterMillis > 250) { if (millis() - lastCounterMillis > 250) {
// if the door was closing, and is now stopped, then the door is closed // if the door was closing, and is now stopped, then the door is closed
if (doorState == "closing") { if (this->doorState == "closing") {
doorState = "closed"; this->doorState = "closed";
ESP_LOGD(TAG, "Closed"); ESP_LOGD(TAG, "Closed");
digitalWrite(STATUS_DOOR, LOW); digitalWrite(STATUS_DOOR, LOW);
} }
// if the door was opening, and is now stopped, then the door is open // if the door was opening, and is now stopped, then the door is open
if (doorState == "opening") { if (this->doorState == "opening") {
doorState = "open"; this->doorState = "open";
ESP_LOGD(TAG, "Open"); ESP_LOGD(TAG, "Open");
digitalWrite(STATUS_DOOR, HIGH); digitalWrite(STATUS_DOOR, HIGH);
} }
@ -150,12 +155,12 @@ void IRAM_ATTR RATGDOComponent::isrDebounce(const char *type) {
if (strcmp(type, "openDoor") == 0) { if (strcmp(type, "openDoor") == 0) {
if (digitalRead(TRIGGER_OPEN) == LOW) { if (digitalRead(TRIGGER_OPEN) == LOW) {
// save the time of the falling edge // save the time of the falling edge
lastOpenDoorTime = currentMillis; this->lastOpenDoorTime = currentMillis;
} else if (currentMillis - lastOpenDoorTime > 500 && } else if (currentMillis - lastOpenDoorTime > 500 &&
currentMillis - lastOpenDoorTime < 10000) { currentMillis - lastOpenDoorTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the // now see if the rising edge was between 500ms and 10 seconds after the
// falling edge // falling edge
dryContactDoorOpen = true; this->dryContactDoorOpen = true;
} }
} }
@ -167,19 +172,19 @@ void IRAM_ATTR RATGDOComponent::isrDebounce(const char *type) {
currentMillis - lastCloseDoorTime < 10000) { currentMillis - lastCloseDoorTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the // now see if the rising edge was between 500ms and 10 seconds after the
// falling edge // falling edge
dryContactDoorClose = true; this->dryContactDoorClose = true;
} }
} }
if (strcmp(type, "toggleLight") == 0) { if (strcmp(type, "toggleLight") == 0) {
if (digitalRead(TRIGGER_LIGHT) == LOW) { if (digitalRead(TRIGGER_LIGHT) == LOW) {
// save the time of the falling edge // save the time of the falling edge
lastToggleLightTime = currentMillis; this->lastToggleLightTime = currentMillis;
} else if (currentMillis - lastToggleLightTime > 500 && } else if (currentMillis - lastToggleLightTime > 500 &&
currentMillis - lastToggleLightTime < 10000) { currentMillis - lastToggleLightTime < 10000) {
// now see if the rising edge was between 500ms and 10 seconds after the // now see if the rising edge was between 500ms and 10 seconds after the
// falling edge // falling edge
dryContactToggleLight = true; this->dryContactToggleLight = true;
} }
} }
} }
@ -191,7 +196,7 @@ void IRAM_ATTR isrDoorClose() { isrDebounce("closeDoor"); }
void IRAM_ATTR isrLight() { isrDebounce("toggleLight"); } void IRAM_ATTR isrLight() { isrDebounce("toggleLight"); }
// Fire on RISING edge of RPM1 // Fire on RISING edge of RPM1
void IRAM_ATTR isrRPM1() { rpm1Pulsed = true; } void IRAM_ATTR isrRPM1() { this->rpm1Pulsed = true; }
// Fire on RISING edge of RPM2 // Fire on RISING edge of RPM2
// When RPM1 HIGH on RPM2 rising edge, door closing: // When RPM1 HIGH on RPM2 rising edge, door closing:
@ -217,8 +222,8 @@ void IRAM_ATTR RATGDOComponent::isrRPM2() {
// continuously fires this ISR. This causes the door counter to change value // continuously fires this ISR. This causes the door counter to change value
// even though the door isn't moving To solve this, check to see if RPM1 // even though the door isn't moving To solve this, check to see if RPM1
// pulsed. If not, do nothing. If yes, reset the pulsed flag // pulsed. If not, do nothing. If yes, reset the pulsed flag
if (rpm1Pulsed) { if (this->rpm1Pulsed) {
rpm1Pulsed = false; this->rpm1Pulsed = false;
} else { } else {
return; return;
} }
@ -228,29 +233,29 @@ void IRAM_ATTR RATGDOComponent::isrRPM2() {
// If the RPM1 state is different from the RPM2 state, then the door is // If the RPM1 state is different from the RPM2 state, then the door is
// opening // opening
if (digitalRead(INPUT_RPM1)) { if (digitalRead(INPUT_RPM1)) {
doorPositionCounter--; this->doorPositionCounter--;
} else { } else {
doorPositionCounter++; this->doorPositionCounter++;
} }
} }
// handle changes to the dry contact state // handle changes to the dry contact state
void RATGDOComponent::dryContactLoop() { void RATGDOComponent::dryContactLoop() {
if (dryContactDoorOpen) { if (this->dryContactDoorOpen) {
ESP_LOGD(TAG, "Dry Contact: open the door"); ESP_LOGD(TAG, "Dry Contact: open the door");
dryContactDoorOpen = false; this->dryContactDoorOpen = false;
openDoor(); openDoor();
} }
if (dryContactDoorClose) { if (this->dryContactDoorClose) {
ESP_LOGD(TAG, "Dry Contact: close the door"); ESP_LOGD(TAG, "Dry Contact: close the door");
dryContactDoorClose = false; this->dryContactDoorClose = false;
closeDoor(); closeDoor();
} }
if (dryContactToggleLight) { if (this->dryContactToggleLight) {
ESP_LOGD(TAG, "Dry Contact: toggle the light"); ESP_LOGD(TAG, "Dry Contact: toggle the light");
dryContactToggleLight = false; this->dryContactToggleLight = false;
toggleLight(); toggleLight();
} }
} }
@ -258,9 +263,9 @@ void RATGDOComponent::dryContactLoop() {
/*************************** OBSTRUCTION DETECTION ***************************/ /*************************** OBSTRUCTION DETECTION ***************************/
void IRAM_ATTR isrObstruction() { void IRAM_ATTR isrObstruction() {
if (digitalRead(INPUT_OBST)) { if (digitalRead(INPUT_OBST)) {
lastObstructionHigh = millis(); this->lastObstructionHigh = millis();
} else { } else {
obstructionLowCount++; this->obstructionLowCount++;
} }
} }
@ -279,14 +284,14 @@ void RATGDOComponent::obstructionLoop() {
// Every 50ms // 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 // check to see if we got between 3 and 8 low pulses on the line
if (obstructionLowCount >= 3 && obstructionLowCount <= 8) { if (this->obstructionLowCount >= 3 && this->obstructionLowCount <= 8) {
obstructionCleared(); obstructionCleared();
// if there have been no pulses the line is steady high or low // if there have been no pulses the line is steady high or low
} else if (obstructionLowCount == 0) { } else if (this->obstructionLowCount == 0) {
// if the line is high and the last high pulse was more than 70ms ago, // if the line is high and the last high pulse was more than 70ms ago,
// then there is an obstruction present // then there is an obstruction present
if (digitalRead(INPUT_OBST) && currentMillis - lastObstructionHigh > 70) { if (digitalRead(INPUT_OBST) && currentMillis - this->lastObstructionHigh > 70) {
obstructionDetected(); obstructionDetected();
} else { } else {
// asleep // asleep
@ -294,7 +299,7 @@ void RATGDOComponent::obstructionLoop() {
} }
lastMillis = currentMillis; lastMillis = currentMillis;
obstructionLowCount = 0; this->obstructionLowCount = 0;
} }
} }
@ -303,7 +308,7 @@ void RATGDOComponent::obstructionDetected() {
unsigned long interruptTime = millis(); unsigned long interruptTime = millis();
// Anything less than 100ms is a bounce and is ignored // Anything less than 100ms is a bounce and is ignored
if (interruptTime - lastInterruptTime > 250) { if (interruptTime - lastInterruptTime > 250) {
doorIsObstructed = true; this->doorIsObstructed = true;
digitalWrite(STATUS_OBST, HIGH); digitalWrite(STATUS_OBST, HIGH);
ESP_LOGD(TAG, "Obstruction Detected"); ESP_LOGD(TAG, "Obstruction Detected");
} }
@ -311,20 +316,20 @@ void RATGDOComponent::obstructionDetected() {
} }
void RATGDOComponent::obstructionCleared() { void RATGDOComponent::obstructionCleared() {
if (doorIsObstructed) { if (this->doorIsObstructed) {
doorIsObstructed = false; this->doorIsObstructed = false;
digitalWrite(STATUS_OBST, LOW); digitalWrite(STATUS_OBST, LOW);
ESP_LOGD(TAG, "Obstruction Cleared"); ESP_LOGD(TAG, "Obstruction Cleared");
} }
} }
void RATGDOComponent::sendDoorStatus() { void RATGDOComponent::sendDoorStatus() {
ESP_LOGD(TAG, "Door state %s", doorState); ESP_LOGD(TAG, "Door state %s", this->doorState);
} }
void RATGDOComponent::sendCurrentCounter() { void RATGDOComponent::sendCurrentCounter() {
String msg = String(rollingCodeCounter); String msg = String(this->rollingCodeCounter);
ESP_LOGD(TAG, "Current counter %d", rollingCodeCounter); ESP_LOGD(TAG, "Current counter %d", this->rollingCodeCounter);
} }
/************************* DOOR COMMUNICATION *************************/ /************************* DOOR COMMUNICATION *************************/
@ -347,54 +352,54 @@ void RATGDOComponent::transmit(byte *payload, unsigned int length) {
} }
void RATGDOComponent::sync() { void RATGDOComponent::sync() {
if (!useRollingCodes) if (!this->useRollingCodes_)
return; return;
getRollingCode("reboot1"); getRollingCode("reboot1");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(45); delay(45);
getRollingCode("reboot2"); getRollingCode("reboot2");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(45); delay(45);
getRollingCode("reboot3"); getRollingCode("reboot3");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(45); delay(45);
getRollingCode("reboot4"); getRollingCode("reboot4");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(45); delay(45);
getRollingCode("reboot5"); getRollingCode("reboot5");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(45); delay(45);
getRollingCode("reboot6"); getRollingCode("reboot6");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(45); delay(45);
writeCounterToFlash(); writeCounterToFlash();
} }
void RATGDOComponent::openDoor() { void RATGDOComponent::openDoor() {
if (doorState == "open" || doorState == "opening") { if (this->doorState == "open" || this->doorState == "opening") {
ESP_LOGD(TAG, "The door is already %s", doorState); ESP_LOGD(TAG, "The door is already %s", doorState);
return; return;
} }
doorState = "opening"; // It takes a couple of pulses to detect this->doorState = "opening"; // It takes a couple of pulses to detect
// opening/closing. by setting here, we can avoid // opening/closing. by setting here, we can avoid
// bouncing from rapidly repeated commands // bouncing from rapidly repeated commands
if (useRollingCodes) { if (this->useRollingCodes) {
getRollingCode("door1"); getRollingCode("door1");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(40); delay(40);
getRollingCode("door2"); getRollingCode("door2");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
writeCounterToFlash(); writeCounterToFlash();
} else { } else {
@ -410,23 +415,23 @@ void RATGDOComponent::openDoor() {
} }
void RATGDOComponent::closeDoor() { void RATGDOComponent::closeDoor() {
if (doorState == "closed" || doorState == "closing") { if (this->doorState == "closed" || this->doorState == "closing") {
ESP_LOGD(TAG, "The door is already %s", doorState); ESP_LOGD(TAG, "The door is already %s", this->doorState);
return; return;
} }
doorState = "closing"; // It takes a couple of pulses to detect this->doorState = "closing"; // It takes a couple of pulses to detect
// opening/closing. by setting here, we can avoid // opening/closing. by setting here, we can avoid
// bouncing from rapidly repeated commands // bouncing from rapidly repeated commands
if (useRollingCodes) { if (this->useRollingCodes_) {
getRollingCode("door1"); getRollingCode("door1");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
delay(40); delay(40);
getRollingCode("door2"); getRollingCode("door2");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
writeCounterToFlash(); writeCounterToFlash();
} else { } else {
@ -442,9 +447,9 @@ void RATGDOComponent::closeDoor() {
} }
void RATGDOComponent::toggleLight() { void RATGDOComponent::toggleLight() {
if (useRollingCodes) { if (this->useRollingCodes) {
getRollingCode("light"); getRollingCode("light");
transmit(rollingCode, CODE_LENGTH); transmit(this->rollingCode, CODE_LENGTH);
writeCounterToFlash(); writeCounterToFlash();
} else { } else {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
@ -458,5 +463,63 @@ void RATGDOComponent::toggleLight() {
} }
} }
void RATGDOComponent::getRollingCode(const char *command, int rollingCodeCounter){
uint64_t id = 0x539;
uint64_t fixed = 0;
uint32_t data = 0;
if(strcmp(command,"reboot1") == 0){
fixed = 0x400000000;
data = 0x0000618b;
}else if(strcmp(command,"reboot2") == 0){
fixed = 0;
data = 0x01009080;
}else if(strcmp(command,"reboot3") == 0){
fixed = 0;
data = 0x0000b1a0;
}else if(strcmp(command,"reboot4") == 0){
fixed = 0;
data = 0x01009080;
}else if(strcmp(command,"reboot5") == 0){
fixed = 0x300000000;
data = 0x00008092;
}else if(strcmp(command,"reboot6") == 0){
fixed = 0x300000000;
data = 0x00008092;
}else if(strcmp(command,"door1") == 0){
fixed = 0x200000000;
data = 0x01018280;
}else if(strcmp(command,"door2") == 0){
fixed = 0x200000000;
data = 0x01009280;
}else if(strcmp(command,"light") == 0){
fixed = 0x200000000;
data = 0x00009281;
}else{
ESP_LOGD(TAG,"ERROR: Invalid command");
return;
}
fixed = fixed | id;
encode_wireline(rollingCodeCounter, fixed, data, rollingCode);
printRollingCode();
if(strcmp(command,"door1") != 0){ // door2 is created with same counter and should always be called after door1
this->rollingCodeCounter = (this->rollingCodeCounter + 1) & 0xfffffff;
}
return;
}
void printRollingCode(){
for(int i = 0; i < CODE_LENGTH; i++){
if(this->rollingCode[i] <= 0x0f) ESP_LOGD(TAG, "0");
ESP_LOGD(TAG, "%x", this->rollingCode[i]);
}
}
} // namespace ratgdo } // namespace ratgdo
} // namespace esphome } // namespace esphome

View File

@ -16,6 +16,13 @@
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
#include "rolling_code.h" #include "rolling_code.h"
extern "C" {
#include "secplus.h"
}
#define CODE_LENGTH 19 // the length of each command sent to the door.
extern byte rollingCode[CODE_LENGTH];
extern unsigned int rollingCodeCounter;
/********************************** PIN DEFINITIONS /********************************** PIN DEFINITIONS
* *****************************************/ * *****************************************/
@ -59,6 +66,7 @@ class RATGDOComponent : public Component {
unsigned long lastObstructionHigh = unsigned long lastObstructionHigh =
0; // count time between high pulses from the obst ISR 0; // count time between high pulses from the obst ISR
bool useRollingCodes = true; // use rolling codes or not
bool doorIsObstructed = false; bool doorIsObstructed = false;
bool dryContactDoorOpen = false; bool dryContactDoorOpen = false;
bool dryContactDoorClose = false; bool dryContactDoorClose = false;
@ -70,6 +78,7 @@ class RATGDOComponent : public Component {
/********************************** FUNCTION DECLARATION /********************************** FUNCTION DECLARATION
* *****************************************/ * *****************************************/
void set_rolling_codes(bool useRollingCodes)
void transmit(byte *payload, unsigned int length); void transmit(byte *payload, unsigned int length);
void sync(); void sync();
void openDoor(); void openDoor();
@ -112,6 +121,12 @@ class RATGDOComponent : public Component {
byte LIGHT_CODE[] = {0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe, byte LIGHT_CODE[] = {0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe,
0xff, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x76, 0xb1}; 0xff, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x76, 0xb1};
protected:
ESPPreferenceObject pref_;
bool useRollingCodes_;
} // RATGDOComponent } // RATGDOComponent
} // namespace ratgdo } // namespace ratgdo

View File

@ -1,98 +0,0 @@
#include "rolling_code.h"
#include "common.h"
void readCounterFromFlash() {
// Open the file
File file = LittleFS.open("/rollingcode.txt", "r");
// Check if the file exists
if (!file) {
Serial.println("rollingcode.txt doesn't exist. creating...");
writeCounterToFlash();
return;
}
rollingCodeCounter = file.parseInt();
// Close the file
file.close();
}
void writeCounterToFlash() {
// Open the file
File file = LittleFS.open("/rollingcode.txt", "w");
// Write to the file
file.print(rollingCodeCounter);
delay(1);
// Close the file
file.close();
Serial.println("Write successful");
}
void getRollingCode(const char *command) {
Serial.print("rolling code for ");
Serial.print(rollingCodeCounter);
Serial.print("|");
Serial.print(command);
Serial.print(" : ");
uint64_t id = 0x539;
uint64_t fixed = 0;
uint32_t data = 0;
if (strcmp(command, "reboot1") == 0) {
fixed = 0x400000000;
data = 0x0000618b;
} else if (strcmp(command, "reboot2") == 0) {
fixed = 0;
data = 0x01009080;
} else if (strcmp(command, "reboot3") == 0) {
fixed = 0;
data = 0x0000b1a0;
} else if (strcmp(command, "reboot4") == 0) {
fixed = 0;
data = 0x01009080;
} else if (strcmp(command, "reboot5") == 0) {
fixed = 0x300000000;
data = 0x00008092;
} else if (strcmp(command, "reboot6") == 0) {
fixed = 0x300000000;
data = 0x00008092;
} else if (strcmp(command, "door1") == 0) {
fixed = 0x200000000;
data = 0x01018280;
} else if (strcmp(command, "door2") == 0) {
fixed = 0x200000000;
data = 0x01009280;
} else if (strcmp(command, "light") == 0) {
fixed = 0x200000000;
data = 0x00009281;
} else {
Serial.println("ERROR: Invalid command");
return;
}
fixed = fixed | id;
encode_wireline(rollingCodeCounter, fixed, data, rollingCode);
printRollingCode();
if (strcmp(command, "door1") != 0) { // door2 is created with same counter and
// should always be called after door1
rollingCodeCounter = (rollingCodeCounter + 1) & 0xfffffff;
}
return;
}
void printRollingCode() {
for (int i = 0; i < CODE_LENGTH; i++) {
if (rollingCode[i] <= 0x0f)
Serial.print("0");
Serial.print(rollingCode[i], HEX);
}
Serial.println("");
}

View File

@ -1,17 +0,0 @@
#ifndef _RATGDO_ROLLING_CODE_H
#define _RATGDO_ROLLING_CODE_H
extern "C" {
#include "secplus.h"
}
void readCounterFromFlash(); // get the rolling code counter from setup.json &
// return it
void writeCounterToFlash(); // write the counter back to setup.json
void getRollingCode(
const char
*command); // get the next rolling code for type
// [reboot1,reboot2,reboot3,reboot4,reboot5,door1,light]
void printRollingCode();
#endif