2 #include <util/delay.h>
3 #include "usb_keycodes.h"
11 static report_mouse_t report
;
12 static report_mouse_t report_prev
;
14 static uint8_t mousekey_repeat
= 0;
16 static void mousekey_debug(void);
20 * TODO: fix acceleration algorithm
21 * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
23 #ifndef MOUSEKEY_DELAY_TIME
24 # define MOUSEKEY_DELAY_TIME 255
27 // acceleration parameters
28 uint8_t mousekey_move_unit
= 2;
29 uint8_t mousekey_resolution
= 5;
32 static inline uint8_t move_unit(void)
34 uint16_t unit
= 5 + mousekey_repeat
*2;
35 return (unit
> 63 ? 63 : unit
);
38 void mousekey_decode(uint8_t code
)
40 if (code
== KB_MS_UP
) report
.y
= -move_unit();
41 else if (code
== KB_MS_DOWN
) report
.y
= move_unit();
42 else if (code
== KB_MS_LEFT
) report
.x
= -move_unit();
43 else if (code
== KB_MS_RIGHT
) report
.x
= move_unit();
44 else if (code
== KB_MS_BTN1
) report
.buttons
|= MOUSE_BTN1
;
45 else if (code
== KB_MS_BTN2
) report
.buttons
|= MOUSE_BTN2
;
46 else if (code
== KB_MS_BTN3
) report
.buttons
|= MOUSE_BTN3
;
47 else if (code
== KB_MS_BTN4
) report
.buttons
|= MOUSE_BTN4
;
48 else if (code
== KB_MS_BTN5
) report
.buttons
|= MOUSE_BTN5
;
49 else if (code
== KB_MS_WH_UP
) report
.v
+= 1;
50 else if (code
== KB_MS_WH_DOWN
) report
.v
-= 1;
51 else if (code
== KB_MS_WH_LEFT
) report
.h
-= 1;
52 else if (code
== KB_MS_WH_RIGHT
)report
.h
+= 1;
55 bool mousekey_changed(void)
57 return (report
.buttons
!= report_prev
.buttons
||
58 report
.x
|| report
.y
|| report
.v
|| report
.h
);
61 void mousekey_send(void)
63 static uint16_t last_timer
= 0;
65 if (!mousekey_changed()) {
67 mousekey_clear_report();
71 // send immediately when buttun state is changed
72 if (report
.buttons
== report_prev
.buttons
) {
73 if (timer_elapsed(last_timer
) < 5) {
74 mousekey_clear_report();
79 if (mousekey_repeat
!= 0xFF) {
83 if (report
.x
&& report
.y
) {
89 host_mouse_send(&report
);
91 last_timer
= timer_read();
92 mousekey_clear_report();
95 void mousekey_clear_report(void)
104 static void mousekey_debug(void)
106 if (!debug_mouse
) return;
107 print("mousekey[btn|x y v h]: ");
108 phex(report
.buttons
); print("|");
109 phex(report
.x
); print(" ");
110 phex(report
.y
); print(" ");
111 phex(report
.v
); print(" ");
113 phex(mousekey_repeat
);