tinance2-doorlock-controller/src/main.cpp
2023-10-30 19:40:06 +01:00

217 lines
No EOL
5.6 KiB
C++

#include "main.h"
volatile uint64_t cardData = 0;
volatile bool newDataAvailable = false;
unsigned long lastDataTime = 0;
const unsigned long displayDelay = 1000; // Delay in milliseconds after which the data is displayed
unsigned int bitCount = 0; // Variable to keep track of the bit count
unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too long of no data.
void commandLockDoor() {
// Add code to lock the door here (e.g., activate a relay, if applicable)
;
if (!settings.DoorDisabled()) {
lockDoor();
Serial.println("DOOR_LOCK_STATUS=" + stateDoor() );
} else {
Serial.println("LOG_MSG=Door Disabled Ignoring Command");
}
}
void commandUnlockDoor() {
// Add code to unlock the door here (e.g., deactivate a relay, if applicable)
if (!settings.DoorDisabled()) {
unlockDoor(false);
Serial.println("DOOR_LOCK_STATUS=" + stateDoor());
delay(RELAY_DELAY);
lockDoor();
Serial.println("DOOR_LOCK_STATUS=" + stateDoor());
} else {
Serial.println("LOG_MSG=Door Disabled Ignoring Command");
}
}
void commandToggleDoor() {
// Add code to lock the door here (e.g., activate a relay, if applicable)
;
if (!settings.DoorDisabled()) {
toggleDoor();
Serial.println("DOOR_LOCK_STATUS=" + stateDoor() );
} else {
Serial.println("LOG_MSG=Door Disabled Ignoring Command");
}
}
void commandLockStateDoor() {
// Add code to get door state
Serial.println("DOOR_LOCK_STATUS=" + stateDoor());
}
void commandDisableDoor() {
// Add code to get door state
settings.setDisableDoor(1);
unlockDoor(1);
Serial.println("DOOR_DISABLED_STATUS=" + String(settings.DoorDisabled() ? "Disabled" : "Enabled"));
}
void commandEnableDoor() {
// Add code to get door state
settings.setDisableDoor(0);
lockDoor();
Serial.println("DOOR_DISABLED_STATUS=" + String(settings.DoorDisabled() ? "Disabled" : "Enabled"));
}
void commandDisabledState() {
// Add code to get door state
settings.setDisableDoor(0);
unlockDoor(true);
Serial.println("DOOR_DISABLED_STATUS=" + String(settings.DoorDisabled() ? "Disabled" : "Enabled"));
}
void handleInterrupt(int bitValue) {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = micros();
if (interruptTime - lastInterruptTime > 200) {
cardData <<= 1;
cardData |= bitValue;
newDataAvailable = true;
bitCount++; // Increment the bit count
}
lastInterruptTime = interruptTime;
lastDataTime = millis(); // Update the time of last received data
}
void handleData0Interrupt() {
handleInterrupt(0);
}
void handleData1Interrupt() {
handleInterrupt(1);
}
void setup() {
Serial.begin(9600);
settings.loadFromEEPROM();
pinMode(DATA0_PIN, INPUT_PULLUP);
pinMode(DATA1_PIN, INPUT_PULLUP);
#ifdef LEDCTL_PIN
pinMode(LEDCTL_PIN, OUTPUT);
#endif
#ifdef RELAY1
pinMode(RELAY1_PIN, OUTPUT);
#endif
attachInterrupt(digitalPinToInterrupt(DATA0_PIN), handleData0Interrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(DATA1_PIN), handleData1Interrupt, FALLING);
#ifdef RELAY1
if (!settings.DoorDisabled()) {
Serial.println("LOG_MSG=Door Enabled");
Serial.println("LOG_MSG=Locking Door");
lockDoor();
} else {
Serial.println("LOG_MSG=Door Disabled");
Serial.println("LOG_MSG=Unlocking Door");
unlockDoor(true);
}
#endif
#ifdef RELAY2
pinMode(RELAY2_PIN, OUTPUT);
#endif
#ifdef BUZZER
pinMode(ALARM_PIN, OUTPUT);
digitalWrite(ALARM_PIN, HIGH); // Do not set to low or it will constantly beep.
#endif
}
void loop() {
// Check if new data is available
if (newDataAvailable) {
newDataAvailable = false;
lastDataTime = millis(); // Reset the time of last received data
}
if (millis() - lastDataTime >= displayDelay && cardData != 0) {
uint64_t facilityID = (cardData >> 17) & 0xFFFF;
uint64_t cardID = (cardData >> 1) & 0xFFFF;
#ifdef DEBUG
Serial.print("DEBUG_MSG=Facility ID (Decimal): ");
Serial.println(facilityID);
Serial.print("DEBUG_MSG=Facility ID (Binary): ");
Serial.println(facilityID, BIN);
Serial.print("DEBUG_MSG=Card ID (Decimal): ");
Serial.println(cardID);
Serial.print("DEBUG_MSG=Card ID (Binary): ");
Serial.println(cardID, BIN);
Serial.print("DEBUG_MSG=Card Data (Binary): ");
Serial.println(cardData, BIN);
Serial.print("DEBUG_MSG=Total Bits Received: ");
Serial.println(bitCount);
#endif
String fullCardID = String(facilityID)+":"+String(cardID);
Serial.println("FULL_CARD_ID=" + fullCardID);
Serial.println(settings.DoorDisabled() ? "DOOR_DISABLED_STATUS=1" : "DOOR_DISABLED_STATUS=0");
cardData = 0; // Reset the card data
lastDataTime = millis(); // Reset the time of last received data
bitCount = 0; // Reset the bit count
}
else if (millis() - lastDataTime >= maxReaderWaitTime) {
cardData = 0; // Reset the card data
lastDataTime = millis(); // Reset the time of last received data
bitCount = 0; // Reset the bit count
}
// Check for commands from the serial interface
if (Serial.available() > 0) {
String command = Serial.readString();
command.trim();
if (command.equals("unlock")) {
commandUnlockDoor();
} else if (command.equals("lock")) {
commandLockDoor();
} else if (command.equals("toggle")) {
commandToggleDoor();
} else if (command.equals("lock_status")) {
commandLockStateDoor();
} else if (command.equals("enable")) {
commandEnableDoor();
} else if (command.equals("disable")) {
commandDisableDoor();
}else if (command.equals("disabled_status")) {
commandDisabledState();
}
}
}