The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Revision:
28:e85a86fc8b59
Parent:
25:1979c129acb0
Child:
29:bbdbe1094a63
--- a/Battery/Battery.cpp	Tue Jun 21 10:14:12 2016 +0000
+++ b/Battery/Battery.cpp	Tue Jun 21 15:36:46 2016 +0000
@@ -77,7 +77,7 @@
     startCharge = false;
     flagHubNotReady=0;
     flagCheckOut=0;
-
+    sending=0;
     // Protected interrupts
     button=NULL;
     txWatch=NULL;
@@ -175,6 +175,8 @@
     int status=0;
     int width=0;
 
+    //INFO("intNrf");
+
     //
     //Get status and pipe
     //
@@ -243,14 +245,15 @@
             }
             case ('S'): {
                 flag = &flagSendFile;
+
                 delay->detach();
                 //  printf("%d\n\r",countSD%16);
                 //delay->attach(this, &Battery::setFlag, 0.05);
 
-                if (countSD % (SDBUFFERSIZE/32) == 0) {
+               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);
+                    delay->attach_us(this, &Battery::setFlag, 800); // was 800
                 }
                 countSD++;
                 break;
@@ -268,7 +271,7 @@
 
 
             case ('M'): {
-                time_t now;
+
                 sscanf (&dataRx[2],"%f",&maxChargeRate );
                 if(maxChargeRate>2.0f) {
                     maxChargeRate=2.0f;
@@ -391,7 +394,7 @@
  */
 void Battery::loop()
 {
-
+    //INFO("In Battery Loop");
     *ledGreen = !checkedIn;
 
 
@@ -407,6 +410,7 @@
     }
     if (flagNextFile) {
         flagNextFile = 0;
+        sending=1;
         DBG("NextFile called");
         requestTime();
         nextFileToTx();
@@ -419,6 +423,7 @@
             INFO("No more files to send.")
             startCharge = true;
             checkOut();
+            sending=0;
         }
     }
     if (flagDirFileName) {
@@ -436,6 +441,7 @@
     }
     if (flagSendFile) {
         flagSendFile = 0;
+        sending=1;
         sendFile();
     }
     if (flagSendFileDone) {
@@ -450,7 +456,8 @@
         flagCheckOut=0;
         doCheckOut();
     }
-
+    //INFO("Out Battery Loop");
+    return;
 }
 
 /**
@@ -458,8 +465,10 @@
  */
 void Battery::requestTime()
 {
-    DBG("getTime");
-    nrf->transmitData("T ",2);
+    if(sending==0) {
+        DBG("getTime");
+        nrf->transmitData("T ",2);
+    }
 }
 
 /**
@@ -467,8 +476,10 @@
  */
 void Battery::requestMaxCharge()
 {
-    DBG("Get Max Charge Rate");
-    nrf->transmitData("M ",2);
+    if(sending==0) {
+        DBG("Get Max Charge Rate");
+        nrf->transmitData("M ",2);
+    }
 }
 /**
  * Selects the next file to transmit
@@ -492,11 +503,12 @@
     FILE *fp = fopen("/sd/lastFileTx.txt", "r");
     if (fp != NULL) {
         if (fscanf(fp, "%s %u", lastFileName, &txLength) != 2) {
-            sprintf(lastFileName, "a");
+            sprintf(lastFileName, "0");
         }
         fclose(fp);
     } else {
-        sprintf(lastFileName, "a");
+        sprintf(lastFileName, "0");
+        txLength = 0;
     }
     DBG("nextFileToTx>Last Filename=%s len=%u",lastFileName,txLength);
     // Open directory and read files
@@ -559,7 +571,7 @@
     flagStartFileTx=1;
     sprintf(dataTx, "D %s", logDir);
 
-    DBG("send Dir Name [%s].\n\r", dataTx);
+    DBG("send Dir Name [%s].", dataTx);
 
     spiNRF();
     nrf->transmitData(dataTx, strlen(dataTx));
@@ -618,6 +630,7 @@
     if (sdBuffPnt>=SDBUFFERSIZE) {
         sdBuffPnt=0;
     }
+    
     if (sdBuffPnt == 0) {
         spiSD();
         FILE *fp = fopen(fullTxFilePath, "rb");
@@ -627,7 +640,9 @@
             return;
         }
         spiSD();
+        INFO("FileLength: %u, fileLeft: %u, SD Buffer Size: %u, Full file name: %s", fileLength, fileLeft, SDBUFFERSIZE, fullTxFilePath);
         fseek(fp, fileLength - fileLeft, SEEK_SET);
+        spiSD();
         sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp);
         fileLeft -= sizeRead;
         fclose(fp);
@@ -635,7 +650,8 @@
         spiNRF();
 
     }
-
+    spiNRF();
+    
     if ((count++) % 256 ==0) {
         DBG("W %d %d", fileLeft, sizeRead);
     }
@@ -647,13 +663,14 @@
         sdBuffPnt=0;
         flagSendFileDone=1;
         DBG("Done sending");
-        startCharge = true;
     } else {
+        
         sizeRead -= size;
+        spiNRF();
         nrf->transmitData(&(sdBuffer[sdBuffPnt]), size);
         sdBuffPnt+=size;
     }
-
+    
 
 }
 
@@ -662,11 +679,14 @@
  */
 void Battery::sendFileDone()
 {
+    DBG("Send File Done. Writing to lastFileTx.txt...");
     if (strlen(txFileName)>0) {
         spiSD();
         FILE *fp = fopen("/sd/lastFileTx.txt", "w");
         if (fp != NULL) {
+            spiSD();
             fprintf(fp, "%s %u \n", txFileName, fileLength);
+            spiSD();
             fclose(fp);
             DBG("sendFileDone> updated lastFileTx.txt %s %u ", txFileName, fileLength);
         } else {
@@ -731,6 +751,6 @@
     txWatch->detach();
     delay->detach();
     nrf->setPwrDown();
-    
+
     return;
 }