Dependencies:   ChaNFSSD mbed BMP085 SHT2x

main.cpp

Committer:
tosihisa
Date:
2011-12-26
Revision:
0:6089ae824f06
Child:
1:83960ee4d9a2

File content as of revision 0:6089ae824f06:

#include "mbed.h"
#include "NMEA_parse.h"
#include "SDFileSystem.h"
#include "libT/mbed/tserialbuffer.h"
#include "BMP085.h"
#include "SHT25.h"
#include "SHT2x.h"
#include "AD7994.h"
//#include "TextLCD.h"
#include "TextLCD_20X4.h"

using namespace libT;

//#define HAVE_CO2

#define _USE_FS_NAME "sd"

Serial debug(USBTX,USBRX);

DigitalIn   gps_int0(p24);
DigitalOut  gps_reset(p25);
InterruptIn gps_pps(p26);
tSerialBuffer gps(p28,p27);
DigitalOut  gps_pps_led(LED2);
//I2C i2c(p9, p10);        // sda, scl
BMP085 bmp085(p9, p10);
//SHT25 sht25(p9, p10);
//SHT2x sht25(p9,p10);
AD7994 ad7994(p9,p10);
TextLCD_20X4 lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7

//SDFileSystem sd(p5, p6, p7, p8, _USE_FS_NAME);

DigitalOut myled(LED1);

#ifdef  HAVE_CO2
tSerialBuffer CO2(p13,p14);
#endif

int pps_count = 0;
void gps_pps_rise()
{
    gps_pps_led = ((pps_count+=1) & 1) ? 1 : 0;
}

void logFile_Init()
{
    DIR *d;
    struct dirent *p;

    d = opendir("/" _USE_FS_NAME);
    if ( d != NULL ) {
        while ( (p = readdir(d)) != NULL ) {
            debug.printf("FILE - %s\x0d\x0a", p->d_name);
        }
        closedir(d);
    }
}

void FileWriteTest()
{
    FILE *fp;
    char *logname = "/" _USE_FS_NAME "/MBEDLOG.TXT";

    debug.printf("FileWriteTest() open(%s) :", logname);
    fp = fopen(logname,"a+w");
    if(fp == NULL){
        printf("NG\n");
        return;
    }
    printf("OK\n");
    fprintf(fp,"ENV Logger \"V1\" Start (BUILD:[" __DATE__ "/" __TIME__ "])\x0d\x0a");
    fclose(fp);
}

#ifdef  HAVE_CO2    /* { */
int CO2_Read(unsigned short *val)
{
    unsigned char sbuf[] = { 0xFE, 0x04, 0x00, 0x03, 0x00, 0x01, 0xD5, 0xC5 };
    unsigned char rbuf[7];
    int i;
    unsigned short crc;
    extern unsigned short modbus_CRC(unsigned char *DataPtr, unsigned short len);

    for(i=0;i < sizeof(sbuf);i++){
        //debug.printf("0x%02x ",sbuf[i]);
        CO2.putc(sbuf[i]);
    }
    for(i=0;i < sizeof(rbuf);i++){
        while(!CO2.readable()){}
        rbuf[i] = CO2.getc();
    }
    crc = rbuf[6];
    crc = (crc << 8) | rbuf[5];
    if(crc != modbus_CRC(rbuf,5)){
        return -1;
    }

    *val = rbuf[3];
    *val = (*val << 8) | rbuf[4];
    return 0;
}
#endif  /* HAVE_CO2 } */

// BMP085 0xee
// AD9774 0x44
#if 0
int i2c_found() {
    printf("Searching for I2C devices...\n");

    int count = 0;
    for (int address=0; address<256; address+=2) {
        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
            printf(" - I2C device found at address 0x%02X\n", address);
            count++;
        }
    }
    printf("%d devices found\n", count);
    return count;
}
#endif

int main() {
    int ret = 0;
    float p, t;
    int temp;
    int userRegister;
    unsigned short CO2_val;
    unsigned long scanCount = 0;
 
    debug.format(8,Serial::None,1);
    debug.baud(115200);
    debug.printf("ENV Logger \"V1\" Start (BUILD:[" __DATE__ "/" __TIME__ "])\n");

    debug.printf("LCD Display\n");

    //lcd.cls();
    lcd.locate(0,0);
    lcd.printf("ENV Logger \"V1\"");
 
    //logFile_Init();
    //FileWriteTest();

    gps_pps.rise(gps_pps_rise);
    gps.format(8,Serial::None,1);
    gps.baud(9600);
    gps.recvStart();

    //�P���Z�b�g�C�O���Z�b�g�J��
    gps_reset = 1;
    wait(0.5);
    gps_reset = 0;

    ad7994.Start();

#ifdef  HAVE_CO2
    CO2.format(8,Serial::None,1);
    CO2.baud(9600);
    CO2.recvStart();
#endif

    //sht25.SHT2x_SoftReset();
    //sht25.SHT2x_ReadUserRegister(&userRegister);  //get actual user reg
    //userRegister = (userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_12_14BIT;
    //sht25.SHT2x_WriteUserRegister(&userRegister); //write changed user reg    

    while(1) {
        //lcd.cls();
        lcd.locate(0,1);
        lcd.printf("SCAN : %-10ld\n",scanCount);

#if 0
        while(gps.readable()) {
            debug.printf("%c",gps.getc());
            //ret = libNMEA_Parse1Char(gps.getc(),&sts);
        }
#endif

#if 1
        bmp085.update();
        p = bmp085.get_pressure();
        t = bmp085.get_temperature();
        lcd.locate(0,2);
        lcd.printf("%-7.2fhPa %-7.2fC\n", p, t);
#endif

#if 0
        sht25.SHT2x_MeasurePoll(TEMP,&temp);
        p = sht25.SHT2x_CalcTemperatureC(temp);
        sht25.SHT2x_MeasurePoll(HUMIDITY,&temp);
        t = sht25.SHT2x_CalcRH(temp);
        debug.printf("SHT25 = p:%6.2f C / t:%6.2f RH\n", p, t);
        sht25.SHT2x_SoftReset();
#endif
#if 0
        sht25.SoftReset();
        printf("SHT25 = %f\n",sht25.get_temperature());
#endif
        //i2c_found();

#ifdef  HAVE_CO2
        if(CO2_Read(&CO2_val) == 0){
            debug.printf("CO2 OK : %d (0x%04x) ppm\n",CO2_val,CO2_val);
        } else {
            debug.printf("CO2 NG\n");
        }
#endif
        ad7994.update();
        lcd.locate(0,3);
        lcd.printf("A/D:%04X/%04X",
                     ad7994.readChn(0),
                     ad7994.readChn(1));

        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);

        scanCount++;
    }
}