First commit
This commit is contained in:
commit
995fd9b815
258
beerbox.c
Normal file
258
beerbox.c
Normal file
@ -0,0 +1,258 @@
|
||||
|
||||
#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;
|
||||
}
|
19
beerbox.config
Normal file
19
beerbox.config
Normal file
@ -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
|
58
beerbox.h
Normal file
58
beerbox.h
Normal file
@ -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
|
57
beerbox.ino
Normal file
57
beerbox.ino
Normal file
@ -0,0 +1,57 @@
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
22
main.c
Normal file
22
main.c
Normal file
@ -0,0 +1,22 @@
|
||||
//#include <stdio.h>
|
||||
//#include <stdlib.h>
|
||||
//#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;
|
||||
//}
|
35
makefile
Normal file
35
makefile
Normal file
@ -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")
|
12
p001.pers
Normal file
12
p001.pers
Normal 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.pers
Normal file
23
p002.pers
Normal 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
|
||||
|
Loading…
Reference in New Issue
Block a user