From 7e482344d26ca869c183cb1de280ba80397c780e Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Thu, 24 Sep 2015 20:01:51 +0200 Subject: [PATCH] Finally fixed the mem issuecd beerbox/! Basic Reading,writing and counting now working --- .DS_Store | Bin 6148 -> 6148 bytes beerbox.h | 18 ++------ beerbox.ino | 115 ++++++++++++++++++++++++++-------------------------- 3 files changed, 60 insertions(+), 73 deletions(-) diff --git a/.DS_Store b/.DS_Store index 97d4de1df0fed384c67e37225714012127e5c98f..5e4b7c5017722737db013ddcae7bdc2d5277f398 100644 GIT binary patch delta 82 zcmZoMXfc?u%);P50|NsKgC0XVLncE>ZoZ34N@+drinks[0].name); + currentDrink = 0; + box->waiting=false; + } @@ -80,6 +92,8 @@ void loop() { checkRFID(); checkButton(); + boxTimer(); + }//end loop @@ -102,8 +116,6 @@ void check_for_file(String filename, String extension) { char tmp_filename[FILE_NAME_LEN + FILE_EXTENSION_LEN + 2]; - - Beerbox* read_beerbox(Beerbox *box) { File read; @@ -123,15 +135,6 @@ 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++) { @@ -139,7 +142,7 @@ Beerbox* read_beerbox(Beerbox *box) { read_line_from_file(read, tmp, sizeof(tmp)); if (strlen(tmp)) { - sscanf(tmp, " drink number %d: %s", &index, box->drinks[i].name); + sscanf(tmp, " drink number %d: %s", &index, &box->drinks[i].name); read_line_from_file(read, tmp, sizeof(tmp)); sscanf(tmp, " %*s %d ", &box->drinks[i].price); @@ -150,20 +153,17 @@ Beerbox* read_beerbox(Beerbox *box) { } } - 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\n", i , box->drinks[i].name, box->drinks[i].price); - Serial.println(leckmich); + sprintf(toprint, "drink number %i: %s\nprice: %d\n", i , box->drinks[i].name, box->drinks[i].price); + Serial.println(toprint); } if (i < NUM_OF_DRINKS) { - sprintf(leckmich, "Slots %d - %d are not in use!\n", i + 1, NUM_OF_DRINKS); + sprintf(toprint, "Slots %d - %d are not in use!\n", i + 1, NUM_OF_DRINKS); drinksMax = i; - Serial.println(leckmich); + Serial.println(toprint); } read.close(); @@ -205,9 +205,8 @@ Person read_person(Beerbox *box, char *filename) { //rfid_uuid memset(aperson.rfid_uuid, 0, 13); read_line_from_file(read, tmp, sizeof(tmp)); - sscanf(tmp, "rfid_uuid: %12s", aperson.rfid_uuid); + sscanf(tmp, "rfid_uuid: %12s", &aperson.rfid_uuid); Serial.println(aperson.rfid_uuid); - //finger_uuid read_line_from_file(read, tmp, sizeof(tmp)); sscanf(tmp, "finger_uuid: %d", &aperson.finger_uuid); Serial.println(aperson.finger_uuid); @@ -241,24 +240,24 @@ Person read_person(Beerbox *box, char *filename) { void print_person(Beerbox *box, Person *aperson) { int i = 0; - 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); - sprintf(leckmich, "credits_left: %u\n", aperson->credits_left); - Serial.print(leckmich); + sprintf(toprint, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name); + Serial.write(toprint); + sprintf(toprint, "rfid_uuid: %s\n", aperson->rfid_uuid); + Serial.write(toprint); + sprintf(toprint, "finger_uuid: %u\n", aperson->finger_uuid); + Serial.print(toprint); + sprintf(toprint, "credits_left: %u\n", aperson->credits_left); + Serial.print(toprint); - for (i = 0; i < NUM_OF_DRINKS; i++) { + for (i = 0; i < drinksMax; i++) { if (aperson->drinks_taken[i] != -1) { - sprintf(leckmich, "\nDrink: %s\n", box->drinks[i].name); - Serial.write(leckmich); - sprintf(leckmich, "Drink %d: %d\n", i,aperson->drinks_taken[i]); - 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); - Serial.write(leckmich); + sprintf(toprint, "\nDrink: %s\n", box->drinks[i].name); + Serial.write(toprint); + sprintf(toprint, "Drink %d: %d\n", i,aperson->drinks_taken[i]); + Serial.write(toprint); + sprintf(toprint, "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(toprint); } } @@ -267,40 +266,39 @@ void print_person(Beerbox *box, Person *aperson) { } -int update_pers_file(Beerbox *box, Person *aperson) { +int update_pers_file() { + //TODO use lastPersonIndex + Person thisPerson = persons[lastPersonIndex]; File write; - char *tmp = NULL; + char tmp[25]; int i = 0; - - write = SD.open(aperson->file_name, FILE_WRITE); - write.seek(0); - + //Serial.println(thisPerson.file_name); + write = SD.open(thisPerson.file_name, FILE_WRITE); + if (write == NULL) { Serial.print("error while reading!\n"); Serial.print("ABORT!\n\n"); while (-1); - } + write.seek(0); - sprintf(leckmich, " %s\n", aperson->name); + + sprintf(tmp, "%s\n", thisPerson.name); write.write(tmp, strlen(tmp)); - sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid); + sprintf(tmp, "rfid_uuid: %s\n", thisPerson.rfid_uuid); write.write(tmp, strlen(tmp)); - sprintf(leckmich, "finger_uuid: %u\n", aperson->finger_uuid); + sprintf(tmp, "finger_uuid: %u\n", thisPerson.finger_uuid); + write.write(tmp, strlen(tmp)); + sprintf(tmp, "credits_left: %u", thisPerson.credits_left); write.write(tmp, strlen(tmp)); - for (i = 0; i < NUM_OF_DRINKS; i++) { + for (i = 0; i < drinksMax; i++) { - if (aperson->drinks_taken[i] != -1) { - - sprintf(leckmich, "\nDrink: %s\n", box->drinks[i].name); + if (thisPerson.drinks_taken[i] != -1) { + sprintf(tmp, "\ndrink_Count_%d: %d", i, thisPerson.drinks_taken[i]); 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)); - + Serial.println(tmp); } } @@ -372,7 +370,7 @@ Person* readAllPersons () { entry.close(); } //now store them - Person readPersons[personCount]; + Person* readPersons = new Person[personCount]; int counter = 0; dir.rewindDirectory(); while (true) { @@ -400,3 +398,4 @@ Person* readAllPersons () { +