Skip to content

Commit

Permalink
Motion detection fix and comment cleanse
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWieland committed May 16, 2023
1 parent 44eeda8 commit ddf5940
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 35 deletions.
13 changes: 8 additions & 5 deletions src/ratgdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
void setup(){
pinMode(INPUT_GDO, INPUT_PULLUP);
pinMode(OUTPUT_GDO, OUTPUT);
// swSerial.begin(9600, SWSERIAL_8N2, INPUT_GDO, OUTPUT_GDO, true);
swSerial.begin(9600, SWSERIAL_8N1, INPUT_GDO, OUTPUT_GDO, true);

Serial.begin(115200); // must remain at 115200 for improv
Expand Down Expand Up @@ -85,7 +84,7 @@ void setup(){
readCounterFromFlash();

Serial.println("Syncing rolling code counter after reboot...");
sync(); // if rolling codes are being used (rolling code counter > 0), send reboot/sync to the opener on startup
sync(); // send reboot/sync to the opener on startup

delay(500);
}
Expand Down Expand Up @@ -280,19 +279,21 @@ void statusUpdateLoop(){
static uint8_t previousDoorState = 0;
static uint8_t previousLightState = 2;
static uint8_t previousLockState = 2;
static uint8_t previousMotionState = 0;
static uint8_t previousObstructionState = 2;

if(doorState != previousDoorState) sendDoorStatus();
if(lightState != previousLightState) sendLightStatus();
if(lockState != previousLockState) sendLockStatus();
if(motionState != previousMotionState) sendMotionStatus();
if(obstructionState != previousObstructionState) sendObstructionStatus();

if(motionState == 1){
sendMotionStatus();
motionState = 0;
}

previousDoorState = doorState;
previousLightState = lightState;
previousLockState = lockState;
previousMotionState = motionState;
previousObstructionState = obstructionState;
}

Expand Down Expand Up @@ -333,6 +334,8 @@ void sendMotionStatus(){
if(isConfigFileOk){
bootstrapManager.publish(motionStatusTopic.c_str(), motionStates[motionState].c_str(), true);
}

motionState = 0; // reset motion state
}

void sendObstructionStatus(){
Expand Down
2 changes: 1 addition & 1 deletion src/ratgdo.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ String lockStates[3] = {"unlocked","locked","unknown"};

String motionStatusTopic = ""; // will be mqttTopicPrefix/deviceName/status/motion
uint8_t motionState = 0;
String motionStates[3] = {"clear","detected"};
String motionStates[2] = {"clear","detected"};

String obstructionStatusTopic = ""; // will be mqttTopicPrefix/deviceName/status/obstruction
uint8_t obstructionState = 2;
Expand Down
32 changes: 3 additions & 29 deletions src/rolling_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,18 @@ void readRollingCode(byte rxRollingCode[CODE_LENGTH], uint8_t &door, uint8_t &li

cmd = ((fixed >> 24) & 0xf00) | (data & 0xff);

// static uint32_t cmdSequence;

// shift cmd onto cmdSequence
// cmdSequence <<= 8;
// cmdSequence |= cmd;

// truncate to 3 bytes
// cmdSequence &= 0x00FFFFFF;

nibble = (data >> 8) & 0xf;
byte1 = (data >> 16) & 0xff;
byte2 = (data >> 24) & 0xff;

printRollingCode(rxRollingCode);
// Serial.print(" cmd: ");
// Serial.print(cmd,HEX);
// Serial.print(" rolling: ");
// Serial.print(rolling,HEX);
// Serial.print(" fixed: ");
// Serial.print(fixed,HEX);
// Serial.print(" data: ");
// Serial.print(data,HEX);

// The sequence of messages are different depending on the length of the obstruction
// short obstruction sequence: 84, 85, 81(clr) -> 84, 85, 81(clr)
// long obstruction sequence: 84, 85, 81(clr), A1, 84, 85, 81(obs) -> 84, 85, 81(obs), A1, 84, 85, 81(clr)
// if(cmd == 0x84 && (cmdSequence & 0xFFFF) != 0xA184){
// Serial.println("toggle");
// obstruction ^= 1;
// }else if(cmd == 0x81 && (cmdSequence & 0xFFFFFF) == 0x848581){
// // do nothing
// }else

if(cmd == 0x81){
door = nibble;
light = (byte2 >> 1) & 1;
lock = byte2 & 1;
motion = 0; // when the status message is read, reset motion state to 0|clear
// obstruction = (byte1 >> 6) & 1;
// obstruction = (byte1 >> 6) & 1; // unreliable due to the time it takes to register an obstruction

Serial.print(" | STATUS:");
Serial.print(" door:");
Expand All @@ -104,7 +78,7 @@ void readRollingCode(byte rxRollingCode[CODE_LENGTH], uint8_t &door, uint8_t &li
Serial.print(light);
}else if(cmd == 0x84){
}else if(cmd == 0x285){
motion ^= 1; // toggle bit
motion = 1; // toggle bit
Serial.print(" | MOTION:");
Serial.print(motion);
}
Expand Down

0 comments on commit ddf5940

Please # to comment.