Mathew Swabey / SDCard

Dependents:   ELEC350_Project2 SDcard

Files at this revision

API Documentation at this revision

Comitter:
Swabey89
Date:
Wed Jan 02 19:45:11 2019 +0000
Parent:
14:8e92eb9fc088
Child:
16:23b3be671415
Commit message:
Updated to use queues more

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 28 12:05:03 2018 +0000
+++ b/SDCard.cpp	Wed Jan 02 19:45:11 2019 +0000
@@ -9,9 +9,12 @@
         
     //Initialise the SD card
     if (sd.init() != 0) {
+        /*
         printlock.lock();
         pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r");
         printlock.unlock();
+        */
+        printQueue.call(printf,"WARNING:SD CARD INITIALISATION FAILED\n\r");
         sd_init = false;
         //lcd.cls();
         //lcd.printf("CANNOT INIT SD");        
@@ -21,9 +24,12 @@
     {
         //Create a filing system for SD Card
         fs = new FATFileSystem("sd", &sd);
+        /*
         printlock.lock();
         pc->printf("\nSD CARD INITIALISED\n\r");
         printlock.unlock();
+        */
+        printQueue.call(printf,"\nSD CARD INITIALISED\n\r");
 
         //Open to WRITE    
         char fileDate[30];
@@ -37,18 +43,24 @@
         
         if (fp == NULL) 
         {
+            /*
             printlock.lock();
             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"); 
             //lcd.cls();
             //lcd.printf("CANNOT OPEN FILE\n\n");
             //errorCode(FATAL);
         }
         else
         {
+            /*
             printlock.lock();
             pc->printf("FILE OPEN FOR WRITING\n\n\n\r");
             printlock.unlock();
+            */
+            printQueue.call(printf,"FILE OPEN FOR WRITING\n\n\n\r");
             sd_init = true;
         }     
         fclose(fp);
@@ -57,70 +69,166 @@
 }
 
 void SDread(int n)
-{
+{   
+    //printlock.lock();
+    bufferLock.lock();
     SD_tout.attach(SD_toutISR,TOUT_TIME_SDREAD);
     //Read n samples from the SD card   
     unsigned int i=0;
     unsigned int j = newestIndex;
-    if (n==-1) {n = (BUFFERSIZE-Nspaces);}
-    
-    printlock.lock();
-    bufferLock.lock();           
+    if (n==-1) {n = (BUFFERSIZE-Nspaces);} 
+               
     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()); 
+    {   
+        printQueue.call(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());        
+        //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(); 
-    SD_tout.detach();
+    printQueue.call(printf,"%d records read\r\n\n", i);
+    //printf("%d records read\r\n\n", i);
+    //printlock.unlock(); 
+    SD_tout.detach();    
 }
 
 void SDdelete(int n)  //MUST RELEASE SPACE AVAILABLE
 {
+    bufferLock.lock();
     SD_tout.attach(SD_toutISR,TOUT_TIME_DEF);
     //Delete n samples from the SD card
     unsigned int i = 0;
     if (n==-1) {n = (BUFFERSIZE-Nspaces);}
-    bufferLock.lock();
     while (i < n)
     {
         spaceAvailable.release();
         i++;
     }
     bufferLock.unlock();
-    Nspaces += i;    
+    Nspaces += i;   
+    /* 
     printlock.lock();
     pc->printf("Deleted %d records\r\n\n\n", i);
     printlock.unlock();
+    */
+    printQueue.call(printf,"Deleted %d records\r\n\n\n", i);
     SD_tout.detach();
 }
 
 
+void SDaddSample(string timedata, double temp, double pressure, float light, int buffind)
+{
+    if(sd_init)
+    {
+        static time_t seconds;
+        char fileDate[30];
+        timeLock.lock();
+        seconds = time(NULL);
+        timeData = localtime(&seconds);
+        
+        //set_time(mktime(timeData));
+        
+        strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
+        timeLock.unlock();
+        
+        fp = fopen(fileDate,"a"); //issue if file already open?
+            
+        if (fp == NULL)
+        {
+            /*
+            printlock.lock();
+            pc->printf("WARNING: FILE COULD NOT BE OPENED\r\n\n");
+            sd_init = false;
+            printlock.unlock();  
+            */
+            printQueue.call(printf,"WARNING: FILE COULD NOT BE OPENED\r\n\n");
+            sd_init = false;
+            samplesInBuffer.release();
+        }
+        else
+        {
+            fprintf(fp,"%s,%5.2f,%5.2f,%5.2f\r", timedata, temp, pressure, light);
+            fclose(fp);
+        }
+                
+        if(logging)
+        {
+            /*
+            printlock.lock();
+            pc->printf("Log file %s updated with sample from position %d in buffer\r\n",fileDate,oldestIndex);
+            pc->printf("newestIndex position %d\r\n",newestIndex);
+            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);
+        }
+    }
+    else samplesInBuffer.release();
+}
+
+/*
+void SDaddSample(void)
+{
+    
+    static time_t seconds;
+    char fileDate[30];
+    timeLock.lock();
+    seconds = time(NULL);
+    timeData = localtime(&seconds);
+    
+    //set_time(mktime(timeData));
+    
+    strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
+    timeLock.unlock();
+    
+    fp = fopen(fileDate,"a"); //issue if file already open?
+    
+    bufferLock.lock();
+    
+    if (fp == NULL)
+    {
+        printlock.lock();
+        pc->printf("WARNING: FILE COULD NOT BE OPENED\r\n\n");
+        sd_init = false;
+        printlock.unlock();  
+        samplesInBuffer.release();
+    }
+    else
+    {
+        //oldestIndex = (oldestIndex+1) % BUFFERSIZE;
+        fprintf(fp,"%s,%5.2f,%5.2f,%5.2f\r", buffer[oldestIndex].getTime(), buffer[oldestIndex].gettemp(), buffer[oldestIndex].getpress(), buffer[oldestIndex].getlight());        
+    }       
+    
+    if(logging)
+    {
+        printlock.lock();
+        pc->printf("Log file %s updated with sample from position %d in buffer\r\n",fileDate,oldestIndex);
+        pc->printf("newestIndex position %d\r\n",newestIndex);
+        pc->printf("oldestIndex position %d\r\n\n",oldestIndex);
+        printlock.unlock();  
+    }   
+    
+    fclose(fp);   
+    bufferLock.unlock(); 
+}
+
+*/
 
 //UNUSED
-void SDaddSample(double temp, double pressure)
-{
-    //Add the sampled data to the SD card    
-    yellowLED = !yellowLED; //debugging
-    fp = fopen("/sd/q.csv","a");
-    fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
-    fclose(fp);
-}
-
+/*
 void SDalive(void)
 {
     //Signal that the SD thread is still alive
     //puts("SD THREAD ALIVE\n");   
 }
+*/
 
 void SDmount(void)
 {
-    while(true)
-    {
-        Thread::signal_wait(SIGNAL_SD);
+    //while(true)
+    //{
+        //Thread::signal_wait(SIGNAL_SD);
         
         SD_tout.attach(SD_toutISR,TOUT_TIME_SDMOUNT);
            
@@ -129,34 +237,65 @@
             fclose(fp);
             sd.deinit();
             sd_init = false;
+            /*
             printlock.lock();
             pc->printf("SD CARD UNMOUNTED\n\r");
             printlock.unlock();
+            */
+            printQueue.call(printf,"SD CARD UNMOUNTED\n\r");
             
-            LCDlock.lock();
-            lcd.cls();
-            lcd.printf("SD UNMOUNTED..");
-            Thread::wait(5000); //Dont like this
-            LCDlock.unlock();          
+            //LCDlock.lock();
+            //lcd.cls();
+            //lcd.printf("SD UNMOUNTED..");
+            //LCDqueue.call(LCD_text,"SD UNMOUNTED..");
+            //Thread::wait(5000); //Dont like this
+            
+            //for(int i=0;i<20;i++)
+            //{
+               // greenLED = 1;
+                //wait(0.5);
+                //greenLED = 0;
+                //wait(0.5);   
+            //}           
+            //LCDlock.unlock();          
+            
+            LCDqueue.call(LCD_sdcardmount,"SD UNMOUNTED..", 0.05);
+              
               
         }
         else
         {
             SDcard();
-            LCDlock.lock();
-            lcd.cls();
+            //LCDlock.lock();
+            //lcd.cls();
             if (sd_init)
             {
-                lcd.printf("SD MOUNTED..");
+                //lcd.printf("SD MOUNTED..");
+                //LCDqueue.call(LCD_text,"SD MOUNTED..");
+                LCDqueue.call(LCD_sdcardmount,"SD MOUNTED..", 0.05);
             }
             else
             {
-                lcd.printf("SD FAILED..");
+                //lcd.printf("SD FAILED..");
+                LCDqueue.call(LCD_sdcardmount,"SD FAILED..", 0.05); //pass in LED to flash RED
             }
-            Thread::wait(5000); //Dont like this
-            LCDlock.unlock();        
+            
+            /*
+            for(int i=0;i<20;i++)
+            {
+                greenLED = 1;
+                wait(0.5);
+                greenLED = 0;
+                wait(0.5);   
+            }
+            */
+            
+            
+            
+            //Thread::wait(5000); //Dont like this
+            //LCDlock.unlock();        
         } 
         SD_tout.detach(); 
-    }   
+    //}   
 }
 
--- a/SDCard.hpp	Fri Dec 28 12:05:03 2018 +0000
+++ b/SDCard.hpp	Wed Jan 02 19:45:11 2019 +0000
@@ -27,10 +27,17 @@
 extern int32_t Nsamples;
 extern int32_t Nspaces;
 extern Semaphore spaceAvailable;
+extern void LCD_sdcardmount(string text, float waittime);
+
+extern Semaphore samplesInBuffer;
+
+//TEST FOR PRINTF
+extern EventQueue printfqueue;
 
 void SDcard(void);
 void SDread(int n);
-void SDaddSample(double temp, double pressure);
+void SDaddSample(string timedata, double temp, double pressure, float light, int buffind);
+//void SDaddSample(void);
 void SDdelete(int n);
 void SDalive(void);