This commit is contained in:
James Devine 2018-03-27 01:17:42 +02:00 committed by GitHub
parent 57b6308506
commit fa9336c41b

View file

@ -3,14 +3,21 @@
#include <ParticleSoftSerial.h>
#include "Particle.h"
//note that the number of live reservations in the system is stored in eeprom location 2046 (i.e. the last slot)
//This is licensed under GPL V3.
//status:
//master rfid is encoded.
//gps position feedback up and running
//Battery Voltage added.
//working on adding reservations
//homebox function needs writing
//Reservation command works -> prints out data locally from parser;
//need to chain this up with the other functions to create a reservation structure and push this to eeprom
//Reservation data is now stored in the EEPROM (36 bytes per reservation, starting from byte 0)
//the number of reservations needs to be limited; to 50 - to allow some space for other persistent variables (?)
//This limiting must be added to the code; if reservations > 50; return -1.
//Recalling reservation data mechanism to be done most easily via publish process
//then work on the indexing + 'live' reservation and reservation deletion.
//Somehow reservations need to be archived in a sequential order. Bubble sort?
@ -58,7 +65,7 @@ bool DoorsOpenBool = false;
void clearEEPROM(); // forward declaration
int EEPROMctr = 0; //eeprom index
int EEPROMctr; //eeprom index
//Note that the 0th value of the EEPROM is used to store the number of reservations
//Values 1+ store reservations in blocks of 36 bytes.
int ReservationSize = 36; //number of bytes per reservation
@ -68,6 +75,8 @@ int ReservationSize = 36; //number of bytes per reservation
//some strings that we'll use for system variables
String ReservationsTable;
String ValidGPSPosition;
int BatteryVoltageRead;
double BatteryVoltage;
//define a master RFID card that can always open/close the door regardless of reservation status (i.e. for pool manager)
String MasterRFIDCard = "983553fe";
@ -103,6 +112,12 @@ void ReservationCmd::extractValues (String stringPassed){
argument = stringPassed;
}
void debug(String message, int value) {
char msg [50];
sprintf(msg, message.c_str(), value);
Particle.publish("DEBUG", msg);
}
void DoorOpenFn()
{
digitalWrite(OpenRelay,HIGH);
@ -126,6 +141,50 @@ void StatusLEDFn()
digitalWrite(StatusLED,LOW);
}
Reservation CreateReservation(String CUserID, int CStartDay, int CStartMonth, int CStartYear, int CStartHour, int CStartMinute, int CDuration)
{
//make a buffer for the reservation
Reservation CreateResvn;
//transfer the data from the function - there's probably a better way to do the string transfer
CreateResvn.UserID[0] = CUserID[0];
CreateResvn.UserID[1] = CUserID[1];
CreateResvn.UserID[2] = CUserID[2];
CreateResvn.UserID[3] = CUserID[3];
CreateResvn.UserID[4] = CUserID[4];
CreateResvn.UserID[5] = CUserID[5];
CreateResvn.UserID[6] = CUserID[6];
CreateResvn.UserID[7] = CUserID[7];
CreateResvn.StartDay = CStartDay;
CreateResvn.StartMonth = CStartMonth;
CreateResvn.StartYear = CStartYear;
CreateResvn.StartHour = CStartHour;
CreateResvn.StartMinute = CStartMinute;
CreateResvn.Duration = CDuration;
//send the created variable back
return CreateResvn;
}
int AddReservation(Reservation ReservationToAdd)
{
//work out the next free spot and put it in that reservation
EEPROM.put((EEPROMctr*ReservationSize), ReservationToAdd);
//update the counter
EEPROMctr++;
//re-write the counter back to eeprom for synchronisation, which we store at the end of the memory space.
EEPROM.put(2046,EEPROMctr);
//return the reservation number
return EEPROMctr;
}
void clearEEPROM() {
for(int addr = 0; addr < 2047; addr++) {
EEPROM.write(addr, 0);
}
}
void setup()
{
@ -133,6 +192,7 @@ void setup()
pinMode(OpenRelay, OUTPUT);
pinMode(CloseRelay, OUTPUT);
pinMode(StatusLED, OUTPUT);
pinMode(A1,AN_INPUT);
//set these outputs correctly
digitalWrite(OpenRelay, LOW);
digitalWrite(CloseRelay, LOW);
@ -152,7 +212,8 @@ void setup()
//define variables
Particle.variable("CarResvn", ReservationsTable);
Particle.variable("CarPos", ValidGPSPosition);
Particle.variable("BatLevel", BatteryVoltage);
//set up serial debug link
Serial.begin();
Serial.printlnf("ready for data");
@ -165,7 +226,7 @@ void setup()
_timer.start();
//initialise the EEPROM counter with the number of reservations that were in memory before poweroff.
EEPROM.get(0, EEPROMctr);
EEPROM.get(2046, EEPROMctr);
}
@ -208,7 +269,8 @@ void loop()
}
ValidGPSPosition = rmc.latitude + ";" + rmc.northSouthIndicator + ";" + rmc.longitude + ";" + rmc.eastWestIndicator + ";" + rmc.utcTime;
Serial.println(ValidGPSPosition);
BatteryVoltageRead = analogRead(A1);
BatteryVoltage = (((BatteryVoltageRead * 3.3)/4096)/.26);
int counter = 0;
unsigned long cardread = 0;
@ -348,6 +410,7 @@ int ReserveString(String mssgArgs){
Serial.println("Valid Reservation Recieved...");
Serial.print("UserID: ");
Serial.println(command.UserIDStr());
debug(command.UserIDStr(),1);
Serial.println("Start Day = ");
Serial.println(command.StartDayInt());
Serial.println("Start Month = ");
@ -362,8 +425,17 @@ int ReserveString(String mssgArgs){
Serial.println(command.DurationInt());
// updateVariables(i, command.mssgText(), command.mssgValue0(), command.mssgValue1()); // Pass decoded variables to function
// badMessage = false;
return 1;
//now create a temporary reservation record
Reservation ResvToInsert;
//and populate it with the values we just parsed
ResvToInsert = CreateReservation(command.UserIDStr(), command.StartDayInt(), command.StartMonInt(), command.StartYearInt(), command.StartHourInt(), command.StartMinuteInt(), command.DurationInt());
//and now insert it into the EEPROM
//initialise a dummy variable to hold the reservation number (it'll be >1 by the time it comes back)
int ReservationNumberAdded = 0;
//use the return from AddReservation to pass the reservation number post insertion
ReservationNumberAdded = AddReservation(ResvToInsert);
//return the reservation number.
return ReservationNumberAdded;
}
int CancelString(String command){
@ -391,11 +463,25 @@ int HomeCoords(String command){
}
int ShowReservations(String command){
if (command=="on") {
digitalWrite(StatusLED,HIGH);
delay(1000);
digitalWrite(StatusLED,LOW);
return 1;
//is the reservation in the table? (i.e. is it higher than the max resvn. number, if so fail)
if (command.toInt()<= EEPROMctr) {
//Create a buffer to store the recalled data
Reservation RetRes;
//extract it from the EEPROM
EEPROM.get(((command.toInt()-1)*ReservationSize), RetRes);
//Now format it as a string
const int STRING_BUF_SIZE = 9;
char stringBuf[STRING_BUF_SIZE];
stringBuf[sizeof(stringBuf) - 1] = 0; // make sure it's null terminated
// Initialize a String object from the buffer
String str(stringBuf);
str = String(RetRes.UserID);
//Print it out
Serial.printlnf("ReservationNum=%d, UserID=%s, Day=%d, Month=%d, Year=%d, Hour=%d, Minute=%d, Duration=%d, sizeof(RetRes)=%d", command.toInt(), str.c_str(), RetRes.StartDay, RetRes.StartMonth, RetRes.StartYear, RetRes.StartHour, RetRes.StartMinute, RetRes.Duration, sizeof(RetRes));
debug(str.c_str(),1);
return 1;
}
else {
return -1;
@ -409,67 +495,12 @@ int WipeReservations(String command) {
//Wipe the EEPROM
clearEEPROM();
//Reset the onboard reservation counter to 0 (assuming EEPROM is wiped)
EEPROM.get(0, EEPROMctr);
EEPROMctr = 0;
EEPROM.put(2046, EEPROMctr);
return 1;
}
else {
return -1;
}
}
Reservation CreateReservation(String CUserID, int CStartDay, int CStartMonth, int CStartYear, int CStartHour, int CStartMinute, int CDuration)
{
//make a buffer for the reservation
Reservation CreateResvn;
//transfer the data from the function - there's probably a better way to do the string transfer
CreateResvn.UserID[0] = CUserID[0];
CreateResvn.UserID[1] = CUserID[1];
CreateResvn.UserID[2] = CUserID[2];
CreateResvn.UserID[3] = CUserID[3];
CreateResvn.UserID[4] = CUserID[4];
CreateResvn.UserID[5] = CUserID[5];
CreateResvn.UserID[6] = CUserID[6];
CreateResvn.UserID[7] = CUserID[7];
CreateResvn.StartDay = CStartDay;
CreateResvn.StartMonth = CStartMonth;
CreateResvn.StartYear = CStartYear;
CreateResvn.StartHour = CStartHour;
CreateResvn.StartMinute = CStartMinute;
CreateResvn.Duration = CDuration;
//send the created variable back
return CreateResvn;
}
int AddReservation(Reservation ReservationToAdd)
{
//work out the next free spot and put it in that reservation
EEPROM.put(((EEPROMctr*ReservationSize)+1), ReservationToAdd);
EEPROMctr++;
return EEPROMctr;
}
void RecallReservation(int ResvnNumber)
{
//Create a buffer to store the recalled data
Reservation RetRes;
//extract it from the EEPROM
EEPROM.get((((ResvnNumber-1)*ReservationSize)+1), RetRes);
//Now format it as a string
const int STRING_BUF_SIZE = 9;
char stringBuf[STRING_BUF_SIZE];
stringBuf[sizeof(stringBuf) - 1] = 0; // make sure it's null terminated
// Initialize a String object from the buffer
String str(stringBuf);
str = String(RetRes.UserID);
//Print it out
Serial.printlnf("ReservationNum=%d, UserID=%s, Day=%d, Month=%d, Year=%d, Hour=%d, Minute=%d, Duration=%d, sizeof(RetRes)=%d", ResvnNumber, str.c_str(), RetRes.StartDay, RetRes.StartMonth, RetRes.StartYear, RetRes.StartHour, RetRes.StartMinute, RetRes.Duration, sizeof(RetRes));
}
void clearEEPROM() {
for(int addr = 0; addr < 256; addr++) {
EEPROM.write(addr, 0);
}
}