Mathew Swabey / SDCard

Dependents:   ELEC350_Project2 SDcard

Files at this revision

API Documentation at this revision

Comitter:
Swabey89
Date:
Sat Dec 22 21:14:56 2018 +0000
Parent:
11:89960c1f2234
Child:
13:5f786448e883
Commit message:
Changes to the way data is saved

Changed in this revision

SDCard.cpp Show annotated file Show diff for this revision Revisions of this file
SDCard.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/SDCard.cpp	Fri Dec 21 09:58:51 2018 +0000
+++ b/SDCard.cpp	Sat Dec 22 21:14:56 2018 +0000
@@ -55,82 +55,40 @@
 
 void SDread(int n)
 {
-    //Read n samples from the SD card    
+    //Read n samples from the SD card   
+    unsigned int i=0;
+    unsigned int j = newestIndex;
+    if (n==-1) {n = (BUFFERSIZE-Nspaces);}
+    
     printlock.lock();
-    if (n == -1)
-    {
-        unsigned int i=newestIndex;
-        unsigned int j = 0;
-        bufferLock.lock();
-        while (i != oldestIndex)
-        {            
-            i = (i ? i : BUFFERSIZE) - 1; //if i is not 0 subtract 1. if it is 0, set to buffersize - 1
-            pc->printf("Date/Time: %s\tTemperature: %5.2f\tPressure: %5.2f\tLight: %5.2f\n\r", buffer[i].getTime(), buffer[i].gettemp(), buffer[i].getpress(), buffer[i].getlight()); 
-            j++;
-        }
-        bufferLock.unlock();
-        
-        printf("%d records read\r\n\n\n", j);
-    }
-    else 
-    {
-        //printf("Received command to read %d\n\r", n);
-        /*
-        Read from newestIndex back by n, until either newestIndex - n or oldestindex
-        */
-        unsigned int i;
-        unsigned int j = newestIndex;
-        bufferLock.lock();
-        for (i = 0; i<n; i++)
-        {
-            if (j == oldestIndex) {break;}
-            j = (j ? j : BUFFERSIZE) - 1;
-            pc->printf("Date/Time: %s\tTemperature: %5.2f\tPressure: %5.2f\tLight: %5.2f\n\r", buffer[j].getTime(), buffer[j].gettemp(), buffer[j].getpress(), buffer[j].getlight());           
-        }
-        bufferLock.unlock();
-        
-        printf("%d records read\r\n\n\n", i);
-    }
-    printlock.unlock();
-    
+    bufferLock.lock();           
+    while (i < n) //ONLY USE SPACE AVAILABLE - KEEP IN MIND LAST READ WAS BEFORE IT WAS DECREMENTED
+    {            
+        pc->printf("Date/Time: %s\tTemperature: %5.2f\tPressure: %5.2f\tLight: %5.2f\n\r", buffer[j].getTime(), buffer[j].gettemp(), buffer[j].getpress(), buffer[j].getlight()); 
+        j = (j?j:BUFFERSIZE)-1; 
+        i++;
+    }        
+    bufferLock.unlock();
+    printf("%d records read\r\n\n", i);
+    printlock.unlock(); 
 }
 
-void SDdelete(int n)
+void SDdelete(int n)  //MUST RELEASE SPACE AVAILABLE
 {
     //Delete n samples from the SD card
-    printlock.lock();
-    if (n == -1) 
+    unsigned int i = 0;
+    if (n==-1) {n = (BUFFERSIZE-Nspaces);}
+    bufferLock.lock();
+    while (i < n)
     {
-        unsigned int i = newestIndex;
-        unsigned int j = 0;
-        
-        while (i != oldestIndex)
-        {
-            i = (i ? i : BUFFERSIZE) - 1;
-            j++;
-        }
-        
-        oldestIndex = 0;
-        newestIndex = 0;
-        pc->printf("Deleted %d records\r\n\n\n", j);        
+        spaceAvailable.release();
+        i++;
     }
-    else 
-    {
-        //printf("Received command to delete %d\n", n); 
-        //move oldestIndex forwards for the given number or until you hit newestIndex
-        unsigned int i;
-        unsigned int j = oldestIndex;
-        bufferLock.lock();
-        for (i = 0; i<n; i++)
-        {
-            if (j == newestIndex) {break;}
-            j = (j + 1) % BUFFERSIZE;
-        } 
-        oldestIndex = j;
-        bufferLock.unlock();
-        printf("Deleted %d records\r\n\n\n", i);
-    }
-    printlock.unlock();  
+    bufferLock.unlock();
+    Nspaces += i;    
+    printlock.lock();
+    pc->printf("Deleted %d records\r\n\n\n", i);
+    printlock.unlock();
 }
 
 
--- a/SDCard.hpp	Fri Dec 21 09:58:51 2018 +0000
+++ b/SDCard.hpp	Sat Dec 22 21:14:56 2018 +0000
@@ -22,6 +22,10 @@
 extern Mutex timeLock;
 extern Mutex bufferLock;
 
+extern int32_t Nsamples;
+extern int32_t Nspaces;
+extern Semaphore spaceAvailable;
+
 void SDcard(void);
 void SDread(int n);
 void SDaddSample(double temp, double pressure);