]> git.gir.st - tmk_keyboard.git/blob - mousekey.c
refactor keyboard.h, host.h
[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
28 static inline uint8_t move_unit(void)
29 {
30 uint16_t unit = 10 + (mousekey_repeat);
31 return (unit > 127 ? 127 : unit);
32 }
33
34 void mousekey_decode(uint8_t code)
35 {
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;
43 /*
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;
50 */
51 }
52
53 bool mousekey_changed(void)
54 {
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);
60 }
61
62 void mousekey_send(void)
63 {
64 static uint16_t last_timer = 0;
65
66 if (!mousekey_changed()) {
67 mousekey_repeat = 0;
68 return;
69 }
70
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))) {
75 return;
76 }
77 }
78
79 if (report.x && report.y) {
80 report.x *= 0.7;
81 report.y *= 0.7;
82 }
83
84 /*
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");
89 */
90
91 mousekey_debug();
92
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();
100 }
101
102 void mousekey_clear_report(void)
103 {
104 report.buttons = 0;
105 report.x = 0;
106 report.y = 0;
107 }
108
109 static void mousekey_debug(void)
110 {
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(" ");
116 /*
117 phex(report.v); print(" ");
118 phex(report.h);
119 */
120 print("\n");
121 }
Imprint / Impressum