![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
sdcard.cpp@0:fcab3b154e49, 2014-11-11 (annotated)
- Committer:
- Trumple
- Date:
- Tue Nov 11 17:33:41 2014 +0000
- Revision:
- 0:fcab3b154e49
- Child:
- 19:70c911d35e67
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Trumple | 0:fcab3b154e49 | 1 | #include <string> |
Trumple | 0:fcab3b154e49 | 2 | #include "sdcard.h" |
Trumple | 0:fcab3b154e49 | 3 | #include <map> |
Trumple | 0:fcab3b154e49 | 4 | |
Trumple | 0:fcab3b154e49 | 5 | sdcard::sdcard(): sd(p5, p6, p7, p8, "sd") |
Trumple | 0:fcab3b154e49 | 6 | { |
Trumple | 0:fcab3b154e49 | 7 | typeLookup[0x00] = "soil_con"; |
Trumple | 0:fcab3b154e49 | 8 | typeLookup[0x01] = "soil_temp"; |
Trumple | 0:fcab3b154e49 | 9 | typeLookup[0x02] = "amb_temp"; |
Trumple | 0:fcab3b154e49 | 10 | |
Trumple | 0:fcab3b154e49 | 11 | //first write sometimes fails, let's ensure the first write isn't real data |
Trumple | 0:fcab3b154e49 | 12 | FILE *fp = fopen("/sd/boot", "w"); |
Trumple | 0:fcab3b154e49 | 13 | fprintf(fp, "boot"); |
Trumple | 0:fcab3b154e49 | 14 | fclose(fp); |
Trumple | 0:fcab3b154e49 | 15 | } |
Trumple | 0:fcab3b154e49 | 16 | |
Trumple | 0:fcab3b154e49 | 17 | int sdcard::write(long int time, d_reply data) |
Trumple | 0:fcab3b154e49 | 18 | { |
Trumple | 0:fcab3b154e49 | 19 | |
Trumple | 0:fcab3b154e49 | 20 | string sd_name = "/sd/"; |
Trumple | 0:fcab3b154e49 | 21 | string sd_location =""; |
Trumple | 0:fcab3b154e49 | 22 | char time_s[64]; |
Trumple | 0:fcab3b154e49 | 23 | |
Trumple | 0:fcab3b154e49 | 24 | string l_type = typeLookup[data.type]; |
Trumple | 0:fcab3b154e49 | 25 | |
Trumple | 0:fcab3b154e49 | 26 | sd_location = sd_name + l_type; |
Trumple | 0:fcab3b154e49 | 27 | mkdir(sd_location.c_str(), 0777); |
Trumple | 0:fcab3b154e49 | 28 | |
Trumple | 0:fcab3b154e49 | 29 | sprintf(time_s, "%d", time); |
Trumple | 0:fcab3b154e49 | 30 | sd_location += "/" + string(time_s) + ".txt"; |
Trumple | 0:fcab3b154e49 | 31 | |
Trumple | 0:fcab3b154e49 | 32 | FILE *fp = fopen(sd_location.c_str(), "w"); |
Trumple | 0:fcab3b154e49 | 33 | |
Trumple | 0:fcab3b154e49 | 34 | if (fp == NULL) |
Trumple | 0:fcab3b154e49 | 35 | { |
Trumple | 0:fcab3b154e49 | 36 | printf("File pointer null, failed to open file\r\n"); |
Trumple | 0:fcab3b154e49 | 37 | } |
Trumple | 0:fcab3b154e49 | 38 | |
Trumple | 0:fcab3b154e49 | 39 | string serializedData; |
Trumple | 0:fcab3b154e49 | 40 | |
Trumple | 0:fcab3b154e49 | 41 | for (int i = 0; i < data.readings.size(); i++) |
Trumple | 0:fcab3b154e49 | 42 | fprintf(fp, "%i", data.readings[i]); |
Trumple | 0:fcab3b154e49 | 43 | |
Trumple | 0:fcab3b154e49 | 44 | fclose(fp); |
Trumple | 0:fcab3b154e49 | 45 | |
Trumple | 0:fcab3b154e49 | 46 | return 1; |
Trumple | 0:fcab3b154e49 | 47 | } |