To Record the time of the car

Dependencies:   mbed TextLCD

Revision:
1:db7799912cac
Parent:
0:9c3829968b11
Child:
2:c9889e740474
--- a/main.cpp	Fri Aug 07 06:19:33 2020 +0000
+++ b/main.cpp	Fri Aug 07 17:10:27 2020 +0000
@@ -7,7 +7,7 @@
     #define ACCEPT_LOCK              0           //if define ACCEPT LOCK == 1,need receive ACCEPT before next tx
     #define RX_MODE                  1           //1 for scanf ; 2 for getc 
     #define BUTTON_DEBOUNCE       0.3f
-    #define LCD_SHOW_PERIOD       2.0f           //show last cycle for 2 sec
+    #define LCD_SHOW_PERIOD       3.0f           //show last cycle for 2 sec
 //Define-Don't Change
     #define m_1             1000000.0f
     #define TX_SIZE                 16
@@ -15,7 +15,7 @@
 #if (!DEBUG)
     #define CYCLE_TIME_PROTECT    2.0f           //used in formal , protect someone trigger the sensor 
 #else 
-    #define CYCLE_TIME_PROTECT    0.0f
+    #define CYCLE_TIME_PROTECT    1.0f
 #endif
     #define ENDURANCE_CYCLE         20
     #define SKIDPAD_CYCLE            4 
@@ -49,7 +49,7 @@
 //Sensor
 #if(DEBUG)
     DigitalOut  sensor_com(PA_8,1);             // D7
-    InterruptIn SENSOR_IT(PB_10,PullDown);      // D6
+    InterruptIn SENSOR_IT(PB_4,PullDown);      //  D5
 #else
     DigitalOut  sensor_com(PB_2,1);
     InterruptIn SENSOR_IT(PB_1,PullDown);
@@ -59,10 +59,10 @@
     DigitalOut key(PC_1,0);                    // not AT Mode
     DigitalIn  bt_state(PA_4,PullDown);        // high when connected with terminal
 //Mode Button
-    DigitalOut button_high(PA_11,1);           // V+ of button
-    InterruptIn MY_BUTTON(PB_12,PullDown);
+    DigitalOut button_high(PA_11,1);           // 最右7
+    InterruptIn MY_BUTTON(PB_12,PullDown);     // 最右8
 //Reset Button
