Skytraq S1315F-RAW-EVK Logger

Dependencies:   TextLCD mbed

Revision:
1:d1bb695fe3bc
Parent:
0:e0ec137da369
Child:
2:7eb11afe02bd
diff -r e0ec137da369 -r d1bb695fe3bc main.cpp
--- a/main.cpp	Sat Dec 18 11:17:21 2010 +0000
+++ b/main.cpp	Sat Dec 18 17:45:25 2010 +0000
@@ -11,6 +11,8 @@
 
 #define OUTPUT_RATE_1HZ
 
+#define FSNAME "sd"
+
 using namespace libT;
 
 Serial debug(USBTX,USBRX);
@@ -22,16 +24,18 @@
 Ticker timer;
 
 TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7
-SDFileSystem sd(p5, p6, p7, p8, "sd");
+SDFileSystem sd(p5, p6, p7, p8, FSNAME);
 
 int logSW_state[2] = { 0 , 0 };
 int logSW_idx = 0;
 unsigned long jif = 0;
+int do_disp = 0;
 void timer_handler()
 {
-    logSW_state[logSW_idx] = (LogSW != 0) ? 1 : 0;
+    logSW_state[logSW_idx] = (LogSW == 0) ? 1 : 0;
     logSW_idx = (logSW_idx + 1) & 1;
     jif++;
+    do_disp++;
 }
 
 int pps_count = 0;
@@ -39,7 +43,7 @@
 {
     pps_count++;
     if(pps_count & 1)
-        PPSLED=0.20;
+        PPSLED=0.80;
     else
         PPSLED=0.0;
 }
@@ -95,6 +99,13 @@
     return 0;
 }
 
+int Skytraq_0x83(Skytraq_bin_info_t *info)
+{
+    debug.printf("\n");
+    debug.printf("ACK ID:0x%02x\n",info->body[0]);
+    return 0;
+}
+
 Skytraq_ID_t Skytraq_ID_List[] = {
     { 0x01 ,NULL,"Input System Restart Force system to restart"},
     { 0x02 ,NULL,"Input Query Software version Query revision information of software"},
@@ -126,7 +137,7 @@
     { 0x80 ,Skytraq_0x80,"Output Software version Software revision of the receiver" },
     { 0x81 ,NULL,"Output Software CRC Software CRC of the receiver" },
     { 0x82 ,NULL,"Output Reserved Reserved" },
-    { 0x83 ,NULL,"Output ACK ACK to a successful input message" },
+    { 0x83 ,Skytraq_0x83,"Output ACK ACK to a successful input message" },
     { 0x84 ,NULL,"Output NACK Response to an unsuccessful input message" },
     { 0x86 ,NULL,"Output Position update rate Position update rate of GPS system" },
     { 0x87 ,NULL,"Output GPS Almanac Data Outputting the GPS Almanac Data of GPS receiver"},
@@ -144,8 +155,7 @@
     { 0xFF ,NULL,"****UNKNOWN****"}
 };
 
-char Skytraq_QUERY_SOFTWARE_VERSION[] = {0xA0,0xA1,0x00,0x02,0x02,0x00,0x02,0x0D,0x0A};
-
+unsigned char Skytraq_QUERY_SOFTWARE_VERSION[] = {0x02,0x01};
 unsigned char Skytraq_set_rate[] = {
     0x12,   /* ID */
     0x00,   /* 00: 1Hz / 01: 2Hz / 02: 4Hz / 03: 5Hz / 04: 10Hz / 05: 20Hz / Others: 20Hz */
@@ -178,17 +188,9 @@
 void Skytraq_bin_parse(unsigned char c,Skytraq_bin_info_t *info)
 {
     if(info->cjobst == 0){
-        if(c == 0xa0){
-            info->cjobst = 1;
-        } else {
-            info->cjobst = 0;
-        }
+        info->cjobst = (c == 0xA0) ? 1 : 0;
     } else if(info->cjobst == 1){
-        if(c == 0xa1){
-            info->cjobst = 2;
-        } else {
-            info->cjobst = 0;
-        }
+        info->cjobst = (c == 0xA1) ? 2 : 0;
     } else if(info->cjobst == 2){
         info->len = c;
         info->cjobst = 3;
@@ -219,6 +221,58 @@
             info->cjobst = 0;
             debug.printf("\nSUMERROR[%02x][%02x]\n",info->sum,c);
         }
+    } else if(info->cjobst == 6){
+        info->cjobst = (c == 0x0D) ? 7 : 0;
+    } else if(info->cjobst == 7){
+        info->cjobst = (c == 0x0A) ? 8 : 0;
+    }
+}
+
+char *logDir = "/" FSNAME "/GPSLOG";
+unsigned short logNextCount = 0;
+int UseLogMemory = 0;
+
+char *logFile_getName(int addDir) {
+    static char fname[32];
+    if (addDir) {
+        snprintf(fname,sizeof(fname)-1,"%s/RAW%05d.STQ",logDir,logNextCount);
+    } else {
+        snprintf(fname,sizeof(fname)-1,"RAW%05d.STQ",logNextCount);
+    }
+    return fname;
+}
+
+void logFile_Init() {
+    DIR *d;
+    struct dirent *p;
+    unsigned short countCandidate;
+    char *endptr;
+    int sts;
+
+    UseLogMemory = 0;
+
+    sts = mkdir(logDir,0777);
+    debug.printf("mkdir(\"%s\") - %d\n",logDir,sts);
+    d = opendir(logDir);
+    if ( d != NULL ) {
+        while ( (p = readdir(d)) != NULL ) {
+            debug.printf("FILE - %s\x0d\x0a", p->d_name);
+            if (strlen(p->d_name) == (sizeof("RAWxxxxx.stq")-1)) {
+                if (    ((p->d_name[0] == 'R') || (p->d_name[0] == 'r'))
+                        && ((p->d_name[1] == 'A') || (p->d_name[1] == 'a'))
+                        && ((p->d_name[2] == 'W') || (p->d_name[2] == 'w'))) {
+                }
+                countCandidate = (unsigned short)strtoul(&(p->d_name[3]),&endptr,10);
+                if (strcasecmp(endptr,".STQ") == 0) {
+                    if (countCandidate > logNextCount) {
+                        logNextCount = countCandidate;
+                    }
+                }
+            }
+        }
+        closedir(d);
+        logNextCount++;
+        UseLogMemory = 1;
     }
 }
 
