tinance2-doorbot/src/hardware.cpp
2023-12-27 22:41:33 +01:00

91 lines
1.8 KiB
C++

#include "hardware.h"
#include "buzzer_ctl.h"
void controlRelay(int relayPin, bool on) {
digitalWrite(relayPin, on ? HIGH : LOW); // Turn the relay on or off
}
void unlockDoor(bool silent) {
#ifdef BUZZER
if (!silent) {
granted_beep();
}
#endif
#ifdef RELAY1
#ifdef RELAY1_REVERSED
controlRelay(RELAY1_PIN,true);
#else
controlRelay(RELAY1_PIN,false);
#endif
#endif
#ifdef SERIAL_DEBUG
Serial.println("door unlocked");
#endif
#ifdef LEDCTL_PIN
digitalWrite(LEDCTL_PIN,LOW);
#endif
#ifdef WEB_SERIAL_DEBUG
WebSerial.println("door unlocked");
#endif
}
void lockDoor(bool silent) {
#ifdef BUZZER
if (!silent) {
short_beep();
}
#endif
#ifdef RELAY1
#ifdef RELAY1_REVERSED
controlRelay(RELAY1_PIN,false);
#else
controlRelay(RELAY1_PIN,true);
#endif
#endif
#ifdef SERIAL_DEBUG
Serial.println("door locked");
#endif
#ifdef LEDCTL_PIN
digitalWrite(LEDCTL_PIN,HIGH);
#endif
#ifdef WEB_SERIAL_DEBUG
WebSerial.println("door locked");
#endif
}
void toggleDoor() {
#ifdef RELAY1
int relayState = digitalRead(RELAY1_PIN);
// Toggle the relay based on the current state
#ifdef RELAY1_REVERSED
if (relayState == HIGH) {
lockDoor(false);
} else {
unlockDoor(false);
}
#else
if (relayState == LOW) {
lockDoor(false);
} else {
unlockDoor(false);
}
#endif
#endif
}
String stateDoor() {
#ifdef RELAY1
int relayState = digitalRead(RELAY1_PIN);
// Toggle the relay based on the current state
if (relayState == LOW) {
return "Unlocked";
} else {
return "Locked";
}
#endif
}