usb mouse input
Dependencies: mbed TextLCD USBHost
main.cpp@3:eac680851b92, 2019-07-26 (annotated)
- Committer:
- duchonic
- Date:
- Fri Jul 26 03:34:02 2019 +0000
- Revision:
- 3:eac680851b92
- Parent:
- 2:c1f316e932bc
added lcd shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duchonic | 0:6eeb987cb865 | 1 | #include "mbed.h" |
duchonic | 0:6eeb987cb865 | 2 | #include "USBHostMouse.h" |
duchonic | 2:c1f316e932bc | 3 | #include "TextLCD.h" |
duchonic | 0:6eeb987cb865 | 4 | |
duchonic | 0:6eeb987cb865 | 5 | DigitalOut led1(LED1); |
duchonic | 2:c1f316e932bc | 6 | Serial pc(SERIAL_TX, SERIAL_RX); |
duchonic | 2:c1f316e932bc | 7 | AnalogIn button(A0); // Init button (SELECT, LEFT, UP, DOWN, RIGHT) |
duchonic | 2:c1f316e932bc | 8 | // LCD (RS, E, D4, D5, D6, D7); |
duchonic | 2:c1f316e932bc | 9 | TextLCD lcd(D8, D9, D4, D5, D6, D7); |
duchonic | 2:c1f316e932bc | 10 | PwmOut backlight(D10); // Backlight LCD |
duchonic | 0:6eeb987cb865 | 11 | |
duchonic | 0:6eeb987cb865 | 12 | void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) |
duchonic | 0:6eeb987cb865 | 13 | { |
duchonic | 2:c1f316e932bc | 14 | static int buttonState = 0; |
duchonic | 2:c1f316e932bc | 15 | |
duchonic | 2:c1f316e932bc | 16 | if (buttons != buttonState){ |
duchonic | 2:c1f316e932bc | 17 | buttonState = buttons; |
duchonic | 2:c1f316e932bc | 18 | |
duchonic | 2:c1f316e932bc | 19 | lcd.cls(); // Clear LCD |
duchonic | 2:c1f316e932bc | 20 | lcd.locate(1,0); // Set locate (1 row, 2 column) |
duchonic | 2:c1f316e932bc | 21 | printf("Buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z); |
duchonic | 2:c1f316e932bc | 22 | lcd.printf("Buttons: %d", buttons); |
duchonic | 2:c1f316e932bc | 23 | } |
duchonic | 0:6eeb987cb865 | 24 | } |
duchonic | 0:6eeb987cb865 | 25 | |
duchonic | 0:6eeb987cb865 | 26 | void mouse_task(void const *) |
duchonic | 0:6eeb987cb865 | 27 | { |
duchonic | 0:6eeb987cb865 | 28 | USBHostMouse mouse; |
duchonic | 0:6eeb987cb865 | 29 | |
duchonic | 0:6eeb987cb865 | 30 | printf("Mouse started\r\n"); |
duchonic | 0:6eeb987cb865 | 31 | |
duchonic | 0:6eeb987cb865 | 32 | while(1) { |
duchonic | 0:6eeb987cb865 | 33 | |
duchonic | 0:6eeb987cb865 | 34 | // Try to connect a USB mouse |
duchonic | 0:6eeb987cb865 | 35 | while(!mouse.connect()) { |
duchonic | 0:6eeb987cb865 | 36 | Thread::wait(500); |
duchonic | 0:6eeb987cb865 | 37 | } |
duchonic | 0:6eeb987cb865 | 38 | |
duchonic | 0:6eeb987cb865 | 39 | // When connected, attach handler called on mouse event |
duchonic | 0:6eeb987cb865 | 40 | mouse.attachEvent(onMouseEvent); |
duchonic | 0:6eeb987cb865 | 41 | |
duchonic | 0:6eeb987cb865 | 42 | // Wait until the mouse is disconnected |
duchonic | 0:6eeb987cb865 | 43 | while(mouse.connected()) { |
duchonic | 0:6eeb987cb865 | 44 | Thread::wait(500); |
duchonic | 0:6eeb987cb865 | 45 | } |
duchonic | 0:6eeb987cb865 | 46 | |
duchonic | 0:6eeb987cb865 | 47 | printf("Mouse disconnected\r\n"); |
duchonic | 0:6eeb987cb865 | 48 | } |
duchonic | 0:6eeb987cb865 | 49 | } |
duchonic | 0:6eeb987cb865 | 50 | |
duchonic | 0:6eeb987cb865 | 51 | int main() |
duchonic | 0:6eeb987cb865 | 52 | { |
duchonic | 2:c1f316e932bc | 53 | // Set backlight period and duty cycle |
duchonic | 2:c1f316e932bc | 54 | backlight.period(0.002); |
duchonic | 2:c1f316e932bc | 55 | backlight = 1; |
duchonic | 2:c1f316e932bc | 56 | |
duchonic | 2:c1f316e932bc | 57 | |
duchonic | 2:c1f316e932bc | 58 | lcd.cls(); // Clear LCD |
duchonic | 2:c1f316e932bc | 59 | lcd.locate(1,0); // Set locate (1 row, 2 column) |
duchonic | 2:c1f316e932bc | 60 | lcd.printf("LCD Key Shield"); |
duchonic | 2:c1f316e932bc | 61 | wait(1); |
duchonic | 2:c1f316e932bc | 62 | |
duchonic | 2:c1f316e932bc | 63 | |
duchonic | 0:6eeb987cb865 | 64 | Thread mouseTask(mouse_task, NULL, osPriorityNormal, 1024* 4); |
duchonic | 0:6eeb987cb865 | 65 | while(1) { |
duchonic | 0:6eeb987cb865 | 66 | led1 = !led1; |
duchonic | 2:c1f316e932bc | 67 | Thread::wait(100); |
duchonic | 0:6eeb987cb865 | 68 | } |
duchonic | 0:6eeb987cb865 | 69 | } |