]> git.gir.st - tmk_keyboard.git/blob - mousekey.c
Synchronous USART support for PS/2 on V-USB stack
[tmk_keyboard.git] / mousekey.c
1 #include <stdint.h>
2 #include <util/delay.h>
3 #include "usb_keycodes.h"
4 #include "host.h"
5 #include "timer.h"
6 #include "print.h"
7 #include "debug.h"
8 #include "mousekey.h"
9
10
11 static report_mouse_t report;
12 static report_mouse_t report_prev;
13
14 static uint8_t mousekey_repeat = 0;
15
16 static void mousekey_debug(void);
17
18
19 /*
20 * TODO: fix acceleration algorithm
21 * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
22 */
23 #ifndef MOUSEKEY_DELAY_TIME
24 # define MOUSEKEY_DELAY_TIME 255
25 #endif
26
27 // acceleration parameters
28 uint8_t mousekey_move_unit = 2;
29 uint8_t mousekey_resolution = 5;
30
31
32 static inline uint8_t move_unit(void)
33 {
34 uint16_t unit = 5 + mousekey_repeat*2;
35 return (unit > 63 ? 63 : unit);
36 }
37
38 void mousekey_decode(uint8_t code)
39 {
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;
53 }
54
55 bool mousekey_changed(void)
56 {
57 return (report.buttons != report_prev.buttons ||
58 report.x || report.y || report.v || report.h);
59 }
60
61 void mousekey_send(void)
62 {
63 static uint16_t last_timer = 0;
64
65 if (!mousekey_changed()) {
66 mousekey_repeat = 0;
67 mousekey_clear_report();
68 return;
69 }
70
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();
75 return;
76 }
77 }
78
79 if (mousekey_repeat != 0xFF) {
80 mousekey_repeat++;
81 }
82
83 if (report.x && report.y) {
84 report.x *= 0.7;
85 report.y *= 0.7;
86 }
87
88 mousekey_debug();
89 host_mouse_send(&report);
90 report_prev = report;
91 last_timer = timer_read();
92 mousekey_clear_report();
93 }
94
95 void mousekey_clear_report(void)
96 {
97 report.buttons = 0;
98 report.x = 0;
99 report.y = 0;
100 report.v = 0;
101 report.h = 0;
102 }
103
104 static void mousekey_debug(void)
105 {
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(" ");
112 phex(report.h);
113 phex(mousekey_repeat);
114 print("\n");
115 }
Imprint / Impressum