TestKeypad
Dependencies: Hotboards_keypad TextLCD mbed
Fork of Chronometer_V2 by
Diff: main.cpp
- Revision:
- 1:26bcd89c18e5
- Parent:
- 0:1a92e4a37697
--- a/main.cpp Thu Nov 23 14:20:48 2017 +0000 +++ b/main.cpp Mon Mar 26 21:13:28 2018 +0000 @@ -21,9 +21,9 @@ //############################################################## #include <stdlib.h> - #include "mbed.h" #include "TextLCD.h" +#include "Hotboards_keypad.h" // Default Number Lap @@ -32,87 +32,54 @@ // 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' } +}; -typedef struct time_screen { - int cents; - int seconds; - int minutes; -} measured_time; +// 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 +// +// Defines the pins connected to the rows +DigitalInOut rowPins[ 4 ] = { PB_0 , PA_4 , PA_1 , PA_0 }; + +// 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); // read Voltage battery, analog pin used PC_3 (Pin 37 of Morpho layout) -AnalogIn vbat(PC_3); +//AnalogIn vbat(PC_3); // User button pressure InterruptIn user_button(USER_BUTTON); // Sensor connected to digital input D9, alias PC7 on Morpho layout +// Now p29 and p31 InterruptIn proximity(D9); - -Timer t; + // LCD Display (RS, E, D4, D5, D6, D7); TextLCD lcd(D2,D3,D4,D5,D6,D7); -int lap = 0; -int last_read = 0; -int lap_time = 0; - - -// Conversion from millisecond to mm:ss:cc data format -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; -} - -// Function to measure time elapsed -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; - } - } - -} -// Function to reset measure done -void reset_measure(){ - t.stop(); - t.reset(); - lap = 0; - last_read = 0; - lcd.cls(); -} - //------------------------------------------------------------ @@ -124,45 +91,25 @@ int main() { proximity.mode(PullDown); - proximity.rise(&measure_time); - user_button.fall(&reset_measure); + - - - - while(true) { - int read = t.read_ms(); - - measured_time time = human_read(read); + while(true) { 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); - } + lcd.printf("Character: "); heartbeat = !heartbeat; - + + // Poll the keypad to look for any activation + char key = kpd.getKey( ); - if(lap > 0 && lap <= NUM_LAP){ - wait(0.1); - }else{ - if(lap == 0){ - //Controllo batteria con partitore resistivo - double battery = ((3.3L * vbat) * 57) / 10; - if(battery < VBAT_MIN){ - lcd.locate(0,1); - lcd.printf("LOW BAT %2.1f",battery); - }else{ - lcd.locate(0,1); - lcd.printf("OK BAT %2.1f",battery); - } - } - wait(1); + // 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" ); } } }