]> git.donarmstrong.com Git - tmk_firmware.git/blob - usb_mouse.c
add mouse function.
[tmk_firmware.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);
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)
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         intr_state = SREG;
38         cli();
39         UENUM = MOUSE_ENDPOINT;
40         timeout = UDFNUML + 50;
41         while (1) {
42                 // are we ready to transmit?
43                 if (UEINTX & (1<<RWAL)) break;
44                 SREG = intr_state;
45                 // has the USB gone offline?
46                 if (!usb_configured()) return -1;
47                 // have we waited too long?
48                 if (UDFNUML == timeout) return -1;
49                 // get ready to try checking again
50                 intr_state = SREG;
51                 cli();
52                 UENUM = MOUSE_ENDPOINT;
53         }
54         UEDATX = mouse_buttons;
55         UEDATX = x;
56         UEDATX = y;
57         UEDATX = wheel;
58         UEINTX = 0x3A;
59         SREG = intr_state;
60         return 0;
61 }
62