]> git.gir.st - tmk_keyboard.git/blob - usb_mouse.c
add horizontal mouse wheel
[tmk_keyboard.git] / usb_mouse.c
1 #include <avr/interrupt.h>
2 #include <util/delay.h>
3 #include "usb_mouse.h"
4
5
6 // which buttons are currently pressed
7 uint8_t mouse_buttons=0;
8
9 // protocol setting from the host. We use exactly the same report
10 // either way, so this variable only stores the setting since we
11 // are required to be able to report which setting is in use.
12 uint8_t mouse_protocol=1;
13
14
15 // Set the mouse buttons. To create a "click", 2 calls are needed,
16 // one to push the button down and the second to release it
17 int8_t usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right)
18 {
19 uint8_t mask=0;
20
21 if (left) mask |= 1;
22 if (middle) mask |= 4;
23 if (right) mask |= 2;
24 mouse_buttons = mask;
25 return usb_mouse_move(0, 0, 0, 0);
26 }
27
28 // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
29 int8_t usb_mouse_move(int8_t x, int8_t y, int8_t wheel, int8_t hwheel)
30 {
31 uint8_t intr_state, timeout;
32
33 if (!usb_configured()) return -1;
34 if (x == -128) x = -127;
35 if (y == -128) y = -127;
36 if (wheel == -128) wheel = -127;
37 if (hwheel == -128) hwheel = -127;
38 intr_state = SREG;
39 cli();
40 UENUM = MOUSE_ENDPOINT;
41 timeout = UDFNUML + 50;
42 while (1) {
43 // are we ready to transmit?
44 if (UEINTX & (1<<RWAL)) break;
45 SREG = intr_state;
46 // has the USB gone offline?
47 if (!usb_configured()) return -1;
48 // have we waited too long?
49 if (UDFNUML == timeout) return -1;
50 // get ready to try checking again
51 intr_state = SREG;
52 cli();
53 UENUM = MOUSE_ENDPOINT;
54 }
55 UEDATX = mouse_buttons;
56 UEDATX = x;
57 UEDATX = y;
58 UEDATX = wheel;
59 UEDATX = hwheel;
60
61 UEINTX = 0x3A;
62 SREG = intr_state;
63 return 0;
64 }
Imprint / Impressum