pslockout/firmware/firmware.ino

95 lines
2.5 KiB
C++

#include "firmware.h"
const char *LOCKOUT_FIRMWARE_VERSION = "MRWIZARD 0010 MRWIZARD";
const char *LOCKOUT_WIFI_SSID PROGMEM = "Protospace";
const char *LOCKOUT_WIFI_PASS PROGMEM = "yycmakers";
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 INFOLOG_URL = String("http://tools-auth.protospace.ca/infolog/");
const String UPDATE_URL = String("http://tools-auth.protospace.ca/update/");
Ticker ticker;
char wifiMACAddr[20] = "";
char cardBuffer[CARD_BUFFER_LENGTH];
struct logData eventLog[LOG_SIZE];
uint16_t logPosition = 0;
enum wifiStates wifiState = WIFI_DISCONNECTED;
enum LEDStates LEDState = LED_OFF;
enum lockStates lockState = LOCK_OFF;
enum commStates commState = COMM_INIT;
void setup()
{
Serial.begin(9600);
if (SERIAL_LOGGING) Serial.println("[INFO] Serial started.");
struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
struct timezone tz = { .tz_minuteswest = 0, .tz_dsttime = 0 };
settimeofday(&tv, &tz);
if (SERIAL_LOGGING) Serial.println("[INFO] Set system time to 0.");
logEvent(LOG_BOOT_UP, &LOCKOUT_FIRMWARE_VERSION[9], 4);
if (SERIAL_LOGGING) Serial.print("[INFO] Booting firmware version: ");
if (SERIAL_LOGGING) Serial.println(LOCKOUT_FIRMWARE_VERSION);
pinMode(RELAY_PIN, OUTPUT);
pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
EEPROM.begin(EEPROM_SIZE);
byte ar[6];
WiFi.macAddress(ar);
sprintf(wifiMACAddr, "%02X%02X%02X%02X%02X%02X", ar[0], ar[1], ar[2], ar[3], ar[4], ar[5]);
if (SERIAL_LOGGING) Serial.print("[INFO] Wifi MAC Address: ");
if (SERIAL_LOGGING) Serial.println(wifiMACAddr);
ticker.attach_ms(DELAY_TIME, tickerLoop);
logEvent(LOG_INIT_COMPLETE);
}
// The stuff in this loop must not be blocked by network delay
void tickerLoop()
{
processLockState();
processLEDState();
if (Serial.available() >= CARD_BUFFER_LENGTH) {
uint8_t bufPos = 0;
while (true) {
char readChar = Serial.read();
if (readChar == -1) {
break;
} else if (readChar == CARD_HEAD_BYTE) {
bufPos = 0;
}
if (bufPos >= CARD_BUFFER_LENGTH) {
break;
}
cardBuffer[bufPos++] = readChar;
if (readChar == CARD_TAIL_BYTE && bufPos == CARD_BUFFER_LENGTH) {
if (lockState == LOCK_OFF && LEDState == LED_OFF) checkCard();
break;
}
}
}
}
void loop()
{
processWifiState();
processCommState();
delay(DELAY_TIME);
}