Samuel Mokrani
/
USBHostHub_HelloWorld
USBHostHub Hello World
main.cpp@0:b755444e7ddd, 2013-03-12 (annotated)
- Committer:
- samux
- Date:
- Tue Mar 12 17:39:44 2013 +0000
- Revision:
- 0:b755444e7ddd
- Child:
- 1:b6239a365c4a
USBHostHub Hello World
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:b755444e7ddd | 1 | #include "mbed.h" |
samux | 0:b755444e7ddd | 2 | #include "USBHostKeyboard.h" |
samux | 0:b755444e7ddd | 3 | #include "USBHostMouse.h" |
samux | 0:b755444e7ddd | 4 | |
samux | 0:b755444e7ddd | 5 | DigitalOut led(LED1); |
samux | 0:b755444e7ddd | 6 | |
samux | 0:b755444e7ddd | 7 | void onKey(uint8_t key) { |
samux | 0:b755444e7ddd | 8 | printf("Key: %c\r\n", key); |
samux | 0:b755444e7ddd | 9 | } |
samux | 0:b755444e7ddd | 10 | |
samux | 0:b755444e7ddd | 11 | void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) { |
samux | 0:b755444e7ddd | 12 | printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z); |
samux | 0:b755444e7ddd | 13 | } |
samux | 0:b755444e7ddd | 14 | |
samux | 0:b755444e7ddd | 15 | void keyboard_task(void const *) { |
samux | 0:b755444e7ddd | 16 | |
samux | 0:b755444e7ddd | 17 | USBHostKeyboard keyboard; |
samux | 0:b755444e7ddd | 18 | |
samux | 0:b755444e7ddd | 19 | while(1) { |
samux | 0:b755444e7ddd | 20 | // try to connect a USB keyboard |
samux | 0:b755444e7ddd | 21 | while(!keyboard.connect()) |
samux | 0:b755444e7ddd | 22 | Thread::wait(500); |
samux | 0:b755444e7ddd | 23 | |
samux | 0:b755444e7ddd | 24 | // when connected, attach handler called on keyboard event |
samux | 0:b755444e7ddd | 25 | keyboard.attach(onKey); |
samux | 0:b755444e7ddd | 26 | |
samux | 0:b755444e7ddd | 27 | // wait until the keyboard is disconnected |
samux | 0:b755444e7ddd | 28 | while(keyboard.connected()) |
samux | 0:b755444e7ddd | 29 | Thread::wait(500); |
samux | 0:b755444e7ddd | 30 | } |
samux | 0:b755444e7ddd | 31 | } |
samux | 0:b755444e7ddd | 32 | |
samux | 0:b755444e7ddd | 33 | void mouse_task(void const *) { |
samux | 0:b755444e7ddd | 34 | |
samux | 0:b755444e7ddd | 35 | USBHostMouse mouse; |
samux | 0:b755444e7ddd | 36 | |
samux | 0:b755444e7ddd | 37 | while(1) { |
samux | 0:b755444e7ddd | 38 | // try to connect a USB mouse |
samux | 0:b755444e7ddd | 39 | while(!mouse.connect()) |
samux | 0:b755444e7ddd | 40 | Thread::wait(500); |
samux | 0:b755444e7ddd | 41 | |
samux | 0:b755444e7ddd | 42 | // when connected, attach handler called on mouse event |
samux | 0:b755444e7ddd | 43 | mouse.attach(onMouseEvent); |
samux | 0:b755444e7ddd | 44 | |
samux | 0:b755444e7ddd | 45 | // wait until the mouse is disconnected |
samux | 0:b755444e7ddd | 46 | while(mouse.connected()) |
samux | 0:b755444e7ddd | 47 | Thread::wait(500); |
samux | 0:b755444e7ddd | 48 | } |
samux | 0:b755444e7ddd | 49 | } |
samux | 0:b755444e7ddd | 50 | |
samux | 0:b755444e7ddd | 51 | int main() { |
samux | 0:b755444e7ddd | 52 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
samux | 0:b755444e7ddd | 53 | Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4); |
samux | 0:b755444e7ddd | 54 | while(1) { |
samux | 0:b755444e7ddd | 55 | led=!led; |
samux | 0:b755444e7ddd | 56 | Thread::wait(500); |
samux | 0:b755444e7ddd | 57 | } |
samux | 0:b755444e7ddd | 58 | } |