1
0

Added print to rfid and finger; fixed getIndex methods; added drink counts to read; added global person array

This commit is contained in:
Lukas Bachschwell 2015-09-22 22:51:27 +02:00
parent 4b854998c8
commit e96cf8e996
6 changed files with 36 additions and 176 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,9 +1,3 @@
#include <Arduino.h>
#ifndef BEERBOX_IS_INCLUDED
#define BEERBOX_IS_INCLUDED
#define NUM_OF_PEOPLE 25 #define NUM_OF_PEOPLE 25
#define NUM_OF_DRINKS 10 #define NUM_OF_DRINKS 10
#define DRINK_NAME_MAX_LENGTH 13 #define DRINK_NAME_MAX_LENGTH 13
@ -27,7 +21,6 @@ typedef struct{
typedef struct{ typedef struct{
Drink drinks[NUM_OF_DRINKS]; Drink drinks[NUM_OF_DRINKS];
int personCount;
}Beerbox; }Beerbox;
typedef struct{ typedef struct{
@ -50,4 +43,3 @@ int update_pers_file(Beerbox *box, Person *aperson);
char* read_line_from_file(FILE *stream, char *str, int max_len); char* read_line_from_file(FILE *stream, char *str, int max_len);
char* read_from_file_until(FILE *stream, char *str, int max_len, char until); char* read_from_file_until(FILE *stream, char *str, int max_len, char until);
#endif

View File

@ -1,29 +1,17 @@
#include <SPI.h> #include <SPI.h>
#include <Wire.h> #include <Wire.h>
#include <SD.h> #include <SD.h>
#include "beerbox.h"
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "beerbox.h"
#include <Adafruit_Fingerprint.h> #include <Adafruit_Fingerprint.h>
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2); Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2);
LiquidCrystal_I2C lcd(0x38, 8, 2); // 0x38 for PCF***A on address 000 LiquidCrystal_I2C lcd(0x38, 8, 2); // 0x38 for PCF***A on address 000
//######### 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
//#######################
File myFile; File myFile;
const int chipSelect = 53; const int chipSelect = 53;
@ -35,6 +23,8 @@ int drinksMax = 1;
int lastButtonState = 1; int lastButtonState = 1;
int currentDrink = 0; int currentDrink = 0;
Person* persons;
void setup() { void setup() {
//generall debugging //generall debugging
@ -71,11 +61,11 @@ void setup() {
Serial.println("initialization done."); Serial.println("initialization done.");
//check_for_file("p001", "per"); //check_for_file("p001", "per");
readAllPersons();
box = new Beerbox; box = new Beerbox;
box = read_beerbox(box); box = read_beerbox(box);
persons = readAllPersons();
lcd.print("BOXready"); lcd.print("BOXready");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
@ -90,99 +80,9 @@ void loop() {
checkRFID(); checkRFID();
checkButton(); checkButton();
//Person myPerson = read_person(box, "p001");
//read_person(box, "p002");
}//end loop }//end loop
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
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 {//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;
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);
//TODO @ALEX Do something with finger ID
// finger.fingerID
//int getFingerIndex(Person* persons, finger.fingerID)
}
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
}
void check_for_file(String filename, String extension) { void check_for_file(String filename, String extension) {
@ -276,7 +176,7 @@ Person read_person(Beerbox *box, char *filename) {
Person aperson; Person aperson;
File read; File read;
char tmp[21];//longest line is rfid_uuid char tmp[25];//longest line is rfid_uuid
//giving the full filename here, since it makes stuff easier //giving the full filename here, since it makes stuff easier
@ -319,18 +219,18 @@ Person read_person(Beerbox *box, char *filename) {
while (read_line_from_file(read, tmp, sizeof(tmp)), strlen(tmp) > 0) { while (read_line_from_file(read, tmp, sizeof(tmp)), strlen(tmp) > 0) {
int tmpCount = 0; int tmpCount = 0;
int tmpDrink = 0; int tmpDrink = 0;
sscanf(tmp, "drink_Count_%d: %d",&tmpDrink, &tmpCount); sscanf(tmp, "drink_Count_%d: %d",&tmpDrink, &tmpCount);
if (strlen(tmp)) { if (strlen(tmp)) {
Serial.print("drink: "); Serial.print("drink: ");
Serial.print(tmpDrink); Serial.print(tmpDrink);
Serial.print(" count: "); Serial.print(" count: ");
Serial.println(tmpCount); Serial.println(tmpCount);
aperson.drinks_taken[tmpDrink] = tmpCount;
} }
} }
//print_person(box, &aperson); print_person(box, &aperson);
//comment back in once it is fixed //comment back in once it is fixed
//update_pers_file(box, &aperson); //update_pers_file(box, &aperson);
@ -341,8 +241,6 @@ Person read_person(Beerbox *box, char *filename) {
void print_person(Beerbox *box, Person *aperson) { void print_person(Beerbox *box, Person *aperson) {
int i = 0; int i = 0;
sprintf(leckmich, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name); sprintf(leckmich, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name);
Serial.write(leckmich); Serial.write(leckmich);
sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid); sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid);
@ -352,18 +250,15 @@ void print_person(Beerbox *box, Person *aperson) {
sprintf(leckmich, "credits_left: %u\n", aperson->credits_left); sprintf(leckmich, "credits_left: %u\n", aperson->credits_left);
Serial.print(leckmich); Serial.print(leckmich);
for (i = 0; i < NUM_OF_DRINKS; i++) { for (i = 0; i < NUM_OF_DRINKS; i++) {
if (aperson->drinks_taken[i] != -1) { if (aperson->drinks_taken[i] != -1) {
/* //Rework that
sprintf(leckmich, "\nDrink: %s\n", box->drinks[i].name); sprintf(leckmich, "\nDrink: %s\n", box->drinks[i].name);
Serial.write(leckmich); Serial.write(leckmich);
sprintf(leckmich, "Consumed: %d\n", aperson->drinks_taken[i]); sprintf(leckmich, "Drink %d: %d\n", i,aperson->drinks_taken[i]);
Serial.write(leckmich); Serial.write(leckmich);
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); 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);
Serial.write(leckmich); Serial.write(leckmich);
*/
} }
} }
@ -456,41 +351,35 @@ char *read_line_from_file(File stream, char *str, int max_len) {
return str; return str;
} }
//
//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(Person* persons, char* rfid) {
for (int i = 0; i < sizeof(persons) / sizeof(Person); i++) {
if (!strcmp(persons[i].rfid_uuid, rfid)) {
return i;
}
}
}
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;
}
}
}
void readAllPersons () {
Person* readAllPersons () {
File dir = SD.open("/"); File dir = SD.open("/");
dir.rewindDirectory(); dir.rewindDirectory();
int personCount = 0;
//count the valid persons:
while (true) { while (true) {
File entry = dir.openNextFile(); File entry = dir.openNextFile();
if (! entry) { if (! entry) {// no more files
// no more files break;
}
if (!entry.isDirectory()) {
if (strstr(strlwr(entry.name() + (strlen(entry.name()) - 4)), ".per")) personCount++; //Yeah it is a person!
}
entry.close();
}
//now store them
Person readPersons[personCount];
int counter = 0;
dir.rewindDirectory();
while (true) {
File entry = dir.openNextFile();
if (! entry) {// no more files
break; break;
} }
if (!entry.isDirectory()) { if (!entry.isDirectory()) {
int8_t len = strlen(entry.name()); int8_t len = strlen(entry.name());
if (strstr(strlwr(entry.name() + (len - 4)), ".per")) if (strstr(strlwr(entry.name() + (len - 4)), ".per"))
@ -499,15 +388,14 @@ void readAllPersons () {
Serial.print("Person: "); Serial.print("Person: ");
Serial.println(entry.name()); Serial.println(entry.name());
Person thisPerson = read_person(box, entry.name()); Person thisPerson = read_person(box, entry.name());
//print_person(box, &thisPerson); readPersons[counter] = thisPerson;
counter++;
} }
} }
entry.close(); entry.close();
} }
//Serial.println("Read all!");
Serial.println("Read all!"); return readPersons;
} }

22
main.c
View File

@ -1,22 +0,0 @@
//#include <stdio.h>
//#include <stdlib.h>
//#include "beerbox.h"
//
//
//
//
//
//int main()
//{
//
// Beerbox *box = calloc(1, sizeof(Beerbox));
//
//
//
// box = read_beerbox(box);
// read_person(box, "p001");
// read_person(box, "p002");
//
//
// return 0;
//}

3
p001.per Normal file → Executable file
View File

@ -1,7 +1,8 @@
Alex Alex
rfid_uuid: 123456789 rfid_uuid: 07005C447660
finger_uuid: 003 finger_uuid: 003
credits_left: 100 credits_left: 100
drink_Count_0: 3
drink_Count_1: 6 drink_Count_1: 6
drink_Count_2: 0 drink_Count_2: 0
drink_Count_3: 0 drink_Count_3: 0

5
p002.per Normal file → Executable file
View File

@ -1,7 +1,8 @@
Lukas Lukas
rfid_uuid: 126789345 rfid_uuid: 07005C447669
finger_uuid: 001 finger_uuid: 001
credits_left: 100 credits_left: 150
drink_Count_0: 1
drink_Count_1: 4 drink_Count_1: 4
drink_Count_2: 3 drink_Count_2: 3
drink_Count_3: 0 drink_Count_3: 0