Files
esp32-fingerprint-safe/app/application.cpp
2025-12-12 15:06:20 +01:00

88 lines
2.2 KiB
C++

#include <SmingCore.h>
#include "FingerPrint.h"
#include "utils.h"
#include "main.h"
HardwareSerial Serial1(UART_ID_1);
NtpClient ntpClient("at.pool.ntp.org", 3600);
static SimpleTimer procTimer;
volatile uint8_t procTimerLoops = 0;
void OnProcTimer()
{
const bool finger = !digitalRead(FINGER_DETECT_PIN);
const bool unlocked = !digitalRead(SAFELOCK_DETECT_PIN);
const bool opened = digitalRead(DOOR_DETECT_PIN);
static Debounce Finger(finger);
static Debounce Unlocked(unlocked);
static Debounce Opened(opened);
int8_t fingerEdge = Finger.debounce(finger);
if(fingerEdge)
g_Main.OnFinger(fingerEdge & 1);
int8_t unlockedEdge = Unlocked.debounce(unlocked);
if(unlockedEdge)
g_Main.OnLock(unlockedEdge & 1);
int8_t openedEdge = Finger.debounce(opened);
if(openedEdge)
g_Main.OnDoor(openedEdge & 1);
procTimerLoops++;
if(procTimerLoops > 25) // 250ms
procTimer.stop();
}
void IRAM_ATTR OnLevelChangeISR()
{
procTimerLoops = 0;
procTimer.start();
}
void ready()
{
debugf("READY!");
g_Main.Init(Serial);
gpio_pin_wakeup_enable(GPIO_ID_PIN(FINGER_DETECT_PIN), GPIO_PIN_INTR_LOLEVEL);
gpio_pin_wakeup_enable(GPIO_ID_PIN(SAFELOCK_DETECT_PIN), GPIO_PIN_INTR_LOLEVEL);
gpio_pin_wakeup_enable(GPIO_ID_PIN(DOOR_DETECT_PIN), GPIO_PIN_INTR_HILEVEL);
attachInterrupt(FINGER_DETECT_PIN, OnLevelChangeISR, CHANGE);
attachInterrupt(SAFELOCK_DETECT_PIN, OnLevelChangeISR, CHANGE);
attachInterrupt(DOOR_DETECT_PIN, OnLevelChangeISR, CHANGE);
procTimer.initializeMs<10>(OnProcTimer);
}
void init()
{
// for fingerprint module
Serial.systemDebugOutput(false);
Serial.begin(115200, SERIAL_8N1, SERIAL_FULL);
Serial.flush(); // TODO: Full flush?
// for debug messages
Serial1.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
Serial1.systemDebugOutput(true);
// p-channel FET to turn on fingerprint module
pinMode(FINGER_ENABLE_PIN, OUTPUT_OPEN_DRAIN);
digitalWrite(FINGER_ENABLE_PIN, 1);
// communication with safe lock
pinMode(SAFELOCK_DATA_PIN, OUTPUT_OPEN_DRAIN);
digitalWrite(SAFELOCK_DATA_PIN, 1);
pinMode(FINGER_DETECT_PIN, INPUT);
pinMode(SAFELOCK_DETECT_PIN, INPUT);
pinMode(DOOR_DETECT_PIN, INPUT_PULLUP);
// mount spiffs
spiffs_mount();
System.onReady(ready);
}