Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBHostKeyboard_HelloWorld by
main.cpp@8:f8122ca2f58f, 2015-03-27 (annotated)
- Committer:
- armdran
- Date:
- Fri Mar 27 14:44:09 2015 +0000
- Revision:
- 8:f8122ca2f58f
- Parent:
- 7:be82ac3c4821
- Child:
- 9:8bcd70b26084
we receive interrupts!
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| samux | 0:88d52e197201 | 1 | #include "mbed.h" |
| samux | 0:88d52e197201 | 2 | #include "USBHostKeyboard.h" |
| wlangenkamp | 7:be82ac3c4821 | 3 | #include "rtos.h" |
| samux | 0:88d52e197201 | 4 | |
| samux | 0:88d52e197201 | 5 | DigitalOut led(LED1); |
| samux | 0:88d52e197201 | 6 | |
| samux | 0:88d52e197201 | 7 | void onKey(uint8_t key) { |
| samux | 1:2063e3713a99 | 8 | printf("Key: %c\r\n", key); |
| samux | 0:88d52e197201 | 9 | } |
| samux | 0:88d52e197201 | 10 | |
| armdran | 8:f8122ca2f58f | 11 | void onKeyMod(uint8_t key, uint8_t modifier) { |
| armdran | 8:f8122ca2f58f | 12 | printf("Key: %c\r\n", key); |
| armdran | 8:f8122ca2f58f | 13 | } |
| armdran | 8:f8122ca2f58f | 14 | |
| armdran | 8:f8122ca2f58f | 15 | |
| samux | 0:88d52e197201 | 16 | void keyboard_task(void const *) { |
| samux | 0:88d52e197201 | 17 | |
| samux | 0:88d52e197201 | 18 | USBHostKeyboard keyboard; |
| samux | 0:88d52e197201 | 19 | |
| samux | 0:88d52e197201 | 20 | while(1) { |
| armdran | 8:f8122ca2f58f | 21 | |
| armdran | 8:f8122ca2f58f | 22 | printf("trying to connect\r\n"); |
| armdran | 8:f8122ca2f58f | 23 | |
| samux | 0:88d52e197201 | 24 | // try to connect a USB keyboard |
| armdran | 8:f8122ca2f58f | 25 | while(!keyboard.connect()) { |
| samux | 0:88d52e197201 | 26 | Thread::wait(500); |
| armdran | 8:f8122ca2f58f | 27 | } |
| armdran | 8:f8122ca2f58f | 28 | |
| armdran | 8:f8122ca2f58f | 29 | printf("connected\r\n"); |
| armdran | 8:f8122ca2f58f | 30 | |
| samux | 0:88d52e197201 | 31 | |
| samux | 0:88d52e197201 | 32 | // when connected, attach handler called on keyboard event |
| samux | 0:88d52e197201 | 33 | keyboard.attach(onKey); |
| armdran | 8:f8122ca2f58f | 34 | keyboard.attach(onKeyMod); |
| armdran | 8:f8122ca2f58f | 35 | |
| armdran | 8:f8122ca2f58f | 36 | printf("eventhandler attached\r\n"); |
| samux | 0:88d52e197201 | 37 | |
| samux | 0:88d52e197201 | 38 | // wait until the keyboard is disconnected |
| armdran | 8:f8122ca2f58f | 39 | while(keyboard.connected()) { |
| armdran | 8:f8122ca2f58f | 40 | USBHost::poll(); |
| armdran | 8:f8122ca2f58f | 41 | } |
| armdran | 8:f8122ca2f58f | 42 | |
| armdran | 8:f8122ca2f58f | 43 | printf("disconnected\r\n"); |
| armdran | 8:f8122ca2f58f | 44 | |
| samux | 0:88d52e197201 | 45 | } |
| samux | 0:88d52e197201 | 46 | } |
| samux | 0:88d52e197201 | 47 | |
| samux | 0:88d52e197201 | 48 | int main() { |
| samux | 0:88d52e197201 | 49 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
| armdran | 8:f8122ca2f58f | 50 | |
| samux | 0:88d52e197201 | 51 | while(1) { |
| samux | 0:88d52e197201 | 52 | led=!led; |
| armdran | 8:f8122ca2f58f | 53 | Thread::wait(2000); |
| samux | 0:88d52e197201 | 54 | } |
| samux | 0:88d52e197201 | 55 | } |
