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 drink number 1: Beer
price: 250 price: 250
quantity: 100
drink number 2: Fanta drink number 2: Fanta
price: 120 price: 120
quantity: 49
drink number 3: Schartnerbombe drink number 3: Schartnerbombe
price: 184 price: 184
quantity: 87
drink number 4: Cola drink number 4: Cola
price: 130 price: 130
quantity: 13
drink number 5: Apfelspritzer drink number 5: Apfelspritzer
price: 100 price: 100
quantity: 120

View File

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

View File

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