Remove some magic numbers

This commit is contained in:
Tanner Collin 2018-12-09 06:01:01 -07:00
parent 1627ffe639
commit d2d63146da

View File

@ -18,6 +18,8 @@ char cardBuffer[CARD_BUFFER_LENGTH];
#define CARD_DATA_LENGTH 10 #define CARD_DATA_LENGTH 10
#define CARD_CHECK_LENGTH 2 #define CARD_CHECK_LENGTH 2
#define CARD_HEAD_BYTE 0x2
#define CARD_TAIL_BYTE 0x3
typedef struct __attribute__((packed)) cardData { typedef struct __attribute__((packed)) cardData {
char head; char head;
char data[CARD_DATA_LENGTH]; char data[CARD_DATA_LENGTH];
@ -48,6 +50,7 @@ typedef struct __attribute__((packed)) cardData {
#define LED_ERROR_BLINK_TIME 50 #define LED_ERROR_BLINK_TIME 50
#define EEPROM_SIZE 4095 #define EEPROM_SIZE 4095
#define EEPROM_END_MARKER '$'
enum wifiStates enum wifiStates
{ {
@ -117,7 +120,7 @@ void tickerLoop()
if (readChar == -1) { if (readChar == -1) {
break; break;
} else if (readChar == 0x2) { } else if (readChar == CARD_HEAD_BYTE) {
bufPos = 0; bufPos = 0;
} }
@ -127,7 +130,7 @@ void tickerLoop()
cardBuffer[bufPos++] = readChar; cardBuffer[bufPos++] = readChar;
if (readChar == 0x3 && bufPos == CARD_BUFFER_LENGTH) { if (readChar == CARD_TAIL_BYTE && bufPos == CARD_BUFFER_LENGTH) {
if (lockState == LOCK_OFF && LEDState == LED_OFF) checkCard(); if (lockState == LOCK_OFF && LEDState == LED_OFF) checkCard();
break; break;
} }
@ -177,7 +180,10 @@ void checkCard()
{ {
cardData_t *cardData = (cardData_t *) cardBuffer; cardData_t *cardData = (cardData_t *) cardBuffer;
if (cardData->head == 0x2 && cardData->tail == 0x3 && checksum(cardData)) { if (cardData->head == CARD_HEAD_BYTE &&
cardData->tail == CARD_TAIL_BYTE &&
checksum(cardData)) {
String cardStr = String(); String cardStr = String();
String authorizedCards = String(); String authorizedCards = String();
@ -188,7 +194,7 @@ void checkCard()
for (uint16_t i = 0; 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 == EEPROM_END_MARKER) break;
} }
if (LOGGING) Serial.println("[INFO] Good scan from card: " + cardStr); if (LOGGING) Serial.println("[INFO] Good scan from card: " + cardStr);
@ -457,7 +463,7 @@ void getCards()
if (cardHTTPCode == HTTP_CODE_OK) { if (cardHTTPCode == HTTP_CODE_OK) {
if (LOGGING) Serial.print("[INFO] Resource found, parsing response: "); if (LOGGING) Serial.print("[INFO] Resource found, parsing response: ");
String cardPayload = cardHTTP.getString(); String cardPayload = cardHTTP.getString();
cardPayload += "$"; // Mark the end cardPayload += String(EEPROM_END_MARKER);
if (LOGGING) Serial.println(cardPayload); if (LOGGING) Serial.println(cardPayload);
noInterrupts(); // commit() disables interrupts, but we want an atomic EEPROM buffer write noInterrupts(); // commit() disables interrupts, but we want an atomic EEPROM buffer write