2 #include <util/delay.h>
3 #include "usb_keycodes.h"
10 static report_mouse_t report;
11 static report_mouse_t report_prev;
13 static uint8_t mousekey_repeat = 0;
17 * TODO: fix acceleration algorithm
18 * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
20 #ifndef MOUSEKEY_DELAY_TIME
21 # define MOUSEKEY_DELAY_TIME 255
25 static inline uint8_t move_unit(void)
27 uint8_t unit = (10 + (mousekey_repeat));
28 return unit > 127 ? 127 : unit;
31 void mousekey_decode(uint8_t code)
33 if (code == KB_MS_UP) report.y -= move_unit();
34 else if (code == KB_MS_DOWN) report.y += move_unit();
35 else if (code == KB_MS_LEFT) report.x -= move_unit();
36 else if (code == KB_MS_RIGHT) report.x += move_unit();
37 else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
38 else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
39 else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
41 else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
42 else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
43 else if (code == KB_MS_WH_UP) report.v += 1;
44 else if (code == KB_MS_WH_DOWN) report.v -= 1;
45 else if (code == KB_MS_WH_LEFT) report.h -= 1;
46 else if (code == KB_MS_WH_RIGHT)report.h += 1;
50 bool mousekey_changed(void)
52 return (report.buttons != report_prev.buttons ||
53 report.x != report_prev.x ||
54 report.y != report_prev.y ||
55 report.x || report.y);
56 //return (report.buttons != report_prev.buttons || report.x || report.y);
59 void mousekey_send(void)
61 static uint16_t last_timer = 0;
63 if (!mousekey_changed()) {
68 // send immediately when buttun state is changed
69 if (report.buttons == report_prev.buttons) {
70 // TODO: delay parameter setting
71 if ((timer_elapsed(last_timer) < (mousekey_repeat == 1 ? 20 : 5))) {
76 if (report.x && report.y) {
82 print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
83 print("timer: "); phex16(timer_read()); print("\n");
84 print("last_timer: "); phex16(last_timer); print("\n");
85 print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
88 host_mouse_send(&report);
89 report_prev.buttons = report.buttons;
90 report_prev.x = report.x;
91 report_prev.y = report.y;
92 if (mousekey_repeat != 0xFF) mousekey_repeat++;
93 last_timer = timer_read();
94 mousekey_clear_report();
97 void mousekey_clear_report(void)