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
28 static inline uint8_t move_unit(void)
30 uint16_t unit
= 10 + (mousekey_repeat
);
31 return (unit
> 127 ? 127 : unit
);
34 void mousekey_decode(uint8_t code
)
36 if (code
== KB_MS_UP
) report
.y
= -move_unit();
37 else if (code
== KB_MS_DOWN
) report
.y
= move_unit();
38 else if (code
== KB_MS_LEFT
) report
.x
= -move_unit();
39 else if (code
== KB_MS_RIGHT
) report
.x
= move_unit();
40 else if (code
== KB_MS_BTN1
) report
.buttons
|= MOUSE_BTN1
;
41 else if (code
== KB_MS_BTN2
) report
.buttons
|= MOUSE_BTN2
;
42 else if (code
== KB_MS_BTN3
) report
.buttons
|= MOUSE_BTN3
;
44 else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
45 else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
46 else if (code == KB_MS_WH_UP) report.v += 1;
47 else if (code == KB_MS_WH_DOWN) report.v -= 1;
48 else if (code == KB_MS_WH_LEFT) report.h -= 1;
49 else if (code == KB_MS_WH_RIGHT)report.h += 1;
53 bool mousekey_changed(void)
55 return (report
.buttons
!= report_prev
.buttons
||
56 report
.x
!= report_prev
.x
||
57 report
.y
!= report_prev
.y
||
58 report
.x
|| report
.y
);
59 //return (report.buttons != report_prev.buttons || report.x || report.y);
62 void mousekey_send(void)
64 static uint16_t last_timer
= 0;
66 if (!mousekey_changed()) {
71 // send immediately when buttun state is changed
72 if (report
.buttons
== report_prev
.buttons
) {
73 // TODO: delay parameter setting
74 if ((timer_elapsed(last_timer
) < (mousekey_repeat
== 1 ? 20 : 5))) {
79 if (report
.x
&& report
.y
) {
85 print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
86 print("timer: "); phex16(timer_read()); print("\n");
87 print("last_timer: "); phex16(last_timer); print("\n");
88 print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
93 host_mouse_send(&report
);
94 report_prev
.buttons
= report
.buttons
;
95 report_prev
.x
= report
.x
;
96 report_prev
.y
= report
.y
;
97 if (mousekey_repeat
!= 0xFF) mousekey_repeat
++;
98 last_timer
= timer_read();
99 mousekey_clear_report();
102 void mousekey_clear_report(void)
109 static void mousekey_debug(void)
111 if (!debug_mouse
) return;
112 print("mousekey[btn|x y v h]: ");
113 phex(report
.buttons
); print("|");
114 phex(report
.x
); print(" ");
115 phex(report
.y
); print(" ");
117 phex(report.v); print(" ");