2015-07-13 14:49:36 +00:00
# include <SPI.h>
2015-09-18 17:32:11 +00:00
# include <Wire.h>
2015-07-13 14:49:36 +00:00
# include <SD.h>
2015-09-18 17:32:11 +00:00
# include <LiquidCrystal_I2C.h>
2015-07-13 17:09:27 +00:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "beerbox.h"
2015-07-16 13:39:44 +00:00
# include <Adafruit_Fingerprint.h>
2015-09-18 17:32:11 +00:00
2015-07-16 13:39:44 +00:00
Adafruit_Fingerprint finger = Adafruit_Fingerprint ( & Serial2 ) ;
2015-09-18 17:32:11 +00:00
LiquidCrystal_I2C lcd ( 0x38 , 8 , 2 ) ; // 0x38 for PCF***A on address 000
2015-07-16 13:39:44 +00:00
//######### RFID CONSTANTS ##########
const int startZeichen = 02 ; // Chip-Kennung beginnt mit 02
const int endeZeichen = 03 ; // ASCII CR bendet Code
const int kennungLaenge = 12 ; // Laenge Chip-Code 10 ASCII-Zeichen
const int gesamtKennungLaenge = 14 ; // Code-Laenge + Start- und Ende-Byte
char code [ kennungLaenge + 1 ] ; // fuer den Code und abschliessende Null
int bytesGelesen = 0 ;
int zaehlerFehlerCode = 0 ;
String zugang = " 000000000000 " ; // zugangscode zwischenspeicher, Standard
//#######################
2015-07-13 14:49:36 +00:00
File myFile ;
const int chipSelect = 53 ;
2015-07-13 17:09:27 +00:00
char leckmich [ 255 ] ;
Beerbox * box ;
2015-07-13 14:49:36 +00:00
2015-09-18 17:32:11 +00:00
int drinksMax = 1 ;
int lastButtonState = 1 ;
int currentDrink = 0 ;
2015-07-13 14:49:36 +00:00
void setup ( ) {
2015-07-16 13:39:44 +00:00
//generall debugging
2015-07-13 14:49:36 +00:00
Serial . begin ( 115200 ) ;
2015-07-13 17:38:17 +00:00
2015-09-18 17:32:11 +00:00
//LCD init
lcd . init ( ) ;
2015-09-19 09:46:46 +00:00
pinMode ( 2 , INPUT_PULLUP ) ;
2015-09-18 17:32:11 +00:00
2015-07-16 13:39:44 +00:00
//Fingerprint init
finger . begin ( 57600 ) ;
if ( finger . verifyPassword ( ) ) {
Serial . println ( " Found fingerprint sensor! " ) ;
} else {
Serial . println ( " Did not find fingerprint sensor :( " ) ;
while ( 1 ) ;
2015-07-13 14:49:36 +00:00
}
2015-07-13 17:38:17 +00:00
2015-07-16 13:39:44 +00:00
// RFID Reader init
Serial1 . begin ( 9600 ) ;
2015-07-13 14:49:36 +00:00
Serial . print ( " Initializing SD card... " ) ;
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
2015-07-13 17:38:17 +00:00
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
2015-07-13 14:49:36 +00:00
pinMode ( SS , OUTPUT ) ;
if ( ! SD . begin ( chipSelect ) ) {
Serial . println ( " initialization failed! " ) ;
return ;
}
Serial . println ( " initialization done. " ) ;
2015-07-13 17:38:17 +00:00
2015-09-19 09:46:46 +00:00
//check_for_file("p001", "per");
readAllPersons ( ) ;
2015-07-13 17:38:17 +00:00
2015-07-13 17:09:27 +00:00
box = new Beerbox ;
box = read_beerbox ( box ) ;
2015-07-13 14:49:36 +00:00
2015-09-19 09:46:46 +00:00
2015-09-18 17:32:11 +00:00
lcd . print ( " BOXready " ) ;
lcd . setCursor ( 0 , 1 ) ;
2015-07-13 14:49:36 +00:00
}
void loop ( ) {
2015-07-16 13:39:44 +00:00
checkFinger ( ) ;
checkRFID ( ) ;
2015-09-18 17:32:11 +00:00
checkButton ( ) ;
2015-07-13 17:38:17 +00:00
2015-07-16 13:39:44 +00:00
//Person myPerson = read_person(box, "p001");
2015-07-13 17:09:27 +00:00
//read_person(box, "p002");
2015-07-13 17:38:17 +00:00
2015-07-13 14:49:36 +00:00
} //end loop
2015-09-18 17:32:11 +00:00
void checkButton ( ) {
//one change per click to make seleting drinks easier for the drunken
//if (digitalRead(2) != lastButtonState) {
2015-09-19 09:46:46 +00:00
//delayMicroseconds(50); //debounce delay
if ( digitalRead ( 2 ) ! = lastButtonState ) {
//ok we have a change!
if ( digitalRead ( 2 ) = = LOW ) {
//pushed
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 ) ;
2015-09-18 17:32:11 +00:00
}
2015-09-19 09:46:46 +00:00
else { //released
}
lastButtonState = digitalRead ( 2 ) ;
}
2015-09-18 17:32:11 +00:00
//}
}
2015-07-16 13:39:44 +00:00
bool checkFinger ( ) {
uint8_t p = finger . getImage ( ) ;
if ( p ! = FINGERPRINT_OK ) return - 1 ;
p = finger . image2Tz ( ) ;
if ( p ! = FINGERPRINT_OK ) return - 1 ;
p = finger . fingerFastSearch ( ) ;
if ( p ! = FINGERPRINT_OK ) return - 1 ;
// found a match!
2015-09-18 17:32:11 +00:00
Serial . print ( " Found ID # " ) ; Serial . print ( finger . fingerID ) ;
Serial . print ( " with confidence of " ) ; Serial . println ( finger . confidence ) ;
2015-07-16 13:39:44 +00:00
//TODO @ALEX Do something with finger ID
// finger.fingerID
2015-09-19 09:46:46 +00:00
//int getFingerIndex(Person* persons, finger.fingerID)
2015-07-16 13:39:44 +00:00
}
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 ) ;
zugang = code ; // Umwandlung Code in String
//TODO @Alex
// Do something with the 12 char string code
zugang = " " ;
delay ( 1000 ) ; // debounce time
serial1_flush_buffer ( ) ;
}
}
}
void serial1_flush_buffer ( )
{
while ( Serial1 . read ( ) > = 0 )
; // do nothing
}
2015-07-13 14:49:36 +00:00
2015-07-13 17:38:17 +00:00
void check_for_file ( String filename , String extension ) {
2015-07-13 14:49:36 +00:00
String str = filename + ' . ' + extension ;
char * buf = NULL ;
str . toCharArray ( buf , str . length ( ) + 1 ) ;
2015-07-13 17:38:17 +00:00
2015-07-13 14:49:36 +00:00
if ( SD . exists ( " p001.per " ) ) {
Serial . println ( str + " exists. " ) ;
}
else {
Serial . println ( str + " doesn't exist. " ) ;
}
}
2015-07-13 17:09:27 +00:00
//BEYOND THIS POINT IS THE LEYER OF BEERBOX -- ENTER AT YOUR OWN RISK
char tmp_filename [ FILE_NAME_LEN + FILE_EXTENSION_LEN + 2 ] ;
2015-07-13 17:38:17 +00:00
Beerbox * read_beerbox ( Beerbox * box ) {
2015-07-13 17:09:27 +00:00
File read ;
int num_read = 0 ;
int index ;
char tmp [ ( DRINK_NAME_MAX_LENGTH + 1 ) * NUM_OF_DRINKS ] ;
int i = 0 ;
read = SD . open ( " bb.con " , FILE_READ ) ;
2015-07-13 17:38:17 +00:00
if ( read = = false ) {
2015-07-13 17:09:27 +00:00
Serial . print ( " error while reading! \n " ) ;
Serial . print ( " -ABORT! \n \n " ) ;
2015-07-13 17:38:17 +00:00
while ( 1 ) ;
2015-07-13 17:09:27 +00:00
}
2015-09-19 09:46:46 +00:00
// get personcount Basically not needed...
// char tmpStr[19];
// read_line_from_file(read, tmpStr, sizeof(tmpStr));
// if (strlen(tmpStr)) {
// sscanf(tmpStr, "personCount: %d", &box->personCount);
// }
//
// sprintf(leckmich, "%i persons known!\n", box->personCount );
// Serial.print(leckmich);
2015-07-13 17:09:27 +00:00
2015-09-19 09:46:46 +00:00
//get drinks
2015-07-13 17:38:17 +00:00
for ( i = 0 ; i < NUM_OF_DRINKS ; i + + ) {
2015-07-13 17:09:27 +00:00
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
2015-07-13 17:38:17 +00:00
if ( strlen ( tmp ) ) {
sscanf ( tmp , " drink number %d: %s " , & index , box - > drinks [ i ] . name ) ;
2015-07-13 17:09:27 +00:00
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
2015-07-13 17:38:17 +00:00
sscanf ( tmp , " %*s %d " , & box - > drinks [ i ] . price ) ;
2015-07-13 17:09:27 +00:00
2015-07-13 17:38:17 +00:00
} else {
strcpy ( box - > drinks [ i ] . name , " slot empty " ) ;
box - > drinks [ i ] . price = - 1 ;
2015-07-13 17:09:27 +00:00
}
}
2015-09-19 09:46:46 +00:00
Serial . print ( i ) ;
Serial . println ( " drinks read! \n " ) ;
2015-07-13 17:09:27 +00:00
2015-07-13 17:38:17 +00:00
for ( i = 0 ; ( i < NUM_OF_DRINKS ) & & ( box - > drinks [ i ] . price ! = - 1 ) ; i + + ) {
2015-09-19 09:46:46 +00:00
sprintf ( leckmich , " drink number %i: %s \n price: %d \n " , i , box - > drinks [ i ] . name , box - > drinks [ i ] . price ) ;
Serial . println ( leckmich ) ;
2015-07-13 17:09:27 +00:00
}
2015-07-13 17:38:17 +00:00
if ( i < NUM_OF_DRINKS ) {
sprintf ( leckmich , " Slots %d - %d are not in use! \n " , i + 1 , NUM_OF_DRINKS ) ;
2015-09-18 17:32:11 +00:00
drinksMax = i ;
2015-09-19 09:46:46 +00:00
Serial . println ( leckmich ) ;
2015-07-13 17:09:27 +00:00
}
read . close ( ) ;
return box ;
}
2015-07-13 17:38:17 +00:00
Person read_person ( Beerbox * box , char * filename ) {
2015-07-13 17:09:27 +00:00
Person aperson ;
File read ;
int num_read = 0 ;
int index ;
int tmp_int = 0 ;
char tmp [ ( DRINK_NAME_MAX_LENGTH + 1 ) * NUM_OF_DRINKS ] ;
char tmp_drink_name [ DRINK_NAME_MAX_LENGTH + 1 ] ;
int i = 0 ;
2015-09-19 09:46:46 +00:00
//giving the full filename here, since it makes stuff easier
Serial . print ( " The filename is: " ) ;
Serial . println ( filename ) ;
read = SD . open ( filename , FILE_READ ) ;
2015-07-13 17:38:17 +00:00
if ( read = = false ) {
2015-07-13 17:09:27 +00:00
Serial . print ( " error while reading! \n " ) ;
Serial . print ( " ABORT! \n \n " ) ;
2015-07-13 17:38:17 +00:00
while ( - 1 ) ;
2015-07-13 17:09:27 +00:00
}
strcpy ( aperson . file_name , filename ) ;
2015-09-19 09:46:46 +00:00
for ( int i = 0 ; i < NUM_OF_DRINKS ; i + + ) {
2015-07-13 17:09:27 +00:00
aperson . drinks_taken [ i ] = - 1 ;
}
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
strcpy ( aperson . name , tmp ) ;
2015-07-13 17:38:17 +00:00
2015-09-19 09:46:46 +00:00
Serial . println ( " before memset " ) ;
2015-07-13 17:32:39 +00:00
memset ( aperson . rfid_uuid , 0 , 13 ) ;
2015-07-13 17:09:27 +00:00
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
2015-07-13 17:32:39 +00:00
sscanf ( tmp , " %*s %12s " , aperson . rfid_uuid ) ;
2015-09-19 09:46:46 +00:00
//Serial.print(aperson.rfid_uuid);
2015-07-13 17:38:17 +00:00
2015-07-13 17:09:27 +00:00
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
sscanf ( tmp , " %*s %d " , & aperson . finger_uuid ) ;
2015-09-19 09:46:46 +00:00
//Serial.println(aperson.finger_uuid);
2015-07-13 17:09:27 +00:00
2015-07-13 17:38:17 +00:00
while ( read_line_from_file ( read , tmp , sizeof ( tmp ) ) , strlen ( tmp ) > 0 ) {
2015-07-13 17:09:27 +00:00
i = 0 ;
sscanf ( tmp , " %*s %s " , tmp_drink_name ) ;
2015-07-13 17:38:17 +00:00
for ( i = 0 ; ( i < NUM_OF_DRINKS ) & & ( strcmp ( box - > drinks [ i ] . name , tmp_drink_name ) ) ; i + + ) {
2015-09-19 09:46:46 +00:00
Serial . print ( " It´ s not drink No : " ) ;
Serial . print ( i + 1 ) ;
Serial . println ( box - > drinks [ i ] . name ) ;
2015-07-13 17:09:27 +00:00
2015-07-13 17:38:17 +00:00
}
2015-09-19 09:46:46 +00:00
Serial . println ( " in " ) ;
2015-07-13 17:38:17 +00:00
if ( i = = NUM_OF_DRINKS ) {
2015-07-13 17:09:27 +00:00
//Is it in?
//Serial.print("Drink \"%s\" does not exist in box!\n", tmp_drink_name);
2015-07-13 17:38:17 +00:00
} else {
2015-07-13 17:09:27 +00:00
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
sscanf ( tmp , " %*s %d " , & aperson . drinks_taken [ i ] ) ;
//Serial.print(">>Quantity: %d\n", aperson.drinks_taken[i].quantity);
read_line_from_file ( read , tmp , sizeof ( tmp ) ) ;
//Serial.print(">>Total: %d\n\n", aperson.drinks_taken[i].total);
}
}
2015-09-19 09:46:46 +00:00
//print_person(box, &aperson);
2015-07-13 17:38:17 +00:00
//comment back in once it is fixed
2015-07-13 17:09:27 +00:00
//update_pers_file(box, &aperson);
read . close ( ) ;
return aperson ;
}
2015-07-13 17:38:17 +00:00
void print_person ( Beerbox * box , Person * aperson ) {
2015-07-13 17:09:27 +00:00
int i = 0 ;
2015-07-13 17:38:17 +00:00
2015-09-19 09:46:46 +00:00
sprintf ( leckmich , " \n Person is called \" %s \" and has the filename \" %s \" . \n " , aperson - > name , aperson - > file_name ) ;
2015-07-13 17:09:27 +00:00
Serial . write ( leckmich ) ;
2015-09-19 09:46:46 +00:00
//sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid);
//Serial.write(leckmich);
2015-07-13 17:32:39 +00:00
sprintf ( leckmich , " finger_uuid: %u \n " , aperson - > finger_uuid ) ;
2015-07-13 17:09:27 +00:00
Serial . print ( leckmich ) ;
2015-07-13 17:38:17 +00:00
for ( i = 0 ; i < NUM_OF_DRINKS ; i + + ) {
if ( aperson - > drinks_taken [ i ] ! = - 1 ) {
2015-07-13 17:09:27 +00:00
sprintf ( leckmich , " \n Drink: %s \n " , box - > drinks [ i ] . name ) ;
Serial . write ( leckmich ) ;
sprintf ( leckmich , " Consumed: %d \n " , aperson - > drinks_taken [ i ] ) ;
Serial . write ( leckmich ) ;
2015-07-13 17:38:17 +00:00
sprintf ( leckmich , " Total: %d.%02d Euro \n " , aperson - > drinks_taken [ i ] * box - > drinks [ i ] . price / 100 , ( aperson - > drinks_taken [ i ] * box - > drinks [ i ] . price ) % 100 ) ;
2015-07-13 17:09:27 +00:00
Serial . write ( leckmich ) ;
}
}
2015-07-13 17:38:17 +00:00
2015-07-13 17:09:27 +00:00
}
2015-07-13 17:38:17 +00:00
int update_pers_file ( Beerbox * box , Person * aperson ) {
2015-07-13 17:09:27 +00:00
File write ;
char * tmp = NULL ;
2015-09-19 09:46:46 +00:00
int i = 0 ;
2015-07-13 17:09:27 +00:00
2015-09-19 09:46:46 +00:00
write = SD . open ( aperson - > file_name , FILE_WRITE ) ;
2015-07-13 17:09:27 +00:00
write . seek ( 0 ) ;
2015-07-13 17:38:17 +00:00
if ( write = = NULL ) {
2015-07-13 17:09:27 +00:00
Serial . print ( " error while reading! \n " ) ;
Serial . print ( " ABORT! \n \n " ) ;
2015-07-13 17:38:17 +00:00
while ( - 1 ) ;
2015-07-13 17:09:27 +00:00
}
sprintf ( leckmich , " %s \n " , aperson - > name ) ;
write . write ( tmp , strlen ( tmp ) ) ;
2015-07-13 17:32:39 +00:00
sprintf ( leckmich , " rfid_uuid: %s \n " , aperson - > rfid_uuid ) ;
2015-07-13 17:09:27 +00:00
write . write ( tmp , strlen ( tmp ) ) ;
2015-07-13 17:32:39 +00:00
sprintf ( leckmich , " finger_uuid: %u \n " , aperson - > finger_uuid ) ;
2015-07-13 17:09:27 +00:00
write . write ( tmp , strlen ( tmp ) ) ;
2015-07-13 17:38:17 +00:00
for ( i = 0 ; i < NUM_OF_DRINKS ; i + + ) {
2015-07-13 17:09:27 +00:00
2015-07-13 17:38:17 +00:00
if ( aperson - > drinks_taken [ i ] ! = - 1 ) {
2015-07-13 17:09:27 +00:00
sprintf ( leckmich , " \n Drink: %s \n " , box - > drinks [ i ] . name ) ;
write . write ( tmp , strlen ( tmp ) ) ;
sprintf ( leckmich , " Consumed: %d \n " , aperson - > drinks_taken [ i ] ) ;
write . write ( tmp , strlen ( tmp ) ) ;
sprintf ( leckmich , " Total: %d \n \n " , aperson - > drinks_taken [ i ] * box - > drinks [ i ] . price ) ;
write . write ( tmp , strlen ( tmp ) ) ;
}
}
write . close ( ) ;
return 0 ;
}
2015-07-13 17:38:17 +00:00
char * read_from_file_until ( File stream , char * str , int max_len , char until ) {
2015-07-13 17:09:27 +00:00
int i = 0 ;
int tmp_int = 0 ;
memset ( str , 0 , max_len ) ;
2015-07-13 17:38:17 +00:00
for ( i = 0 ; ( tmp_int ! = until ) & & ( i < ( max_len - 1 ) ) & & ( tmp_int ! = - 1 ) ; i + + ) {
2015-07-13 17:09:27 +00:00
tmp_int = stream . read ( ) ;
//Serial.print("%c", tmp_int);
str [ i ] = tmp_int ;
}
2015-07-13 17:38:17 +00:00
if ( tmp_int = = - 1 ) {
2015-07-13 17:09:27 +00:00
//May occur more often as the function tries to read a certain number of drinks.
//Serial.print("END OF FILE REACHED!\nABORT!!\n\n");
str [ 0 ] = 0 ;
return str ;
}
2015-07-13 17:38:17 +00:00
if ( str [ 0 ] ! = ' \n ' ) str [ i - 1 ] = 0 ;
2015-07-13 17:09:27 +00:00
return str ;
}
2015-07-13 17:38:17 +00:00
char * read_line_from_file ( File stream , char * str , int max_len ) {
2015-07-13 17:09:27 +00:00
2015-07-13 17:38:17 +00:00
do {
2015-07-13 17:09:27 +00:00
str = read_from_file_until ( stream , str , max_len , ' \n ' ) ;
2015-07-13 17:38:17 +00:00
} while ( ( str [ 0 ] = = ' \n ' ) ) ;
2015-07-13 17:09:27 +00:00
return str ;
}
//
2015-07-13 17:38:17 +00:00
//LB
2015-07-13 17:09:27 +00:00
//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.
2015-07-13 17:38:17 +00:00
int getRFIDIndex ( Person * persons , char * rfid ) {
2015-07-13 17:31:57 +00:00
2015-07-13 17:38:17 +00:00
for ( int i = 0 ; i < sizeof ( persons ) / sizeof ( Person ) ; i + + ) {
if ( ! strcmp ( persons [ i ] . rfid_uuid , rfid ) ) {
return i ;
}
2015-07-13 17:31:57 +00:00
}
2015-07-13 17:38:17 +00:00
}
int getFingerIndex ( Person * persons , uint8_t fingerID ) {
for ( int i = 0 ; i < sizeof ( persons ) / sizeof ( Person ) ; i + + ) {
if ( persons [ i ] . finger_uuid = = fingerID ) {
return i ;
}
2015-07-13 17:31:57 +00:00
}
}
2015-07-16 13:39:44 +00:00
2015-07-13 17:09:27 +00:00
2015-09-19 09:46:46 +00:00
void readAllPersons ( ) {
File dir = SD . open ( " / " ) ;
dir . rewindDirectory ( ) ;
while ( true ) {
File entry = dir . openNextFile ( ) ;
if ( ! entry ) {
// no more files
break ;
}
if ( ! entry . isDirectory ( ) ) {
int8_t len = strlen ( entry . name ( ) ) ;
if ( strstr ( strlwr ( entry . name ( ) + ( len - 4 ) ) , " .per " ) )
{
//Yeah it is a person!
Serial . print ( " Person: " ) ;
Serial . println ( entry . name ( ) ) ;
Person thisPerson = read_person ( box , entry . name ( ) ) ;
//print_person(box, &thisPerson);
}
}
entry . close ( ) ;
}
Serial . println ( " Read all! " ) ;
}