usb mouse input

Dependencies:   mbed TextLCD USBHost

Committer:
duchonic
Date:
Wed Jul 24 18:30:59 2019 +0000
Revision:
0:6eeb987cb865
Child:
1:643faa48282f
first

Who changed what in which revision?

UserRevisionLine numberNew contents of line
duchonic 0:6eeb987cb865 1 #include "mbed.h"
duchonic 0:6eeb987cb865 2 #include "USBHostMouse.h"
duchonic 0:6eeb987cb865 3
duchonic 0:6eeb987cb865 4 DigitalOut led1(LED1);
duchonic 0:6eeb987cb865 5 DigitalOut led2(LED2);
duchonic 0:6eeb987cb865 6 DigitalOut led3(LED3);
duchonic 0:6eeb987cb865 7
duchonic 0:6eeb987cb865 8 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z)
duchonic 0:6eeb987cb865 9 {
duchonic 0:6eeb987cb865 10 printf("Buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
duchonic 0:6eeb987cb865 11 }
duchonic 0:6eeb987cb865 12
duchonic 0:6eeb987cb865 13 void mouse_task(void const *)
duchonic 0:6eeb987cb865 14 {
duchonic 0:6eeb987cb865 15 USBHostMouse mouse;
duchonic 0:6eeb987cb865 16
duchonic 0:6eeb987cb865 17 printf("Mouse started\r\n");
duchonic 0:6eeb987cb865 18
duchonic 0:6eeb987cb865 19 while(1) {
duchonic 0:6eeb987cb865 20
duchonic 0:6eeb987cb865 21 // Try to connect a USB mouse
duchonic 0:6eeb987cb865 22 while(!mouse.connect()) {
duchonic 0:6eeb987cb865 23 Thread::wait(500);
duchonic 0:6eeb987cb865 24 }
duchonic 0:6eeb987cb865 25
duchonic 0:6eeb987cb865 26 // When connected, attach handler called on mouse event
duchonic 0:6eeb987cb865 27 mouse.attachEvent(onMouseEvent);
duchonic 0:6eeb987cb865 28
duchonic 0:6eeb987cb865 29 // Wait until the mouse is disconnected
duchonic 0:6eeb987cb865 30 while(mouse.connected()) {
duchonic 0:6eeb987cb865 31 Thread::wait(500);
duchonic 0:6eeb987cb865 32 }
duchonic 0:6eeb987cb865 33
duchonic 0:6eeb987cb865 34 printf("Mouse disconnected\r\n");
duchonic 0:6eeb987cb865 35 }
duchonic 0:6eeb987cb865 36 }
duchonic 0:6eeb987cb865 37
duchonic 0:6eeb987cb865 38 int main()
duchonic 0:6eeb987cb865 39 {
duchonic 0:6eeb987cb865 40 Thread mouseTask(mouse_task, NULL, osPriorityNormal, 1024* 4);
duchonic 0:6eeb987cb865 41 while(1) {
duchonic 0:6eeb987cb865 42 led1 = !led1;
duchonic 0:6eeb987cb865 43 led2 = !led2;
duchonic 0:6eeb987cb865 44 led3 = !led3;
duchonic 0:6eeb987cb865 45 Thread::wait(500);
duchonic 0:6eeb987cb865 46 }
duchonic 0:6eeb987cb865 47 }