Finally fixed the mem issuecd beerbox/! Basic Reading,writing and counting now working

This commit is contained in:
Lukas Bachschwell 2015-09-24 20:01:51 +02:00
parent e96cf8e996
commit 7e482344d2
3 changed files with 60 additions and 73 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -6,11 +6,7 @@
#define FILE_EXTENSION "per" #define FILE_EXTENSION "per"
#define NAME_LEN 10 #define NAME_LEN 10
extern int __drinks_taken;
extern int __drinks_put_in;
//Beware! Only valid inside the function it has been set in!
extern char tmp_filename[];
typedef struct{ typedef struct{
/*Drink spevifier*/ /*Drink spevifier*/
@ -21,25 +17,17 @@ typedef struct{
typedef struct{ typedef struct{
Drink drinks[NUM_OF_DRINKS]; Drink drinks[NUM_OF_DRINKS];
int waiting;
}Beerbox; }Beerbox;
//this shall always just exist in a short term!
typedef struct{ typedef struct{
char file_name[FILE_NAME_LEN + 1]; char file_name[FILE_NAME_LEN + 1];
int drinks_taken[NUM_OF_DRINKS];
char name[NAME_LEN + 1]; char name[NAME_LEN + 1];
char rfid_uuid[13]; char rfid_uuid[13];
uint8_t finger_uuid; uint8_t finger_uuid;
uint8_t credits_left; uint8_t credits_left;
int drinks_taken[NUM_OF_DRINKS];
}Person; }Person;
Beerbox* read_beerbox(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);

View File

@ -13,19 +13,27 @@ LiquidCrystal_I2C lcd(0x38, 8, 2); // 0x38 for PCF***A on address 000
File myFile; File myFile;
const int chipSelect = 53; const int chipSelect = 53;
char leckmich[255]; char toprint[255];
Beerbox *box; Beerbox *box;
int drinksMax = 1; int drinksMax = 1; // TODO add this to the box struct
int lastButtonState = 1; int lastButtonState = 1;
int currentDrink = 0; int currentDrink = 0;
int lastPersonIndex = -1;
Person* persons; Person* persons;
unsigned long oldTime = 0;
void setup() { void setup() {
//generall debugging //generall debugging
Serial.begin(115200); Serial.begin(115200);
@ -69,6 +77,10 @@ void setup() {
lcd.print("BOXready"); lcd.print("BOXready");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print(box->drinks[0].name);
currentDrink = 0;
box->waiting=false;
} }
@ -80,6 +92,8 @@ void loop() {
checkRFID(); checkRFID();
checkButton(); checkButton();
boxTimer();
}//end loop }//end loop
@ -102,8 +116,6 @@ void check_for_file(String filename, String extension) {
char tmp_filename[FILE_NAME_LEN + FILE_EXTENSION_LEN + 2]; char tmp_filename[FILE_NAME_LEN + FILE_EXTENSION_LEN + 2];
Beerbox* read_beerbox(Beerbox *box) { Beerbox* read_beerbox(Beerbox *box) {
File read; File read;
@ -123,15 +135,6 @@ Beerbox* read_beerbox(Beerbox *box) {
while (1); 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 //get drinks
for (i = 0 ; i < NUM_OF_DRINKS ; i++) { 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)); read_line_from_file(read, tmp, sizeof(tmp));
if (strlen(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)); read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d ", &box->drinks[i].price); 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++) { for (i = 0; (i < NUM_OF_DRINKS) && (box->drinks[i].price != -1); i++) {
sprintf(toprint, "drink number %i: %s\nprice: %d\n", i , box->drinks[i].name, box->drinks[i].price);
sprintf(leckmich, "drink number %i: %s\nprice: %d\n", i , box->drinks[i].name, box->drinks[i].price); Serial.println(toprint);
Serial.println(leckmich);
} }
if (i < NUM_OF_DRINKS) { 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; drinksMax = i;
Serial.println(leckmich); Serial.println(toprint);
} }
read.close(); read.close();
@ -205,9 +205,8 @@ Person read_person(Beerbox *box, char *filename) {
//rfid_uuid //rfid_uuid
memset(aperson.rfid_uuid, 0, 13); memset(aperson.rfid_uuid, 0, 13);
read_line_from_file(read, tmp, sizeof(tmp)); 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); Serial.println(aperson.rfid_uuid);
//finger_uuid
read_line_from_file(read, tmp, sizeof(tmp)); read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, "finger_uuid: %d", &aperson.finger_uuid); sscanf(tmp, "finger_uuid: %d", &aperson.finger_uuid);
Serial.println(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) { 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(toprint, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name);
Serial.write(leckmich); Serial.write(toprint);
sprintf(leckmich, "rfid_uuid: %s\n", aperson->rfid_uuid); sprintf(toprint, "rfid_uuid: %s\n", aperson->rfid_uuid);
Serial.write(leckmich); Serial.write(toprint);
sprintf(leckmich, "finger_uuid: %u\n", aperson->finger_uuid); sprintf(toprint, "finger_uuid: %u\n", aperson->finger_uuid);
Serial.print(leckmich); Serial.print(toprint);
sprintf(leckmich, "credits_left: %u\n", aperson->credits_left); sprintf(toprint, "credits_left: %u\n", aperson->credits_left);
Serial.print(leckmich); Serial.print(toprint);
for (i = 0; i < NUM_OF_DRINKS; i++) { for (i = 0; i < drinksMax; i++) {
if (aperson->drinks_taken[i] != -1) { if (aperson->drinks_taken[i] != -1) {
sprintf(leckmich, "\nDrink: %s\n", box->drinks[i].name); sprintf(toprint, "\nDrink: %s\n", box->drinks[i].name);
Serial.write(leckmich); Serial.write(toprint);
sprintf(leckmich, "Drink %d: %d\n", i,aperson->drinks_taken[i]); sprintf(toprint, "Drink %d: %d\n", i,aperson->drinks_taken[i]);
Serial.write(leckmich); Serial.write(toprint);
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(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(leckmich); 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; File write;
char *tmp = NULL; char tmp[25];
int i = 0; int i = 0;
//Serial.println(thisPerson.file_name);
write = SD.open(aperson->file_name, FILE_WRITE); write = SD.open(thisPerson.file_name, FILE_WRITE);
write.seek(0);
if (write == NULL) { if (write == NULL) {
Serial.print("error while reading!\n"); Serial.print("error while reading!\n");
Serial.print("ABORT!\n\n"); Serial.print("ABORT!\n\n");
while (-1); while (-1);
} }
write.seek(0);
sprintf(leckmich, " %s\n", aperson->name);
sprintf(tmp, "%s\n", thisPerson.name);
write.write(tmp, strlen(tmp)); 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)); 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)); 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) { if (thisPerson.drinks_taken[i] != -1) {
sprintf(tmp, "\ndrink_Count_%d: %d", i, thisPerson.drinks_taken[i]);
sprintf(leckmich, "\nDrink: %s\n", box->drinks[i].name);
write.write(tmp, strlen(tmp)); write.write(tmp, strlen(tmp));
sprintf(leckmich, "Consumed: %d\n", aperson->drinks_taken[i]); Serial.println(tmp);
write.write(tmp, strlen(tmp));
sprintf(leckmich, "Total: %d\n\n", aperson->drinks_taken[i]*box->drinks[i].price);
write.write(tmp, strlen(tmp));
} }
} }
@ -372,7 +370,7 @@ Person* readAllPersons () {
entry.close(); entry.close();
} }
//now store them //now store them
Person readPersons[personCount]; Person* readPersons = new Person[personCount];
int counter = 0; int counter = 0;
dir.rewindDirectory(); dir.rewindDirectory();
while (true) { while (true) {
@ -400,3 +398,4 @@ Person* readAllPersons () {