Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
109:3e6f0e8fca0d
Parent:
108:29b5a760adc2
Child:
110:ffddff3ad2f2
--- a/main.cpp	Sun Jun 30 14:45:01 2013 +0000
+++ b/main.cpp	Sun Jun 30 17:52:58 2013 +0000
@@ -22,7 +22,7 @@
 #include "mbed.h"
 #include "CAN.h"
 #include "beep.h"
-#include "MSCFileSystem.h"
+#include "ff.h"
 #include "PowerControl.h"
 #include "EthernetPowerControl.h"
 #include "utility.h"
@@ -30,11 +30,12 @@
 #include "TOUCH_TFTx2.h"
 
 char revStr[7] = "108";
-
+FATFS USBdrive;
 LocalFileSystem local("local");
-
+bool waitasec = true;
 // to write to USB Flash Drives, or equivalent (SD card in Reader/Writer)
-MSCFileSystem fs("usb"); // to write to a USB Flash Drive
+//MSCFileSystem fs("usb"); // to write to a USB Flash Drive
+FRESULT mfr=f_mount(0,&USBdrive);
 
 time_t seconds ;
 
@@ -70,8 +71,10 @@
 bool yesBattLog = false; // gg - Batt Log
 unsigned char tNavRow = 3; // gg - 4x4 touch
 
-FILE *hfile; // config file
-FILE *lfile; // log file
+FILE *hfile; // history file
+FIL efile; // external usb file
+FRESULT efr; // external file access flags
+unsigned int bytesRW;
 char fileName[35] = "";
 char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write
 char indexLastMsg[0x800]={0}; // index table for last message
@@ -230,18 +233,20 @@
     secsNoTouch=2;
     while (true) {
         if (!logOpen) { // Open new file if one is not already open
-            if(logEn&&usbEn){ //logging enables and USB device detected
+            if(logEn&&usbEn&&!waitasec){ //logging enables and USB device detected
+                strftime(fileName, 32, "%m%d%H%M.alc", &t); //mmddhhmm.alc
+                efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS);
                 seconds = time(NULL);
                 t = *localtime(&seconds) ;
-                strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
-                lfile = fopen(fileName, "ab");
                 lastDMode[0]=99;//force refresh
                 lastDMode[1]=99;//force refresh
-                if(lfile==NULL){
-                    sprintf(sTemp,"\nUnable to open %s\n\n\n\n",fileName);
+                if(efr != FR_OK){
+                    sprintf(sTemp,"\nERR:%d Unable to open %s\n\n\n\n",efr,fileName);
                     printMsg(sTemp); // cannot open alc file
                     logEn=false;
                     spkr.beep(1000,0.25);
+                    wait_ms(500);
+                    spkr.beep(1000,0.25);
                 } else {
                     logOpen = true;
                     readPointer=writePointer;
@@ -259,7 +264,8 @@
             pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
             if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
                 // Dump buffer if > 1/16 full or canbus has stopped
-                if (lfile == NULL) {
+                //if (&efile == NULL) {
+                if (efr != FR_OK) {
                     logOpen = false;
                     sprintf(sTemp,"Failed to append log file.\n");
                     printMsg(sTemp); // failed to append 
@@ -270,9 +276,7 @@
                     logEn=false;
                 } else {
                     while (readPointer != writePointer) {
-                        for (j = 0; j<13; j++){
-                            fprintf(lfile,"%c",writeBuffer[readPointer][j]);
-                        }
+                        efr=f_write(&efile,&writeBuffer[readPointer][0],13,&bytesRW);
                         if(++readPointer >= maxBufLen){
                             readPointer=0;
                             led4 = !led4;
@@ -283,7 +287,7 @@
             if (!logEn) {
                 sprintf(sTemp,"Stopping Can Log %s\n",fileName);
                 printMsg(sTemp); // stopping alc log file 
-                fclose(lfile);
+                f_close(&efile);
                 logOpen=false;
                 pointerSep=0;
                 led4=false;
@@ -294,7 +298,7 @@
                 autoPoll.detach();
             }
             if (logOpen){
-                fclose(lfile);
+                f_close(&efile);
             } // if (logOpen)*/
             seconds = time(NULL);
             t = *localtime(&seconds) ;
@@ -328,7 +332,7 @@
                 logOpen = false; // Start new file if asleep for more than 30 minutes
                 if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter
             } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now)
-                lfile = fopen(fileName, "ab");
+                efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS);
                 logEvent("WakingUp"); // gg - use messeges
                 logTS(); // Date-Time at wakeup
             }
@@ -524,9 +528,9 @@
                                     // Start/stop playback
                                     if(!playbackOpen){
                                         if(!logOpen){
-                                            lfile = fopen("/usb/playback.alc", "rb");                                          
+                                            efr = f_open(&efile,"playback.alc",FA_READ|FA_OPEN_EXISTING);
                                             lastDMode[whichTouched]=99;//force refresh
-                                            if(lfile==NULL){
+                                            if(efr != FR_OK){
                                                 sprintf(sTemp,"Unable to open /usb/playback.alc\n");
                                                 printMsg(sTemp); // no playback.alc
                                                 spkr.beep(1000,0.25);
@@ -546,7 +550,7 @@
                                         }
                                     } else {
                                         playback.detach();
-                                        fclose(lfile);
+                                        f_close(&efile);
                                         playbackOpen=false;
                                         playbackEn=false;
                                         can1.attach(&recieve1);// Restore CAN data recieve
@@ -680,6 +684,7 @@
         }
 
         if(tick){ // Executes once a second
+            waitasec=false; // work around to avoid hang when USB tries to init immediately
             tick=false;
             headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
             accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
@@ -782,12 +787,13 @@
         if(step){ // playback
             if(playbackOpen&&playbackEn){
                 for(i=0;i<120;i++){
-                    if(!feof(lfile)){
-                        fscanf(lfile,"%5c%8c",&header,&data);
+                    if(!f_eof(&efile)){
+                        efr=f_read(&efile,&header,5,&bytesRW);
+                        efr=f_read(&efile,&data,8,&bytesRW);
                         logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8)); // Playback
                     } else {
-                        fclose(lfile); // restart
-                        lfile = fopen("/usb/playback.alc", "rb");                                          
+                        f_close(&efile); // restart                                       
+                        efr = f_open(&efile,"playback.alc",FA_READ|FA_OPEN_EXISTING);
                         lastDMode[whichTouched]=99;//force refresh
                         spkr.beep(2000,0.25);
                     }