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 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{
/*Drink spevifier*/
@ -21,25 +17,17 @@ typedef struct{
typedef struct{
Drink drinks[NUM_OF_DRINKS];
int waiting;
}Beerbox;
//this shall always just exist in a short term!
typedef struct{
char file_name[FILE_NAME_LEN + 1];
int drinks_taken[NUM_OF_DRINKS];
char name[NAME_LEN + 1];
char rfid_uuid[13];
uint8_t finger_uuid;
uint8_t credits_left;
int drinks_taken[NUM_OF_DRINKS];
}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;
const int chipSelect = 53;
char leckmich[255];
char toprint[255];
Beerbox *box;
int drinksMax = 1;
int drinksMax = 1; // TODO add this to the box struct
int lastButtonState = 1;
int currentDrink = 0;
int lastPersonIndex = -1;
Person* persons;
unsigned long oldTime = 0;
void setup() {
//generall debugging
Serial.begin(115200);
@ -69,7 +77,11 @@ void setup() {
lcd.print("BOXready");
lcd.setCursor(0, 1);
lcd.print(box->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 () {