Yifeng Yuan
/
ese519_lab1_7seg_letter
display letter on 7-segment
Diff: main.cpp
- Revision:
- 3:42fb220c3e1d
- Parent:
- 1:ad5433d86676
--- a/main.cpp Thu Feb 05 22:09:15 2015 +0000 +++ b/main.cpp Mon Feb 09 02:16:36 2015 +0000 @@ -10,28 +10,27 @@ int global_code; //global variable //-1 - error //1 - dot - //0 - dash - //2 - space + //2 - dash + //3 - space int global_buffer; //stores the codes of each letter int global_length; //stores the length of a coded word -//Timeout wait_for_timeout; -Timer t; -Timeout timeout_function; -InterruptIn en1(p21); //declear en1 as interrupt input using pin 21 -//DigitalOut led1(LED1); -InterruptIn en2(p22); //declear en2 as interrupt input using pin 22 -//DigitalOut led2(LED2); -DigitalOut dp2(p23); -DigitalOut d(p24); -DigitalOut c(p15); -DigitalOut g(p16); -DigitalOut b(p17); -DigitalOut e(p18); -DigitalOut f(p19); -DigitalOut a(p8); -PwmOut pwm1(p25); +Timer t; // Adding timer to track timing between key presses +Timeout timeout_function; // +InterruptIn en1(p21); //declare en1 as interrupt input using pin 21 +InterruptIn en2(p22); //declare en2 as interrupt input using pin 22 +DigitalOut dp2(p23); //7seg decimal point +DigitalOut d(p24); //7seg +DigitalOut c(p15); //7seg +DigitalOut g(p16); //7seg +DigitalOut b(p17); //7seg +DigitalOut e(p18); //7seg +DigitalOut f(p19); //7seg +DigitalOut a(p8); //7seg +PwmOut pwm1(p25); //declare pwm1 as digital output pin that provides pwm wave + +// Functions declarations void released(); void depressed(); void light_7seg(); @@ -41,17 +40,15 @@ void print_word(); void show_letter_7seg(char letter); -char word[10]; -int letter[5]; - +// This function handles what the device should do when a key is pressed void depressed() { timeout_function.detach(); t.stop(); t.reset(); t.start(); - } +// This function handles what the device should do when a key is released void released() { t.stop(); int duration = t.read_ms(); @@ -68,43 +65,36 @@ t.start(); //add timeout timeout_function.attach(&print_symbol,0.5); - + } void print_symbol() { char letter; - //convert code into letter - letter = morse2char(global_buffer,global_length); - //display letter - printf("%c", letter); - show_letter_7seg(letter); - //clear buffer - global_buffer = 0; - //clear length - //printf("%d\n",global_length); - global_length = 0; - - //a space is detected - global_code = 2; - //printf("You pressed the key %d\n", global_code); - //letter = '_'; - //print letter - //printf("_"); - - timeout_function.detach(); - timeout_function.attach(&print_word,1.5); - - + //convert code into letter + letter = morse2char(global_buffer,global_length); + //display letter + printf("%c", letter); + show_letter_7seg(letter); + //clear buffer + global_buffer = 0; + //clear length + //printf("%d\n",global_length); + global_length = 0; + //a space is detected + global_code = 2; + + timeout_function.detach(); + timeout_function.attach(&print_word,1.5); } +// This function prints a [space] to signal the end of a word void print_word() { - printf(" "); - } +// Light the 7-Segment Display for a dot or dash (depending on the global_code) void light_7seg() { if (global_code == 1) { @@ -117,9 +107,9 @@ wait(0.2); d = 1; } - } +// This function displays letters on 7-segment LEDs void show_letter_7seg(char letter) { float wait_time = 0.2; @@ -190,24 +180,28 @@ } } +// This function handles interpretation of the key press duration to a Morse Code symbol (ie. dot, dash, space) int hard_limiter(int duration) { int symbol; + // Interpret a dot if (duration >= 30 && duration <= 250){ symbol = 1; } + // Interpret a dash else if (duration > 250) { symbol = 0; } + // Interpret a space else { symbol = -1; } return symbol; - } - +// This function takes in the integer value of morse code and the code word length as inputs, and determines the +// letter or number they represent for char morse2char(int morse, int length) { switch (length) { case 1: