ESP8266-Firmware/src/Interface.cpp

210 lines
6.6 KiB
C++

#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);
}
int Interface::getState(){
return _display_state;
}
void Interface::render()
{
if (this->_keyboard->available())
{
this->_inputTimeout = millis();
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->_display_state==states::ENTER_PIN_ADD_NUM||this->_display_state==states::ENTER_PIN_START){
if(millis()-_inputTimeout>10000){
this->_keyboard->clear();
this->setState(states::ABORT);
}
}
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 = 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;
}
}
}