SD card functionality

Dependents:   ELEC350_Project2 SDcard

SDCard.cpp

Committer:
Swabey89
Date:
2018-12-17
Revision:
7:393fa8184388
Parent:
6:5646450f583b
Child:
8:ee8f65745141

File content as of revision 7:393fa8184388:

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

void SDcard(void)
{
    static time_t seconds;
        
    //REQUIRES IMPROVEMENTS, SEE ONENOTE
        
    //Initialise the SD card
    if ( sd.init() != 0) {
        printf("Init failed \n");
        lcd.cls();
        lcd.printf("CANNOT INIT SD");        
        errorCode(FATAL);
    } 
    
    //Create a filing system for SD Card
    //FATFileSystem fs("sd", &sd);     
    fs = new FATFileSystem("sd", &sd);
    
    //Open to WRITE

    char fileDate[30];
    seconds = time(NULL);
    timeData = localtime(&seconds);
    set_time(mktime(timeData));
    strftime(fileDate, 30, "sd/sampledata/%d_%m_%y.csv", timeData);
    
    fp = fopen(fileDate,"a");
    if (fp == NULL) {
        error("Could not open file for write\n");
        lcd.cls();
        lcd.printf("CANNOT OPEN FILE\n\n");
        errorCode(FATAL);
    }    
    
    fclose(fp);
    
    //Last message before sampling begins
    lcd.cls();
    lcd.printf("SYSTEM READY\n\n");
    
    
    
    //move?   
    //Press either switch to unmount
    /*
    while ((SW1 == 0) && (SW2 == 0)) {
        
             
        //Write to SD (potentially slow)
        //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
        
    }
    
    //Close File
    fclose(fp);
    
    //Close down
    sd.deinit();
    printf("Unmounted...\n");
    lcd.cls();
    lcd.printf("Unmounted...\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");   
}