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@1:6c7141895545, 2011-07-10 (annotated)
- Committer:
- okini3939
- Date:
- Sun Jul 10 15:36:46 2011 +0000
- Revision:
- 1:6c7141895545
- Parent:
- 0:bdb53686c194
- Child:
- 2:a3e5edf84f74
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 1:6c7141895545 | 1 | /* |
okini3939 | 1:6c7141895545 | 2 | * Weather Station - mbed Weather Platform |
okini3939 | 1:6c7141895545 | 3 | * Copyright (c) 2011 Hiroshi Suga |
okini3939 | 1:6c7141895545 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 1:6c7141895545 | 5 | */ |
okini3939 | 1:6c7141895545 | 6 | |
okini3939 | 1:6c7141895545 | 7 | /** @file |
okini3939 | 1:6c7141895545 | 8 | * @brief Weather Station |
okini3939 | 1:6c7141895545 | 9 | */ |
okini3939 | 1:6c7141895545 | 10 | |
okini3939 | 0:bdb53686c194 | 11 | #include "mbed.h" |
okini3939 | 0:bdb53686c194 | 12 | #include "weather.h" |
okini3939 | 0:bdb53686c194 | 13 | #include "SDHCFileSystem.h" |
okini3939 | 0:bdb53686c194 | 14 | #include "MSCFileSystem.h" |
okini3939 | 0:bdb53686c194 | 15 | #include "ConfigFile.h" |
okini3939 | 0:bdb53686c194 | 16 | |
okini3939 | 0:bdb53686c194 | 17 | static SDFileSystem sd(p5, p6, p7, p8, "sd"); |
okini3939 | 0:bdb53686c194 | 18 | static LocalFileSystem *local; |
okini3939 | 0:bdb53686c194 | 19 | static MSCFileSystem *usb; |
okini3939 | 1:6c7141895545 | 20 | static DigitalIn sd_ins(p27); |
okini3939 | 0:bdb53686c194 | 21 | ConfigFile cfg; |
okini3939 | 0:bdb53686c194 | 22 | static char filename[30] = ""; |
okini3939 | 0:bdb53686c194 | 23 | |
okini3939 | 0:bdb53686c194 | 24 | int write_log (const char *buf) { |
okini3939 | 0:bdb53686c194 | 25 | FILE *fp; |
okini3939 | 0:bdb53686c194 | 26 | |
okini3939 | 0:bdb53686c194 | 27 | if (filename[0] == 0) return 0; |
okini3939 | 1:6c7141895545 | 28 | #ifdef USE_SD_INS |
okini3939 | 1:6c7141895545 | 29 | if (sd_ins != 0) return -1; |
okini3939 | 1:6c7141895545 | 30 | #endif |
okini3939 | 0:bdb53686c194 | 31 | |
okini3939 | 0:bdb53686c194 | 32 | LED_FILE_ON; |
okini3939 | 0:bdb53686c194 | 33 | fp = fopen(filename, "a"); |
okini3939 | 0:bdb53686c194 | 34 | if (fp) { |
okini3939 | 0:bdb53686c194 | 35 | fprintf(fp, buf); |
okini3939 | 0:bdb53686c194 | 36 | fclose(fp); |
okini3939 | 0:bdb53686c194 | 37 | } else { |
okini3939 | 0:bdb53686c194 | 38 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 39 | return -1; |
okini3939 | 0:bdb53686c194 | 40 | } |
okini3939 | 0:bdb53686c194 | 41 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 42 | return 0; |
okini3939 | 0:bdb53686c194 | 43 | } |
okini3939 | 0:bdb53686c194 | 44 | |
okini3939 | 0:bdb53686c194 | 45 | int init_file () { |
okini3939 | 0:bdb53686c194 | 46 | int seq = 0; |
okini3939 | 0:bdb53686c194 | 47 | char buf[128], buf2[10] = ""; |
okini3939 | 0:bdb53686c194 | 48 | FILE *fp; |
okini3939 | 0:bdb53686c194 | 49 | |
okini3939 | 1:6c7141895545 | 50 | |
okini3939 | 0:bdb53686c194 | 51 | if (cfg.getValue("FILE", buf, sizeof(buf))) { |
okini3939 | 0:bdb53686c194 | 52 | |
okini3939 | 0:bdb53686c194 | 53 | if (strncmp(buf, "SD", 2) == 0) { |
okini3939 | 0:bdb53686c194 | 54 | strcpy(buf2, "/sd/"); |
okini3939 | 0:bdb53686c194 | 55 | } else |
okini3939 | 0:bdb53686c194 | 56 | if (strncmp(buf, "USB", 3) == 0) { |
okini3939 | 0:bdb53686c194 | 57 | usb = new MSCFileSystem("usb"); |
okini3939 | 0:bdb53686c194 | 58 | if (usb == NULL) return -1; |
okini3939 | 0:bdb53686c194 | 59 | strcpy(buf2, "/usb/"); |
okini3939 | 0:bdb53686c194 | 60 | } else { |
okini3939 | 0:bdb53686c194 | 61 | return -1; |
okini3939 | 0:bdb53686c194 | 62 | } |
okini3939 | 0:bdb53686c194 | 63 | |
okini3939 | 0:bdb53686c194 | 64 | // seq num |
okini3939 | 0:bdb53686c194 | 65 | strcpy(filename, buf2); |
okini3939 | 0:bdb53686c194 | 66 | strcat(filename, "weather.seq"); |
okini3939 | 0:bdb53686c194 | 67 | fp = fopen(filename, "r"); |
okini3939 | 0:bdb53686c194 | 68 | if (fp) { |
okini3939 | 0:bdb53686c194 | 69 | fscanf(fp, "%d", &seq); |
okini3939 | 0:bdb53686c194 | 70 | fclose(fp); |
okini3939 | 0:bdb53686c194 | 71 | } |
okini3939 | 0:bdb53686c194 | 72 | seq ++; |
okini3939 | 0:bdb53686c194 | 73 | |
okini3939 | 0:bdb53686c194 | 74 | // save seq num |
okini3939 | 0:bdb53686c194 | 75 | fp = fopen(filename, "w"); |
okini3939 | 0:bdb53686c194 | 76 | if (fp) { |
okini3939 | 0:bdb53686c194 | 77 | fprintf(fp, "%d", seq); |
okini3939 | 0:bdb53686c194 | 78 | fclose(fp); |
okini3939 | 0:bdb53686c194 | 79 | } |
okini3939 | 0:bdb53686c194 | 80 | |
okini3939 | 0:bdb53686c194 | 81 | // csv filename |
okini3939 | 0:bdb53686c194 | 82 | sprintf(filename, "%sw%05d.csv", buf2, seq); |
okini3939 | 0:bdb53686c194 | 83 | pc.printf("CSV Filename: %s\r\n", filename); |
okini3939 | 0:bdb53686c194 | 84 | } |
okini3939 | 0:bdb53686c194 | 85 | return 0; |
okini3939 | 0:bdb53686c194 | 86 | } |
okini3939 | 0:bdb53686c194 | 87 | |
okini3939 | 0:bdb53686c194 | 88 | int init_conf () { |
okini3939 | 0:bdb53686c194 | 89 | |
okini3939 | 1:6c7141895545 | 90 | #ifdef USE_SD_INS |
okini3939 | 1:6c7141895545 | 91 | sd_ins.mode(PullUp); |
okini3939 | 1:6c7141895545 | 92 | #endif |
okini3939 | 1:6c7141895545 | 93 | |
okini3939 | 0:bdb53686c194 | 94 | // load config |
okini3939 | 0:bdb53686c194 | 95 | LED_FILE_ON; |
okini3939 | 1:6c7141895545 | 96 | #ifdef USE_SD_INS |
okini3939 | 1:6c7141895545 | 97 | if (sd_ins == 0 && cfg.read("/sd/" CONFIG_FILE)) { |
okini3939 | 1:6c7141895545 | 98 | #else |
okini3939 | 0:bdb53686c194 | 99 | if (cfg.read("/sd/" CONFIG_FILE)) { |
okini3939 | 1:6c7141895545 | 100 | #endif |
okini3939 | 0:bdb53686c194 | 101 | // from sd |
okini3939 | 0:bdb53686c194 | 102 | |
okini3939 | 0:bdb53686c194 | 103 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 104 | cfg.setValue("DIR", "/sd/"); |
okini3939 | 0:bdb53686c194 | 105 | |
okini3939 | 0:bdb53686c194 | 106 | } else { |
okini3939 | 0:bdb53686c194 | 107 | local = new LocalFileSystem("local"); |
okini3939 | 0:bdb53686c194 | 108 | if (local == NULL) return -1; |
okini3939 | 0:bdb53686c194 | 109 | if (cfg.read("/local/" CONFIG_FILE)) { |
okini3939 | 0:bdb53686c194 | 110 | // from usb |
okini3939 | 0:bdb53686c194 | 111 | |
okini3939 | 0:bdb53686c194 | 112 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 113 | cfg.setValue("DIR", "/local/"); |
okini3939 | 0:bdb53686c194 | 114 | |
okini3939 | 0:bdb53686c194 | 115 | } else { |
okini3939 | 0:bdb53686c194 | 116 | // none |
okini3939 | 0:bdb53686c194 | 117 | LED_FILE_OFF; |
okini3939 | 0:bdb53686c194 | 118 | return -1; |
okini3939 | 0:bdb53686c194 | 119 | } |
okini3939 | 0:bdb53686c194 | 120 | } |
okini3939 | 0:bdb53686c194 | 121 | |
okini3939 | 0:bdb53686c194 | 122 | #ifdef DEBUG |
okini3939 | 0:bdb53686c194 | 123 | pc.printf("Configration: %d\r\n", cfg.getCount()); |
okini3939 | 0:bdb53686c194 | 124 | #endif |
okini3939 | 0:bdb53686c194 | 125 | return 0; |
okini3939 | 0:bdb53686c194 | 126 | } |
okini3939 | 0:bdb53686c194 | 127 | |
okini3939 | 0:bdb53686c194 | 128 | char* chop (char *s) { |
okini3939 | 0:bdb53686c194 | 129 | int i; |
okini3939 | 0:bdb53686c194 | 130 | |
okini3939 | 0:bdb53686c194 | 131 | for (i = strlen(s) - 1; i >= 0; i --) { |
okini3939 | 0:bdb53686c194 | 132 | if (s[i] == ' ' || s[i] == '\n' || s[i] == '\r') { |
okini3939 | 0:bdb53686c194 | 133 | s[i] = 0; |
okini3939 | 0:bdb53686c194 | 134 | } else { |
okini3939 | 0:bdb53686c194 | 135 | break; |
okini3939 | 0:bdb53686c194 | 136 | } |
okini3939 | 0:bdb53686c194 | 137 | } |
okini3939 | 0:bdb53686c194 | 138 | return s; |
okini3939 | 0:bdb53686c194 | 139 | } |