beerbox/inputs.ino

217 lines
5.7 KiB
Arduino
Raw Normal View History

2015-11-19 19:38:42 +00:00
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;
2015-11-19 19:38:42 +00:00
currentDrink++;
2016-07-04 13:37:55 +00:00
if(debug)Serial.println(box->drinks[currentDrink].name);
2015-11-19 19:38:42 +00:00
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}
else
{
//revert save
persons[lastPersonIndex].drinks_taken[currentDrink]--;
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!
2016-07-04 13:37:55 +00:00
// 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);
2015-11-19 19:38:42 +00:00
//ok now check which person that is!
if (getFingerIndex(finger.fingerID) != -1) {
2016-07-04 13:37:55 +00:00
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);
2015-11-19 19:38:42 +00:00
}
}
bool checkRFID() {
2015-11-19 19:38:42 +00:00
Serial1.flush();
if (Serial1.available() >= gesamtKennungLaenge) // wenn genug Zeichen eingegangen ...
{
if (Serial1.read() == startZeichen) // und das Startzeichen erkannt wurde ...
{
2016-07-04 13:37:55 +00:00
//if(debug)Serial.print("Start-Byte erkannt ... ");
2015-11-19 19:38:42 +00:00
bytesGelesen = 0; // starten Lesen, also Zaehler auf 0
while (bytesGelesen < kennungLaenge) // lese 12-stelligen Code
{
char zeichen = Serial1.read(); // lesen Zeichen
2016-07-04 13:37:55 +00:00
//if(debug)Serial.print(zeichen); // zur Kontrolle
//if(debug)Serial.print(" ");
2015-11-19 19:38:42 +00:00
if (zeichen == endeZeichen) // suche nach Ende-Befehl
{
2016-07-04 13:37:55 +00:00
//if(debug)Serial.println("CR erkannt ...");
2015-11-19 19:38:42 +00:00
break;
}
code[bytesGelesen] = zeichen; // speicher Zeichen im Puffer
bytesGelesen = bytesGelesen + 1; // ein Zeichen weiter
}
code[bytesGelesen] = 0; // beende Zeichenkette
2016-07-04 13:37:55 +00:00
if(debug)Serial.print("RFID-Code lautet: ");
if(debug)Serial.println(code);
2015-11-19 19:38:42 +00:00
digitalWrite(led,HIGH);
ledTime = millis();
2016-07-04 13:37:55 +00:00
//if(debug)Serial.print("This is ");
//if(debug)Serial.println(persons[getRFIDIndex(persons, code)].name);
if(getRFIDIndex( code)!=-1){
if(!box->scanning)
2015-11-19 19:38:42 +00:00
countUp(getRFIDIndex( code), 1); // 1 for rfid context
else print_person_JSON(box,&persons[getRFIDIndex( code)],&Serial3);
}else{
2016-07-04 13:37:55 +00:00
if(debug)Serial.println("Error: Nonexisting Card");
}
2015-11-19 19:38:42 +00:00
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++) {
2015-11-19 19:38:42 +00:00
if (!strcmp(persons[i].rfid_uuid, rfid)) {
return i;
}
}
return -1;
}
int getFingerIndex( uint8_t fingerID) {
for (int i = 0; i < box->personCount; i++) {
2015-11-19 19:38:42 +00:00
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;
}
2015-11-19 19:38:42 +00:00
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...
2015-11-19 19:38:42 +00:00
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;
}
}
}