mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/
Dependencies: ChaNFSSD EthernetNetIf I2CLEDDisp Agentbed ChaNFSUSB ILinterpreter mbed BMP085 WeatherMeters ConfigFile ChaNFS I2CLCD
file.cpp@0:bdb53686c194, 2011-07-04 (annotated)
- Committer:
- okini3939
- Date:
- Mon Jul 04 15:16:45 2011 +0000
- Revision:
- 0:bdb53686c194
- Child:
- 1:6c7141895545
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:bdb53686c194 | 1 | #include "mbed.h" |
okini3939 | 0:bdb53686c194 | 2 | #include "weather.h" |
okini3939 | 0:bdb53686c194 | 3 | #include "SDHCFileSystem.h" |
okini3939 | 0:bdb53686c194 | 4 | #include "MSCFileSystem.h" |
okini3939 | 0:bdb53686c194 | 5 | #include "ConfigFile.h" |
okini3939 | 0:bdb53686c194 | 6 | |
okini3939 | 0:bdb53686c194 | 7 | static SDFileSystem sd(p5, p6, p7, p8, "sd"); |
okini3939 | 0:bdb53686c194 | 8 | static LocalFileSystem *local; |
okini3939 | 0:bdb53686c194 | 9 | static MSCFileSystem *usb; |
okini3939 | 0:bdb53686c194 | 10 | static DigitalIn sd_cd(p21); |
okini3939 | 0:bdb53686c194 | 11 | ConfigFile cfg; |
okini3939 | 0:bdb53686c194 | 12 | static char filename[30] = ""; |
okini3939 | 0:bdb53686c194 | 13 | |
okini3939 | 0:bdb53686c194 | 14 | int write_log (const char *buf) { |
okini3939 | 0:bdb53686c194 | 15 | FILE *fp; |
okini3939 | 0:bdb53686c194 | 16 | |
okini3939 | 0:bdb53686c194 | 17 | if (filename[0] == 0) return 0; |
okini3939 | 0:bdb53686c194 | 18 | |
okini3939 | 0:bdb53686c194 | 19 | LED_FILE_ON; |
okini3939 | 0:bdb53686c194 | 20 | fp = fopen(filename, "a"); |
okini3939 | 0:bdb53686c194 | 21 | if (fp) { |
okini3939 | 0:bdb53686c194 | 22 | fprintf(fp, buf); |
okini3939 | 0:bdb53686c194 | 23 | fclose(fp); |
okini3939 | 0:bdb53686c194 | 24 | } else { |
okini3939 | 0:bdb53686c194 | 25 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 26 | return -1; |
okini3939 | 0:bdb53686c194 | 27 | } |
okini3939 | 0:bdb53686c194 | 28 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 29 | return 0; |
okini3939 | 0:bdb53686c194 | 30 | } |
okini3939 | 0:bdb53686c194 | 31 | |
okini3939 | 0:bdb53686c194 | 32 | int init_file () { |
okini3939 | 0:bdb53686c194 | 33 | int seq = 0; |
okini3939 | 0:bdb53686c194 | 34 | char buf[128], buf2[10] = ""; |
okini3939 | 0:bdb53686c194 | 35 | FILE *fp; |
okini3939 | 0:bdb53686c194 | 36 | |
okini3939 | 0:bdb53686c194 | 37 | if (cfg.getValue("FILE", buf, sizeof(buf))) { |
okini3939 | 0:bdb53686c194 | 38 | |
okini3939 | 0:bdb53686c194 | 39 | if (strncmp(buf, "SD", 2) == 0) { |
okini3939 | 0:bdb53686c194 | 40 | strcpy(buf2, "/sd/"); |
okini3939 | 0:bdb53686c194 | 41 | } else |
okini3939 | 0:bdb53686c194 | 42 | if (strncmp(buf, "USB", 3) == 0) { |
okini3939 | 0:bdb53686c194 | 43 | usb = new MSCFileSystem("usb"); |
okini3939 | 0:bdb53686c194 | 44 | if (usb == NULL) return -1; |
okini3939 | 0:bdb53686c194 | 45 | strcpy(buf2, "/usb/"); |
okini3939 | 0:bdb53686c194 | 46 | } else { |
okini3939 | 0:bdb53686c194 | 47 | return -1; |
okini3939 | 0:bdb53686c194 | 48 | } |
okini3939 | 0:bdb53686c194 | 49 | |
okini3939 | 0:bdb53686c194 | 50 | // seq num |
okini3939 | 0:bdb53686c194 | 51 | strcpy(filename, buf2); |
okini3939 | 0:bdb53686c194 | 52 | strcat(filename, "weather.seq"); |
okini3939 | 0:bdb53686c194 | 53 | fp = fopen(filename, "r"); |
okini3939 | 0:bdb53686c194 | 54 | if (fp) { |
okini3939 | 0:bdb53686c194 | 55 | fscanf(fp, "%d", &seq); |
okini3939 | 0:bdb53686c194 | 56 | fclose(fp); |
okini3939 | 0:bdb53686c194 | 57 | } |
okini3939 | 0:bdb53686c194 | 58 | seq ++; |
okini3939 | 0:bdb53686c194 | 59 | |
okini3939 | 0:bdb53686c194 | 60 | // save seq num |
okini3939 | 0:bdb53686c194 | 61 | fp = fopen(filename, "w"); |
okini3939 | 0:bdb53686c194 | 62 | if (fp) { |
okini3939 | 0:bdb53686c194 | 63 | fprintf(fp, "%d", seq); |
okini3939 | 0:bdb53686c194 | 64 | fclose(fp); |
okini3939 | 0:bdb53686c194 | 65 | } |
okini3939 | 0:bdb53686c194 | 66 | |
okini3939 | 0:bdb53686c194 | 67 | // csv filename |
okini3939 | 0:bdb53686c194 | 68 | sprintf(filename, "%sw%05d.csv", buf2, seq); |
okini3939 | 0:bdb53686c194 | 69 | pc.printf("CSV Filename: %s\r\n", filename); |
okini3939 | 0:bdb53686c194 | 70 | } |
okini3939 | 0:bdb53686c194 | 71 | return 0; |
okini3939 | 0:bdb53686c194 | 72 | } |
okini3939 | 0:bdb53686c194 | 73 | |
okini3939 | 0:bdb53686c194 | 74 | int init_conf () { |
okini3939 | 0:bdb53686c194 | 75 | |
okini3939 | 0:bdb53686c194 | 76 | // load config |
okini3939 | 0:bdb53686c194 | 77 | LED_FILE_ON; |
okini3939 | 0:bdb53686c194 | 78 | if (cfg.read("/sd/" CONFIG_FILE)) { |
okini3939 | 0:bdb53686c194 | 79 | // from sd |
okini3939 | 0:bdb53686c194 | 80 | |
okini3939 | 0:bdb53686c194 | 81 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 82 | cfg.setValue("DIR", "/sd/"); |
okini3939 | 0:bdb53686c194 | 83 | |
okini3939 | 0:bdb53686c194 | 84 | } else { |
okini3939 | 0:bdb53686c194 | 85 | local = new LocalFileSystem("local"); |
okini3939 | 0:bdb53686c194 | 86 | if (local == NULL) return -1; |
okini3939 | 0:bdb53686c194 | 87 | if (cfg.read("/local/" CONFIG_FILE)) { |
okini3939 | 0:bdb53686c194 | 88 | // from usb |
okini3939 | 0:bdb53686c194 | 89 | |
okini3939 | 0:bdb53686c194 | 90 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 91 | cfg.setValue("DIR", "/local/"); |
okini3939 | 0:bdb53686c194 | 92 | |
okini3939 | 0:bdb53686c194 | 93 | } else { |
okini3939 | 0:bdb53686c194 | 94 | // none |
okini3939 | 0:bdb53686c194 | 95 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 96 | return -1; |
okini3939 | 0:bdb53686c194 | 97 | } |
okini3939 | 0:bdb53686c194 | 98 | } |
okini3939 | 0:bdb53686c194 | 99 | |
okini3939 | 0:bdb53686c194 | 100 | #ifdef DEBUG |
okini3939 | 0:bdb53686c194 | 101 | pc.printf("Configration: %d\r\n", cfg.getCount()); |
okini3939 | 0:bdb53686c194 | 102 | #endif |
okini3939 | 0:bdb53686c194 | 103 | return 0; |
okini3939 | 0:bdb53686c194 | 104 | } |
okini3939 | 0:bdb53686c194 | 105 | |
okini3939 | 0:bdb53686c194 | 106 | char* chop (char *s) { |
okini3939 | 0:bdb53686c194 | 107 | int i; |
okini3939 | 0:bdb53686c194 | 108 | |
okini3939 | 0:bdb53686c194 | 109 | for (i = strlen(s) - 1; i >= 0; i --) { |
okini3939 | 0:bdb53686c194 | 110 | if (s[i] == ' ' || s[i] == '\n' || s[i] == '\r') { |
okini3939 | 0:bdb53686c194 | 111 | s[i] = 0; |
okini3939 | 0:bdb53686c194 | 112 | } else { |
okini3939 | 0:bdb53686c194 | 113 | break; |
okini3939 | 0:bdb53686c194 | 114 | } |
okini3939 | 0:bdb53686c194 | 115 | } |
okini3939 | 0:bdb53686c194 | 116 | return s; |
okini3939 | 0:bdb53686c194 | 117 | } |