SD card functionality

Dependents:   ELEC350_Project2 SDcard

Revision:
16:23b3be671415
Parent:
15:e1f62dd17e3c
Child:
17:c9ac3fc764b9
--- a/SDCard.cpp	Wed Jan 02 19:45:11 2019 +0000
+++ b/SDCard.cpp	Thu Jan 03 10:32:41 2019 +0000
@@ -48,7 +48,7 @@
             pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r");
             printlock.unlock();
             */
-            printQueue.call(printf,"WARNING: COULD NOT OPEN FILE FOR WRITE\n\r"); 
+            printQueue.call(printf,"WARNING: COULD NOT OPEN FILE FOR WRITE\r\n\n"); 
             //lcd.cls();
             //lcd.printf("CANNOT OPEN FILE\n\n");
             //errorCode(FATAL);
@@ -60,7 +60,7 @@
             pc->printf("FILE OPEN FOR WRITING\n\n\n\r");
             printlock.unlock();
             */
-            printQueue.call(printf,"FILE OPEN FOR WRITING\n\n\n\r");
+            printQueue.call(printf,"FILE OPEN FOR WRITING\r\n\n");
             sd_init = true;
         }     
         fclose(fp);
@@ -131,7 +131,7 @@
         strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
         timeLock.unlock();
         
-        fp = fopen(fileDate,"a"); //issue if file already open?
+        fp = fopen(fileDate,"a");
             
         if (fp == NULL)
         {
@@ -144,6 +144,7 @@
             printQueue.call(printf,"WARNING: FILE COULD NOT BE OPENED\r\n\n");
             sd_init = false;
             samplesInBuffer.release();
+            oldestIndex = (oldestIndex?oldestIndex:BUFFERSIZE)-1; //Doesn't work properly - oldestindex will be increased by the conditional statement in the producer
         }
         else
         {
@@ -151,7 +152,7 @@
             fclose(fp);
         }
                 
-        if(logging)
+        if(logging && sd_init)
         {
             /*
             printlock.lock();
@@ -160,11 +161,15 @@
             pc->printf("oldestIndex position %d\r\n\n",oldestIndex);
             printlock.unlock();  
             */
-            //printQueue.call(printf,"Log file %s updated with sample from position %d in buffer\r\nnewestIndex position %d\r\noldestIndex position %d\r\n\n", fileDate, buffind, newestIndex, oldestIndex);
-            printQueue.call(printf,"Log file updated with sample from position %d in buffer\r\nnewestIndex position %d\r\noldestIndex position %d\r\n\n", buffind, newestIndex, oldestIndex);
+            printQueue.call(printf,"Log file %s updated with sample from position %d in buffer\r\nnewestIndex position %d\r\noldestIndex position %d\r\n\n", fileDate, buffind, newestIndex, oldestIndex);
+            //printQueue.call(printf,"Log file updated with sample from position %d in buffer\r\nnewestIndex position %d\r\noldestIndex position %d\r\n\n", buffind, newestIndex, oldestIndex);
         }
     }
-    else samplesInBuffer.release();
+    else 
+    {
+        samplesInBuffer.release();
+        oldestIndex = (oldestIndex?oldestIndex:BUFFERSIZE)-1; //Doesn't work properly - oldestindex will be increased by the conditional statement in the producer
+    }
 }
 
 /*