-    DigitalOut button_high_2(PA_6,1);
+    DigitalOut button_high_2(PA_6,1);          // Mode的左邊
     InterruptIn MY_BUTTON_2(PA_7,PullDown);
     /*
       ###############################
@@ -137,6 +137,7 @@
     void sensor_in(void);
     void button_fall(void);
     void button_fall_2(void);
+    void test_button(void);
     void RxCallback(void);
 //protect parameters
     void parameters_protect(void);
@@ -186,10 +187,14 @@
         lcd.locate(0,1);
         lcd.printf("Initializing");
         //bt
-        if(bt_state.read())  bt.printf("NTHU RACING TIME RECORDER INITIALIZING\r\n\r\n");
+        if(bt_state.read())  {
+            bt.printf("######################################\r\n");
+            bt.printf("NTHU RACING TIME RECORDER INITIALIZING\r\n\r\n");
+        }
         else{
             lcd.locate(12,0);
-            lcd.printf("b0"); 
+            lcd.printf("b0");
+            pc.printf("######################################\r\n");
             pc.printf("NTHU RACING TIME RECORDER INITIALIZING\r\n\r\n");   
         }
         wait(1.5);
@@ -206,11 +211,11 @@
         lcd.locate(0,0);
         lcd.printf("Set up ok!"); 
         if(bt_state.read()){
-              bt.printf("NTHU RACING TIME RECORDER SET UP DONE\r\n\r\n");
+              bt.printf("NTHU RACING TIME RECORDER SET UP DONE\r\n");
               bt.printf("#####################################\r\n");
         }
         else{
-              pc.printf("NTHU RACING TIME RECORDER SET UP DONE\r\n\r\n");
+              pc.printf("NTHU RACING TIME RECORDER SET UP DONE\r\n");
               pc.printf("#####################################\r\n");
         }
         wait(1); 
@@ -222,6 +227,15 @@
         bt_print_flag = 0;
         this_cycle_timer.stop();
         this_cycle_timer.reset();
+        if( mode != 1 || (mode == 1 && this_rcmd == RESTART) || (mode == 1 && last_tcmd == RESTART) ){
+            cycle = 1;
+            best_cycle = 1;
+            last_cycle_time = 0.0;
+            best_cycle_time = 0.0;
+            total_time = 0.0;
+            lcd_end_flag = 0;
+            bt_end_flag = 0;
+        }
         if(mode == 1){
             memset(TX_buffer,0,TX_SIZE);
             memset(RX_buffer,0,RX_SIZE);
@@ -234,13 +248,8 @@
             last_tcmd = 0;
             this_rcmd = 0;
         }
-        if( mode != 1 || (mode == 1 && this_rcmd == RESTART) || (mode == 1 && last_tcmd == RESTART) ){
-            cycle = 1;
-            best_cycle = 1;
-            last_cycle_time = 0.0;
-            best_cycle_time = 0.0;
-            total_time = 0.0;
-        }
+        if(bt_state.read() ) bt.printf("#####################################\r\n");
+        else pc.printf("#####################################\r\n");
     }
     void show_all(){
         parameters_protect();
@@ -268,44 +277,46 @@
     void lcd_show(){
     //process 4 update 
         lcd.locate(0,0);
-        if( (!lcd_print_flag) && (process == 4 && cycle > 1 || mode == 1 && process == 4 && cycle >= 1)){
+        if( (!lcd_print_flag)&& last_cycle_time > 0.0f && (process == 4 && cycle >= 1 || mode == 1 && process == 4 && cycle >= 1)){
         //print out last cycle time flag
             lcd_print_flag = 1;
-            if(mode == 1)
             lcd_show_result_time = us_ticker_read()/m_1;
         }
         else if((!lcd_end_flag) && (cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)){
         //mode end
-            lcd_end_flag = 1;
-            lcd_end_time = us_ticker_read()/m_1;
+            if(us_ticker_read()/m_1 - lcd_show_result_time > LCD_SHOW_PERIOD){
+                lcd_end_flag = 1;
+                lcd_end_time = us_ticker_read()/m_1;
+            }
         }
         
         //row 1 show
-        if(us_ticker_read()/m_1 - lcd_show_result_time < LCD_SHOW_PERIOD)
+        if(us_ticker_read()/m_1 - lcd_show_result_time < LCD_SHOW_PERIOD && last_cycle_time != 0)
             lcd.printf("LCT : %.2f sec  ",last_cycle_time);
-        else if(us_ticker_read()/m_1 - lcd_end_time < LCD_SHOW_PERIOD)
-            lcd.printf("BC : %d         ",best_cycle);
+        else if(us_ticker_read()/m_1 - lcd_end_time < LCD_SHOW_PERIOD && last_cycle_time != 0)
+            lcd.printf("BC  :%d         ",best_cycle);
         else{
             switch(mode){
                 case 0 : 
-                    lcd.printf("SING b%dn%d C%d  ",bt_state.read(),rf_state,cycle);
+                    lcd.printf("SING b%dn%d C%d   ",bt_state.read(),rf_state,cycle);
                     break;
                 case 1:
-                    lcd.printf("ACC  b%dn%d C%d  ",bt_state.read(),rf_state,cycle);     
+                    lcd.printf("ACC  b%dn%d C%d   ",bt_state.read(),rf_state,cycle);     
                     break;
                 case 2:
-                    lcd.printf("SKID b%dn%d C%d  ",bt_state.read(),rf_state,cycle);     
+                    lcd.printf("SKID b%dn%d C%d   ",bt_state.read(),rf_state,cycle);     
                     break;
                 case 3:
-                    lcd.printf("ENDU b%dn%d C%d  ",bt_state.read(),rf_state,cycle);     
+                    lcd.printf("ENDU b%dn%d C%d   ",bt_state.read(),rf_state,cycle);     
                     break;
             }
         }
         lcd.locate(0,1);
     //row 2 show
         if(cycle == 1 && process == 0) lcd.printf("Ready to Go   !");                   //first in 
-        else if(lcd_end_flag) lcd.printf("BCT :%.2f    ",best_cycle_time);
+        else if(lcd_end_flag) lcd.printf("BCT :%.2f sec",best_cycle_time);
         else if(mode == 1) lcd.printf("TC:%.2f sec  ",this_cycle_timer.read());
+        else if((cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)) lcd.printf("Mode %d End!   ",mode);
         else lcd.printf("%.2f,%.2f       ",this_cycle_timer.read(),this_cycle_timer.read()+total_time);
     //lcd life check   
         lcd.locate(15,1);
@@ -318,27 +329,29 @@
         if(bt_state.read() && process == 3){
             //process change
             process = 4;
+            bt_print_flag = 1;
             if (last_cycle_time == 0.0f) bt.printf("First in\r\n");
             else {
-                bt.printf("Mode:%d,cycle: %d,last cycle time:%.2f\r\n",mode,cycle,last_cycle_time);
+                bt.printf("Mode:%d,cycle: %d,last cycle time:%.2f sec\r\n",mode,cycle,last_cycle_time);
                 if(cycle == SKIDPAD_CYCLE && mode == 2 ||  cycle == ENDURANCE_CYCLE && mode == 3){
-                    bt.printf("Mode End & Reset\r\n");
+                    bt.printf("Best Cycle:%d\r\n",best_cycle);
+                    bt.printf("Best Cycle Time:%.2f sec\r\n",best_cycle_time);
+                    bt.printf("Mode %d End & Reset\r\n",mode);
                     bt_end_flag = 1;
                 }   
             }
-            bt_print_flag = 1;
         }
         else if(process == 3){
-             process = 4;
+            process = 4;
+            bt_print_flag = 1;
             if (last_cycle_time == 0.0f) pc.printf("First in\r\n");
             else {
-                pc.printf("Mode:%d,cycle: %d,last cycle time:%.2f\r\n",mode,cycle,last_cycle_time);
+                pc.printf("Mode:%d,cycle: %d,last cycle time:%.2f sec\r\n",mode,cycle,last_cycle_time);
                 if(cycle == SKIDPAD_CYCLE && mode == 2 ||  cycle == ENDURANCE_CYCLE && mode == 3){
-                    pc.printf("Mode End & Reset\r\n");
+                    pc.printf("Mode %d End & Reset\r\n",mode);
                     bt_end_flag = 1;
                 }   
             }
-            bt_print_flag = 1;
         }
         if(last_rf_state != rf_state){
             last_rf_state = rf_state;
@@ -359,7 +372,9 @@
             process = 0;
             lcd_print_flag = 0;
             bt_print_flag = 0;
-            if(mode != 1) cycle++;
+            if(mode != 1) {
+                cycle++;
+            }
         }
         if(lcd_end_flag && bt_end_flag){
         //notice reset
@@ -373,7 +388,6 @@
         //calculation time variable
         //last_cycle_time update in sensor in
         if(mode != 1)  total_time += last_cycle_time;
-        
         if(cycle == 1){
             best_cycle = cycle;
             best_cycle_time = last_cycle_time;
@@ -549,10 +563,20 @@
     
 //IT
     void sensor_in(){
-        process = 1;
-        if(mode!=1 && this_cycle_timer.read() > CYCLE_TIME_PROTECT){
-            last_cycle_time = this_cycle_timer.read();
-            this_cycle_timer.reset();
+        if(mode!=1){
+            if(cycle == 1){
+                this_cycle_timer.start();
+                process = 1;
+            }
+            if(this_cycle_timer.read() > CYCLE_TIME_PROTECT){
+                process = 1;
+                last_cycle_time = this_cycle_timer.read();
+                this_cycle_timer.reset();
+            }
+        //check mode2,3
+            if((cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)){
+                process = 0;
+            }
         }
         else if(mode == 1){
         //mode acc
@@ -575,7 +599,6 @@
             #endif
             }
         }
-        else process = 0;
     }
      void button_fall(void){
         //debounce
@@ -583,7 +606,9 @@
         if(button_RTC - button_LTC > BUTTON_DEBOUNCE){
             button_LTC = button_RTC;
             (mode >= 3 || mode < 0) ? mode = 0 : mode++ ;
+            last_tcmd = RESTART;
             reset_parameters();
+            last_tcmd = ACCEPT;
             if(bt_state.read()) bt.printf("Mode change to :%d \r\n",mode);
             else pc.printf("Mode change to :%d \r\n",mode);
         }
@@ -599,6 +624,10 @@
                 bt.printf("Restart!\r\n\r\n\r\n");
                 bt.printf("#####################\r\n");
             }
+            else{
+                pc.printf("Restart!\r\n\r\n\r\n");
+                pc.printf("#####################\r\n");
+            }
         }
     }
     void RxCallback(){