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
Diff: file.cpp
- Revision:
- 0:bdb53686c194
- Child:
- 1:6c7141895545
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/file.cpp Mon Jul 04 15:16:45 2011 +0000 @@ -0,0 +1,117 @@ +#include "mbed.h" +#include "weather.h" +#include "SDHCFileSystem.h" +#include "MSCFileSystem.h" +#include "ConfigFile.h" + +static SDFileSystem sd(p5, p6, p7, p8, "sd"); +static LocalFileSystem *local; +static MSCFileSystem *usb; +static DigitalIn sd_cd(p21); +ConfigFile cfg; +static char filename[30] = ""; + +int write_log (const char *buf) { + FILE *fp; + + if (filename[0] == 0) return 0; + + LED_FILE_ON; + fp = fopen(filename, "a"); + if (fp) { + fprintf(fp, buf); + fclose(fp); + } else { + LED_FILE_OFF; + return -1; + } + LED_FILE_OFF; + return 0; +} + +int init_file () { + int seq = 0; + char buf[128], buf2[10] = ""; + FILE *fp; + + if (cfg.getValue("FILE", buf, sizeof(buf))) { + + if (strncmp(buf, "SD", 2) == 0) { + strcpy(buf2, "/sd/"); + } else + if (strncmp(buf, "USB", 3) == 0) { + usb = new MSCFileSystem("usb"); + if (usb == NULL) return -1; + strcpy(buf2, "/usb/"); + } else { + return -1; + } + + // seq num + strcpy(filename, buf2); + strcat(filename, "weather.seq"); + fp = fopen(filename, "r"); + if (fp) { + fscanf(fp, "%d", &seq); + fclose(fp); + } + seq ++; + + // save seq num + fp = fopen(filename, "w"); + if (fp) { + fprintf(fp, "%d", seq); + fclose(fp); + } + + // csv filename + sprintf(filename, "%sw%05d.csv", buf2, seq); + pc.printf("CSV Filename: %s\r\n", filename); + } + return 0; +} + +int init_conf () { + + // load config + LED_FILE_ON; + if (cfg.read("/sd/" CONFIG_FILE)) { + // from sd + + LED_FILE_OFF; + cfg.setValue("DIR", "/sd/"); + + } else { + local = new LocalFileSystem("local"); + if (local == NULL) return -1; + if (cfg.read("/local/" CONFIG_FILE)) { + // from usb + + LED_FILE_OFF; + cfg.setValue("DIR", "/local/"); + + } else { + // none + LED_FILE_OFF; + return -1; + } + } + +#ifdef DEBUG + pc.printf("Configration: %d\r\n", cfg.getCount()); +#endif + return 0; +} + +char* chop (char *s) { + int i; + + for (i = strlen(s) - 1; i >= 0; i --) { + if (s[i] == ' ' || s[i] == '\n' || s[i] == '\r') { + s[i] = 0; + } else { + break; + } + } + return s; +}