vehicle CAN message log to SD file system using ring buffer memory

Dependencies:   SDFileSystem mbed

Files at this revision

API Documentation at this revision

Comitter:
ym1784
Date:
Wed Jan 30 05:03:35 2013 +0000
Parent:
0:de9553dd79ec
Commit message:
clean up

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r de9553dd79ec -r 4a538cae9042 main.cpp
--- a/main.cpp	Fri Jan 25 21:04:19 2013 +0000
+++ b/main.cpp	Wed Jan 30 05:03:35 2013 +0000
@@ -35,7 +35,7 @@
 void can_check(void);
 void check_sw();
 
-// CAN message buffer
+// CAN message ring buffer
 #define BUFFER_SIZE 512  // buffer size must be power of 2
 #define BUFFER_MASK ( BUFFER_SIZE - 1 )
 unsigned int second[BUFFER_SIZE];
@@ -51,7 +51,7 @@
     button2.mode(PullUp);
     button3.mode(PullUp);
     button4.mode(PullUp);
-    sw_check.attach_us(&check_sw, 5000);    // status check every 5 msec
+    sw_check.attach_us(&check_sw, 5000);    // button switch status check every 5 msec
 
     can2.frequency(500000);
     pc.baud(9600);
@@ -74,10 +74,10 @@
         pc.printf("Unable to create the file '%s'\r\n", file_name);
         pc.printf("Please try again\r\n");
 
-        User_Led1 = LED_OFF;
+        User_Led1 = LED_OFF;    // file open fail
         file_open = 0;
     } else {
-        User_Led1 = LED_ON;
+        User_Led1 = LED_ON;     // file open
         file_open = 1;
     }
 
@@ -101,9 +101,8 @@
 
             tail = tmptail; // update tail after data sent
             if ( tail == 0 ) {
-                // toggle led3
-                User_Led3 = !User_Led3;
-                t = time( NULL ) + 32400;  // UTC + 9 hours for JST
+                User_Led3 = !User_Led3;     // toggle led3 showing new cycle of ring buffer
+                t = time( NULL ) + 32400;   // UTC + 9 hours for JST
                 strftime( file_name, 20, "%T", localtime( &t ) );
                 pc.printf("new cycle of ring buffer at %s\r\n", file_name);
             }
@@ -127,12 +126,12 @@
     if ( file_open == 1 ) {
         while ( can2.read(can_MsgRx) ) {
             temp_head = (head + 1) & BUFFER_MASK;
-            if ( temp_head == tail ) {          // buffer overflow error
+            if ( temp_head == tail ) {      // buffer overflow error
                 fclose(fp);
                 fprintf(stderr, "buffer overflow\r\n");
                 fprintf(stderr, "head = %d\r\n", head);
                 fprintf(stderr, "tail = %d\r\n", tail);
-                User_Led4 = 1;
+                User_Led4 = 1;              // buffer overflow error
                 exit(1);
             } // if
 
@@ -143,8 +142,7 @@
                 data[i][temp_head] = can_MsgRx.data[i];
             } // for
             head = temp_head;   // update head after data stored
-            // toggle led2
-            User_Led2 = !User_Led2;
+            User_Led2 = !User_Led2;     // toggle led2 showing CAN message reading
         } // while
     } // if open
 } // can_check