moved maxID to beerbox struct and update in readPerson

moved drinksMax to beerbox struct and refactored to maxDrink
added if(debug) to all serial write and serial print statements

Moved BLE com to seperat file and added command and subcommandparsing
Added filename JSON method
This commit is contained in:
Lukas Bachschwell 2016-07-07 09:52:14 +02:00
parent bed4087886
commit df94ff0654
5 changed files with 143 additions and 87 deletions

View File

@ -1,4 +1,4 @@
#define NUM_OF_PEOPLE 25
#define NUM_OF_PEOPLE 25 // This is (thank myself) not used
#define NUM_OF_DRINKS 10
#define DRINK_NAME_MAX_LENGTH 13
#define FILE_NAME_LEN 8
@ -31,6 +31,8 @@ typedef struct{
typedef struct{
Drink drinks[NUM_OF_DRINKS];
int waiting;
int maxDrink=0;
int maxID=0;
}Beerbox;
//this shall always just exist in a short term!

View File

@ -17,11 +17,10 @@ LiquidCrystal_I2C lcd(0x38, 8, 2); // 0x38 for PCF***A on address 000
File myFile;
const int chipSelect = 53;
char toprint[255];
char toprint[255]; // General print Buffer
Beerbox *box;
int drinksMax = 1; // TODO add this to the box struct
int maxID = 0;
//int maxDrink = 1; // TODO add this to the box struct
int lastButtonState = 1;
int currentDrink = 0;
@ -31,12 +30,9 @@ Person* persons;
int personCount;
String rfID;
unsigned long oldTime = 0;
unsigned long ledTime = 0;
void setup() {
//generall debugging
Serial.begin(115200);
@ -106,70 +102,8 @@ void loop() {
boxTimer();
ledTimer();
if (Serial3.available() > 0) {
char incomingByte = Serial3.read();
if (incomingByte == '!') { //2 createnew user
incomingByte = Serial3.read();
String personName;
while (incomingByte != ';')
{
personName = String(personName + incomingByte);
incomingByte = Serial3.read();
}
if(debug)Serial.println(personName);
lcd.clear();
lcd.print(personName);
if(debug)Serial.println(maxID);
//lcd.setCursor(0,1);
//lcd.print("Fin pls");
int fingerReturn = -1;
while (fingerReturn != 0)
{
fingerReturn = getFingerprintEnroll(maxID + 1);
}
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("RFID pls");
zugang = "";
while (zugang == "") {
getRFIDEnroll();
}
if(debug)Serial.println(zugang);
maxID++;
Person* newPerson = new Person;
personName.toCharArray(newPerson->name, personName.length()+1);
char tmp[FILE_NAME_LEN + 1];
sprintf(tmp,"p%03d%s",maxID,FILE_EXTENSION);
strcpy(newPerson->file_name, tmp);
zugang.toCharArray(newPerson->rfid_uuid, zugang.length()+1);
newPerson->finger_uuid = maxID;
newPerson->credits_left = 1000;
for (int i = 0; i < drinksMax; i++) {
newPerson->drinks_taken[i]=0;
}
write_pers_file(newPerson);
zugang = ""; // reset here
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("Sucess!");
delay(1000);
//and revert to normal:
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}
}
bluetoothCommands();
}//end loop
@ -189,7 +123,7 @@ void check_for_file(String filename, String extension) {
}
}
//BEYOND THIS POINT IS THE LEYER OF BEERBOX -- ENTER AT YOUR OWN RISK
//BEYOND THIS POINT IS THE LEYER OF BEERBOX -- ENTER AT YOUR OWN RISK######################################
char tmp_filename[FILE_NAME_LEN + FILE_EXTENSION_LEN + 2];
@ -239,7 +173,7 @@ Beerbox* read_beerbox(Beerbox *box) {
if (i < NUM_OF_DRINKS) {
sprintf(toprint, "Slots %d - %d are not in use!\n", i + 1, NUM_OF_DRINKS);
drinksMax = i;
box->maxDrink = i;
if(debug)Serial.println(toprint);
}
@ -304,7 +238,7 @@ Person read_person(Beerbox *box, char *filename) {
aperson.drinks_taken[tmpDrink] = tmpCount;
}
}
if (aperson.finger_uuid > box->maxID) box->maxID = aperson.finger_uuid;
print_person(box, &aperson);
//comment back in once it is fixed
@ -318,24 +252,23 @@ void print_person(Beerbox *box, Person *aperson) {
int i = 0;
sprintf(toprint, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name);
Serial.write(toprint);
if(debug)Serial.write(toprint);
sprintf(toprint, "rfid_uuid: %s\n", aperson->rfid_uuid);
Serial.write(toprint);
if(debug)Serial.write(toprint);
sprintf(toprint, "finger_uuid: %u\n", aperson->finger_uuid);
if (aperson->finger_uuid > maxID) maxID = aperson->finger_uuid;
if(debug)Serial.print(toprint);
sprintf(toprint, "credits_left: %u\n", aperson->credits_left);
if(debug)Serial.print(toprint);
for (i = 0; i < drinksMax; i++) {
for (i = 0; i < box->maxDrink; i++) {
if (aperson->drinks_taken[i] != -1) {
sprintf(toprint, "\nDrink: %s\n", box->drinks[i].name);
Serial.write(toprint);
if(debug)Serial.write(toprint);
sprintf(toprint, "Drink %d: %d\n", i, aperson->drinks_taken[i]);
Serial.write(toprint);
if(debug)Serial.write(toprint);
sprintf(toprint, "Total: %d.%02d Euro\n", aperson->drinks_taken[i]*box->drinks[i].price / 100, (aperson->drinks_taken[i]*box->drinks[i].price) % 100);
Serial.write(toprint);
if(debug)Serial.write(toprint);
}
}
@ -344,6 +277,7 @@ void print_person(Beerbox *box, Person *aperson) {
}
int update_pers_file() {
Person thisPerson = persons[lastPersonIndex];
File write;
@ -370,7 +304,7 @@ int update_pers_file() {
sprintf(tmp, "credits_left: %u\n", thisPerson.credits_left);
write.write(tmp, strlen(tmp));
for (i = 0; i < drinksMax; i++) {
for (i = 0; i < box->maxDrink; i++) {
if (thisPerson.drinks_taken[i] != -1) {
sprintf(tmp, "\ndrink_Count_%d: %d\n", i, thisPerson.drinks_taken[i]);

123
bluetooth.ino Normal file
View File

@ -0,0 +1,123 @@
void bluetoothCommands(){
if (Serial3.available() > 0) {
char command = Serial3.read();
char subCommand = Serial3.read();
if (command == '#') { //1 General Info Command
if(subCommand == '1'){ // Get All Drinks
char incomingByte = Serial3.read(); // ignore ;
} else if(subCommand == '2'){ // Get All filenames
char incomingByte = Serial3.read(); // ignore ;
//while
//filenames_JSON(box,person)
}
} else if (command == '!') { //2 User Managment Command
if(subCommand == '1'){ // Create new User
char incomingByte = Serial3.read();
String personName;
while (incomingByte != ';'){
personName = String(personName + incomingByte);
incomingByte = Serial3.read();
}
if(debug)Serial.println(personName);
lcd.clear();
lcd.print(personName);
if(debug)Serial.println(box->maxID);
//lcd.setCursor(0,1);
//lcd.print("Fin pls");
int fingerReturn = -1;
while (fingerReturn != 0)
{
fingerReturn = getFingerprintEnroll(box->maxID + 1);
}
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("RFID pls");
zugang = "";
while (zugang == "") {
getRFIDEnroll();
}
if(debug)Serial.println(zugang);
box->maxID++;
Person* newPerson = new Person;
personName.toCharArray(newPerson->name, personName.length()+1);
char tmp[FILE_NAME_LEN + 1];
sprintf(tmp,"p%03d%s",box->maxID,FILE_EXTENSION);
strcpy(newPerson->file_name, tmp);
zugang.toCharArray(newPerson->rfid_uuid, zugang.length()+1);
newPerson->finger_uuid = box->maxID;
newPerson->credits_left = 1000;
for (int i = 0; i < box->maxDrink; i++) {
newPerson->drinks_taken[i]=0;
}
write_pers_file(newPerson);
zugang = ""; // reset here
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("Sucess!");
delay(1000);
//and revert to normal:
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//new User
else if(subCommand == '2'){ //get user by filename
}//get user
else if(subCommand == '3'){ //trigger user scan for ID
//return filename
}//user scan
else if(subCommand == '4'){ //revoke user finger by filename
}//revoke finger
else if(subCommand == '5'){ //revoke user rfid by filename
}//revoke rfid
else if(subCommand == '6'){ //delete user by filename
}//delete user
else if(subCommand == '6'){ //set user credits
}//set credits
else if(subCommand == '6'){ //set name by filename
}//set name
} // User Managment Command
} //BLE available
}//end function
void filenames_JSON(Beerbox *box, Person *aperson) {
int i = 0;
sprintf(toprint, "{\"name\": \"%s\",\"filename\":\"%s\",\"creditsleft\":\"%s\",\"drinks\":[", aperson->name, aperson->file_name, aperson->credits_left);
Serial.write(toprint);
for (i = 0; i < box->maxDrink; i++) {
if (aperson->drinks_taken[i] != -1) {
if(i=0) sprintf(toprint, "{\"%s\":\"%s\"}", box->drinks[i].name,aperson->drinks_taken[i]);
else sprintf(toprint, ",{\"%s\":\"%s\"}", box->drinks[i].name,aperson->drinks_taken[i]);
Serial.write(toprint);
}
}
Serial.write("]}");
} // end print JSON

View File

@ -221,9 +221,6 @@ void getRFIDEnroll() {
delay(1000); // debounce time
serial1_flush_buffer();
}
}
}
@ -255,7 +252,7 @@ int write_pers_file(Person* thisPerson) {
sprintf(tmp, "credits_left: %u\n", thisPerson->credits_left);
write.write(tmp, strlen(tmp));
for (i = 0; i < drinksMax; i++) {
for (i = 0; i < box->maxDrink; i++) {
if (thisPerson->drinks_taken[i] != -1) {
sprintf(tmp, "\ndrink_Count_%d: %d\n", i, thisPerson->drinks_taken[i]);
write.write(tmp, strlen(tmp));

View File

@ -11,7 +11,7 @@ void checkButton() {
digitalWrite(led,HIGH);
ledTime = millis();
if (box->waiting == 0) {
if (currentDrink == drinksMax - 1) currentDrink = -1;
if (currentDrink == box->maxDrink - 1) currentDrink = -1;
currentDrink++;
if(debug)Serial.println(box->drinks[currentDrink].name);
lcd.clear();