From 995fd9b81557a8543e9c7bb72d2372cbe597fc15 Mon Sep 17 00:00:00 2001 From: Alexander Junk Date: Mon, 13 Jul 2015 16:49:36 +0200 Subject: [PATCH] First commit --- beerbox.c | 258 +++++++++++++++++++++++++++++++++++++++++++++++++ beerbox.config | 19 ++++ beerbox.h | 58 +++++++++++ beerbox.ino | 57 +++++++++++ main.c | 22 +++++ makefile | 35 +++++++ p001.pers | 12 +++ p002.pers | 23 +++++ prog | Bin 0 -> 13836 bytes prog2 | Bin 0 -> 8700 bytes 10 files changed, 484 insertions(+) create mode 100644 beerbox.c create mode 100644 beerbox.config create mode 100644 beerbox.h create mode 100644 beerbox.ino create mode 100644 main.c create mode 100644 makefile create mode 100644 p001.pers create mode 100644 p002.pers create mode 100755 prog create mode 100755 prog2 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 0000000000000000000000000000000000000000..313f5dccdc7b289176e77bed4ee21f971f0185de GIT binary patch literal 13836 zcmeHOZEPIJd7iT*TV~`Q?MjK-$ks*>W7QuNDTsz*i74rC(^W${v`hwqV|8`BJBk;R zNA^ByG6)M@q&04}S%X1CM@5^2No+uHKSU%_K)a?*S+0(95Q%NmLMRAZMU-~wG6jn^ z2^*3*_f5uru%1JoC;wGw<&0?p^-nPjB8OL}-l=NAD0qRO1|9Cxj(5 zP(u6$P6MZDwm1Kd{)~R$GnB48LMz!-IpXX<2d3GfAM99ZBHL>#Wq$Vpz6rP^?)25B znTZc(mTk-N>V6G_f%>me^B})gk*ZuPj+U~fna*Up(nV1@-p{uvyl&qj;DZM1eY|D= zh-dc1dk5kv7?tBad%wba-3K7gxEGIG{EMbDsYLRKZ%~f+;Rh66(w_wKrN;_vC`>b( zd_IwknTcd~zvNfW$MLNSk8{q0d^v{FsM0j!J!W?*(iac-m3T8gUY*~FJl5K653Z9u z#$uW+%^l4pW}5O#E>McW%YzFR2iFO4gSj@%WUrYX>g($7H3u@O+v54nC0?mtoX>l4 z28U@L=`UG!x8PN4%D?q~1M=l~zaD_0HqGA1|K{IjwC9xO{Wj!F@i^X%zK)e?M*I8v z`jdgEJbq)E!mA1lPzSN1h<%kFoCme(*uX!Uz*nO|d%X}-ka4YT#(8*`5ZHR+%x)nLLjNJq?Kn%1yTR}*jcci% zw1)OI; zVT(6bhTC`y(B3F*+(sAlxbR$XZvkP+wwh3SZQu)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