1
0
beerbox/inputs.ino
Lukas Bachschwell 0ff99e00f3 implemented delete
implemented namechange
fixed bug in saving credits
added reset all counts!
added less outputmode to print Person JSON
2016-07-29 21:32:49 +02:00

219 lines
5.8 KiB
C++

void checkButton() {
//one change per click to make seleting drinks easier for the drunken
//if (digitalRead(2) != lastButtonState) {
//delayMicroseconds(50); //debounce delay
if (digitalRead(2) != lastButtonState) {
//ok we have a change!
if (digitalRead(2) == LOW) {
//pushed
digitalWrite(led,HIGH);
ledTime = millis();
if (box->waiting == 0) {
if (currentDrink == box->maxDrink - 1) currentDrink = -1;
currentDrink++;
if(debug)Serial.println(box->drinks[currentDrink].name);
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}
else
{
//revert save
persons[lastPersonIndex].drinks_taken[currentDrink]--;
persons[personIndex].credits_left = persons[personIndex].credits_left + box->drinks[currentDrink].price;
box->waiting = 0;
lcd.clear();
lcd.print("Abort!");
delay(500);
//reset to default
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}
}
else {//released
}
lastButtonState = digitalRead(2);
}
//}
}
bool checkFinger() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
digitalWrite(led,HIGH);
ledTime = millis();
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
// if(debug)Serial.print("Found ID #"); if(debug)Serial.print(finger.fingerID);
// if(debug)Serial.print(" with confidence of "); if(debug)Serial.println(finger.confidence);
//ok now check which person that is!
if (getFingerIndex(finger.fingerID) != -1) {
if(debug)Serial.print("This is ");
if(debug)Serial.println(persons[getFingerIndex(finger.fingerID)].name);
if(!box->scanning) countUp(getFingerIndex(finger.fingerID), 0);//for finger context
else print_person_JSON(box,&persons[getFingerIndex(finger.fingerID)],&Serial3, false);
}
}
bool checkRFID() {
Serial1.flush();
if (Serial1.available() >= gesamtKennungLaenge) // wenn genug Zeichen eingegangen ...
{
if (Serial1.read() == startZeichen) // und das Startzeichen erkannt wurde ...
{
//if(debug)Serial.print("Start-Byte erkannt ... ");
bytesGelesen = 0; // starten Lesen, also Zaehler auf 0
while (bytesGelesen < kennungLaenge) // lese 12-stelligen Code
{
char zeichen = Serial1.read(); // lesen Zeichen
//if(debug)Serial.print(zeichen); // zur Kontrolle
//if(debug)Serial.print(" ");
if (zeichen == endeZeichen) // suche nach Ende-Befehl
{
//if(debug)Serial.println("CR erkannt ...");
break;
}
code[bytesGelesen] = zeichen; // speicher Zeichen im Puffer
bytesGelesen = bytesGelesen + 1; // ein Zeichen weiter
}
code[bytesGelesen] = 0; // beende Zeichenkette
if(debug)Serial.print("RFID-Code lautet: ");
if(debug)Serial.println(code);
digitalWrite(led,HIGH);
ledTime = millis();
//if(debug)Serial.print("This is ");
//if(debug)Serial.println(persons[getRFIDIndex(persons, code)].name);
if(getRFIDIndex( code)!=-1){
if(!box->scanning)
countUp(getRFIDIndex( code), 1); // 1 for rfid context
else print_person_JSON(box,&persons[getRFIDIndex( code)],&Serial3, false);
}else{
if(debug)Serial.println("Error: Nonexisting Card");
}
zugang = "";
delay(1000); // debounce time
serial1_flush_buffer();
}
}
}
void serial1_flush_buffer()
{
while (Serial1.read() >= 0)
; // do nothing
}
//
//LB
//Funktion schreiben, welche ein Array vom struct Person (Beliebige Groesse) sowie eine rfid-kennung uebernimmt und den Index der entsprechenden Person im array (beginnend bei 0)zurueckgiebt.
int getRFIDIndex( char* rfid) {
for (int i = 0; i < box->personCount; i++) {
if (!strcmp(persons[i].rfid_uuid, rfid)) {
return i;
}
}
return -1;
}
int getFingerIndex( uint8_t fingerID) {
for (int i = 0; i < box->personCount; i++) {
if (persons[i].finger_uuid == fingerID) {
return i;
}
}
return -1;
}
int getFileIndex( char* filename) {
for (int i = 0; i < box->personCount; i++) {
if(!strcmp(persons[i].file_name,filename)) {
return i;
}
}
return -1;
}
void countUp(int personIndex, int context) {
lcd.clear();
lcd.print(persons[personIndex].name);
lcd.setCursor(0, 1);
persons[personIndex].drinks_taken[currentDrink]++;
//now care about credits
persons[personIndex].credits_left = persons[personIndex].credits_left - box->drinks[currentDrink].price ;
lcd.print(persons[personIndex].drinks_taken[currentDrink]); // Todo this can be changed with the credits left later...
lcd.print(" Ok? ");
lcd.setCursor(7, 1);
lcd.print("3");
lastPersonIndex = personIndex;
box->waiting = 3;
oldTime = millis();
}
void boxTimer() {
if (box->waiting != 0) {
if ((millis() - oldTime) > 1000)
{
if (box->waiting == 3) {
lcd.setCursor(7, 1);
lcd.print("2");
box->waiting = 2;
}
else if (box->waiting == 2) {
lcd.setCursor(7, 1);
lcd.print("1");
box->waiting = 1;
} else if (box->waiting == 1) {
update_pers_file();
lcd.clear();
lcd.print("Saved!");
delay(500);
//reset to default
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
box->waiting = 0;
digitalWrite(led,false);
}
oldTime = millis();
}
}
}
void ledTimer() {
if (ledTime != 0) {
if ((millis() - ledTime) > 5000)
{
digitalWrite(led,LOW);
ledTime = 0;
}
}
}