Dual CANbus monitor and instrumentation cluster supporting ILI9341 display controller

Dependencies:   SPI_TFTx2_ILI9341 TOUCH_TFTx2_ILI9341 TFT_fonts mbed

Fork of CANary by Tick Tock

Revision:
160:905fe45ed54b
Parent:
159:577e9d015693
Child:
161:71ac85d11f03
--- a/utility.cpp	Fri Nov 08 16:07:53 2013 +0000
+++ b/utility.cpp	Wed Nov 20 13:13:54 2013 +0000
@@ -3,6 +3,9 @@
 unsigned long brkMonRate = 400000;
 unsigned short brkMonThr = 4000;
 Timeout beepOff;
+unsigned char bCount = 2;
+float bFreq[2] = { 1000, 1000 };
+float bTime[2] = { 0.1, 0.1 };
 
 void mbed_reset();
 
@@ -48,7 +51,7 @@
     static unsigned char ii = 0;
     static unsigned char lasti = 0; // indexindex
     static unsigned char bdi=0;
-    static signed short imotorRPM = 0;
+    static signed short imotorRPM_x2 = 0;
     static unsigned short nLost = 0; // gg - overrun
  
     char sTemp[40];    
@@ -349,20 +352,20 @@
             }
             numWsamples++;
         }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
-            imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
-            if(imotorRPM<0){ // take absolute value
-                imotorRPM=-imotorRPM;
+            imotorRPM_x2=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
+            if(imotorRPM_x2<0){ // take absolute value
+                imotorRPM_x2=-imotorRPM_x2;
             }
-            motorRPM+=imotorRPM;
+            motorRPM_x2+=imotorRPM_x2;
             numSsamples++;
 //        }else if((mType==2)&&(canRXmsg.id==0x1ca)){ //Brake Pressure
         }else if((mType==2)&&(canRXmsg.id==0x292)){ //Brake Pressure
             if(brakeMon){
                 if(canRXmsg.data[0]<0xff){
-                    if((canRXmsg.data[6]*imotorRPM)<brkMonThr){
+                    if((canRXmsg.data[6]*imotorRPM_x2)<brkMonThr){
                         chirpInt=0;
                     }else{
-                        chirpInt=brkMonRate/imotorRPM;
+                        chirpInt=brkMonRate/imotorRPM_x2;
                         chirpInt/=canRXmsg.data[6];
                     }
                 }
@@ -517,7 +520,7 @@
     char buffer[bufSize];
     char sTemp[16];
     printMsg("Saving local config file.\n");
-    wait(2);
+    //wait(2);
     cfile = fopen("/local/config.txt", "w");    
     fprintf(cfile,"format 9\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
@@ -560,7 +563,7 @@
     
     // Make copy of CONFIG.TXT
     printMsg("Saving CONFIG.BAK.\n");
-    wait(2);
+    //wait(2);
     cfile = fopen("/local/CONFIG.TXT", "r");
     sfr = f_open(&efile,"CONFIG.BAK",FA_WRITE|FA_CREATE_NEW);    
     if((cfile != NULL)&&(sfr == FR_OK)){
@@ -577,7 +580,7 @@
 
     // Make copy of ehist.cny
     printMsg("Saving ehist.bak.\n");
-    wait(2);
+    //wait(2);
     cfile = fopen("/local/ehist.cny", "r");
     sfr = f_open(&efile,"ehist.bak",FA_WRITE|FA_CREATE_NEW);    
     if((cfile != NULL)&&(sfr == FR_OK)){
@@ -591,7 +594,7 @@
         fclose(cfile);
         f_close(&efile);
     }
-    wait(2);
+    //wait(2);
 }
 
 void readConfig(){
@@ -777,6 +780,7 @@
     struct tm t; // pointer to a static tm structure
     time_t seconds ;
     CANMessage msg;
+    static unsigned char lastHour, numMatched;
     seconds = time(NULL);
     t = *localtime(&seconds);
     if(modelYear<2013){ //MY2011,2012
@@ -796,8 +800,15 @@
         msg = lastMsg[indexLastMsg[0x509]];
         t.tm_sec = msg.data[2]>>2;
     }
-    if((t.tm_mon>=0)&&(t.tm_mon<12)&&(t.tm_mday>0)&&(t.tm_mday<32)&&(t.tm_hour>=0)&&(t.tm_hour<24)&&(t.tm_min>=0)&&(t.tm_min<60)&&(t.tm_sec>=0)&&(t.tm_sec<60)){ // sanity check result before using
+    if(t.tm_hour==lastHour){ //filter
+        numMatched++;
+    }else{
+        numMatched=0;
+    }
+    lastHour=t.tm_hour;
+    if((numMatched>5)&&(t.tm_mon>=0)&&(t.tm_mon<12)&&(t.tm_mday>0)&&(t.tm_mday<32)&&(t.tm_hour>=0)&&(t.tm_hour<24)&&(t.tm_min>=0)&&(t.tm_min<60)&&(t.tm_sec>=0)&&(t.tm_sec<60)){ // sanity check result before using
         set_time(mktime(&t));
+        numMatched=0;
         return(true);
     }else{
         return(false);
@@ -971,7 +982,7 @@
         int fwc_tmp = fwCount;
         readConfig();
         fwCount = fwc_tmp; // Do no overwrite fwcount when loading new config
-        wait(2);
+        //wait(2);
     }
 
     // Check for history file on USB drive
@@ -990,7 +1001,7 @@
             fclose(lfile);
         }
         f_close(&efile);
-        wait(2);
+        //wait(2);
     }
 
     sfr = f_open(&efile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
@@ -1029,7 +1040,7 @@
     } else {
       //could not open directory
         printf("Couldn't open folder.\n");
-        wait(2);
+        wait(3);
         return;
     }        
     printf("Copying new firmware.\n");
@@ -1037,12 +1048,12 @@
     //The newest bin file is the one that is used by the mbed  
     sprintf(sTemp,"/local/fw%d.bin",fwCount);
     printf("Writing %s.\n",sTemp);
-    wait(2);
+    //wait(2);
     lfile = fopen(sTemp, "wb");
     if(lfile == NULL)
     {
         printf("Couldn't open destination.\n");
-        wait(2);
+        wait(3);
         return;
     }
 
@@ -1073,8 +1084,13 @@
 }
 
 void spkrOff(void){
-    spkr=0;
-    dled.period(.001);
+    if(bCount<2){
+        spkr.period(1.0/bFreq[bCount]);
+        beepOff.attach(&spkrOff, bTime[bCount++]);   
+    }else{
+        spkr=0;
+        dled.period(.001);
+    }
 }
 
 void beep(float freq, float time){
@@ -1090,6 +1106,15 @@
     }
 }
 
+void beep3(float freq1, float time1, float freq2, float time2, float freq3, float time3){
+    bFreq[0]=freq2;
+    bTime[0]=time2;
+    bFreq[1]=freq3;
+    bTime[1]=time3;
+    bCount=0;
+    beep(freq1, time1);
+}
+
 void chirp(void){
     static unsigned short counter=0;