Merged berbox.cpp with main sketch

This commit is contained in:
Alexander Junk 2015-07-13 18:23:51 +02:00
parent 3a1796123a
commit 173e464808

View File

@ -1,258 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "beerbox.h"
#include <SPI.h>
#include <SD.h>
//Beware! Only valid inside the function it has been set in!
char tmp_filename[FILE_NAME_LEN + FILE_EXTENSION_LEN + 2];
Beerbox* read_beerbox(Beerbox *box){
FILE *read = NULL;
int num_read = 0;
int index;
char tmp[(DRINK_NAME_MAX_LENGTH + 1) * NUM_OF_DRINKS];
int i = 0;
read = SD.open("beerbox.config", FILE_READ);
if(read == NULL){
perror("error while reading!\n");
printf("ABORT!\n\n");
exit(-1);
}
for(i = 0 ; i < NUM_OF_DRINKS ; i++){
read_line_from_file(read, tmp, sizeof(tmp));
if(strlen(tmp)){
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);
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;
}
}
printf("%d drinks read!\n\n", i);
for(i = 0; (i < NUM_OF_DRINKS)&&(box->drinks[i].price != -1); i++){
printf("drink number %d: %s\nprice: %d\nquantity: %d\n\n",i+1,box->drinks[i].name,box->drinks[i].price,box->drinks[i].quantity);
}
if(i < NUM_OF_DRINKS)printf("Slots %d - %d are not in use!\n", i+1, NUM_OF_DRINKS);
fclose(read);
return box;
}
Person read_person(Beerbox *box, char *filename){
Person aperson;
FILE *read = NULL;
int num_read = 0;
int index;
int tmp_int = 0;
char tmp[(DRINK_NAME_MAX_LENGTH + 1) * NUM_OF_DRINKS];
char tmp_drink_name[DRINK_NAME_MAX_LENGTH + 1];
int i = 0;
int j = 0;
memset(tmp_filename, 0, sizeof(tmp_filename));
strcat(tmp_filename, filename);
strcat(tmp_filename, ".");
strcat(tmp_filename, FILE_EXTENSION);
read = fopen(tmp_filename, "r");
if(read == NULL){
perror("error while reading!\n");
printf("ABORT!\n\n");
exit(-1);
}
strcpy(aperson.file_name, filename);
for(i = 0; i < NUM_OF_DRINKS; i++){
aperson.drinks_taken[i] = -1;
}
read_line_from_file(read, tmp, sizeof(tmp));
strcpy(aperson.name, tmp);
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d", &aperson.rfid_uuid);
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d", &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++){
//printf("It´s not drink No %d: %s\n", i+1, box->drinks[i].name );
}
if(i == NUM_OF_DRINKS){
//Is it in?
//printf("Drink \"%s\" does not exist in box!\n", tmp_drink_name);
}else{
read_line_from_file(read, tmp, sizeof(tmp));
sscanf(tmp, " %*s %d", &aperson.drinks_taken[i]);
//printf(">>Quantity: %d\n", aperson.drinks_taken[i].quantity);
read_line_from_file(read, tmp, sizeof(tmp));
//printf(">>Total: %d\n\n", aperson.drinks_taken[i].total);
}
}
print_person(box, &aperson);
update_pers_file(box, &aperson);
fclose(read);
return aperson;
}
void print_person(Beerbox *box, Person *aperson){
int i = 0;
memset(tmp_filename, 0, sizeof(tmp_filename));
strcat(tmp_filename, aperson->file_name);
strcat(tmp_filename, ".");
strcat(tmp_filename, FILE_EXTENSION);
printf("\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, tmp_filename);
printf("rfid_uuid: %d\n", aperson->rfid_uuid);
printf("finger_uuid: %d\n", aperson->finger_uuid);
for(i = 0; i < NUM_OF_DRINKS; i++){
if(aperson->drinks_taken[i] != -1){
printf("\nDrink: %s\n", box->drinks[i].name);
printf("Consumed: %d\n", aperson->drinks_taken[i]);
printf("Total: %d.%02d Euro\n", aperson->drinks_taken[i]*box->drinks[i].price/100, (aperson->drinks_taken[i]*box->drinks[i].price)%100);
}
}
}
int update_pers_file(Beerbox *box, Person *aperson){
FILE *write = NULL;
char *tmp = NULL;
//write = fopen()
int i = 0;
memset(tmp_filename, 0, sizeof(tmp_filename));
strcat(tmp_filename, aperson->file_name);
strcat(tmp_filename, ".");
strcat(tmp_filename, FILE_EXTENSION);
write = fopen(tmp_filename, "w");
if(write == NULL){
perror("error while reading!\n");
printf("ABORT!\n\n");
exit(-1);
}
fprintf(write, "%s\n", aperson->name);
fprintf(write, "rfid_uuid: %d\n", aperson->rfid_uuid);
fprintf(write, "finger_uuid: %d\n", aperson->finger_uuid);
for(i = 0; i < NUM_OF_DRINKS; i++){
if(aperson->drinks_taken[i] != -1){
fprintf(write, "\nDrink: %s\n", box->drinks[i].name);
fprintf(write, "Consumed: %d\n", aperson->drinks_taken[i]);
fprintf(write, "Total: %d\n\n", aperson->drinks_taken[i]*box->drinks[i].price);
}
}
fclose(write);
return 0;
}
char* read_from_file_until(FILE *stream, char *str, int max_len, char until){
int i = 0;
int tmp_int = 0;
memset(str, 0, max_len);
for(i = 0; (tmp_int != until)&&(i < (max_len-1))&&(tmp_int != EOF); i++){
tmp_int = fgetc(stream);
//printf("%c", tmp_int);
str[i] = tmp_int;
}
if(tmp_int == EOF){
//May occur more often as the function tries to read a certain number of drinks.
//printf("END OF FILE REACHED!\nABORT!!\n\n");
str[0] = 0;
return str;
}
if(str[0] != '\n')str[i-1] = 0;
return str;
}
char *read_line_from_file(FILE *stream, char *str, int max_len){
do{
str = read_from_file_until(stream, str, max_len, '\n');
}while((str[0] == '\n'));
return str;
}