Application using FRDM-K64F board to emulate USB keyboard. When one of two buttons (present on the board) are pressed, predefined text is printed (as if it was input with a keyboard).
Fork of frdm_USB-HID-Mouse-Keyboard_auto by
Revision 1:a5ca446fb520, committed 2017-12-18
- Comitter:
- jaknaw
- Date:
- Mon Dec 18 17:33:47 2017 +0000
- Parent:
- 0:92846b9895f1
- Commit message:
- Use FRDM-K64F board to simulate USB keyboard and print predefined text using interrupts when one of two buttons available on the board are pressed.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 92846b9895f1 -r a5ca446fb520 main.cpp --- a/main.cpp Thu Dec 31 17:24:55 2015 +0000 +++ b/main.cpp Mon Dec 18 17:33:47 2017 +0000 @@ -1,21 +1,83 @@ +/* + * SiSK-FRDM-K64F-USB-Keyboard + * + * Application written for "Standardy i Systemy Komunikacyjne" (SiSK) laboratory + * classes conducted at AGH UST, Faculty of Computer Science, Electronics and + * Telecommunications. + * + * This software presents interrupts handling and USB Keyboard emulation using + * FRDM-K64F board. + * + * User is encouradged to change "print_txt_sw2" and "print_txt_sw3" functions, + * in order to define one's own strings that are going to be printed when SW2 + * or SW3 buttons are pressed on FRDM-K64F board. + * + * Important! After programming the FRDM-K64F board, remember to connect it to + * the PC using "K64 USB" port and not "SDA USB" port. + * + * Author: Jakub Nawała <jakub.tadeusz.nawala@gmail.com> + */ + #include "mbed.h" #include "USBMouseKeyboard.h" -DigitalOut led(LED1); -USBMouseKeyboard key_mouse; +/* Create interrupts for handling buttons press events */ +InterruptIn button_sw2(SW2); +InterruptIn button_sw3(SW3); + +/* Create green LED object */ +DigitalOut green_led(LED_GREEN); + +/* Create USB keyboard object */ +USBKeyboard frdm_keyboard; + +/* Define global flags for control of prints connected with button presses */ +bool sw2_pressed = false; +bool sw3_pressed = false; +/* Interrupt handlers for buttons press events */ +void sw2_int_handler() { + sw2_pressed = true; + green_led = !green_led; +} + +void sw3_int_handler() { + sw3_pressed = true; + green_led = !green_led; +} + +/* Functions for printing a text bonded with each button */ +void print_txt_sw2() { + frdm_keyboard.printf("foo\n\r"); +} + +void print_txt_sw3() { + frdm_keyboard.printf("bar\n\r"); +} + +/* Main body of the application */ int main(void) { - while (1) { - key_mouse.move(60, 0); - key_mouse.doubleClick(); - key_mouse.keyCode(KEY_CAPS_LOCK); - key_mouse.printf("hello world from frdm-k64f. \n"); - wait(2); - key_mouse.move(-60, 0); - key_mouse.doubleClick(); - key_mouse.keyCode(KEY_CAPS_LOCK); - key_mouse.printf("hello world from frdm-k64f. \n"); - led = ((key_mouse.lockStatus() & 0x2) >> 1); /* second bit is caps lock */ - wait(2); + /* Initialize LED */ + green_led = 0; + + /* Setup interrupts for falling edge detected on the buttons pins */ + button_sw2.fall(&sw2_int_handler); + button_sw3.fall(&sw3_int_handler); + + /* Check what button was pressed and print appropriate text */ + while (true) { + if(sw2_pressed) { + print_txt_sw2(); + sw2_pressed = false; + } + if(sw3_pressed) { + print_txt_sw3(); + sw3_pressed = false; + } + + /* Wait for interrupts */ + wait(0.5f); } + + /* This place shall never be reached! */ }