

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 == drinksMax - 1) currentDrink = -1;
        currentDrink++;
        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]--;
        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!
  // Serial.print("Found ID #"); Serial.print(finger.fingerID);
  // Serial.print(" with confidence of "); Serial.println(finger.confidence);

  //ok now check which person that is!
  
  if (getFingerIndex(finger.fingerID) != -1) {
    Serial.print("This is ");
    Serial.println(persons[getFingerIndex(finger.fingerID)].name);
    countUp(getFingerIndex(finger.fingerID), 0);//for finger context
  }
}

void checkRFID() {
  Serial1.flush();
  if (Serial1.available() >=  gesamtKennungLaenge) // wenn genug Zeichen eingegangen ...
  {
    if (Serial1.read() == startZeichen) // und das Startzeichen erkannt wurde ...
    {
      //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
        //Serial.print(zeichen); // zur Kontrolle
        //Serial.print(" ");
        if (zeichen == endeZeichen) // suche nach Ende-Befehl
        {
          //Serial.println("CR erkannt ...");
          break;
        }
        code[bytesGelesen] = zeichen; // speicher Zeichen im Puffer
        bytesGelesen = bytesGelesen + 1; // ein Zeichen weiter
      }
      code[bytesGelesen] = 0; // beende Zeichenkette
      Serial.print("RFID-Code lautet: ");
      Serial.println(code);
      digitalWrite(led,HIGH);
      ledTime = millis();

      //Serial.print("This is ");
      //Serial.println(persons[getRFIDIndex(persons, code)].name);
      if(getRFIDIndex( code)!=-1)
      countUp(getRFIDIndex( code), 1); // 1 for rfid context
      else
      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 < personCount; i++) {
    if (!strcmp(persons[i].rfid_uuid, rfid)) {
      return i;
    }
  }
  return -1;

}

int getFingerIndex( uint8_t fingerID) {
  for (int i = 0; i < personCount; i++) {
    if (persons[i].finger_uuid == fingerID) {
      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]++;
  lcd.print(persons[personIndex].drinks_taken[currentDrink]);
  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;
    }
  }
}

