Flash red LED if any button is pressed while arming

This commit is contained in:
Tanner Collin 2018-11-28 01:00:07 -07:00
parent 2c8f12edd4
commit 2c9542a2bc

View File

@ -5,9 +5,9 @@
#include <EEPROM.h> #include <EEPROM.h>
#include <Ticker.h> #include <Ticker.h>
const char* WIFI_SSID = "Protospace"; const char *WIFI_SSID PROGMEM = "Protospace";
const char* WIFI_PASS = "yycmakers"; const char *WIFI_PASS PROGMEM = "yycmakers";
char wifiMACAddr[18]; char wifiMACAddr[20] = "";
const String SOCKET_URL = String("http://tools-socket.protospace.ca/api/lockout/"); const String SOCKET_URL = String("http://tools-socket.protospace.ca/api/lockout/");
const String CARD_URL = String("http://tools-auth.protospace.ca/cards/"); const String CARD_URL = String("http://tools-auth.protospace.ca/cards/");
@ -43,10 +43,9 @@ typedef struct __attribute__((packed)) cardData {
#define DELAY_TIME 10 #define DELAY_TIME 10
#define COMM_LOCK_IDLE_TIME 50 #define COMM_LOCK_IDLE_TIME 50
#define COMM_CARD_IDLE_TIME 1000 #define COMM_CARD_IDLE_TIME 1000
#define LED_DENIED_IDLE_TIME 50 #define LED_DELAYED_IDLE_TIME 50
#define EEPROM_SIZE 4095 #define EEPROM_SIZE 4095
#define EEPROM_START 0
enum wifiStates enum wifiStates
{ {
@ -60,8 +59,8 @@ enum LEDStates
LED_OFF, LED_OFF,
LED_ARMED, LED_ARMED,
LED_ON, LED_ON,
LED_DENIED, LED_DELAYED,
LED_DENIED_IDLE, LED_DELAYED_IDLE,
} LEDState = LED_OFF; } LEDState = LED_OFF;
enum lockStates enum lockStates
@ -155,7 +154,7 @@ bool checksum(cardData_t *cardData)
int8_t even = 0, odd = 0; int8_t even = 0, odd = 0;
for (int i = 0; i < CARD_DATA_LENGTH; i++) { for (int8_t i = 0; i < CARD_DATA_LENGTH; i++) {
int8_t num = charToNum(cardData->data[i]); int8_t num = charToNum(cardData->data[i]);
if (num == -1) return false; if (num == -1) return false;
@ -181,11 +180,11 @@ void checkCard()
String cardStr = String(); String cardStr = String();
String authorizedCards = String(); String authorizedCards = String();
for (int i = 0; i < CARD_DATA_LENGTH; i++) { for (uint8_t i = 0; i < CARD_DATA_LENGTH; i++) {
cardStr += cardData->data[i]; cardStr += cardData->data[i];
} }
for (int i = EEPROM_START; i < EEPROM_SIZE; i++) { for (uint16_t i = 0; i < EEPROM_SIZE; i++) {
char tmp = EEPROM.read(i); char tmp = EEPROM.read(i);
authorizedCards += tmp; authorizedCards += tmp;
if (tmp == '$') break; if (tmp == '$') break;
@ -200,7 +199,7 @@ void checkCard()
} }
} else { } else {
if (LOGGING) Serial.println("[INFO] Card not authorized on machine."); if (LOGGING) Serial.println("[INFO] Card not authorized on machine.");
LEDState = LED_DENIED; LEDState = LED_DELAYED;
} }
} }
} }
@ -250,7 +249,7 @@ void processLockState()
{ {
switch (lockState) { switch (lockState) {
case LOCK_OFF: case LOCK_OFF:
if (LEDState < LED_DENIED) LEDState = LED_OFF; if (LEDState < LED_DELAYED) LEDState = LED_OFF;
relayOff(); relayOff();
break; break;
@ -260,10 +259,11 @@ void processLockState()
lockState = LOCK_ARMED; lockState = LOCK_ARMED;
} else { } else {
lockState = LOCK_OFF; lockState = LOCK_OFF;
LEDState = LED_DELAYED;
} }
break; break;
case LOCK_ARMED: case LOCK_ARMED:
if (LEDState < LED_DENIED) LEDState = LED_ARMED; if (LEDState < LED_DELAYED) LEDState = LED_ARMED;
relayOff(); relayOff();
@ -285,7 +285,7 @@ void processLockState()
} }
break; break;
case LOCK_ON: case LOCK_ON:
if (LEDState < LED_DENIED) LEDState = LED_ON; if (LEDState < LED_DELAYED) LEDState = LED_ON;
relayOn(); relayOn();
@ -308,7 +308,7 @@ void redLEDOff() { digitalWrite(RED_LED_PIN, LED_PIN_OFF); }
void processLEDState() void processLEDState()
{ {
static uint16_t LEDDeniedIdleCount = 0; static uint16_t LEDDelayedIdleCount = 0;
switch (LEDState) { switch (LEDState) {
case LED_OFF: case LED_OFF:
@ -326,18 +326,18 @@ void processLEDState()
redLEDOn(); redLEDOn();
break; break;
case LED_DENIED: case LED_DELAYED:
LEDDeniedIdleCount = 0; LEDDelayedIdleCount = 0;
LEDState = LED_DENIED_IDLE; LEDState = LED_DELAYED_IDLE;
break; break;
case LED_DENIED_IDLE: case LED_DELAYED_IDLE:
LEDDeniedIdleCount++; LEDDelayedIdleCount++;
if (LEDDeniedIdleCount < LED_DENIED_IDLE_TIME) { if (LEDDelayedIdleCount < LED_DELAYED_IDLE_TIME) {
greenLEDOff(); greenLEDOff();
redLEDOn(); redLEDOn();
} else if (LEDDeniedIdleCount < LED_DENIED_IDLE_TIME * 2) { } else if (LEDDelayedIdleCount < LED_DELAYED_IDLE_TIME * 2) {
greenLEDOff(); greenLEDOff();
redLEDOff(); redLEDOff();
} else { } else {
@ -357,7 +357,7 @@ String serializeLockJson(uint8_t lockState)
{ {
// Generated with: https://arduinojson.org/assistant/ // Generated with: https://arduinojson.org/assistant/
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
DynamicJsonBuffer jsonBuffer(bufferSize); StaticJsonBuffer<bufferSize> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
root["lockState"] = (uint8_t) lockState; root["lockState"] = (uint8_t) lockState;
@ -371,7 +371,7 @@ String deserializeLockJson(String input)
{ {
// Generated with: https://arduinojson.org/assistant/ // Generated with: https://arduinojson.org/assistant/
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50; const size_t bufferSize = JSON_OBJECT_SIZE(1) + 50;
DynamicJsonBuffer jsonBuffer(bufferSize); StaticJsonBuffer<bufferSize> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(input); JsonObject& root = jsonBuffer.parseObject(input);
String action = root["action"]; String action = root["action"];
@ -416,6 +416,8 @@ void postState()
} else { } else {
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str()); if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", lockHTTP.errorToString(lockHTTPCode).c_str());
} }
lockHTTP.end();
} }
void getCards() void getCards()
@ -440,7 +442,7 @@ void getCards()
cardPayload += "$"; // Mark the end cardPayload += "$"; // Mark the end
if (LOGGING) Serial.println(cardPayload); if (LOGGING) Serial.println(cardPayload);
for (int i = EEPROM_START; i < cardPayload.length(); i++) { for (int i = 0; i < cardPayload.length(); i++) {
if (i >= EEPROM_SIZE) break; if (i >= EEPROM_SIZE) break;
EEPROM.write(i, cardPayload.charAt(i)); EEPROM.write(i, cardPayload.charAt(i));
} }
@ -453,6 +455,8 @@ void getCards()
} else { } else {
if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str()); if (LOGGING) Serial.printf("[ERROR] POST failed, error: %s\n", cardHTTP.errorToString(cardHTTPCode).c_str());
} }
cardHTTP.end();
} }
void processCommState() void processCommState()
@ -484,6 +488,8 @@ void processCommState()
commLockIdleCount = 0; commLockIdleCount = 0;
commState = COMM_IDLE; commState = COMM_IDLE;
Serial.printf("******free heap size: %u\n", ESP.getFreeHeap());
} }
break; break;
case COMM_CARD: case COMM_CARD:
@ -493,9 +499,6 @@ void processCommState()
commCardIdleCount = 0; commCardIdleCount = 0;
commState = COMM_IDLE; commState = COMM_IDLE;
Serial.printf("******heap size: %u\n", ESP.getFreeHeap());
Serial.printf("******frag.: %u\n", ESP.getHeapFragmentation());
} }
break; break;
} }