Dependencies:   ChaNFSSD mbed BMP085 SHT2x

main.cpp

Committer:
tosihisa
Date:
2012-01-13
Revision:
1:83960ee4d9a2
Parent:
0:6089ae824f06
Child:
2:f546aaa0e1d5

File content as of revision 1:83960ee4d9a2:

/*
 * このソフトウェアは、大阪市立大学と京都大学生存圏ミッション研究の共同研究の成果物です。
 * ソースコードそのもののライセンスは、各ソースコードのライセンスに沿って公開します。
 * 2012-1-4 Toshihisa T
 */
#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"
#include "UBXPacket.h"

using namespace libT;

#define _CO2_TEST
//#define _I2C_TEST

#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);
#ifdef _I2C_TEST /* { */
I2C i2c(p9, p10);        // sda, scl
#endif  /* } */
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  _CO2_TEST
tSerialBuffer CO2(p13,p14);
#endif

struct UBXPacket_s UBXPacket;

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  _CO2_TEST    /* { */
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();
    }
    if(rbuf[0] != 0xFE){
        return -4;
    }
    if(rbuf[1] != 0x04){
        return -1;
    }
    if(rbuf[2] != 0x02){
        return -2;
    }
    crc = rbuf[6];
    crc = (crc << 8) | rbuf[5];
    if(crc != modbus_CRC(rbuf,5)){
        return -3;
    }
    *val = rbuf[3];
    *val = (*val << 8) | rbuf[4];
    return 0;
}
#endif  /* HAVE_CO2 } */

// BMP085 0xee
// AD9774 0x44
#ifdef _I2C_TEST /* { */
int i2c_found() {
    int count = 0;
    lcd.locate(0,2);
    lcd.printf("%-19s","I2C Dev:");
    lcd.locate(8,2);
    for (int address=0; address<256; address+=2) {
        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
            lcd.printf("%02X ",address);
            count++;
        }
    }
    lcd.printf("\n");
    lcd.printf("%d devices found", count);
    return count;
}
#endif /* } */

unsigned char NMEA_CalcSum(unsigned char *str,int len)
{
    unsigned char sum = 0;
    int i;
    for(i = 0;i < len;i++){
        sum = sum ^ (*(str + i));
    }
    return sum;
}

void UBX_CalcSum(unsigned char *str,int len,unsigned char *sum)
{
    int i;
    *(sum + 0) = *(sum + 1) = 0;
    for(i = 0;i < len;i++){
        *(sum + 0) = *(sum + 0) + *(str+i);
        *(sum + 1) = *(sum + 1) + *(sum + 0);
    }
}

int UBX_WaitAck(struct UBXPacket_s *info)
{
    UBXPacket.cjobst = 0;

    while(1){
        while(gps.readable()) {
            if(UBXPacket_Parse(&UBXPacket,gps.getc()) == 100){
                if((UBXPacket.cls == 0x05) && (UBXPacket.id == 0x01)){
                    return 1; /* ACK */
                } else if((UBXPacket.cls == 0x05) && (UBXPacket.id == 0x00)){
                    return 0; /* NAK */
                } else {
                    UBXPacket.cjobst = 0;
                }
            }
        }
    }
    return -1;
}

unsigned char wbuf[1024];
int widx;

int main() {
    int ret = 0;
    float p, t;
    int temp;
    int userRegister;
    unsigned short CO2_val;
    unsigned long scanCount = 0;
    FILE *fp;
    char *logname = "/" _USE_FS_NAME "/ENVLOG.TXT";
    char c;
 
    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\"");
#ifdef  _CO2_TEST
    wait(5.0);
#endif
 
    //logFile_Init();
    //FileWriteTest();

    UBXPacket.cjobst = 0;

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

    gps_reset = 1;
    wait(0.5);
    gps_reset = 0;

#if 0
    wait(0.5);
    if(1){
        //unsigned char modeStr[] = "PUBX,41,1,0007,0003,115200,0";
        unsigned char modeStr[] = "PUBX,41,1,0007,0001,115200,0";
        unsigned char sum = NMEA_CalcSum(modeStr,strlen((char *)modeStr));
        debug.printf("SEND:[%s](0x%02X)\n",modeStr,sum);
        gps.printf("$%s*%02X%c%c",modeStr,sum,0x0d,0x0a);

        debug.printf("CHG BAUD:115200\n");
        gps.format(8,Serial::None,1);
        gps.baud(115200);
    }

    gps.recvStart();
    wait(0.5);

    if(1){
        unsigned char chkStr[][11] = {
            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x10,0x02,0xFF,0xFF }, //RXM-RAW (0x02 0x10)
            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x30,0x02,0xFF,0xFF }, //RXM-ALM (0x02 0x30)
            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x31,0x02,0xFF,0xFF }, //RXM-EPH (0x02 0x31)
            //{ 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x41,0x02,0xFF,0xFF },
            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x11,0x02,0xFF,0xFF }, //RXM-SFRB (0x02 0x11)
            { 0xB5,0x62,0x06,0x01,0x03,0x00,0x02,0x20,0x02,0xFF,0xFF }, //RXM-SVSI (0x02 0x20)
            { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF }, //END
        };
        int i,j;

        for(j=0;chkStr[j][0] != 0x00;j++){
            UBX_CalcSum(&chkStr[j][2],7,&chkStr[j][9]);
            for(i = 0; i < sizeof(chkStr[0]);i++){
                gps.putc(chkStr[j][i]);
            }
            debug.printf("%d : SET UBX Rate : %s\n",j,(UBX_WaitAck(&UBXPacket) == 1) ? "ACK" : "NAK");
       }
    }
    UBXPacket.cjobst = 0;
#endif

#if 0
    debug.printf("open(%s) :", logname);
    if((fp = fopen(logname,"a+w")) == NULL){
        printf("NG\n");
    } else {
        printf("OK\n");
    }
    widx = 0;
#endif

//  ad7994.Start();

#ifdef  _CO2_TEST
    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",scanCount);

#if 0
        while(gps.readable()) {
            c = gps.getc();
            if(UBXPacket_Parse(&UBXPacket,c) == 100){
                debug.printf("%ld : GET UBX Packet (Class=0x%02X,ID=0x%02X,LEN=%d)\n",
                            scanCount,
                            UBXPacket.cls,
                            UBXPacket.id,
                            UBXPacket.len );
                UBXPacket.cjobst = 0;
                if(scanCount > 120){
                    if(widx >= 0){
                        fwrite(&wbuf,sizeof(wbuf[0]),widx,fp);
                        widx = 0;
                    }
                    fclose(fp);
                }
            }
            if(fp != NULL){
                wbuf[widx] = c;
                widx++;
                if(widx >= sizeof(wbuf)){
                    fwrite(&wbuf,sizeof(wbuf[0]),widx,fp);
                    widx = 0;
                }
            }
        }
#endif

#if 0
        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

#ifdef _I2C_TEST /* { */
        i2c_found();
#endif  /* } */

#ifdef  _CO2_TEST
        lcd.locate(0,2);
        ret = CO2_Read(&CO2_val);
        if(ret == 0){
            lcd.printf("CO2:%5dppm\n",CO2_val);
        } else {
            lcd.printf("CO2:NG(%d)  \n",ret);
        }
#endif

#if 0
        ad7994.update();
        lcd.locate(0,3);
        lcd.printf("A/D:%04X/%04X",
                     ad7994.readChn(0),
                     ad7994.readChn(1));
#endif

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

        scanCount++;
    }
}