![](/media/cache/profiles/ndu.jpg.50x50_q85.jpg)
keyboard input, serial com test
Dependencies: mbed DISCO_L475VG_IOT01A_wifi TextLCD USBHost
src/input.cpp@6:9d975a9d2728, 2019-08-15 (annotated)
- Committer:
- duchonic
- Date:
- Thu Aug 15 19:33:04 2019 +0000
- Revision:
- 6:9d975a9d2728
- Parent:
- 5:0466445897b8
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duchonic | 5:0466445897b8 | 1 | #include "mbed.h" |
duchonic | 5:0466445897b8 | 2 | #include "USBHostKeyboard.h" |
duchonic | 5:0466445897b8 | 3 | #include "inc/display.h" |
duchonic | 5:0466445897b8 | 4 | |
duchonic | 5:0466445897b8 | 5 | AnalogIn button(A0); // Init button (SELECT, LEFT, UP, DOWN, RIGHT) |
duchonic | 5:0466445897b8 | 6 | |
duchonic | 5:0466445897b8 | 7 | uint8_t lastKey; |
duchonic | 5:0466445897b8 | 8 | |
duchonic | 5:0466445897b8 | 9 | void onKey(uint8_t key) { |
duchonic | 5:0466445897b8 | 10 | lastKey = key; |
duchonic | 5:0466445897b8 | 11 | displayKey(key); |
duchonic | 5:0466445897b8 | 12 | printf("key:%c\n\r", key); |
duchonic | 5:0466445897b8 | 13 | } |
duchonic | 5:0466445897b8 | 14 | |
duchonic | 5:0466445897b8 | 15 | |
duchonic | 5:0466445897b8 | 16 | void keyboard_task(void const *) { |
duchonic | 5:0466445897b8 | 17 | |
duchonic | 5:0466445897b8 | 18 | USBHostKeyboard keyboard; |
duchonic | 5:0466445897b8 | 19 | |
duchonic | 5:0466445897b8 | 20 | while(1) { |
duchonic | 5:0466445897b8 | 21 | // try to connect a USB keyboard |
duchonic | 5:0466445897b8 | 22 | while(!keyboard.connect()) |
duchonic | 5:0466445897b8 | 23 | Thread::wait(500); |
duchonic | 5:0466445897b8 | 24 | |
duchonic | 5:0466445897b8 | 25 | printLine("connected",0); |
duchonic | 5:0466445897b8 | 26 | // when connected, attach handler called on keyboard event |
duchonic | 5:0466445897b8 | 27 | keyboard.attach(onKey); |
duchonic | 5:0466445897b8 | 28 | |
duchonic | 5:0466445897b8 | 29 | // wait until the keyboard is disconnected |
duchonic | 5:0466445897b8 | 30 | while(keyboard.connected()) |
duchonic | 5:0466445897b8 | 31 | Thread::wait(500); |
duchonic | 5:0466445897b8 | 32 | printLine("disconnected",0); |
duchonic | 5:0466445897b8 | 33 | } |
duchonic | 5:0466445897b8 | 34 | } |
duchonic | 5:0466445897b8 | 35 | |
duchonic | 5:0466445897b8 | 36 | void inputInit(void) { |
duchonic | 5:0466445897b8 | 37 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
duchonic | 5:0466445897b8 | 38 | printf("keyboard input thread created\n\r"); |
duchonic | 5:0466445897b8 | 39 | while(1) { |
duchonic | 5:0466445897b8 | 40 | Thread::wait(500); |
duchonic | 5:0466445897b8 | 41 | } |
duchonic | 5:0466445897b8 | 42 | } |
duchonic | 5:0466445897b8 | 43 | |
duchonic | 5:0466445897b8 | 44 | uint8_t getKey(void){ |
duchonic | 5:0466445897b8 | 45 | return lastKey; |
duchonic | 5:0466445897b8 | 46 | } |