The field version of the solarnano grid on the ionQubes

Fork of SolarNanoGridv3 by SONG Project

Revision:
30:47f73b850a5e
Parent:
29:bbdbe1094a63
Child:
31:3bf76548e024
--- a/Battery/Battery.cpp	Thu Jun 23 11:54:53 2016 +0000
+++ b/Battery/Battery.cpp	Thu Jun 23 13:30:17 2016 +0000
@@ -250,7 +250,7 @@
                 //  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); // was 800
@@ -619,58 +619,15 @@
  */
 void Battery::sendFile()
 {
-
-    //  printf("send File [%s] at %d  %d\n\r",fullTxFilePath,fileLength, fileLeft );
+    char * packet;
+    unsigned int packetSize=32;
+    spiNRF();
 
-    //  if (fileLength < 1) {
-    //      INFO("File Sent");
-    //      sdBuffPnt=0;
-    //      return;
-    //  }
-    if (sdBuffPnt>=SDBUFFERSIZE) {
-        sdBuffPnt=0;
-    }
-    
-    if (sdBuffPnt == 0) {
-        spiSD();
-        FILE *fp = fopen(fullTxFilePath, "rb");
-        if (fp == NULL) {
-            spiNRF();
-            INFO("sendFile> File %s not found for reading",fullTxFilePath);
-            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);
-        //      printf("File %s %d \n\r",fullTxFilePath,sizeRead);
-        spiNRF();
-
-    }
-    spiNRF();
-    
-    if ((count++) % 256 ==0) {
-        DBG("W %d %d", fileLeft, sizeRead);
-    }
-    int size=32;
-    if (sizeRead<32) {
-        size=sizeRead;
-    }
-    if ((fileLeft==0)&&(sizeRead==0)) {
-        sdBuffPnt=0;
-        flagSendFileDone=1;
-        DBG("Done sending");
-    } else {
-        
-        sizeRead -= size;
-        spiNRF();
-        nrf->transmitData(&(sdBuffer[sdBuffPnt]), size);
-        sdBuffPnt+=size;
-    }
-    
+    while (packetSize>0) {
+        packet = getPacket(&packetSize,fullTxFilePath);
+        nrf->transmitData(packet, packetSize);
+    };
+    resetPackets();
 
 }
 
@@ -754,3 +711,79 @@
 
     return;
 }
+/**
+ * Resets the buffers to start reading the file from the beginning again.
+ *
+ */
+
+char* Battery::resetPackets()
+{
+    sdBuffPnt=0;
+    fileLeft = fileLength;
+}
+/**
+ * Gathers a packet from the file defined by fullTxFilePath
+ * @param readOnly if true then it does not move the internal pointers on.
+ */
+char* Battery::getPacket(unsigned int *sizePackett,char * fullTxFilePath)
+{
+    //DBG("Get packet");
+
+    //  printf("send File [%s] at %d  %d\n\r",fullTxFilePath,fileLength, fileLeft );
+
+    //  if (fileLength < 1) {
+    //      INFO("File Sent");
+    //      sdBuffPnt=0;
+    //      return;
+    //  }
+    if (sdBuffPnt>=SDBUFFERSIZE) {
+        sdBuffPnt=0;
+    }
+    if (sdBuffPnt == 0) {
+        DBG("File %s",fullTxFilePath);
+        spiSD();
+        FILE *fp;
+        fp= fopen(fullTxFilePath, "rb");
+        wait(0.1);
+        DBG("Open");
+        while (fp == NULL) {
+            WARN("sendFile> File %s not found for reading",fullTxFilePath);
+            fp= fopen(fullTxFilePath, "rb");
+        }
+        spiSD();
+        //DBG("Seek");
+        fseek(fp, fileLength - fileLeft, SEEK_SET);
+        //DBG("Read");
+        sizeRead = fread(sdBuffer, 1,SDBUFFERSIZE, fp);
+        //DBG("Read %d",sizeRead);
+        fileLeft -= sizeRead;
+
+        fclose(fp);
+        DBG("File %s %d \n\r",fullTxFilePath,sizeRead);
+        //spiNRF();
+
+    }
+
+    unsigned int bpNow =sdBuffPnt;
+
+    unsigned int size=*sizePackett;
+    if (sizeRead<size) {
+        size=sizeRead;
+    }
+    if ((fileLeft==0)&&(sizeRead==0)) {
+        sdBuffPnt=0;
+        //flagSendFileDone=1;
+        *sizePackett = 0;
+        INFO("Done sending file %s",fullTxFilePath);
+
+    } else {
+
+        sizeRead -= size;
+        //sendPacket(&(sdBuffer[sdBuffPnt]), size);
+        sdBuffPnt+=size;
+        //*sizePackett = size;
+        DBG("Left to send %d",(fileLeft+sizeRead));
+        return &(sdBuffer[bpNow]);
+    }
+    return NULL;
+}