SD card functionality

Dependents:   ELEC350_Project2 SDcard

SDCard.cpp

Committer:
Swabey89
Date:
2018-12-28
Revision:
14:8e92eb9fc088
Parent:
13:5f786448e883
Child:
15:e1f62dd17e3c

File content as of revision 14:8e92eb9fc088:

#include "SDCard.hpp"
#include <string>

void SDcard(void)
{
    SD_tout.attach(SD_toutISR,TOUT_TIME_DEF);
    
    static time_t seconds; //static reqiured?
        
    //Initialise the SD card
    if (sd.init() != 0) {
        printlock.lock();
        pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r");
        printlock.unlock();
        sd_init = false;
        //lcd.cls();
        //lcd.printf("CANNOT INIT SD");        
        //errorCode(FATAL);
    } 
    else
    {
        //Create a filing system for SD Card
        fs = new FATFileSystem("sd", &sd);
        printlock.lock();
        pc->printf("\nSD CARD INITIALISED\n\r");
        printlock.unlock();

        //Open to WRITE    
        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");
        
        if (fp == NULL) 
        {
            printlock.lock();
            pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r");
            printlock.unlock();
            //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();
            sd_init = true;
        }     
        fclose(fp);
    }
    SD_tout.detach();
}

void SDread(int n)
{
    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();           
    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(); 
    SD_tout.detach();
}

void SDdelete(int n)  //MUST RELEASE SPACE AVAILABLE
{
    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;    
    printlock.lock();
    pc->printf("Deleted %d records\r\n\n\n", i);
    printlock.unlock();
    SD_tout.detach();
}



//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);
        
        SD_tout.attach(SD_toutISR,TOUT_TIME_SDMOUNT);
           
        if (sd_init)
        {
            fclose(fp);
            sd.deinit();
            sd_init = false;
            printlock.lock();
            pc->printf("SD CARD UNMOUNTED\n\r");
            printlock.unlock();
            
            LCDlock.lock();
            lcd.cls();
            lcd.printf("SD UNMOUNTED..");
            Thread::wait(5000); //Dont like this
            LCDlock.unlock();          
              
        }
        else
        {
            SDcard();
            LCDlock.lock();
            lcd.cls();
            if (sd_init)
            {
                lcd.printf("SD MOUNTED..");
            }
            else
            {
                lcd.printf("SD FAILED..");
            }
            Thread::wait(5000); //Dont like this
            LCDlock.unlock();        
        } 
        SD_tout.detach(); 
    }   
}