#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcdPos(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
class Button { // a simple class for buttons based on the "state change detection" example
const byte buttonPin;
static constexpr byte debounceDelay = 30; // the debounce time; increase if the output flickers
const bool active; // is the pin active HIGH or active LOW (will also activate the pullups!)
bool lastButtonState = HIGH; // the previous reading from the input pin
byte lastDebounceTime = 0; // the last time the output pin was toggled - we check only ONE byte, so I didn't mess around with unsigned long
public:
/**
\brief constructor for a button
The constructor takes the GPIO as parameter.
If you omit the second parameter, the library will activate the internal pullup resistor
and the button should connect to GND.
If you set the second parameter to HIGH, the button is active HIGH. The button should
connect to VCC. The internal pullups will not be used.
\param attachTo the GPIO for the button
\param active LOW (default) - if button connects to GND, HIGH if button connects to VCC
*/
Button(byte attachTo, bool active = LOW) : buttonPin(attachTo), active(active) {}
/**
\brief set the pin to the proper state
Call this function in your setup().
The pinMode will be set according to your constructor.
*/
void begin() {
if (active == LOW)
pinMode(buttonPin, INPUT_PULLUP);
else
pinMode(buttonPin, INPUT);
}
/**
\brief indicate if button was pressed since last call
@return HIGH if button was pressed since last call - debounce
*/
bool wasPressed() {
bool buttonState = LOW; // the current reading from the input pin
byte reading = LOW;
if (digitalRead(buttonPin) == active) reading = HIGH; // if we are using INPUT_PULLUP we are checking invers to LOW Pin
if (((millis() & 0xFF ) - lastDebounceTime) > debounceDelay) // If the switch changed, AFTER any pressing or noise
{
if (reading != lastButtonState && lastButtonState == LOW) // If there was a change and and last state was LOW
{
buttonState = HIGH;
}
lastDebounceTime = millis() & 0xFF;
lastButtonState = reading;
}
return buttonState;
}
};
/* ein POS Terminal */
class POS {
protected:
Button btnAdd; // Button zum hochzählen
Button btnReduce; // Button zum runterzählen
Button btnEnter; // Button zum bestätigen
int currentCar = -1; // das aktuelle Auto
int topupAmount = 0;
enum State {IDLE, // auf ein Auto warten
ACTIVE // einen Kunden bedienen
};
State state;
public:
POS (byte addPin, byte reducePin, byte enterPin) : btnAdd{addPin}, btnReduce{reducePin}, btnEnter{enterPin} {}
void begin()
{
btnAdd.begin();
btnReduce.begin();
btnEnter.begin();
}
void start(int newCar) // Übergabe der Car ID an die Terminal Klasse
{
if (state == IDLE)
{
currentCar = newCar;
state = ACTIVE;
lcdPos.setCursor(0, 1);
lcdPos.print(F("topup credit"));
}
else
{
Serial.println(F("Kundensession ist schon aktiv - kein neues Auto möglich"));
}
}
void showTopupAmount() // Ausgabe des gewählten Betrages am LCD
{
lcdPos.setCursor(0, 2);
lcdPos.print(F("topup: "));
lcdPos.setCursor(7, 2);
lcdPos.print(topupAmount);
}
void update()
{
switch (state)
{
case IDLE:
if (btnAdd.wasPressed() ) {
Serial.println(F("add idle")); // nur damit man sieht das sich was tut
start(0); // nur Simulation Auto n geht zur Kassa.
}
if (btnReduce.wasPressed() ) {
Serial.println(F("reduce idle"));
start(1); // nur Simulation Auto n geht zur Kassa.
}
if (btnEnter.wasPressed() ) {
Serial.println(F("enter idle"));
start(2); // nur Simulation Auto n geht zur Kassa.
}
break;
case ACTIVE:
if (btnAdd.wasPressed()) {
Serial.println(F("add pressed"));
topupAmount += 10;
showTopupAmount();
}
if (btnReduce.wasPressed()) {
Serial.println(F("reduce pressed"));
if (topupAmount - 10 > 0) topupAmount -= 10;
showTopupAmount();
}
if (btnEnter.wasPressed()) {
Serial.println(F("Enter pressed"));
int32_t saldo;
// saldo = car[currentCar].getSaldo; // aktuellen Saldo lesen, todo: getter fehlt vermutlich noch
saldo += topupAmount;
// car[currentCar].setSaldo (saldo);
// irgend eine Ausgabe:
lcdPos.setCursor(0, 2);
lcdPos.print(F("Saldo neu "));
lcdPos.setCursor(10, 2);
lcdPos.print(saldo);
currentCar = -1; // auto löschen
state = IDLE;
}
break;
}
}
};
POS pos{A0, A1, A2}; // ein POS Terminal mit 3 Tasten
void setup()
{
Serial.begin(115200);
lcdPos.init(); // initialize the lcd
// Print a message to the LCD.
lcdPos.backlight();
lcdPos.setCursor(3, 0);
lcdPos.print(F("Hello, world!"));
pos.begin();
}
void loop()
{
pos.update();
}