@@ -228,12 +282,9 @@
 int main() {
     Skytraq_bin_info_t bin_info;
     int i;
-    unsigned long recv_count = 0;
     unsigned long recv_bytes = 0;
     FILE *fp = NULL;
-    int save_done = 0;
     unsigned char c;
-    int do_log = 0;
 
     bin_info.cjobst = 0;
 
@@ -251,6 +302,10 @@
     lcd.printf("Please wait");
     wait(1.0);
 
+    logFile_Init();
+
+    lcd.cls();
+
     gps.format(8,Serial::None,1);
     gps.baud(115200);
     gps.recvStart();
@@ -261,34 +316,41 @@
 #endif
     Skytraq_bin_send(sizeof(Skytraq_set_rate),Skytraq_set_rate);
 
-    timer.attach_us(timer_handler,1000*100);
+    //Skytraq_bin_send(sizeof(Skytraq_QUERY_SOFTWARE_VERSION),Skytraq_QUERY_SOFTWARE_VERSION);
+
+    timer.attach_us(timer_handler,1000*50);
 
     while(1) {
-//    for(i = 0;i < sizeof(Skytraq_QUERY_SOFTWARE_VERSION);i++){
-//        gps.putc(Skytraq_QUERY_SOFTWARE_VERSION[i]);
-//    }
-        if((logSW_state[0] == 0) && (logSW_state[0] == 0)){
-            LOGLED=0;
-            do_log = 0;
-#if 0
-                if(recv_bytes >= (2024*1024)){
-                    fclose(fp);
-                    fp = NULL;
-                    debug.printf("FILE Close\n");
-                    save_done = 1;
+        if(do_disp){
+            do_disp = 0;
+            lcd.locate(0,0);
+            if(UseLogMemory){
+                lcd.printf("LOG:%s",logFile_getName(0));
+            } else {
+                lcd.printf("NO SD Card");
+            }
+            lcd.locate(0,1);
+            lcd.printf("R:%-10lu",recv_bytes);
+        }
+        if((logSW_state[0] == 0) && (logSW_state[1] == 0)){
+            if((fp != NULL) && (bin_info.cjobst == 0)){
+                if(wcnt > 0){
+                    fwrite(wbuf,sizeof(wbuf[0]),wcnt,fp);
+                    wcnt = 0;
                 }
-#endif
-        }
-        if((logSW_state[0] != 0) && (logSW_state[0] != 0)){
-            LOGLED=0.20;
-            do_log = 1;
-#if 0
-                        if(fp == NULL){
-                            fp = fopen("/sd/rawdata.dat","w+b");
-                            debug.printf("FILE Open=%p\n",fp);
-                            wcnt = 0;
-                        }
-#endif
+                fclose(fp);
+                fp = NULL;
+                logNextCount++;
+                recv_bytes=0;
+                LOGLED=0;
+            }
+        } else if((logSW_state[0] != 0) && (logSW_state[1] != 0)){
+            if((fp == NULL) && (bin_info.cjobst == 0)){
+                if((fp = fopen(logFile_getName(1),"w+b")) != NULL){
+                    recv_bytes=0;
+                    LOGLED=0.80;
+                }
+            }
         }
 
         if (gps.readable()) {
@@ -299,23 +361,20 @@
                 wbuf[wcnt] = c;
                 wcnt++;
                 if(wcnt >= sizeof(wbuf)){
-                    fwrite(wbuf,sizeof(wbuf[0]),sizeof(wbuf)/sizeof(wbuf[0]),fp);
+                    fwrite(wbuf,sizeof(wbuf[0]),wcnt,fp);
                     wcnt = 0;
                 }
             }
-            if(bin_info.cjobst == 6){
+            if(bin_info.cjobst == 8){
                 for(i = 0;Skytraq_ID_List[i].ID != 0xFF;i++){
                     if(bin_info.ID == Skytraq_ID_List[i].ID){
                         break;
                     }
                 }
-                recv_count++;
-                lcd.locate(0,1);
-                lcd.printf("R:%-10lu %c",recv_bytes,(do_log) ? '*' : ' ');
                 debug.printf("ID=%02x len=%5d [%s]\n",bin_info.ID,bin_info.len,Skytraq_ID_List[i].name);
-                //if(Skytraq_ID_List[i].func != NULL){
-                //    (*Skytraq_ID_List[i].func)(&bin_info);
-                //}
+                if(Skytraq_ID_List[i].func != NULL){
+                    (*Skytraq_ID_List[i].func)(&bin_info);
+                }
                 bin_info.cjobst = 0;
             }
             //debug.printf("[%02x]",gps.getc());