SD card functionality

Dependents:   ELEC350_Project2 SDcard

SDCard.cpp

Committer:
Swabey89
Date:
2018-12-19
Revision:
8:ee8f65745141
Parent:
7:393fa8184388
Child:
9:113f4934b907

File content as of revision 8:ee8f65745141:

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

void SDcard(void)
{
    static time_t seconds;
        
    //REQUIRES IMPROVEMENTS, SEE ONENOTE
        
    //Initialise the SD card
    if (sd.init() != 0) {
        pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r");
        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);
        pc->printf("SD CARD INITIALISED\n\r");

        //Open to WRITE    
        char fileDate[30];
        seconds = time(NULL);
        timeData = localtime(&seconds);
        set_time(mktime(timeData));
        strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
        
        fp = fopen(fileDate,"a");
        
        if (fp == NULL) 
        {
            pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r");
            //lcd.cls();
            //lcd.printf("CANNOT OPEN FILE\n\n");
            //errorCode(FATAL);
        }
        else
        {
            pc->printf("FILE OPEN FOR WRITING\n\r"); 
            sd_init = true;
        }     
        fclose(fp);
    }
    
    //Last message before sampling begins - probably remove
    lcd.cls();
    lcd.printf("SYSTEM READY\n\n");
}

void SDread(int n)
{
    //Read n samples from the SD card
    if (n == -1) puts("Received command to read all");
    else printf("Received command to read %d\n", n);
}

void SDdelete(int n)
{
    //Delete n samples from the SD card
    if (n == -1) puts("Received command to delete all");
    else printf("Received command to delete %d\n", n);   
}

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");   
}