VR FabLab - RoboVal / Mbed 2 deprecated Chronometer_V2

Dependencies:   mbed keypadLib TextLCD

Revision:
2:bd0c735e81d6
Parent:
1:26bcd89c18e5
Child:
3:fd1353986910
--- a/main.cpp	Mon Mar 26 21:13:28 2018 +0000
+++ b/main.cpp	Fri May 18 23:15:11 2018 +0000
@@ -23,45 +23,34 @@
 #include <stdlib.h>
 #include "mbed.h"
 #include "TextLCD.h"
-#include "Hotboards_keypad.h"
- 
- 
+#include "keypad.h"
+
 // Default Number Lap
 #define NUM_LAP 3
 
 // Reference for Low/Min Voltage battery
 #define VBAT_MIN 7.2
- 
-// Defines the keys array with it's respective number of rows & cols,
-// and with the value of each key
-char keys[ 4 ][ 4 ] =
-{
-    { '1' , '2' , '3' , 'A' },
-    { '4' , '5' , '6' , 'B' },
-    { '7' , '8' , '9' , 'C' },
-    { '*' , '0' , '#' , 'D' }
-};
- 
-// Pin keypad 4x4
-//  1,    2,     3,    4,     5,    6,    7,    8
-//  C4,   C3,    C2,   C1,    R4,   R3,   R2,   R1
-// PB_0, PA_4 , PA_1 , PA_0, PA_13, PA_14, PC_14, PC_15
-// PC_15, PC_14, PA_14, PA_13, PA_0, PA_1, PA_4, PB_0
-// 
+
+
+// Modalità di funzionamento (in attesa della tastiera)
+enum modes {
+    LABYRINTH,
+    SPEED
+} mode = SPEED;
+
+typedef struct time_screen {
+   int cents;
+   int seconds;
+   int minutes;
+} measured_time;
+
+
 // Defines the pins connected to the rows
-DigitalInOut rowPins[ 4 ] = { PB_0 , PA_4 , PA_1 , PA_0 };
+Keypad keypad(PA_0 , PA_1 , PA_4 , PB_0,PA_13 , PA_14 , PC_2 , PC_3 );
 
-// Defines the pins connected to the cols
-DigitalInOut colPins[ 4 ] = { PB_0 , PA_4 , PA_1 , PA_0  };
- 
-// Creates a new keyboard with the values entered before
-Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
- 
 // Configures the serial port
 Serial pc( USBTX , USBRX );
  
-
-
  // read Voltage battery
 DigitalOut heartbeat(LED1);
 
@@ -73,43 +62,165 @@
 
 // Sensor connected to digital input D9, alias PC7 on Morpho layout
 // Now p29 and p31
-InterruptIn proximity(D9); 
-
+InterruptIn gateStart(PA_15); 
+InterruptIn gateEnd(PB_7); 
 
 // LCD Display (RS, E, D4, D5, D6, D7);
 TextLCD lcd(D2,D3,D4,D5,D6,D7); 
+
+Timer t;
  
+int lap = 0;
+int last_read = 0;
+int lap_time = 0;
+
+void (*loopMethod)(void);
+
+
+//Conversione da millisecondi a mm:ss:cc
+measured_time human_read(int ms){
+    measured_time read;
+    div_t qr = div(ms,1000);
+    
+    read.cents = qr.rem % 100;
+    
+    qr = div(qr.quot,60);
+    read.seconds = qr.rem;
+    
+    qr = div(qr.quot,60);
+    read.minutes = qr.rem;
+    
+    return read;    
+}
+
+void measure_time(){
+    int read = t.read_ms();
+    
+    if(lap == 0){
+        t.start();
+        lap++;
+    }else{
+        //Dabouncing per evitare problemi
+        if(read - last_read > 1000){
+            
+            lap_time = read - last_read;
+            
+            if(lap >= NUM_LAP){
+                t.stop();
+                lap++;
+            }else{
+                lap++;
+            }
+            
+            last_read = read;     
+        }
+    }
+    
+}
+
+void reset_measure(){
+    t.stop();
+    t.reset();
+    lap = 0;
+    last_read = 0;
+    lcd.cls();
+}
+
 
- 
- 
+void speedLoop() {
+    int read = t.read_ms(); 
+    
+    measured_time time = human_read(read);
+    
+    lcd.locate(0,0);
+    lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents);
+    
+    //Gestione dei parziali
+    if(lap > 1){
+        time = human_read(lap_time);
+        lcd.locate(0,1);
+        lcd.printf("Giro %d %02d:%02d:%02d",lap - 1,time.minutes,time.seconds,time.cents);
+    }
+}
+
+void start_time() {
+    lap = 0;
+    t.start();
+}
+
+void stop_time() {
+    lap = 1;
+    t.stop();
+    int read = t.read_ms(); 
+    
+    measured_time time = human_read(read);
+    
+    lcd.locate(0,1);
+    lcd.printf("                ");
+    lcd.locate(0,1);
+    lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents);
+}
+
+void labyrinthLoop() {
+    if (!lap) {
+        int read = t.read_ms(); 
+        
+        measured_time time = human_read(read);
+        
+        lcd.locate(0,1);
+        lcd.printf("Elapsed %02d:%02d:%02d",time.minutes,time.seconds,time.cents);
+    }
+}
+
+void configMode() {
+    switch(mode) {
+        case LABYRINTH:
+            gateStart.rise(&start_time);
+            gateEnd.rise(&stop_time);
+            loopMethod = &labyrinthLoop;
+            break;
+        
+        case SPEED:
+        default:
+            gateStart.rise(&measure_time);
+            loopMethod = &speedLoop;
+            break;
+    }
+}
+
 //------------------------------------------------------------ 
 //
 //       Main body
 //
 //------------------------------------------------------------
- 
 int main() {
-    
-    proximity.mode(PullDown);
-  
-    
-    while(true) {       
-        
-        lcd.locate(0,0);
-        lcd.printf("Character: ");
-        
+    char key;
+    user_button.fall(&reset_measure);
+
+    gateStart.mode(PullDown);
+    gateEnd.mode(PullDown);
+
+    configMode();
+
+    while(true) {
         heartbeat = !heartbeat;
-                
-        // Poll the keypad to look for any activation
-        char key = kpd.getKey( );
-        
-        // If any key was pressed
-        if( key )
-        {
-            // Display the key pressed on serial port
-            lcd.locate(0,1);            
-            lcd.printf( "%c" , key );
-            //lcd.printf( "\n\r" );
+        loopMethod();
+        wait(0.1);
+        key = keypad.getKey(); 
+        if (key != KEY_RELEASED) {
+            if(key == 'A') {
+                lcd.locate(0,0);
+                lcd.printf("Mode: LABYRINTH ");
+                mode = LABYRINTH;
+                configMode();
+            } else if(key == 'B') {
+                lcd.locate(0,0);
+                lcd.printf("Mode: SPEED     ");
+                mode = SPEED;
+                configMode();
+            } else if(key == '*') {
+                reset_measure();
+            }
         }
     }
 }