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

Committer:
okini3939
Date:
2011-07-10
Revision:
1:6c7141895545
Parent:
0:bdb53686c194
Child:
2:a3e5edf84f74

File content as of revision 1:6c7141895545:

/*
 * Weather Station - mbed Weather Platform
 * Copyright (c) 2011 Hiroshi Suga
 * Released under the MIT License: http://mbed.org/license/mit
 */

/** @file
 * @brief Weather Station
 */

#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_ins(p27);
ConfigFile cfg;
static char filename[30] = "";

int write_log (const char *buf) {
    FILE *fp;

    if (filename[0] == 0) return 0;
#ifdef USE_SD_INS
    if (sd_ins != 0) return -1;
#endif

    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 () {

#ifdef USE_SD_INS
    sd_ins.mode(PullUp);
#endif

    // load config
    LED_FILE_ON;
#ifdef USE_SD_INS
    if (sd_ins == 0 && cfg.read("/sd/" CONFIG_FILE)) {
#else
    if (cfg.read("/sd/" CONFIG_FILE)) {
#endif
        // 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;
}