Add prearm state and abstract buttons

This commit is contained in:
Tanner Collin 2018-11-07 19:09:34 -07:00
parent f56be14c80
commit 5c2938ad4f

View File

@ -36,10 +36,10 @@ enum wifiStates
enum lockStates enum lockStates
{ {
LOCK_OFF, LOCK_OFF,
LOCK_PREARM,
LOCK_ARMED, LOCK_ARMED,
LOCK_ON_PRESSED, LOCK_ON_PRESSED,
LOCK_ON, LOCK_ON,
LOCK_OFF_PRESSED,
} lockState = LOCK_OFF; } lockState = LOCK_OFF;
enum commStates enum commStates
@ -112,6 +112,14 @@ void processWifiState()
} }
} }
bool greenButton() { return digitalRead(GREEN_BUTTON_PIN) == BUTTON_CLOSED; }
bool redButton() { return digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED; }
void greenLEDOn() { digitalWrite(GREEN_LED_PIN, LED_ON); }
void greenLEDOff() { digitalWrite(GREEN_LED_PIN, LED_OFF); }
void redLEDOn() { digitalWrite(RED_LED_PIN, LED_ON); }
void redLEDOff() { digitalWrite(RED_LED_PIN, LED_OFF); }
void processLockState() void processLockState()
{ {
switch (lockState) { switch (lockState) {
@ -121,25 +129,33 @@ void processLockState()
digitalWrite(RELAY_PIN, RELAY_OPEN); digitalWrite(RELAY_PIN, RELAY_OPEN);
break; break;
case LOCK_PREARM:
if (!greenButton() && !redButton()) {
if (LOGGING) Serial.println("[INFO] Arming interlock.");
lockState = LOCK_ARMED;
} else {
lockState = LOCK_OFF;
}
break;
case LOCK_ARMED: case LOCK_ARMED:
digitalWrite(GREEN_LED_PIN, LED_OFF); digitalWrite(GREEN_LED_PIN, LED_OFF);
digitalWrite(RED_LED_PIN, LED_ON); digitalWrite(RED_LED_PIN, LED_ON);
digitalWrite(RELAY_PIN, RELAY_OPEN); digitalWrite(RELAY_PIN, RELAY_OPEN);
if (digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED) { if (redButton()) {
if (LOGGING) Serial.println("[INFO] Unarming interlock."); if (LOGGING) Serial.println("[INFO] Unarming interlock.");
lockState = LOCK_OFF_PRESSED; lockState = LOCK_OFF_PRESSED;
} else if (digitalRead(GREEN_BUTTON_PIN) == BUTTON_CLOSED) { } else if (greenButton()) {
if (LOGGING) Serial.println("[INFO] On button pressed."); if (LOGGING) Serial.println("[INFO] On button pressed.");
lockState = LOCK_ON_PRESSED; lockState = LOCK_ON_PRESSED;
} }
break; break;
case LOCK_ON_PRESSED: case LOCK_ON_PRESSED:
if (digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED) { if (redButton()) {
if (LOGGING) Serial.println("[ERROR] Both buttons pressed, aborting."); if (LOGGING) Serial.println("[ERROR] Off button pressed, aborting.");
lockState = LOCK_OFF_PRESSED; lockState = LOCK_OFF_PRESSED;
} else if (digitalRead(GREEN_BUTTON_PIN) == BUTTON_OPEN) { } else if (!greenButton()) {
if (LOGGING) Serial.println("[INFO] Turning machine on."); if (LOGGING) Serial.println("[INFO] Turning machine on.");
lockState = LOCK_ON; lockState = LOCK_ON;
} }
@ -150,14 +166,8 @@ void processLockState()
digitalWrite(RELAY_PIN, RELAY_CLOSED); digitalWrite(RELAY_PIN, RELAY_CLOSED);
if (digitalRead(RED_BUTTON_PIN) == BUTTON_CLOSED) { if (redButton()) {
if (LOGGING) Serial.println("[INFO] Off button pressed."); if (LOGGING) Serial.println("[INFO] Off button pressed.");
lockState = LOCK_OFF_PRESSED;
}
break;
case LOCK_OFF_PRESSED:
if (digitalRead(RED_BUTTON_PIN) == BUTTON_OPEN) {
if (LOGGING) Serial.println("[INFO] Turning machine off.");
lockState = LOCK_OFF; lockState = LOCK_OFF;
} }
break; break;
@ -229,11 +239,9 @@ void processCommState()
if (LOGGING) Serial.println(payload); if (LOGGING) Serial.println(payload);
String action = deserializeJson(payload); String action = deserializeJson(payload);
if (action == "arm" && lockState == LOCK_OFF && digitalRead(GREEN_BUTTON_PIN) == BUTTON_OPEN) { if (action == "arm" && lockState == LOCK_OFF) {
lockState = LOCK_ARMED; lockState = LOCK_PREARMED;
} else if (action == "disarm" && lockState == LOCK_ARMED) { } else if (action == "disarm") {
lockState = LOCK_OFF;
} else if (action == "disarm" && lockState == LOCK_ON) {
lockState = LOCK_OFF; lockState = LOCK_OFF;
} }