Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed keypadLib TextLCD
Diff: main.cpp
- Revision:
- 4:23472c2b246b
- Parent:
- 3:fd1353986910
- Child:
- 5:47a452a6f248
--- a/main.cpp Sun May 20 23:19:17 2018 +0000 +++ b/main.cpp Tue May 22 20:34:09 2018 +0000 @@ -23,7 +23,6 @@ #include <stdlib.h> #include "mbed.h" #include "TextLCD.h" -#include "keypad.h" // Default Number Lap #define NUM_LAP 3 @@ -45,13 +44,6 @@ int seconds; int minutes; } measured_time; - - -// Keypad connection (4 columns, 4 rows) -Keypad keypad(PA_0 , PA_1 , PA_4 , PB_0, PA_13 , PA_14 , PC_2 , PC_3 ); - -// Configures the serial port -Serial pc( USBTX , USBRX ); // Heartbeat LED DigitalOut heartbeat(LED1); @@ -84,6 +76,53 @@ // Pointer to the loop function (depending on the selected mode) void (*loopMethod)(void); +/*** Gestione keypad ****/ +PinName rowPins[4] = { PA_13, PA_14, PC_2, PC_3 }; +PinName colPins[4] = { PA_0, PA_1, PA_4, PB_0 }; + +DigitalOut* _rows[4]; +DigitalIn* _cols[4]; + +// Define your own keypad values +char Keytable[] = { + '1', '2', '3', 'A', // r0 + '4', '5', '6', 'B', // r1 + '7', '8', '9', 'C', // r2 + '*', '0', '#', 'D' // r3 + // c0 c1 c2 c3 +}; + +int getKeyIndex() { + int result = -1; + for (int r = 0; r < 4; r++) { + _rows[r]->write(1); + for(int c = 0;c < 4 ;c++){ + DigitalIn *col = _cols[c]; + if(col->read() == 1) { + result = r*4+c; + } + } + _rows[r]->write(0); + } + return result; +} + +char getKey() { + int idx = getKeyIndex(); + return idx != -1 ? Keytable[idx] : '\0'; +} + +void keypadInit() { + for(int i = 0;i < 4; i++){ + _rows[i] = new DigitalOut(rowPins[i]); + _rows[i]->write(0); + } + for(int i = 0;i < 4; i++){ + _cols[i] = new DigitalIn(colPins[i],PullDown); + } +} +/**** fine gestione keypad ****/ + //Conversione da millisecondi a mm:ss:cc measured_time human_read(int ms){ measured_time read; @@ -128,7 +167,7 @@ } } - +// Handler for loop when in SPEED mode void speedLoop() { int read = t.read_ms(); @@ -136,19 +175,30 @@ lcd.locate(0,0); lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); - - //Gestione dei parziali - if(lap > 0) { - time = human_read(lap_time); - lcd.locate(0,1); - lcd.printf("Giro %d %02d:%02d:%02d",lap,time.minutes,time.seconds,time.cents); - } - if (lap >= NUM_LAP) { - time = human_read(best_time); - lcd.locate(0,1); - wait(1); - lcd.printf("Best %d %02d:%02d:%02d",best_lap,time.minutes,time.seconds,time.cents); - wait(1); + + // Handle lap time display + switch(lap) { + // only display time if at least 1 lap completed + case -1: + case 0: + break; + // all laps completed - display best lap time + case NUM_LAP + 1: + time = human_read(best_time); + lcd.locate(0,1); + lcd.printf("Best %d %02d:%02d:%02d",best_lap,time.minutes,time.seconds,time.cents); + break; + // Default - display last completed lap time + // In case of last lap, wait 1 sec -- next time, best lap will be displayed + default: + time = human_read(lap_time); + lcd.locate(0,1); + lcd.printf("Giro %d %02d:%02d:%02d",lap,time.minutes,time.seconds,time.cents); + if (lap == NUM_LAP) { + wait(1); + lap++; + } + break; } } @@ -166,25 +216,24 @@ lcd.cls(); lcd.locate(0,0); - lcd.printf("Ended."); + lcd.printf("Finish!"); lcd.locate(0,1); lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); + gateStart.disable_irq(); + gateEnd.disable_irq(); } void labyrinthLoop() { - if (lap == 0) { - 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); - } + 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.enable_irq(); gateStart.rise(&start_time); gateEnd.enable_irq(); gateEnd.rise(&stop_time); @@ -193,11 +242,11 @@ lcd.locate(0,0); lcd.printf("Mode: LABYRINTH "); wait(1); - lcd.cls(); break; case SPEED: default: + gateStart.enable_irq(); gateStart.rise(&measure_time); gateEnd.disable_irq(); loopMethod = &speedLoop; @@ -217,14 +266,12 @@ last_read = 0; best_lap = 0; best_time = -1; - lcd.cls(); + configMode(); } void switchMode() { mode = mode == SPEED ? LABYRINTH : SPEED; reset_measure(); - configMode(); - } //------------------------------------------------------------ @@ -234,31 +281,31 @@ //------------------------------------------------------------ int main() { char key; + keypadInit(); + user_button.fall(&switchMode); gateStart.mode(PullDown); gateEnd.mode(PullDown); - configMode(); + reset_measure(); while(true) { heartbeat = !heartbeat; loopMethod(); wait(0.1); - /* - key = keypad.getKey(); - if (key != KEY_RELEASED) { + key = getKey(); + if (key != '\0') { if(key == 'A') { mode = LABYRINTH; - configMode(); + reset_measure(); } else if(key == 'B') { mode = SPEED; - configMode(); - } else if(key == '*') { + reset_measure(); + } else if(key == '#') { reset_measure(); } } - */ } }