adding userdb
This commit is contained in:
parent
24783454e8
commit
06a1750719
200
src/Interface.cpp
Normal file
200
src/Interface.cpp
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
#include "Interface.h"
|
||||||
|
|
||||||
|
Interface::Interface(/* args */) : _lcd(0x27, 16, 2)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Interface::~Interface()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void Interface::begin(Keyboard *Keyboard)
|
||||||
|
{
|
||||||
|
this->_keyboard = Keyboard;
|
||||||
|
this->_lcd.init();
|
||||||
|
this->_lcd.backlight();
|
||||||
|
}
|
||||||
|
void Interface::setState(states state)
|
||||||
|
{
|
||||||
|
this->_display_state = state;
|
||||||
|
this->_displayUpdate = true;
|
||||||
|
}
|
||||||
|
void Interface::greetUser(String username)
|
||||||
|
{
|
||||||
|
this->_username = username;
|
||||||
|
this->setState(states::GREET);
|
||||||
|
}
|
||||||
|
String Interface::getPin()
|
||||||
|
{
|
||||||
|
String a = _pin_number;
|
||||||
|
this->_pin_number = "";
|
||||||
|
this->_pin_available = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
bool Interface::pinAvailable()
|
||||||
|
{
|
||||||
|
return _pin_available && _pin_number.length() > 0;
|
||||||
|
}
|
||||||
|
void Interface::showMessage(String msg1, String msg2, unsigned long display_time)
|
||||||
|
{
|
||||||
|
this->_msg1 = msg1;
|
||||||
|
this->_msg2 = msg2;
|
||||||
|
this->_msg_display_time = display_time;
|
||||||
|
this->setState(states::MSG);
|
||||||
|
}
|
||||||
|
void Interface::render()
|
||||||
|
{
|
||||||
|
if (this->_keyboard->available())
|
||||||
|
{
|
||||||
|
if (this->_keyboard->getLastChr() == 'X')
|
||||||
|
{
|
||||||
|
this->_keyboard->clear();
|
||||||
|
this->setState(states::ABORT);
|
||||||
|
}
|
||||||
|
else if (('0' <= this->_keyboard->getLastChr()) && (this->_keyboard->getLastChr() <= '9'))
|
||||||
|
{
|
||||||
|
Serial.print(this->_keyboard->getLastChr());
|
||||||
|
if (this->_display_state == states::ENTER_PIN_START)
|
||||||
|
this->setState(states::ENTER_PIN_ADD_NUM);
|
||||||
|
else if (this->_display_state != states::ENTER_PIN_ADD_NUM)
|
||||||
|
this->setState(states::ENTER_PIN_START);
|
||||||
|
else if (this->_display_state == states::ENTER_PIN_ADD_NUM)
|
||||||
|
this->_displayUpdate = true;
|
||||||
|
}
|
||||||
|
else if (this->_keyboard->getLastChr() == 'O')
|
||||||
|
{
|
||||||
|
if (this->_display_state == 2 || this->_display_state == 1)
|
||||||
|
{
|
||||||
|
this->_keyboard->clear();
|
||||||
|
this->_pin_available = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->showMessage("Error", "Please enter pin!", 5000);
|
||||||
|
this->_keyboard->clear();
|
||||||
|
}
|
||||||
|
//this->setState(states::VALIDATE_PIN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->_displayUpdate)
|
||||||
|
{
|
||||||
|
switch (this->_display_state)
|
||||||
|
{
|
||||||
|
case states::MAIN:
|
||||||
|
this->_lcd.setCursor(0, 0);
|
||||||
|
this->_lcd.print("Welcome!");
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
this->_lcd.print("Enter Pin / Card");
|
||||||
|
this->_displayUpdate = false;
|
||||||
|
break;
|
||||||
|
case states::ENTER_PIN_START: // Starting Pin
|
||||||
|
_pin_number.clear();
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_lcd.print("Please enter Pin");
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
this->_pin_number.concat(this->_keyboard->getString());
|
||||||
|
this->_lcd.print("Pin: " + _pin_number);
|
||||||
|
this->_display_state = 2;
|
||||||
|
this->_displayUpdate = false;
|
||||||
|
break;
|
||||||
|
case states::ENTER_PIN_ADD_NUM:
|
||||||
|
{
|
||||||
|
String input = this->_keyboard->getString();
|
||||||
|
this->_pin_number.concat(input);
|
||||||
|
this->_lcd.print(input);
|
||||||
|
this->_displayUpdate = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case states::VALIDATE_PIN:
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_lcd.setCursor(0, 0);
|
||||||
|
this->_lcd.print("Validating PIN");
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
if (_pin_number == "2626")
|
||||||
|
this->_lcd.print("Accepted!");
|
||||||
|
else
|
||||||
|
this->_lcd.print("Acess denied!");
|
||||||
|
this->_pin_number.clear();
|
||||||
|
this->_displayTimer1 = millis() + 3000;
|
||||||
|
this->_display_state = states::DELAY;
|
||||||
|
break;
|
||||||
|
case states::ABORT:
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_lcd.print("Input aborted!");
|
||||||
|
this->_displayTimer1 = millis() + 3000;
|
||||||
|
this->_display_state = states::DELAY;
|
||||||
|
break;
|
||||||
|
case states::DELAY: // Delay
|
||||||
|
if ((this->_displayTimer1 != 0) && _displayTimer1 < millis())
|
||||||
|
{
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_display_state = states::MAIN;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case states::READ_RFID:
|
||||||
|
{
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_lcd.print("Card detected.");
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
|
||||||
|
this->_lcd.print("rfid");
|
||||||
|
this->_displayTimer1 = millis() + 3000;
|
||||||
|
this->_display_state = states::DELAY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case states::GREET:
|
||||||
|
{
|
||||||
|
if (this->_scroll_index==0)
|
||||||
|
{
|
||||||
|
this->_displayTimer1 = millis();
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_lcd.setCursor(0, 0);
|
||||||
|
this->_lcd.print("Welcome back");
|
||||||
|
}
|
||||||
|
if (_username.length() > 16)
|
||||||
|
{
|
||||||
|
if (this->_scroll_index + 16 <= _username.length() && this->_displayTimer2 + 800 < millis())
|
||||||
|
{
|
||||||
|
this->_displayTimer2 = millis();
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
this->_lcd.print(_username.substring(0 + this->_scroll_index, 16 + _scroll_index));
|
||||||
|
this->_scroll_index++;
|
||||||
|
}
|
||||||
|
else if (this->_scroll_index + 16 > _username.length())
|
||||||
|
{
|
||||||
|
this->_displayTimer1 = millis() + 3000;
|
||||||
|
this->_display_state = states::DELAY;
|
||||||
|
this->_scroll_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
this->_lcd.print(_username);
|
||||||
|
this->_displayTimer1 = millis() + 3000;
|
||||||
|
this->_display_state = states::DELAY;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case states::MSG:
|
||||||
|
this->_lcd.clear();
|
||||||
|
this->_lcd.setCursor(0, 0);
|
||||||
|
this->_lcd.print(this->_msg1);
|
||||||
|
this->_msg1 = "";
|
||||||
|
this->_lcd.setCursor(0, 1);
|
||||||
|
this->_lcd.print(this->_msg2);
|
||||||
|
this->_msg2 = "";
|
||||||
|
if (this->_msg_display_time == 0)
|
||||||
|
this->_displayUpdate = false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->_displayTimer1 = millis() + this->_msg_display_time;
|
||||||
|
this->_display_state = states::DELAY;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
src/Interface.h
Normal file
44
src/Interface.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <LiquidCrystal_I2C.h>
|
||||||
|
#include "Keyboard.h"
|
||||||
|
class Interface
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
LiquidCrystal_I2C _lcd;
|
||||||
|
Keyboard* _keyboard;
|
||||||
|
String _pin_number;
|
||||||
|
bool _pin_available;
|
||||||
|
int _display_state = 0;
|
||||||
|
unsigned long _lastDisplayUpdate = 0;
|
||||||
|
unsigned long _displayTimer1 = 0;
|
||||||
|
unsigned long _displayTimer2 = 0;
|
||||||
|
bool _displayUpdate = true;
|
||||||
|
String _username = "";
|
||||||
|
unsigned int _scroll_index = 0;
|
||||||
|
String _msg1 = "";
|
||||||
|
String _msg2 = "";
|
||||||
|
unsigned long _msg_display_time = 0;
|
||||||
|
;
|
||||||
|
public:
|
||||||
|
enum states {
|
||||||
|
MAIN,
|
||||||
|
ENTER_PIN_START,
|
||||||
|
ENTER_PIN_ADD_NUM,
|
||||||
|
VALIDATE_PIN,
|
||||||
|
ABORT,
|
||||||
|
DELAY,
|
||||||
|
READ_RFID,
|
||||||
|
GREET,
|
||||||
|
MSG
|
||||||
|
|
||||||
|
};;
|
||||||
|
Interface(/* args */);
|
||||||
|
~Interface();
|
||||||
|
void begin(Keyboard* Keyboard);
|
||||||
|
void render();
|
||||||
|
void setState(states state);
|
||||||
|
void greetUser(String username);
|
||||||
|
String getPin();
|
||||||
|
bool pinAvailable();
|
||||||
|
void showMessage(String msg1, String msg2, unsigned long display_time);
|
||||||
|
};
|
36
src/Rfid.cpp
36
src/Rfid.cpp
@ -2,7 +2,7 @@
|
|||||||
#define SS_PIN D8
|
#define SS_PIN D8
|
||||||
#define RST_PIN D1
|
#define RST_PIN D1
|
||||||
#define RFID_TIMEOUT 3000
|
#define RFID_TIMEOUT 3000
|
||||||
Rfid::Rfid(/* args */) : mfrc522(SS_PIN)
|
Rfid::Rfid(/* args */) : _mfrc522(SS_PIN)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,37 +13,37 @@ void Rfid::begin()
|
|||||||
{
|
{
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
SPI.setClockDivider(SPI_CLOCK_DIV8);
|
SPI.setClockDivider(SPI_CLOCK_DIV8);
|
||||||
this->mfrc522.PCD_Init();
|
this->_mfrc522.PCD_Init();
|
||||||
this->mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
|
this->_mfrc522.PCD_SetAntennaGain(_mfrc522.RxGain_max);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rfid::scan()
|
void Rfid::scan()
|
||||||
{
|
{
|
||||||
if (this->mfrc522.PICC_IsNewCardPresent() && this->mfrc522.PICC_ReadCardSerial())
|
if (this->_mfrc522.PICC_IsNewCardPresent() && this->_mfrc522.PICC_ReadCardSerial())
|
||||||
{
|
{
|
||||||
this->rfid = "";
|
this->_rfid = "";
|
||||||
for (byte i = 0; i < this->mfrc522.uid.size; i++)
|
for (byte i = 0; i < this->_mfrc522.uid.size; i++)
|
||||||
{
|
{
|
||||||
this->rfid += this->mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ";
|
this->_rfid += this->_mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ";
|
||||||
this->rfid += String(this->mfrc522.uid.uidByte[i], HEX);
|
this->_rfid += String(this->_mfrc522.uid.uidByte[i], HEX);
|
||||||
}
|
}
|
||||||
this->rfid.trim();
|
this->_rfid.trim();
|
||||||
this->rfid.toUpperCase();
|
this->_rfid.toUpperCase();
|
||||||
if (this->rfid != this->lastRfid || millis() > this->lastRfidScan + RFID_TIMEOUT)
|
if (this->_rfid != this->_lastRfid || millis() > this->_lastRfidScan + RFID_TIMEOUT)
|
||||||
{
|
{
|
||||||
status = 1;
|
_status = 1;
|
||||||
Serial.print(this->rfid);
|
Serial.print(this->_rfid);
|
||||||
this->lastRfid = this->rfid;
|
this->_lastRfid = this->_rfid;
|
||||||
this->lastRfidScan = millis();
|
this->_lastRfidScan = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rfid::available()
|
bool Rfid::available()
|
||||||
{
|
{
|
||||||
if (this->status==1)
|
if (this->_status==1)
|
||||||
{
|
{
|
||||||
this->status = 0;
|
this->_status = 0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -51,5 +51,5 @@ bool Rfid::available()
|
|||||||
}
|
}
|
||||||
String Rfid::getID()
|
String Rfid::getID()
|
||||||
{
|
{
|
||||||
return this->rfid;
|
return this->_rfid;
|
||||||
}
|
}
|
12
src/Rfid.h
12
src/Rfid.h
@ -4,12 +4,12 @@
|
|||||||
class Rfid
|
class Rfid
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
MFRC522 mfrc522; // Create MFRC522 instance
|
MFRC522 _mfrc522; // Create MFRC522 instance
|
||||||
MFRC522::MIFARE_Key key;
|
MFRC522::MIFARE_Key _key;
|
||||||
int status = 0;
|
int _status = 0;
|
||||||
String rfid = "";
|
String _rfid = "";
|
||||||
String lastRfid = "";
|
String _lastRfid = "";
|
||||||
unsigned long lastRfidScan = 0;
|
unsigned long _lastRfidScan = 0;
|
||||||
public:
|
public:
|
||||||
Rfid(/* args */);
|
Rfid(/* args */);
|
||||||
~Rfid();
|
~Rfid();
|
||||||
|
152
src/main.cpp
152
src/main.cpp
@ -3,151 +3,67 @@
|
|||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include "Keyboard.h"
|
#include "Keyboard.h"
|
||||||
#include "Rfid.h"
|
#include "Rfid.h"
|
||||||
|
#include "Interface.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// Rfid
|
// Rfid
|
||||||
Rfid rfid;
|
Rfid rfid;
|
||||||
// i2C Bus
|
// i2C Bus
|
||||||
#define PIN_WIRE_SDA D3
|
#define PIN_WIRE_SDA D3
|
||||||
#define PIN_WIRE_SCL D4
|
#define PIN_WIRE_SCL D4
|
||||||
#include <LiquidCrystal_I2C.h>
|
|
||||||
Keyboard keyboard(200);
|
Keyboard keyboard(200);
|
||||||
LiquidCrystal_I2C lcd(0x27, 20, 4);
|
Interface iface;
|
||||||
int display_state = 0;
|
|
||||||
String pin;
|
// User DB
|
||||||
unsigned long lastDisplayUpdate = 0;
|
|
||||||
unsigned long displayTimer1 = 0;
|
typedef struct
|
||||||
bool displayUpdate = true;
|
{
|
||||||
|
String uid;
|
||||||
|
String first_name;
|
||||||
|
String last_name;
|
||||||
|
String rfid_uid;
|
||||||
|
String user_pin;
|
||||||
|
} user_account;
|
||||||
|
std::vector<user_account> userdb;
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.print("Starting");
|
Serial.print("Starting");
|
||||||
delay(500);
|
delay(500);
|
||||||
keyboard.begin(&Wire);
|
keyboard.begin(&Wire);
|
||||||
|
|
||||||
// LCD
|
|
||||||
lcd.init();
|
|
||||||
lcd.backlight();
|
|
||||||
rfid.begin();
|
rfid.begin();
|
||||||
|
iface.begin(&keyboard);
|
||||||
|
userdb.push_back(user_account{.uid = "010202", .first_name = "Max", .last_name = "Mustermann and Sons", .rfid_uid = "D3 4E 03 0B", .user_pin = "1212"});
|
||||||
|
userdb.push_back(user_account{.uid = "012345", .first_name = "John", .last_name = "Doe", .rfid_uid = "8C 75 6C 17", .user_pin = "2424"});
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace states
|
|
||||||
{
|
|
||||||
enum screenstates
|
|
||||||
{
|
|
||||||
MAIN,
|
|
||||||
ENTER_PIN_START,
|
|
||||||
ENTER_PIN_ADD_NUM,
|
|
||||||
VALIDATE_PIN,
|
|
||||||
ABORT,
|
|
||||||
DELAY,
|
|
||||||
READ_RFID
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
keyboard.scanAsync();
|
keyboard.scanAsync();
|
||||||
rfid.scan();
|
rfid.scan();
|
||||||
if(rfid.available()){
|
if (iface.pinAvailable())
|
||||||
display_state = states::READ_RFID;
|
|
||||||
displayUpdate = true;
|
|
||||||
}
|
|
||||||
if (keyboard.available())
|
|
||||||
{
|
{
|
||||||
if (keyboard.getLastChr() == 'X')
|
String entered_pin = iface.getPin();
|
||||||
|
std::for_each(userdb.begin(), userdb.end(), [=](user_account user)
|
||||||
{
|
{
|
||||||
keyboard.clear();
|
if (entered_pin == user.user_pin)
|
||||||
display_state = states::ABORT;
|
|
||||||
displayUpdate = true;
|
|
||||||
}
|
|
||||||
else if (('0' <= keyboard.getLastChr()) && (keyboard.getLastChr() <= '9'))
|
|
||||||
{
|
{
|
||||||
Serial.print(keyboard.getLastChr());
|
iface.greetUser(user.first_name + " " + user.last_name);
|
||||||
if (display_state == states::ENTER_PIN_START)
|
|
||||||
display_state = states::ENTER_PIN_ADD_NUM;
|
|
||||||
else if (display_state != states::ENTER_PIN_ADD_NUM)
|
|
||||||
display_state = states::ENTER_PIN_START;
|
|
||||||
displayUpdate = true;
|
|
||||||
}
|
}
|
||||||
else if (keyboard.getLastChr() == 'O' && (display_state == 2 || display_state == 1))
|
});
|
||||||
|
}
|
||||||
|
std::cop
|
||||||
|
else if (rfid.available())
|
||||||
{
|
{
|
||||||
keyboard.clear();
|
String rfid_card_uid = rfid.getID();
|
||||||
display_state = states::VALIDATE_PIN;
|
std::for_each(userdb.begin(), userdb.end(), [=](user_account user)
|
||||||
displayUpdate = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (displayUpdate)
|
|
||||||
{
|
{
|
||||||
switch (display_state)
|
if (rfid_card_uid == user.rfid_uid)
|
||||||
{
|
{
|
||||||
case states::MAIN:
|
iface.greetUser(user.first_name + " " + user.last_name);
|
||||||
lcd.setCursor(0, 0);
|
|
||||||
lcd.print("Welcome!");
|
|
||||||
lcd.setCursor(0, 1);
|
|
||||||
lcd.print("Enter Pin / Card");
|
|
||||||
displayUpdate = false;
|
|
||||||
break;
|
|
||||||
case states::ENTER_PIN_START: // Starting Pin
|
|
||||||
pin.clear();
|
|
||||||
lcd.clear();
|
|
||||||
lcd.print("Please enter Pin");
|
|
||||||
lcd.setCursor(0, 1);
|
|
||||||
pin.concat(keyboard.getString());
|
|
||||||
lcd.print("Pin: " + pin);
|
|
||||||
display_state = 2;
|
|
||||||
displayUpdate = false;
|
|
||||||
break;
|
|
||||||
case states::ENTER_PIN_ADD_NUM:
|
|
||||||
{
|
|
||||||
String input = keyboard.getString();
|
|
||||||
pin.concat(input);
|
|
||||||
lcd.print(input);
|
|
||||||
displayUpdate = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case states::VALIDATE_PIN:
|
});
|
||||||
lcd.clear();
|
|
||||||
lcd.setCursor(0, 0);
|
|
||||||
lcd.print("Validating PIN");
|
|
||||||
lcd.setCursor(0, 1);
|
|
||||||
if (pin == "2626")
|
|
||||||
lcd.print("Accepted!");
|
|
||||||
else
|
|
||||||
lcd.print("Acess denied!");
|
|
||||||
pin.clear();
|
|
||||||
displayTimer1 = millis() + 3000;
|
|
||||||
display_state = states::DELAY;
|
|
||||||
break;
|
|
||||||
case states::ABORT:
|
|
||||||
lcd.clear();
|
|
||||||
lcd.print("Input aborted!");
|
|
||||||
displayTimer1 = millis() + 3000;
|
|
||||||
display_state = states::DELAY;
|
|
||||||
break;
|
|
||||||
case states::DELAY: // Delay
|
|
||||||
if (displayTimer1 != 0 && displayTimer1 < millis())
|
|
||||||
{
|
|
||||||
lcd.clear();
|
|
||||||
display_state = states::MAIN;
|
|
||||||
}
|
}
|
||||||
break;
|
iface.render();
|
||||||
case states::READ_RFID:
|
|
||||||
{
|
|
||||||
lcd.clear();
|
|
||||||
lcd.print("Card detected.");
|
|
||||||
lcd.setCursor(0, 1);
|
|
||||||
|
|
||||||
lcd.print(rfid.getID());
|
|
||||||
displayTimer1 = millis() + 3000;
|
|
||||||
display_state = states::DELAY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//String input = keyboard.getString();
|
|
||||||
//Serial.println("Key " + input + "was pressed");
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user