StarBoard Orange - Example application No.4 (Version 0.0.1)

Dependencies:   mbed SDFileSystem

Revision:
2:3885fc2eefd8
Parent:
1:cda1048cb9c9
--- a/main.cpp	Mon Sep 20 06:32:27 2010 +0000
+++ b/main.cpp	Mon Sep 20 22:08:21 2010 +0000
@@ -33,7 +33,6 @@
 BusOut led(LED4, LED3, LED2, LED1);
 SerialGPS gps(p9, p10);
 SDFileSystem sd(p5, p6, p7, p8, "sd");
-Ticker ticker;
 ReceiverIR ir_rx(p15);
 
 /*
@@ -41,6 +40,19 @@
  */
 static int display_mode = 0;
 
+static const int SAMPLE_PER_FILE = 500;
+typedef struct {
+    int count;
+    FILE *fp;
+    bool enabled;
+} log_t;
+
+log_t logger = {
+    .count = 0,
+    .fp = NULL,
+    .enabled = false
+};
+
 /*
  * Functions.
  */
@@ -48,7 +60,7 @@
 /**
  * A ticker function.
  */
-void func_ticker(void) {
+void loggle_led(void) {
     led = led + 1;
 }
 
@@ -56,7 +68,58 @@
  * A callback function for logging data.
  */
 void cbfunc_log(char *s) {
-    // printf("%s\n", s);
+    /*
+     * Rotate a file for log.
+     */
+    if ((logger.count % SAMPLE_PER_FILE) == 0) {
+        /*
+         * Close a file.
+         */
+        if (logger.fp != NULL) {
+            fclose(logger.fp);
+        }
+        /*
+         * Open file for write.
+         */
+        char fname[32];
+        sprintf(fname, "/sd/gps%05d.txt", logger.count / SAMPLE_PER_FILE);
+        logger.fp = fopen(fname, "w");
+        if (logger.fp == NULL) {
+            logger.enabled = false;
+        } else {
+            logger.enabled = true;
+        }
+    }
+
+    /*
+     * Write a data to the file.
+     */
+    if (logger.enabled) {
+        if ((fwrite(s, strlen(s), 1, logger.fp) != 1) || (fputc('\n', logger.fp) != '\n')) {
+            logger.enabled = false;
+            fclose(logger.fp);
+            logger.fp = NULL;
+        }
+    }
+
+    /*
+     * Display statuses.
+     */
+    if (display_mode == 6) {
+        lcd.locate(0, 1);
+        if (logger.enabled) {
+            lcd.printf("%06d:%04d/%04d", logger.count / SAMPLE_PER_FILE, (logger.count % SAMPLE_PER_FILE) + 1, SAMPLE_PER_FILE);
+        } else {
+            lcd.printf("Logger disabled!");
+        }
+    }
+
+    /*
+     * Count up.
+     */
+    logger.count++;
+    
+    loggle_led();
 }
 
 /**
@@ -132,23 +195,26 @@
     gps.attach(&cb);
 
     /*
-     * Attach a ticker for interrupt test.
-     */
-    ticker.attach_us(&func_ticker, 250 * 1000);
-
-    /*
      * Initial display.
      */
     lcd.cls();
     lcd.printf("GGA (Time)");
 
     /*
-     * Loop.
+     * Application loop.
      */
     int irlen;
     uint8_t irbuf[32];
     RemoteIR::Format irfmt;
     while (1) {
+        /*
+         * GPS processing.
+         */
+        gps.processing();
+
+        /*
+         * Get a data from IR receiver.
+         */
         irlen = ir_rx.getData(&irfmt, irbuf, sizeof(irbuf) * 8);
         if (0 < irlen) {
             uint64_t n = 0;
@@ -157,7 +223,6 @@
                     n = n | (1 << i);
                 }
             }
-            // printf("%d:0x%llx\n", irlen, n);
             if ((irlen == 12) && (irfmt == RemoteIR::SONY)) {
                 switch (n) {
                     case 0x80:
@@ -190,11 +255,15 @@
                         lcd.cls();
                         lcd.printf("RMC (Time)");
                         break;
+                    case 0x86:
+                        display_mode = 6;
+                        lcd.cls();
+                        lcd.printf("LOGGER (Status)");
+                        break;
                     default:
                         break;
                 }
             }
         }
-        gps.processing();
     }
 }