GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Diff: sdcard.cpp
- Revision:
- 0:fcab3b154e49
- Child:
- 19:70c911d35e67
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdcard.cpp Tue Nov 11 17:33:41 2014 +0000 @@ -0,0 +1,47 @@ +#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; +} \ No newline at end of file