GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

sdcard.cpp

Committer:
Trumple
Date:
2014-11-11
Revision:
0:fcab3b154e49
Child:
19:70c911d35e67

File content as of revision 0:fcab3b154e49:

#include <string>
#include "sdcard.h"
#include <map>

sdcard::sdcard(): sd(p5, p6, p7, p8, "sd")
{
    typeLookup[0x00] = "soil_con";
    typeLookup[0x01] = "soil_temp";
    typeLookup[0x02] = "amb_temp";
    
    //first write sometimes fails, let's ensure the first write isn't real data
    FILE *fp = fopen("/sd/boot", "w");
    fprintf(fp, "boot");
    fclose(fp);
}
 
int sdcard::write(long int time, d_reply data)
{
 
    string sd_name = "/sd/";
    string sd_location ="";
    char time_s[64];
 
    string l_type = typeLookup[data.type];
 
    sd_location = sd_name + l_type;
    mkdir(sd_location.c_str(), 0777);
    
    sprintf(time_s, "%d", time);
    sd_location += "/" + string(time_s) + ".txt";
    
    FILE *fp = fopen(sd_location.c_str(), "w");
    
    if (fp == NULL)
    {
        printf("File pointer null, failed to open file\r\n");
    }
    
    string serializedData;
    
    for (int i = 0; i < data.readings.size(); i++)
        fprintf(fp, "%i", data.readings[i]);
    
    fclose(fp);
 
    return 1;
}