Dependencies:   ChaNFSSD mbed BMP085 SHT2x

Revision:
1:83960ee4d9a2
Parent:
0:6089ae824f06
Child:
2:f546aaa0e1d5
--- a/main.cpp	Mon Dec 26 15:58:32 2011 +0000
+++ b/main.cpp	Fri Jan 13 10:15:08 2012 +0000
@@ -1,3 +1,8 @@
+/*
+ * このソフトウェアは、大阪市立大学と京都大学生存圏ミッション研究の共同研究の成果物です。
+ * ソースコードそのもののライセンスは、各ソースコードのライセンスに沿って公開します。
+ * 2012-1-4 Toshihisa T
+ */
 #include "mbed.h"
 #include "NMEA_parse.h"
 #include "SDFileSystem.h"
@@ -8,10 +13,12 @@
 #include "AD7994.h"
 //#include "TextLCD.h"
 #include "TextLCD_20X4.h"
+#include "UBXPacket.h"
 
 using namespace libT;
 
-//#define HAVE_CO2
+#define _CO2_TEST
+//#define _I2C_TEST
 
 #define _USE_FS_NAME "sd"
 
@@ -22,21 +29,25 @@
 InterruptIn gps_pps(p26);
 tSerialBuffer gps(p28,p27);
 DigitalOut  gps_pps_led(LED2);
-//I2C i2c(p9, p10);        // sda, scl
+#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);
+SDFileSystem sd(p5, p6, p7, p8, _USE_FS_NAME);
 
 DigitalOut myled(LED1);
 
-#ifdef  HAVE_CO2
+#ifdef  _CO2_TEST
 tSerialBuffer CO2(p13,p14);
 #endif
 
+struct UBXPacket_s UBXPacket;
+
 int pps_count = 0;
 void gps_pps_rise()
 {
@@ -73,7 +84,7 @@
     fclose(fp);
 }
 
-#ifdef  HAVE_CO2    /* { */
+#ifdef  _CO2_TEST    /* { */
 int CO2_Read(unsigned short *val)
 {
     unsigned char sbuf[] = { 0xFE, 0x04, 0x00, 0x03, 0x00, 0x01, 0xD5, 0xC5 };
@@ -90,12 +101,20 @@
         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 -1;
+        return -3;
     }
-
     *val = rbuf[3];
     *val = (*val << 8) | rbuf[4];
     return 0;
@@ -104,21 +123,66 @@
 
 // BMP085 0xee
 // AD9774 0x44
-#if 0
+#ifdef _I2C_TEST /* { */
 int i2c_found() {
-    printf("Searching for I2C devices...\n");
-
     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
-            printf(" - I2C device found at address 0x%02X\n", address);
+            lcd.printf("%02X ",address);
             count++;
         }
     }
-    printf("%d devices found\n", count);
+    lcd.printf("\n");
+    lcd.printf("%d devices found", count);
     return count;
 }
-#endif
+#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;
@@ -127,6 +191,9 @@
     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);
@@ -137,23 +204,76 @@
     //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.recvStart();
 
-    //�P���Z�b�g�C�O���Z�b�g�J��
     gps_reset = 1;
     wait(0.5);
     gps_reset = 0;
 
-    ad7994.Start();
+#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);
 
-#ifdef  HAVE_CO2
+    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();
@@ -167,16 +287,38 @@
     while(1) {
         //lcd.cls();
         lcd.locate(0,1);
-        lcd.printf("SCAN : %-10ld\n",scanCount);
+        lcd.printf("SCAN: %-10ld",scanCount);
 
 #if 0
         while(gps.readable()) {
-            debug.printf("%c",gps.getc());
-            //ret = libNMEA_Parse1Char(gps.getc(),&sts);
+            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 1
+#if 0
         bmp085.update();
         p = bmp085.get_pressure();
         t = bmp085.get_temperature();
@@ -192,29 +334,38 @@
         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 _I2C_TEST /* { */
+        i2c_found();
+#endif  /* } */
 
-#ifdef  HAVE_CO2
-        if(CO2_Read(&CO2_val) == 0){
-            debug.printf("CO2 OK : %d (0x%04x) ppm\n",CO2_val,CO2_val);
+#ifdef  _CO2_TEST
+        lcd.locate(0,2);
+        ret = CO2_Read(&CO2_val);
+        if(ret == 0){
+            lcd.printf("CO2:%5dppm\n",CO2_val);
         } else {
-            debug.printf("CO2 NG\n");
+            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.2);
+        wait(0.5);
         myled = 0;
-        wait(0.2);
+        wait(0.5);
 
         scanCount++;
     }