The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Revision:
18:be77ad141fac
Parent:
17:dc19b3b39790
Child:
22:93fde34d9a94
--- a/Battery/Battery.cpp	Mon Jun 13 09:04:53 2016 +0000
+++ b/Battery/Battery.cpp	Tue Jun 14 21:43:17 2016 +0000
@@ -34,7 +34,8 @@
 // Constructors
 // ------------------------------------------------------------------------
 
-Battery::Battery(FILE* fp, Serial *pc) : SolarNanoGrid(fp,pc){
+Battery::Battery(FILE* fp, Serial *pc) : SolarNanoGrid(fp,pc)
+{
     DBG("Initialize class");
     // ***** Variable initialisation *****
     // Public variable initialisation
@@ -63,6 +64,7 @@
 
     // Protected flags: *
     checkedIn=0;
+    flagDirFileName=0;
     flagGetTime=0;
     flagNextFile=0;
     flagSendFileName=0;
@@ -71,7 +73,10 @@
     flagSendFileDone=0;
     flagGetMaxCharge=0;
     sendingFile=0;
+    flagStartFileTx=0;
     startCharge = false;
+    flagHubNotReady=0;
+    flagCheckOut=0;
 
     // Protected interrupts
     button=NULL;
@@ -146,12 +151,13 @@
 /**
  * When the button is pressed transmit something
  */
-void Battery::intButton(){
-    if (!maxRT){
+void Battery::intButton()
+{
+    if (!maxRT) {
         *ce = 1;
-        if(checkedIn==0){
+        if(checkedIn==0) {
             checkIn();
-        } else{
+        } else {
             flagNextFile = 1;
         }
 
@@ -162,7 +168,8 @@
  * Called when the nrf creates an interrupt.
  *
  */
-void Battery::intNrf(){
+void Battery::intNrf()
+{
     //  DBG("NRF Interrupt Started.");
     int pipe=0;
     int status=0;
@@ -180,12 +187,20 @@
     // Check if max RT is reached
     //
     if (((status >>MAX_RT)& 01) ==1) {
-        maxRT=true;
-        DBG("MAX RT flag is set. PTB20= %d status=%x",(unsigned int) *ce,status);
-        *ce = 0;
-        DBG("PTB20= %d status=%x",(unsigned int) *ce,status);
-        nrf->clearStatus();
-        *ledRed=0;
+        if (flagStartFileTx==1) { // Locker not ready to reacieve - wait 25s.
+            flagHubNotReady = 1;
+            nrf->clearStatus();
+            flag = &flagDirFileName;
+            delay->detach();
+            delay->attach(this, &Battery::setFlag, 25.0f);
+        } else {
+            maxRT=true;
+            DBG("MAX RT flag is set. PTB20= %d status=%x",(unsigned int) *ce,status);
+            *ce = 0;
+            DBG("PTB20= %d status=%x",(unsigned int) *ce,status);
+            nrf->clearStatus();
+            *ledRed=0;
+        }
     }
 
     //
@@ -194,65 +209,77 @@
     if (((status >>RX_DR)& 01) ==1) { // Received a packet
         width= nrf->getRxData(dataRx);
         dataRx[width]='\0';
-        if (dataRx[0]!='S'){
+        if (dataRx[0]!='S') {
             DBG("intNrf>D[p=%1d]=<%2s> ",pipe,dataRx);
         }
         maxRT=false;
+        flagHubNotReady = 0;
         *ce = 1;
         nrf->clearStatus();
         switch (dataRx[0]) {
-        case ('C'):{
-            doCheckIn();
-            flagNextFile = 1;
-            DBG("Flag Set");
-            break;}
-        case ('D'):{
-            flag = &flagSendFileName;
-            delay->detach();
-            delay->attach(this, &Battery::setFlag, 1.5);
-            break;}
-        case ('F'):{
-            flag = &flagSendFileSize;
-            countSD = 0;
-            delay->detach();
-            delay->attach(this, &Battery::setFlag, 1.50);
-            break;}
-        case ('S'):{
-            flag = &flagSendFile;
-            delay->detach();
-            //  printf("%d\n\r",countSD%16);
-            //delay->attach(this, &Battery::setFlag, 0.05);
+            case ('C'): {
+                doCheckIn();
+                flagNextFile = 1;
+                DBG("Flag Set");
+                break;
+            }
+            case ('D'): {
+                flagStartFileTx=0; // Hub commmunicating.
+                flag = &flagSendFileName;
+                delay->detach();
+                delay->attach(this, &Battery::setFlag, 1.5);
+                break;
+            }
+            case ('F'): {
+                flag = &flagSendFileSize;
+                countSD = 0;
+                delay->detach();
+                delay->attach(this, &Battery::setFlag, 1.50);
+                break;
+            }
+            case ('O'): {
+                doCheckOut();
+                break;
+            }
+            case ('S'): {
+                flag = &flagSendFile;
+                delay->detach();
+                //  printf("%d\n\r",countSD%16);
+                //delay->attach(this, &Battery::setFlag, 0.05);
 
-            if (countSD % (SDBUFFERSIZE/32) == 0) {
-                delay->attach(this, &Battery::setFlag, 1.5); // wait to write to SD card
-            } else {
-                delay->attach_us(this, &Battery::setFlag, 800);
+                if (countSD % (SDBUFFERSIZE/32) == 0) {
+                    delay->attach(this, &Battery::setFlag, 1.5); // wait to write to SD card
+                } else {
+                    delay->attach_us(this, &Battery::setFlag, 800);
+                }
+                countSD++;
+                break;
             }
-            countSD++;
-            break;}
-        case ('T'):{
-            time_t now;
-            sscanf (&dataRx[2],"%x",&now );
-            set_time(now);
-            now= time(NULL);
-            struct tm * timeInf = localtime(&now);
-            DBG ("Time is now: %04d-%02d-%02d %02d:%02d:%02d \n\r",timeInf->tm_year+1900,timeInf->tm_mon+1,timeInf->tm_mday,timeInf->tm_hour, timeInf->tm_min,timeInf->tm_sec);
+            case ('T'): {
+                time_t now;
+                sscanf (&dataRx[2],"%x",&now );
+                set_time(now);
+                now= time(NULL);
+                struct tm * timeInf = localtime(&now);
+                DBG ("Time is now: %04d-%02d-%02d %02d:%02d:%02d \n\r",timeInf->tm_year+1900,timeInf->tm_mon+1,timeInf->tm_mday,timeInf->tm_hour, timeInf->tm_min,timeInf->tm_sec);
 
-            break;}
+                break;
+            }
 
 
-        case ('M'):{
-            time_t now;
-            sscanf (&dataRx[2],"%f",&maxChargeRate );
-            if(maxChargeRate>2.0f){
-                maxChargeRate=2.0f;
+            case ('M'): {
+                time_t now;
+                sscanf (&dataRx[2],"%f",&maxChargeRate );
+                if(maxChargeRate>2.0f) {
+                    maxChargeRate=2.0f;
+                }
+                if (maxChargeRate<0.0f) {
+                    maxChargeRate=0.0f;
+                }
+                DBG ("Max charge %f \n\r",maxChargeRate);
+
+                break;
             }
-            if (maxChargeRate<0.0f){
-                maxChargeRate=0.0f;
-            }
-            DBG ("Max charge %f \n\r",maxChargeRate);
-
-            break;}
         }
 
     }
@@ -276,7 +303,8 @@
  * Checks to see if transmit failed and then retransmit
  *
  */
-void Battery::retransmit(){
+void Battery::retransmit()
+{
 
     if (maxRT) {
         DBG("Retransmit");
@@ -287,7 +315,7 @@
         nrf->flushRx();
         nrf->retransmitData();
     }
-    if ((!sendingFile)&&(checkedIn == 1)){
+    if ((!sendingFile)&&(checkedIn == 1)) {
         flagGetTime=1;
         flagGetMaxCharge=1;
     }
@@ -302,20 +330,37 @@
  * Transmits "C ID"
  *
  */
-void Battery::checkIn() {
+void Battery::checkIn()
+{
     sprintf(dataTx, "C %04X", (int) (id&0XFFFF));
     DBG("Check into locker [%s]", dataTx);
     spiNRF();
     nrf->transmitData(dataTx, strlen(dataTx));
 }
 
+
+/**
+* Checks out. Sends O %d, ID to locker
+*
+*/
+void Battery::checkOut()
+{
+    sprintf(dataTx, "O %04X", (int) (id&0XFFFF));
+    DBG("Check out locker [%s]", dataTx);
+    spiNRF();
+    nrf->transmitData(dataTx, strlen(dataTx));
+    // Reset to locker public comms channel
+}
+
+
 /**
  * Checks in the Battery to the locker
  * Acknowledge received.
  * Changes the TX pipe address.
  *
  */
-void Battery::doCheckIn() {
+void Battery::doCheckIn()
+{
     nrf->setTxAddress(privateAddr);
     checkedIn = 1;
     DBG("Checked into locker");
@@ -330,7 +375,8 @@
  * Changes the TX pipe address.
  *
  */
-void Battery::doCheckOut() {
+void Battery::doCheckOut()
+{
     nrf->setTxAddress(openAddr);
     checkedIn = 0;
     DBG("Checked out of locker");
@@ -343,10 +389,11 @@
 /**
  * Slow routines for Battery
  */
-void Battery::loop() {
+void Battery::loop()
+{
 
     *ledGreen = !checkedIn;
-    
+
 
     if (flagGetTime) {
         flagGetTime=0;
@@ -363,14 +410,20 @@
         DBG("NextFile called");
         requestTime();
         nextFileToTx();
-        if (fileLength>0){
+        if (fileLength>0) {
             INFO("Sending file %s.",txFileName)
-            sendDirName();
+            flagDirFileName=1;
         } else {
             sendingFile=0;
+            flagStartFileTx=0;
             INFO("No more files to send.")
+            checkOut();
         }
     }
+    if (flagDirFileName) {
+        flagDirFileName=0;
+        sendDirName();
+    }
     if (flagSendFileName) {
         flagSendFileName = 0;
         sendFileName();
@@ -392,13 +445,18 @@
         delay->detach();
         delay->attach(this, &Battery::setFlag, 2.5);
     }
+    if (flagCheckOut) {
+        flagCheckOut=0;
+        doCheckOut();
+    }
 
 }
 
 /**
  * sends a request for time update.
  */
-void Battery::requestTime() {
+void Battery::requestTime()
+{
     DBG("getTime");
     nrf->transmitData("T ",2);
 }
@@ -406,7 +464,8 @@
 /**
  * sends a request for time update.
  */
-void Battery::requestMaxCharge() {
+void Battery::requestMaxCharge()
+{
     DBG("getTime");
     nrf->transmitData("M ",2);
 }
@@ -414,7 +473,8 @@
  * Selects the next file to transmit
  * Uses /sd/lastFileTx.txt to store the lst file transmitted
  */
-void Battery::nextFileToTx() {
+void Battery::nextFileToTx()
+{
 
     DBG("> nextTxFile");
 
@@ -488,11 +548,14 @@
     DBG("nextTxFile> Next is [%s] length %d", txFileName, fileLength);
 
 }
+
 /**
  * Sends the directory name, allows the otherside to create.
  */
 
-void Battery::sendDirName() {
+void Battery::sendDirName()
+{
+    flagStartFileTx=1;
     sprintf(dataTx, "D %s", logDir);
 
     DBG("send Dir Name [%s].\n\r", dataTx);
@@ -505,7 +568,8 @@
  * Sends the file name, if fileLength >0
  */
 
-void Battery::sendFileName() {
+void Battery::sendFileName()
+{
     // delay->detach();
     if (fileLength < 1) {
         return;
@@ -521,7 +585,8 @@
 /**
  * Sends the file length to the locker - this is also the cue that the following bytes are the file.
  */
-void Battery::sendFileSize() {
+void Battery::sendFileSize()
+{
     if (fileLength < 1) {
         return;
     }
@@ -539,7 +604,8 @@
 /**
  * Sends the file length to the locker - this is also the cue that the following bytes are the file.
  */
-void Battery::sendFile() {
+void Battery::sendFile()
+{
 
     //  printf("send File [%s] at %d  %d\n\r",fullTxFilePath,fileLength, fileLeft );
 
@@ -548,7 +614,7 @@
     //      sdBuffPnt=0;
     //      return;
     //  }
-    if (sdBuffPnt>=SDBUFFERSIZE){
+    if (sdBuffPnt>=SDBUFFERSIZE) {
         sdBuffPnt=0;
     }
     if (sdBuffPnt == 0) {
@@ -573,15 +639,15 @@
         DBG("W %d %d", fileLeft, sizeRead);
     }
     int size=32;
-    if (sizeRead<32){
+    if (sizeRead<32) {
         size=sizeRead;
     }
-    if ((fileLeft==0)&&(sizeRead==0)){
+    if ((fileLeft==0)&&(sizeRead==0)) {
         sdBuffPnt=0;
         flagSendFileDone=1;
         DBG("Done sending");
         startCharge = true;
-    }else{
+    } else {
         sizeRead -= size;
         nrf->transmitData(&(sdBuffer[sdBuffPnt]), size);
         sdBuffPnt+=size;
@@ -593,15 +659,16 @@
 /**
  * Update the last file sent file
  */
-void Battery::sendFileDone() {
-    if (strlen(txFileName)>0){
+void Battery::sendFileDone()
+{
+    if (strlen(txFileName)>0) {
         spiSD();
         FILE *fp = fopen("/sd/lastFileTx.txt", "w");
         if (fp != NULL) {
             fprintf(fp, "%s %u \n", txFileName, fileLength);
             fclose(fp);
             DBG("sendFileDone> updated lastFileTx.txt %s %u ", txFileName, fileLength);
-        }else{
+        } else {
             INFO("sendFileDone> Cannot update file lastFiletx.txt");
         }
         spiNRF();
@@ -613,7 +680,8 @@
 /**
  * Sets the value pointed to by flag to 1.
  */
-void Battery::setFlag() {
+void Battery::setFlag()
+{
     if (flag != NULL) {
         *flag = 1;
     }
@@ -625,14 +693,16 @@
  * Returns the max current that can be drawn from the system, in amps
  */
 
-float Battery::getMaxCurrent(){
+float Battery::getMaxCurrent()
+{
     return maxChargeRate;
 }
 
 /**
  * returns a string of the log directory
  */
-char* Battery::getLogDirectory(){
+char* Battery::getLogDirectory()
+{
 
     return logDir;
 }
@@ -640,14 +710,16 @@
 /**
  * Returns true if we are authorized to start charging.
  */
-bool Battery::startCharging(void){
+bool Battery::startCharging(void)
+{
     return startCharge;
 }
 
-bool Battery::isCheckedIn(void){
-    if(checkedIn == 1){
+bool Battery::isCheckedIn(void)
+{
+    if(checkedIn == 1) {
         return true;
-    }else{
+    } else {
         return false;
     }
 }