1
0

Lots of changes, bug in read_person...

This commit is contained in:
Lukas Bachschwell 2015-09-19 11:46:46 +02:00
parent 835c455762
commit a149928996
5 changed files with 132 additions and 86 deletions

View File

@ -1,19 +1,16 @@
personCount: 003
drink number 1: Beer
price: 250
quantity: 100
drink number 2: Fanta
price: 120
quantity: 49
drink number 3: Schartnerbombe
price: 184
quantity: 87
drink number 4: Cola
price: 130
quantity: 13
drink number 5: Apfelspritzer
price: 100
quantity: 120

View File

@ -7,8 +7,8 @@
#define NUM_OF_PEOPLE 25
#define NUM_OF_DRINKS 10
#define DRINK_NAME_MAX_LENGTH 13
#define FILE_NAME_LEN 4
#define FILE_EXTENSION_LEN 4
#define FILE_NAME_LEN 8
#define FILE_EXTENSION_LEN 3
#define FILE_EXTENSION "per"
#define NAME_LEN 10
@ -19,34 +19,25 @@ extern int __drinks_put_in;
extern char tmp_filename[];
typedef struct{
/*Drink spevifier*/
char name[DRINK_NAME_MAX_LENGTH + 1];
/*Value in cents*/
int price;
/*available quantity*/
int quantity;
}Drink;
typedef struct{
Drink drinks[NUM_OF_DRINKS];
int personCount;
}Beerbox;
typedef struct{
char file_name[FILE_NAME_LEN + 1];
char name[NAME_LEN + 1];
char rfid_uuid[13];
uint8_t finger_uuid;
int drinks_taken[NUM_OF_DRINKS];
}Person;
@ -55,7 +46,8 @@ Beerbox* read_beerbox(Beerbox *box);
Beerbox* add_drink(Beerbox *box);
Person read_person(Beerbox *box, char *filename);
void print_person(Beerbox *box, Person *aperson);
int update_pers_file(Beerbox *box, Person *aperson);
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);
int update_pers_file(Beerbox *box, Person *aperson);
#endif

View File

