tinance2-doorbot/src/hardware.cpp
2024-04-17 15:08:13 +02:00

107 lines
2.2 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("Unlocked");
#endif
#ifdef LEDCTL_PIN
digitalWrite(LEDCTL_PIN,LOW);
#endif
#ifdef WIFI
if (settings.webhookUnlockEnabled) {
if (settings.webhookUnlockHook != "") {
ExecuteHTTPRequest(settings.webhookUnlockHook);
}
}
#ifdef TINANCE2_BACKEND
tinance2logrequest("Unlocked");
#endif
#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("Locked");
#endif
#ifdef LEDCTL_PIN
digitalWrite(LEDCTL_PIN,HIGH);
#endif
#ifdef WIFI
if (settings.webhookLockEnabled) {
if (settings.webhookLockHook != "") {
ExecuteHTTPRequest(settings.webhookLockHook);
}
}
#ifdef TINANCE2_BACKEND
tinance2logrequest("Locked");
#endif
#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
return ""; // Add a return statement to handle the case when RELAY1 is not defined
}