The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Revision:
18:be77ad141fac
Parent:
13:de43f28c0365
Child:
24:6aba7f16e0c6
--- a/Locker/Locker.cpp	Mon Jun 13 09:04:53 2016 +0000
+++ b/Locker/Locker.cpp	Tue Jun 14 21:43:17 2016 +0000
@@ -31,7 +31,8 @@
 #include "defs.h"
 
 Locker::Locker(FILE* fp,Serial *pc) :
-SolarNanoGrid(fp,pc) {
+    SolarNanoGrid(fp,pc)
+{
     DBG("Initialize class locker");
 
 
@@ -57,6 +58,7 @@
     width = 0;
     now=0;
     lastRxTme=0;
+    maxCharge=1.0;
 
     // flags
     flagRotate=false;
@@ -78,6 +80,7 @@
 
     temp  = (char *) calloc(64, 1);
     timeValue = (char *) calloc(32, 1);
+    maxChargeValue= (char *) calloc(32, 1);
     sdBuffer = (char *) calloc(SDBUFFERSIZE,1);
     doOneSecond();
     //
@@ -142,13 +145,17 @@
 // Interrupt routines
 // ------------------------------------------------------------------------
 
-void Locker::intOneSecond() {
+void Locker::intOneSecond()
+{
     flagOneSecond = 1;
 }
 
-void Locker::doOneSecond() {
+void Locker::doOneSecond()
+{
     now = time(NULL);
     sprintf(timeValue, "T %x", now);
+    sprintf(maxChargeValue,"M %4.2f",maxCharge);
+
     *ledGreen = !*ledGreen;
 
     //  DBG("One second: %s", timeValue);
@@ -157,7 +164,8 @@
  * called when the nrf creates an interrupt.
  *
  */
-void Locker::intNrf() {
+void Locker::intNrf()
+{
 
     int bID = 0;
     int status = 0;
@@ -183,6 +191,9 @@
         } else if (dataRx[0] == 'T') {
             // Sends the time - this is updated in doOneSecond()
             nrf->acknowledgeData(timeValue, strlen(timeValue), pipe);
+        } else if (dataRx[0] == 'M') {
+            // Sends the maxCharge value - this is updated in doOneSecond()
+            nrf->acknowledgeData(maxChargeValue, strlen(maxChargeValue), pipe);
         } else  {
             nrf->acknowledgeData(dataRx, 2, pipe);
             DBG("intNrf>%s %x",dataRx,bID);
@@ -200,22 +211,32 @@
                     WARN("Battery %04X is already checked in.", battID);
                 }
                 flagRotate = 1;
-            }else  if (dataRx[0] == 'b') { //Battery drop off
+            } else  if (dataRx[0] == 'b') { //Battery drop off
                 DBG("intNrf>User check in %s",dataRx);
                 sscanf (&dataRx[2], "%x %x", &userIDDrop, &numBatDrop);
                 int pod = (userIDDrop&0XF0)>>4;
-                if (dataRx[1] == 'd'){
+                if (dataRx[1] == 'd') {
                     turnPodOn(pod,1);
                     *ledRed=0;
                 }
-                if (dataRx[1] == 'p'){
+                if (dataRx[1] == 'p') {
                     turnPodOn(pod,0);
                     *ledRed=1;
                 }
 
 
             }
-        }else if (pipe>1) {
+        } else if (pipe>1) {
+            if (dataRx[0] == 'O') { //Request for check out
+                int battID;
+                sscanf(&dataRx[2], "%x", &battID);
+                bID = battID & 0x00ff;
+
+                battIn[bID] = CHECKED_OUT;
+                DBG("intNrf>Check out %d", bID);
+
+                flagRotate = 1;
+            }
             flagNrf = 1;
         }
 
@@ -243,41 +264,41 @@
             saveFile(p,dataRx,width);
         } else {
             switch (dataRx[0]) {
-            case ('D'): { // Directory name.
-                *ce = 0;
-                sscanf (&dataRx[2],"%s",&(dirNames[p][0]));
-                spiSD();
-                DBG("doNrf>>>Making dir %s:",dirNames[p]);
-                int ok = mkdir(dirNames[p],777);
-                spiNRF();
-                DBG("doNrf>>>Directory name[%d] = <%s> OK=%d",p,dirNames[p],ok);
-                *ce = 1;
-                break;
-            }
-            case ('F'): { // File name
-                *ce = 0;
-                strncpy(&fileNames[p][0],&dataRx[2],30);
-                //  sscanf (&dataRx[2],"%20s",&fileNames[p][0]);
-                sprintf(temp,"%s/%s",dirNames[p],fileNames[p]);
-                DBG("doNrf>>> Creating File name<%s>",temp);
-                spiSD();
-                // Make sure file is created and reset
-                FILE *fp = fopen(temp,"wb");
-                fclose(fp);
-                spiNRF();
-                DBG("doNrf>>>File name[%d] = <%s>",p,fileNames[p]);
-                *ce = 1;
-                break;
+                case ('D'): { // Directory name.
+                    *ce = 0;
+                    sscanf (&dataRx[2],"%s",&(dirNames[p][0]));
+                    spiSD();
+                    DBG("doNrf>>>Making dir %s:",dirNames[p]);
+                    int ok = mkdir(dirNames[p],777);
+                    spiNRF();
+                    DBG("doNrf>>>Directory name[%d] = <%s> OK=%d",p,dirNames[p],ok);
+                    *ce = 1;
+                    break;
+                }
+                case ('F'): { // File name
+                    *ce = 0;
+                    strncpy(&fileNames[p][0],&dataRx[2],30);
+                    //  sscanf (&dataRx[2],"%20s",&fileNames[p][0]);
+                    sprintf(temp,"%s/%s",dirNames[p],fileNames[p]);
+                    DBG("doNrf>>> Creating File name<%s>",temp);
+                    spiSD();
+                    // Make sure file is created and reset
+                    FILE *fp = fopen(temp,"wb");
+                    fclose(fp);
+                    spiNRF();
+                    DBG("doNrf>>>File name[%d] = <%s>",p,fileNames[p]);
+                    *ce = 1;
+                    break;
 
-            }
-            case ('S'): { // File name
-                battIn[battPipes[p]] = SENDING;
-                sscanf (&dataRx[2],"%x",&lengthFile[p]);
-                sdBuffPnt=0; // set buffer to start
-                DBG("doNrf>>>File Length[%d] = <%u>",p,lengthFile[p]);
-                break;
+                }
+                case ('S'): { // File name
+                    battIn[battPipes[p]] = SENDING;
+                    sscanf (&dataRx[2],"%x",&lengthFile[p]);
+                    sdBuffPnt=0; // set buffer to start
+                    DBG("doNrf>>>File Length[%d] = <%u>",p,lengthFile[p]);
+                    break;
 
-            }
+                }
 
             }
         }
@@ -288,7 +309,8 @@
 /**
  * When the button is pressed print status
  */
-void Locker::intButton() {
+void Locker::intButton()
+{
     DBG("int Button");
     intRxClean();
 }
@@ -296,8 +318,9 @@
 /**
  * Cleans the receiver
  */
-void Locker::intRxClean() {
-    if (now - lastRxTme>60){
+void Locker::intRxClean()
+{
+    if (now - lastRxTme>60) {
         nrfFlush();
         wait(0.01);
         nrfFlush();
@@ -308,7 +331,8 @@
 
 // Loop through slow routines
 
-void Locker::loop(void) {
+void Locker::loop(void)
+{
     while (1) {
         if (flagRotate == 1) {
             flagRotate = 0;
@@ -332,7 +356,8 @@
 /**
  * Rotates one battery the ON_AIR batteries.
  */
-void Locker::doRotate() {
+void Locker::doRotate()
+{
     DBG("Rotate");
 
     // Select battery
@@ -450,7 +475,8 @@
 /**
  * Turns a pod power on
  */
-void Locker::turnPodOn(int pod,int on){
+void Locker::turnPodOn(int pod,int on)
+{
     DBG("Pod %d on %d",pod,on);
     pods[pod]->write(on);
 }