77 lines
1.9 KiB
C++
77 lines
1.9 KiB
C++
|
#include "lock.h"
|
||
|
|
||
|
void processLockState()
|
||
|
{
|
||
|
static uint16_t lockArmedTimeoutCount;
|
||
|
|
||
|
if (lockState != LOCK_ARMED) lockArmedTimeoutCount = 0;
|
||
|
|
||
|
switch (lockState) {
|
||
|
case LOCK_OFF:
|
||
|
if (LEDState != LED_ERROR) LEDState = LED_OFF;
|
||
|
|
||
|
relayOff();
|
||
|
break;
|
||
|
case LOCK_PREARM:
|
||
|
if (!greenButton() && !redButton()) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[INFO] Arming interlock.");
|
||
|
logEvent(LOG_LOCK_ARMED);
|
||
|
lockState = LOCK_ARMED;
|
||
|
} else {
|
||
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Buttons held, aborting.");
|
||
|
logEvent(LOG_LOCK_ERROR);
|
||
|
LEDState = LED_ERROR;
|
||
|
lockState = LOCK_OFF;
|
||
|
}
|
||
|
break;
|
||
|
case LOCK_ARMED:
|
||
|
if (LEDState != LED_ERROR) LEDState = LED_ARMED;
|
||
|
|
||
|
relayOff();
|
||
|
lockArmedTimeoutCount++;
|
||
|
|
||
|
if (redButton()) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[INFO] Unarming interlock.");
|
||
|
logEvent(LOG_LOCK_DISARM);
|
||
|
lockState = LOCK_OFF;
|
||
|
} else if (greenButton()) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[INFO] On button pressed.");
|
||
|
lockState = LOCK_ON_PRESSED;
|
||
|
}
|
||
|
|
||
|
if (lockArmedTimeoutCount > LOCK_ARMED_TIMEOUT) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[INFO] Arming timed out, disarming.");
|
||
|
logEvent(LOG_LOCK_TIMEOUT);
|
||
|
lockState = LOCK_OFF;
|
||
|
LEDState = LED_ERROR;
|
||
|
}
|
||
|
break;
|
||
|
case LOCK_ON_PRESSED:
|
||
|
if (redButton()) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Off button pressed, aborting.");
|
||
|
logEvent(LOG_LOCK_ERROR);
|
||
|
lockState = LOCK_OFF;
|
||
|
} else if (!greenButton()) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[INFO] Turning machine on.");
|
||
|
logEvent(LOG_LOCK_ON);
|
||
|
lockState = LOCK_ON;
|
||
|
}
|
||
|
break;
|
||
|
case LOCK_ON:
|
||
|
if (LEDState != LED_ERROR) LEDState = LED_ON;
|
||
|
|
||
|
relayOn();
|
||
|
|
||
|
if (redButton()) {
|
||
|
if (SERIAL_LOGGING) Serial.println("[INFO] Off button pressed.");
|
||
|
logEvent(LOG_LOCK_OFF);
|
||
|
lockState = LOCK_OFF;
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
if (SERIAL_LOGGING) Serial.println("[ERROR] Invalid lock state.");
|
||
|
lockState = LOCK_OFF;
|
||
|
break;
|
||
|
}
|
||
|
}
|