USBHostGamepad library usage example
Dependencies: USBHostGamepad mbed
main.cpp@0:e0b65c2c3cc8, 2017-12-20 (annotated)
- Committer:
- abougouffa
- Date:
- Wed Dec 20 11:19:41 2017 +0000
- Revision:
- 0:e0b65c2c3cc8
USBHostGamepad example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abougouffa | 0:e0b65c2c3cc8 | 1 | #include "mbed.h" |
abougouffa | 0:e0b65c2c3cc8 | 2 | #include "USBHostGamepad.h" |
abougouffa | 0:e0b65c2c3cc8 | 3 | |
abougouffa | 0:e0b65c2c3cc8 | 4 | DigitalOut led(LED3); |
abougouffa | 0:e0b65c2c3cc8 | 5 | |
abougouffa | 0:e0b65c2c3cc8 | 6 | void onGamepadEvent(uint8_t x, uint8_t y, uint8_t z, uint8_t rz, uint16_t buttons) { |
abougouffa | 0:e0b65c2c3cc8 | 7 | printf("x: %02X, y: %02X, z: %02X, rz: %02X, buttons: %04X\r\n", |
abougouffa | 0:e0b65c2c3cc8 | 8 | x, y, z, rz, buttons); |
abougouffa | 0:e0b65c2c3cc8 | 9 | } |
abougouffa | 0:e0b65c2c3cc8 | 10 | |
abougouffa | 0:e0b65c2c3cc8 | 11 | void gamepad_task() { |
abougouffa | 0:e0b65c2c3cc8 | 12 | USBHostGamepad gamepad; |
abougouffa | 0:e0b65c2c3cc8 | 13 | |
abougouffa | 0:e0b65c2c3cc8 | 14 | while(1) { |
abougouffa | 0:e0b65c2c3cc8 | 15 | // try to connect a USB Gamepad |
abougouffa | 0:e0b65c2c3cc8 | 16 | while(!gamepad.connect()) |
abougouffa | 0:e0b65c2c3cc8 | 17 | Thread::wait(500); |
abougouffa | 0:e0b65c2c3cc8 | 18 | |
abougouffa | 0:e0b65c2c3cc8 | 19 | // when connected, attach handler called on Gamepad event |
abougouffa | 0:e0b65c2c3cc8 | 20 | gamepad.attachEvent(onGamepadEvent); |
abougouffa | 0:e0b65c2c3cc8 | 21 | |
abougouffa | 0:e0b65c2c3cc8 | 22 | // wait until the Gamepad is disconnected |
abougouffa | 0:e0b65c2c3cc8 | 23 | while(gamepad.connected()) |
abougouffa | 0:e0b65c2c3cc8 | 24 | Thread::wait(500); |
abougouffa | 0:e0b65c2c3cc8 | 25 | } |
abougouffa | 0:e0b65c2c3cc8 | 26 | } |
abougouffa | 0:e0b65c2c3cc8 | 27 | |
abougouffa | 0:e0b65c2c3cc8 | 28 | int main() { |
abougouffa | 0:e0b65c2c3cc8 | 29 | Thread gamepadTask(osPriorityNormal, 1024 * 4); |
abougouffa | 0:e0b65c2c3cc8 | 30 | gamepadTask.start(gamepad_task); |
abougouffa | 0:e0b65c2c3cc8 | 31 | |
abougouffa | 0:e0b65c2c3cc8 | 32 | printf("MBED USB GAMEPAD\n\r"); |
abougouffa | 0:e0b65c2c3cc8 | 33 | |
abougouffa | 0:e0b65c2c3cc8 | 34 | while(1) { |
abougouffa | 0:e0b65c2c3cc8 | 35 | led=!led; |
abougouffa | 0:e0b65c2c3cc8 | 36 | Thread::wait(500); |
abougouffa | 0:e0b65c2c3cc8 | 37 | } |
abougouffa | 0:e0b65c2c3cc8 | 38 | } |