GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
sdcard.cpp@31:9cf3f1e5ad68, 2015-01-28 (annotated)
- Committer:
- Trumple
- Date:
- Wed Jan 28 21:19:52 2015 +0000
- Revision:
- 31:9cf3f1e5ad68
- Parent:
- 23:b57a47c7862a
Marge master branch
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") |
jakehodges | 19:70c911d35e67 | 6 | { |
Trumple | 0:fcab3b154e49 | 7 | //first write sometimes fails, let's ensure the first write isn't real data |
Trumple | 0:fcab3b154e49 | 8 | FILE *fp = fopen("/sd/boot", "w"); |
Trumple | 0:fcab3b154e49 | 9 | fprintf(fp, "boot"); |
Trumple | 0:fcab3b154e49 | 10 | fclose(fp); |
Trumple | 0:fcab3b154e49 | 11 | } |
Trumple | 0:fcab3b154e49 | 12 | |
Trumple | 0:fcab3b154e49 | 13 | int sdcard::write(long int time, d_reply data) |
Trumple | 0:fcab3b154e49 | 14 | { |
Trumple | 0:fcab3b154e49 | 15 | |
Trumple | 0:fcab3b154e49 | 16 | string sd_name = "/sd/"; |
Trumple | 0:fcab3b154e49 | 17 | string sd_location =""; |
Trumple | 0:fcab3b154e49 | 18 | char time_s[64]; |
jakehodges | 23:b57a47c7862a | 19 | |
jakehodges | 23:b57a47c7862a | 20 | char buffer[20]; |
jakehodges | 23:b57a47c7862a | 21 | sprintf(buffer, "%i", data.type); |
jakehodges | 23:b57a47c7862a | 22 | string dataTypeStr = string(buffer); |
jakehodges | 23:b57a47c7862a | 23 | |
jakehodges | 23:b57a47c7862a | 24 | sd_location = sd_name + dataTypeStr; |
Trumple | 0:fcab3b154e49 | 25 | mkdir(sd_location.c_str(), 0777); |
Trumple | 0:fcab3b154e49 | 26 | |
Trumple | 0:fcab3b154e49 | 27 | sprintf(time_s, "%d", time); |
Trumple | 0:fcab3b154e49 | 28 | sd_location += "/" + string(time_s) + ".txt"; |
Trumple | 0:fcab3b154e49 | 29 | |
Trumple | 0:fcab3b154e49 | 30 | FILE *fp = fopen(sd_location.c_str(), "w"); |
Trumple | 0:fcab3b154e49 | 31 | |
Trumple | 0:fcab3b154e49 | 32 | if (fp == NULL) |
Trumple | 0:fcab3b154e49 | 33 | { |
jakehodges | 23:b57a47c7862a | 34 | printf("[SD] File pointer null, failed to open file\r\n"); |
Trumple | 0:fcab3b154e49 | 35 | } |
Trumple | 0:fcab3b154e49 | 36 | |
Trumple | 0:fcab3b154e49 | 37 | string serializedData; |
Trumple | 0:fcab3b154e49 | 38 | |
Trumple | 0:fcab3b154e49 | 39 | for (int i = 0; i < data.readings.size(); i++) |
jakehodges | 23:b57a47c7862a | 40 | fprintf(fp, "%i\r\n", data.readings[i]); |
Trumple | 0:fcab3b154e49 | 41 | |
Trumple | 0:fcab3b154e49 | 42 | fclose(fp); |
Trumple | 0:fcab3b154e49 | 43 | |
Trumple | 0:fcab3b154e49 | 44 | return 1; |
Trumple | 0:fcab3b154e49 | 45 | } |