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