2023-10-30 15:03:50 +00:00
|
|
|
#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 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);
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2023-10-30 15:13:07 +00:00
|
|
|
Serial.println("Enabling Relay: Locking Door");
|
2023-10-30 15:03:50 +00:00
|
|
|
lockDoor();
|
|
|
|
#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() {
|
2023-10-30 15:20:21 +00:00
|
|
|
// Check if new data is available
|
2023-10-30 15:03:50 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-10-30 15:20:21 +00:00
|
|
|
#ifdef DEBUG
|
2023-10-30 15:03:50 +00:00
|
|
|
Serial.print("Facility ID (Decimal): ");
|
|
|
|
Serial.println(facilityID);
|
|
|
|
Serial.print("Facility ID (Binary): ");
|
|
|
|
Serial.println(facilityID, BIN);
|
|
|
|
|
|
|
|
Serial.print("Card ID (Decimal): ");
|
|
|
|
Serial.println(cardID);
|
|
|
|
Serial.print("Card ID (Binary): ");
|
|
|
|
Serial.println(cardID, BIN);
|
|
|
|
|
|
|
|
Serial.print("Card Data (Binary): ");
|
|
|
|
Serial.println(cardData, BIN);
|
|
|
|
Serial.print("Total Bits Received: ");
|
|
|
|
Serial.println(bitCount);
|
2023-10-30 15:20:21 +00:00
|
|
|
#endif
|
2023-10-30 15:03:50 +00:00
|
|
|
|
|
|
|
String fullCardID = String(facilityID)+":"+String(cardID);
|
2023-10-30 15:21:04 +00:00
|
|
|
Serial.print("FULL-CARD-ID: ");
|
2023-10-30 15:03:50 +00:00
|
|
|
Serial.println(fullCardID);
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|