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
main.cpp@1:a5ca446fb520, 2017-12-18 (annotated)
- Committer:
- jaknaw
- Date:
- Mon Dec 18 17:33:47 2017 +0000
- Revision:
- 1:a5ca446fb520
- Parent:
- 0:92846b9895f1
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.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaknaw | 1:a5ca446fb520 | 1 | /* |
jaknaw | 1:a5ca446fb520 | 2 | * SiSK-FRDM-K64F-USB-Keyboard |
jaknaw | 1:a5ca446fb520 | 3 | * |
jaknaw | 1:a5ca446fb520 | 4 | * Application written for "Standardy i Systemy Komunikacyjne" (SiSK) laboratory |
jaknaw | 1:a5ca446fb520 | 5 | * classes conducted at AGH UST, Faculty of Computer Science, Electronics and |
jaknaw | 1:a5ca446fb520 | 6 | * Telecommunications. |
jaknaw | 1:a5ca446fb520 | 7 | * |
jaknaw | 1:a5ca446fb520 | 8 | * This software presents interrupts handling and USB Keyboard emulation using |
jaknaw | 1:a5ca446fb520 | 9 | * FRDM-K64F board. |
jaknaw | 1:a5ca446fb520 | 10 | * |
jaknaw | 1:a5ca446fb520 | 11 | * User is encouradged to change "print_txt_sw2" and "print_txt_sw3" functions, |
jaknaw | 1:a5ca446fb520 | 12 | * in order to define one's own strings that are going to be printed when SW2 |
jaknaw | 1:a5ca446fb520 | 13 | * or SW3 buttons are pressed on FRDM-K64F board. |
jaknaw | 1:a5ca446fb520 | 14 | * |
jaknaw | 1:a5ca446fb520 | 15 | * Important! After programming the FRDM-K64F board, remember to connect it to |
jaknaw | 1:a5ca446fb520 | 16 | * the PC using "K64 USB" port and not "SDA USB" port. |
jaknaw | 1:a5ca446fb520 | 17 | * |
jaknaw | 1:a5ca446fb520 | 18 | * Author: Jakub Nawała <jakub.tadeusz.nawala@gmail.com> |
jaknaw | 1:a5ca446fb520 | 19 | */ |
jaknaw | 1:a5ca446fb520 | 20 | |
GregC | 0:92846b9895f1 | 21 | #include "mbed.h" |
GregC | 0:92846b9895f1 | 22 | #include "USBMouseKeyboard.h" |
GregC | 0:92846b9895f1 | 23 | |
jaknaw | 1:a5ca446fb520 | 24 | /* Create interrupts for handling buttons press events */ |
jaknaw | 1:a5ca446fb520 | 25 | InterruptIn button_sw2(SW2); |
jaknaw | 1:a5ca446fb520 | 26 | InterruptIn button_sw3(SW3); |
jaknaw | 1:a5ca446fb520 | 27 | |
jaknaw | 1:a5ca446fb520 | 28 | /* Create green LED object */ |
jaknaw | 1:a5ca446fb520 | 29 | DigitalOut green_led(LED_GREEN); |
jaknaw | 1:a5ca446fb520 | 30 | |
jaknaw | 1:a5ca446fb520 | 31 | /* Create USB keyboard object */ |
jaknaw | 1:a5ca446fb520 | 32 | USBKeyboard frdm_keyboard; |
jaknaw | 1:a5ca446fb520 | 33 | |
jaknaw | 1:a5ca446fb520 | 34 | /* Define global flags for control of prints connected with button presses */ |
jaknaw | 1:a5ca446fb520 | 35 | bool sw2_pressed = false; |
jaknaw | 1:a5ca446fb520 | 36 | bool sw3_pressed = false; |
GregC | 0:92846b9895f1 | 37 | |
jaknaw | 1:a5ca446fb520 | 38 | /* Interrupt handlers for buttons press events */ |
jaknaw | 1:a5ca446fb520 | 39 | void sw2_int_handler() { |
jaknaw | 1:a5ca446fb520 | 40 | sw2_pressed = true; |
jaknaw | 1:a5ca446fb520 | 41 | green_led = !green_led; |
jaknaw | 1:a5ca446fb520 | 42 | } |
jaknaw | 1:a5ca446fb520 | 43 | |
jaknaw | 1:a5ca446fb520 | 44 | void sw3_int_handler() { |
jaknaw | 1:a5ca446fb520 | 45 | sw3_pressed = true; |
jaknaw | 1:a5ca446fb520 | 46 | green_led = !green_led; |
jaknaw | 1:a5ca446fb520 | 47 | } |
jaknaw | 1:a5ca446fb520 | 48 | |
jaknaw | 1:a5ca446fb520 | 49 | /* Functions for printing a text bonded with each button */ |
jaknaw | 1:a5ca446fb520 | 50 | void print_txt_sw2() { |
jaknaw | 1:a5ca446fb520 | 51 | frdm_keyboard.printf("foo\n\r"); |
jaknaw | 1:a5ca446fb520 | 52 | } |
jaknaw | 1:a5ca446fb520 | 53 | |
jaknaw | 1:a5ca446fb520 | 54 | void print_txt_sw3() { |
jaknaw | 1:a5ca446fb520 | 55 | frdm_keyboard.printf("bar\n\r"); |
jaknaw | 1:a5ca446fb520 | 56 | } |
jaknaw | 1:a5ca446fb520 | 57 | |
jaknaw | 1:a5ca446fb520 | 58 | /* Main body of the application */ |
GregC | 0:92846b9895f1 | 59 | int main(void) { |
jaknaw | 1:a5ca446fb520 | 60 | /* Initialize LED */ |
jaknaw | 1:a5ca446fb520 | 61 | green_led = 0; |
jaknaw | 1:a5ca446fb520 | 62 | |
jaknaw | 1:a5ca446fb520 | 63 | /* Setup interrupts for falling edge detected on the buttons pins */ |
jaknaw | 1:a5ca446fb520 | 64 | button_sw2.fall(&sw2_int_handler); |
jaknaw | 1:a5ca446fb520 | 65 | button_sw3.fall(&sw3_int_handler); |
jaknaw | 1:a5ca446fb520 | 66 | |
jaknaw | 1:a5ca446fb520 | 67 | /* Check what button was pressed and print appropriate text */ |
jaknaw | 1:a5ca446fb520 | 68 | while (true) { |
jaknaw | 1:a5ca446fb520 | 69 | if(sw2_pressed) { |
jaknaw | 1:a5ca446fb520 | 70 | print_txt_sw2(); |
jaknaw | 1:a5ca446fb520 | 71 | sw2_pressed = false; |
jaknaw | 1:a5ca446fb520 | 72 | } |
jaknaw | 1:a5ca446fb520 | 73 | if(sw3_pressed) { |
jaknaw | 1:a5ca446fb520 | 74 | print_txt_sw3(); |
jaknaw | 1:a5ca446fb520 | 75 | sw3_pressed = false; |
jaknaw | 1:a5ca446fb520 | 76 | } |
jaknaw | 1:a5ca446fb520 | 77 | |
jaknaw | 1:a5ca446fb520 | 78 | /* Wait for interrupts */ |
jaknaw | 1:a5ca446fb520 | 79 | wait(0.5f); |
GregC | 0:92846b9895f1 | 80 | } |
jaknaw | 1:a5ca446fb520 | 81 | |
jaknaw | 1:a5ca446fb520 | 82 | /* This place shall never be reached! */ |
GregC | 0:92846b9895f1 | 83 | } |