@ -42,7 +42,7 @@ void setup() {
//LCD init
lcd.init();
pinMode(2,INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
//Fingerprint init
finger.begin(57600);
@ -70,11 +70,13 @@ void setup() {
}
Serial.println("initialization done.");
check_for_file("p001", "per");
//check_for_file("p001", "per");
readAllPersons();
box = new Beerbox;
box = read_beerbox(box);
lcd.print("BOXready");
lcd.setCursor(0, 1);
@ -97,30 +99,25 @@ void loop() {
void checkButton() {
//one change per click to make seleting drinks easier for the drunken
//if (digitalRead(2) != lastButtonState) {
//delayMicroseconds(50); //debounce delay
//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);
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);
}
else {//released
}
lastButtonState = digitalRead(2);
}
//}
}
@ -142,13 +139,12 @@ bool checkFinger() {
//TODO @ALEX Do something with finger ID
// finger.fingerID
int getFingerIndex(Person* persons, finger.fingerID)
//int getFingerIndex(Person* persons, finger.fingerID)
}
void checkRFID() {
Serial1.flush();
if (Serial1.available() >= gesamtKennungLaenge) // wenn genug Zeichen eingegangen ...
{
@ -227,7 +223,17 @@ Beerbox* read_beerbox(Beerbox *box) {
while (1);
}
// 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);
//get drinks
for (i = 0 ; i < NUM_OF_DRINKS ; i++) {
read_line_from_file(read, tmp, sizeof(tmp));
@ -238,29 +244,26 @@ Beerbox* read_beerbox(Beerbox *box) {
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d ", &box->drinks[i].price);
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d", &box->drinks[i].quantity);
} else {
strcpy(box->drinks[i].name, "slot empty");
box->drinks[i].price = -1;
box->drinks[i].quantity = -1;
}
}
Serial.print(i + " drinks read!\n\n");
Serial.print(i);
Serial.println(" drinks read!\n");
for (i = 0; (i < NUM_OF_DRINKS) && (box->drinks[i].price != -1); i++) {
sprintf(leckmich, "drink number %i: %s\nprice: %d\nquantity: %d\n\n", i ,box->drinks[i].name, box->drinks[i].price, box->drinks[i].quantity);
Serial.print(leckmich);
sprintf(leckmich, "drink number %i: %s\nprice: %d\n", i , box->drinks[i].name, box->drinks[i].price);
Serial.println(leckmich);
}
if (i < NUM_OF_DRINKS) {
sprintf(leckmich, "Slots %d - %d are not in use!\n", i + 1, NUM_OF_DRINKS);
drinksMax = i;
Serial.print(leckmich);
Serial.println(leckmich);
}
read.close();
@ -282,15 +285,12 @@ Person read_person(Beerbox *box, char *filename) {
char tmp_drink_name[DRINK_NAME_MAX_LENGTH + 1];
int i = 0;
int j = 0;
memset(tmp_filename, 0, FILE_NAME_LEN + FILE_EXTENSION_LEN + 2);
strcat(tmp_filename, filename);
strcat(tmp_filename, ".");
strcat(tmp_filename, FILE_EXTENSION);
read = SD.open(tmp_filename, FILE_READ);
//giving the full filename here, since it makes stuff easier
Serial.print("The filename is: ");
Serial.println(filename);
read = SD.open(filename, FILE_READ);
if (read == false) {
Serial.print("error while reading!\n");
@ -301,7 +301,7 @@ Person read_person(Beerbox *box, char *filename) {
strcpy(aperson.file_name, filename);
for (i = 0; i < NUM_OF_DRINKS; i++) {
for (int i = 0; i < NUM_OF_DRINKS; i++) {
aperson.drinks_taken[i] = -1;
}
@ -309,28 +309,28 @@ Person read_person(Beerbox *box, char *filename) {
read_line_from_file(read, tmp, sizeof(tmp));
strcpy(aperson.name, tmp);
Serial.println("before memset");
memset(aperson.rfid_uuid, 0, 13);
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %12s", aperson.rfid_uuid);
Serial.print(aperson.rfid_uuid);
//Serial.print(aperson.rfid_uuid);
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d", &aperson.finger_uuid);
Serial.println(aperson.finger_uuid);
//Serial.println(aperson.finger_uuid);
j = 0;
while (read_line_from_file(read, tmp, sizeof(tmp)), strlen(tmp) > 0) {
i = 0;
sscanf(tmp, " %*s %s", tmp_drink_name);
for (i = 0; (i < NUM_OF_DRINKS) && (strcmp(box->drinks[i].name, tmp_drink_name)); i++) {
//Serial.print("It´s not drink No %d: %s\n", i+1, box->drinks[i].name );
Serial.print("It´s not drink No :");
Serial.print(i+1);
Serial.println(box->drinks[i].name);
}
Serial.println("in");
if (i == NUM_OF_DRINKS) {
//Is it in?
//Serial.print("Drink \"%s\" does not exist in box!\n", tmp_drink_name);
@ -345,8 +345,7 @@ Person read_person(Beerbox *box, char *filename) {
}
print_person(box, &aperson);
//print_person(box, &aperson);
//comment back in once it is fixed
//update_pers_file(box, &aperson);
@ -359,16 +358,11 @@ void print_person(Beerbox *box, Person *aperson) {
int i = 0;
memset(tmp_filename, 0, FILE_NAME_LEN + FILE_EXTENSION_LEN + 2);
strcat(tmp_filename, aperson->file_name);
strcat(tmp_filename, ".");
strcat(tmp_filename, FILE_EXTENSION);
sprintf(leckmich, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, tmp_filename);
Serial.write(leckmich);
sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid);
sprintf(leckmich, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name);
Serial.write(leckmich);
//sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid);
//Serial.write(leckmich);
sprintf(leckmich, "finger_uuid: %u\n", aperson->finger_uuid);
Serial.print(leckmich);
@ -395,15 +389,10 @@ void print_person(Beerbox *box, Person *aperson) {
int update_pers_file(Beerbox *box, Person *aperson) {
File write;
char *tmp = NULL;
//write = fopen()
int i = 0;
memset(tmp_filename, 0, FILE_NAME_LEN + FILE_EXTENSION_LEN + 2);
strcat(tmp_filename, aperson->file_name);
strcat(tmp_filename, ".");
strcat(tmp_filename, FILE_EXTENSION);
write = SD.open(tmp_filename, FILE_WRITE);
write = SD.open(aperson->file_name, FILE_WRITE);
write.seek(0);
if (write == NULL) {
@ -504,3 +493,36 @@ int getFingerIndex(Person* persons, uint8_t fingerID) {
}
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!");
}

12
p001.per Normal file
View File

@ -0,0 +1,12 @@
Alex
rfid_uuid: 123456789
finger_uuid: 987654321
Drink: Beer
Consumed: 6
Total: 1500
Drink: Schartnerbombe
Consumed: 6
Total: 1104

23
p002.per Normal file
View File

@ -0,0 +1,23 @@
Lukas
rfid_uuid: 123456789
finger_uuid: 987654321
Drink: Beer
Consumed: 6
Total: 1500
Drink: Fanta
Consumed: 6
Total: 720
Drink: Schartnerbombe
Consumed: 6
Total: 1104
Drink: Cola
Consumed: 6
Total: 780