The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Revision:
12:d9b13f31206d
Parent:
11:87ab310924f0
Child:
13:de43f28c0365
--- a/Battery/Battery.cpp	Wed Jun 08 22:12:52 2016 +0000
+++ b/Battery/Battery.cpp	Wed Jun 08 22:48:09 2016 +0000
@@ -39,7 +39,7 @@
     // ***** Variable initialisation *****
     // Public variable initialisation
 
-    maxChargeRate=0.0; // Charge rate in amps
+    maxChargeRate=1.0; // Charge rate in amps
 
     // Protected variables initialisation
     count=0;
@@ -69,6 +69,8 @@
     flagSendFileSize=0;
     flagSendFile=0;
     flagSendFileDone=0;
+    flagGetMaxCharge=0;
+    sendingFile=0;
 
     // Protected interrupts
     button=NULL;
@@ -236,8 +238,23 @@
 
             break;}
 
+
+        case ('M'):{
+            time_t now;
+            sscanf (&dataRx[2],"%f",&maxChargeRate );
+            if(maxChargeRate>2.0){
+                maxChargeRate=2.0;
+            }
+            if (maxChargeRate<0.0){
+                maxChargeRate=0.0;
+            }
+            DBG ("Max charge %f \n\r",maxChargeRate);
+
+            break;}
         }
+
     }
+
     if (((status >>TX_DS)& 01) ==1) {
         //  maxRT=false;
         nrf->flushTx();
@@ -268,6 +285,10 @@
         nrf->flushRx();
         nrf->retransmitData();
     }
+    if ((!sendingFile)&&(checkedIn == 1)){
+        flagGetTime=1;
+        flagGetMaxCharge=1;
+    }
 }
 
 // ------------------------------------------------------------------------
@@ -321,57 +342,70 @@
  * Slow routines for Battery
  */
 void Battery::loop() {
-    while (1) {
-        *ledGreen = !checkedIn;
+
+    *ledGreen = !checkedIn;
 
-        if (flagGetTime) {
-            flagGetTime=0;
-            getTime();
+    if (flagGetTime) {
+        flagGetTime=0;
+        requestTime();
+
+    }
+    if (flagGetMaxCharge) {
+        flagGetMaxCharge=0;
+        requestMaxCharge();
 
-        }
-        if (flagNextFile) {
-            flagNextFile = 0;
-            getTime();
-            nextFileToTx();
-            if (fileLength>0){
-                INFO("Sending file %s.",txFileName)
-                        sendDirName();
-            } else {
-                INFO("No more files to send.")
-            }
+    }
+    if (flagNextFile) {
+        flagNextFile = 0;
+        requestTime();
+        nextFileToTx();
+        if (fileLength>0){
+            INFO("Sending file %s.",txFileName)
+                                                                sendDirName();
+        } else {
+            sendingFile=0;
+            INFO("No more files to send.")
         }
-        if (flagSendFileName) {
-            flagSendFileName = 0;
-            sendFileName();
+    }
+    if (flagSendFileName) {
+        flagSendFileName = 0;
+        sendFileName();
 
-        }
-        if (flagSendFileSize) {
-            flagSendFileSize = 0;
-            sendFileSize();
-        }
-        if (flagSendFile) {
-            flagSendFile = 0;
-            sendFile();
-        }
-        if (flagSendFileDone) {
-            flagSendFileDone = 0;
-            sendFileDone();
-            INFO("File sent <%s>",txFileName);
-            flag = &flagNextFile;
-            delay->detach();
-            delay->attach(this, &Battery::setFlag, 2.5);
-        }
-        __WFI();
+    }
+    if (flagSendFileSize) {
+        flagSendFileSize = 0;
+        sendFileSize();
+    }
+    if (flagSendFile) {
+        flagSendFile = 0;
+        sendFile();
     }
+    if (flagSendFileDone) {
+        flagSendFileDone = 0;
+        sendFileDone();
+        INFO("File sent <%s>",txFileName);
+        flag = &flagNextFile;
+        delay->detach();
+        delay->attach(this, &Battery::setFlag, 2.5);
+    }
+
 }
 
 /**
  * sends a request for time update.
  */
-void Battery::getTime() {
+void Battery::requestTime() {
     DBG("getTime");
     nrf->transmitData("T ",2);
 }
+
+/**
+ * sends a request for time update.
+ */
+void Battery::requestMaxCharge() {
+    DBG("getTime");
+    nrf->transmitData("M ",2);
+}
 /**
  * Selects the next file to transmit
  * Uses /sd/lastFileTx.txt to store the lst file transmitted
@@ -487,6 +521,7 @@
     if (fileLength < 1) {
         return;
     }
+    sendingFile=1;
     filePointer = 0;
     sprintf(dataTx, "S %X", fileLength);
 
@@ -579,3 +614,20 @@
     }
     flag = NULL;
 }
+
+
+/**
+ * Returns the max current that can be drawn from the system, in amps
+ */
+
+float Battery::getMaxCurrent(){
+    return maxChargeRate;
+}
+
+/**
+ * returns a string of the log directory
+ */
+char* Battery::getLogDirectory(){
+
+    return logDir;
+}