commit 995fd9b81557a8543e9c7bb72d2372cbe597fc15 Author: Alexander Junk Date: Mon Jul 13 16:49:36 2015 +0200 First commit diff --git a/beerbox.c b/beerbox.c new file mode 100644 index 0000000..ef30311 --- /dev/null +++ b/beerbox.c @@ -0,0 +1,258 @@ + +#include +#include +#include +#include "beerbox.h" +#include +#include + +//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; +} diff --git a/beerbox.config b/beerbox.config new file mode 100644 index 0000000..c0029fd --- /dev/null +++ b/beerbox.config @@ -0,0 +1,19 @@ +drink number 1: Beer +price: 250 +quantity: 100 + +drink number 2: Fanta +price: 120 +quantity: 49 + +drink number 3: Schartnerbombe +price: 184 +quantity: 87 + +drink number 4: Cola +price: 130 +quantity: 13 + +drink number 5: Apfelspritzer +price: 100 +quantity: 120 diff --git a/beerbox.h b/beerbox.h new file mode 100644 index 0000000..6c2c0e1 --- /dev/null +++ b/beerbox.h @@ -0,0 +1,58 @@ +#ifndef BEERBOX_IS_INCLUDED +#define BEERBOX_IS_INCLUDED + +#define NUM_OF_PEOPLE 25 +#define NUM_OF_DRINKS 10 +#define DRINK_NAME_MAX_LENGTH 13 +#define FILE_NAME_LEN 4 +#define FILE_EXTENSION_LEN 4 +#define FILE_EXTENSION "pers" +#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*/ + char name[DRINK_NAME_MAX_LENGTH + 1]; + /*Value in cents*/ + int price; + /*available quantity*/ + int quantity; + +}Drink; + + + + +typedef struct{ + + Drink drinks[NUM_OF_DRINKS]; + +}Beerbox; + +typedef struct{ + + char file_name[FILE_NAME_LEN + 1]; + char name[NAME_LEN + 1]; + int rfid_uuid; + int finger_uuid; + + int drinks_taken[NUM_OF_DRINKS]; + +}Person; + + + +Beerbox* read_beerbox(Beerbox *box); +Beerbox* add_drink(Beerbox *box); +Person read_person(Beerbox *box, char *filename); +void print_person(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); +int update_pers_file(Beerbox *box, Person *aperson); +#endif diff --git a/beerbox.ino b/beerbox.ino new file mode 100644 index 0000000..216ff51 --- /dev/null +++ b/beerbox.ino @@ -0,0 +1,57 @@ +#include +#include + +File myFile; +const int chipSelect = 53; + + + + +void setup() { + Serial.begin(115200); + + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + Serial.print("Initializing SD card..."); + // On the Ethernet Shield, CS is pin 4. It's set as an output by default. + // Note that even if it's not used as the CS pin, the hardware SS pin + // (10 on most Arduino boards, 53 on the Mega) must be left as an output + // or the SD library functions will not work. + pinMode(SS, OUTPUT); + + if (!SD.begin(chipSelect)) { + Serial.println("initialization failed!"); + return; + } + Serial.println("initialization done."); + + check_for_file("p001", "per"); + + + + + +} + + +void loop() { + + +}//end loop + + +void check_for_file(String filename, String extension){ + String str = filename + '.' + extension; + char* buf = NULL; + str.toCharArray(buf, str.length() + 1); + + if (SD.exists("p001.per")) { + Serial.println(str + " exists."); + } + else { + Serial.println(str + " doesn't exist."); + } +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..77acac2 --- /dev/null +++ b/main.c @@ -0,0 +1,22 @@ +//#include +//#include +//#include "beerbox.h" +// +// +// +// +// +//int main() +//{ +// +// Beerbox *box = calloc(1, sizeof(Beerbox)); +// +// +// +// box = read_beerbox(box); +// read_person(box, "p001"); +// read_person(box, "p002"); +// +// +// return 0; +//} diff --git a/makefile b/makefile new file mode 100644 index 0000000..efdd804 --- /dev/null +++ b/makefile @@ -0,0 +1,35 @@ +CFLAGS= +PNAME=prog +CHECKPRUN=java -jar checkproject.jar +DRCHECKPRUN=java -Dcheckmem=yes -jar checkproject.jar +FETCH=curl -O "https://inf-swe-jenkins.technikum-wien.at/job/SYS-BMR-PAD-checkproject/lastSuccessfulBuild/artifact/deploy/{checkproject.jar,checkproject.sh,properties.txt}" --progress-bar +FETCHMEM=curl -L -O https://bintray.com/artifact/download/bruening/DrMemory/DrMemory-MacOS-1.8.1-0.tar.gz --progress-bar +LINE=\n-----------------------------------------------\n + +all: clean + gcc $(CFLAGS) *.c -o $(PNAME) + +clean: + rm -rf *o $(PNAME) $(PNAME).* + +run: all + clear + ./prog + +debug: all + lldb ./$(PNAME) + +gdebug: all + lldb -s gui ./(PNAME) + +check: getcheck all + $(CHECKPRUN) + +drcheck: getcheck getdrmem all + $(DRCHECKPRUN) + +getcheck: + @test -s ./checkproject.jar || (echo "\033[31m$(LINE)checkproject seems to be missing!\nFetching NOW!\033[0m";$(FETCH);echo "\033[31mmodding properties.txt\033[0m";sed -i '' -e 's/# exec=/exec=prog/' properties.txt;sed -i '' -e 's/checkmem=no/#checkmem=yes/' properties.txt;sed -i '' -e 's/# drMemoryPath=/drMemoryPath=..\/..\/drmemory/' properties.txt;echo "\033[34mDONE$(LINE)\033[0m") + +getdrmem: + @test -d ../../drmemory || (echo "\033[31m$(LINE)DRMemory seems to be missing!\nFetching NOW!\033[0m";$(FETCHMEM);echo "\033[31m$ Extracting package\033[0m";tar -zxf DrMemory-MacOS-1.8.1-0.tar.gz ;echo "\033[34m$(LINE)Deleting image!\033[0m";echo "\033[34m$(LINE)Moving extracted Folder!\033[0m";mv DrMemory-MacOS-1.8.1-0 drmemory;mv drmemory ../../;echo "\033[34mDONE$(LINE)\033[0m") diff --git a/p001.pers b/p001.pers new file mode 100644 index 0000000..8f359fa --- /dev/null +++ b/p001.pers @@ -0,0 +1,12 @@ +Alex +rfid_uuid: 123456789 +finger_uuid: 987654321 + +Drink: Beer +Consumed: 6 +Total: 1500 + + +Drink: Schartnerbombe +Consumed: 6 +Total: 1104 diff --git a/p002.pers b/p002.pers new file mode 100644 index 0000000..b3b3007 --- /dev/null +++ b/p002.pers @@ -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 + diff --git a/prog b/prog new file mode 100755 index 0000000..313f5dc Binary files /dev/null and b/prog differ diff --git a/prog2 b/prog2 new file mode 100755 index 0000000..a4f1ff2 Binary files /dev/null and b/prog2 differ