2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <util/delay.h>
29 static report_mouse_t mouse_report
= {};
30 static uint8_t mousekey_repeat
= 0;
31 static uint8_t mousekey_accel
= 0;
33 static void mousekey_debug(void);
37 * Mouse keys acceleration algorithm
38 * http://en.wikipedia.org/wiki/Mouse_keys
40 * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
42 /* milliseconds between the initial key press and first repeated motion event (0-2550) */
43 uint8_t mk_delay
= MOUSEKEY_DELAY
/10;
44 /* milliseconds between repeated motion events (0-255) */
45 uint8_t mk_interval
= MOUSEKEY_INTERVAL
;
46 /* steady speed (in action_delta units) applied each event (0-255) */
47 uint8_t mk_max_speed
= MOUSEKEY_MAX_SPEED
;
48 /* number of events (count) accelerating to steady speed (0-255) */
49 uint8_t mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
50 /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
51 //int8_t mk_curve = 0;
53 uint8_t mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
54 uint8_t mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
57 static uint16_t last_timer
= 0;
60 static uint8_t move_unit(void)
63 if (mousekey_accel
& (1<<0)) {
64 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
)/4;
65 } else if (mousekey_accel
& (1<<1)) {
66 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
)/2;
67 } else if (mousekey_accel
& (1<<2)) {
68 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
);
69 } else if (mousekey_repeat
== 0) {
70 unit
= MOUSEKEY_MOVE_DELTA
;
71 } else if (mousekey_repeat
>= mk_time_to_max
) {
72 unit
= MOUSEKEY_MOVE_DELTA
* mk_max_speed
;
74 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
* mousekey_repeat
) / mk_time_to_max
;
76 return (unit
> MOUSEKEY_MOVE_MAX
? MOUSEKEY_MOVE_MAX
: (unit
== 0 ? 1 : unit
));
79 static uint8_t wheel_unit(void)
82 if (mousekey_accel
& (1<<0)) {
83 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
)/4;
84 } else if (mousekey_accel
& (1<<1)) {
85 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
)/2;
86 } else if (mousekey_accel
& (1<<2)) {
87 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
);
88 } else if (mousekey_repeat
== 0) {
89 unit
= MOUSEKEY_WHEEL_DELTA
;
90 } else if (mousekey_repeat
>= mk_wheel_time_to_max
) {
91 unit
= MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
;
93 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
* mousekey_repeat
) / mk_wheel_time_to_max
;
95 return (unit
> MOUSEKEY_WHEEL_MAX
? MOUSEKEY_WHEEL_MAX
: (unit
== 0 ? 1 : unit
));
98 void mousekey_task(void)
100 if (timer_elapsed(last_timer
) < (mousekey_repeat
? mk_interval
: mk_delay
*10))
103 if (mouse_report
.x
== 0 && mouse_report
.y
== 0 && mouse_report
.v
== 0 && mouse_report
.h
== 0)
106 if (mousekey_repeat
!= UINT8_MAX
)
110 if (mouse_report
.x
> 0) mouse_report
.x
= move_unit();
111 if (mouse_report
.x
< 0) mouse_report
.x
= move_unit() * -1;
112 if (mouse_report
.y
> 0) mouse_report
.y
= move_unit();
113 if (mouse_report
.y
< 0) mouse_report
.y
= move_unit() * -1;
115 /* diagonal move [1/sqrt(2) = 0.7] */
116 if (mouse_report
.x
&& mouse_report
.y
) {
117 mouse_report
.x
*= 0.7;
118 mouse_report
.y
*= 0.7;
121 if (mouse_report
.v
> 0) mouse_report
.v
= wheel_unit();
122 if (mouse_report
.v
< 0) mouse_report
.v
= wheel_unit() * -1;
123 if (mouse_report
.h
> 0) mouse_report
.h
= wheel_unit();
124 if (mouse_report
.h
< 0) mouse_report
.h
= wheel_unit() * -1;
129 void mousekey_on(uint8_t code
)
131 if (code
== KC_MS_UP
) mouse_report
.y
= move_unit() * -1;
132 else if (code
== KC_MS_DOWN
) mouse_report
.y
= move_unit();
133 else if (code
== KC_MS_LEFT
) mouse_report
.x
= move_unit() * -1;
134 else if (code
== KC_MS_RIGHT
) mouse_report
.x
= move_unit();
135 else if (code
== KC_MS_WH_UP
) mouse_report
.v
= wheel_unit();
136 else if (code
== KC_MS_WH_DOWN
) mouse_report
.v
= wheel_unit() * -1;
137 else if (code
== KC_MS_WH_LEFT
) mouse_report
.h
= wheel_unit() * -1;
138 else if (code
== KC_MS_WH_RIGHT
) mouse_report
.h
= wheel_unit();
139 else if (code
== KC_MS_BTN1
) mouse_report
.buttons
|= MOUSE_BTN1
;
140 else if (code
== KC_MS_BTN2
) mouse_report
.buttons
|= MOUSE_BTN2
;
141 else if (code
== KC_MS_BTN3
) mouse_report
.buttons
|= MOUSE_BTN3
;
142 else if (code
== KC_MS_BTN4
) mouse_report
.buttons
|= MOUSE_BTN4
;
143 else if (code
== KC_MS_BTN5
) mouse_report
.buttons
|= MOUSE_BTN5
;
144 else if (code
== KC_MS_ACCEL0
) mousekey_accel
|= (1<<0);
145 else if (code
== KC_MS_ACCEL1
) mousekey_accel
|= (1<<1);
146 else if (code
== KC_MS_ACCEL2
) mousekey_accel
|= (1<<2);
149 void mousekey_off(uint8_t code
)
151 if (code
== KC_MS_UP
&& mouse_report
.y
< 0) mouse_report
.y
= 0;
152 else if (code
== KC_MS_DOWN
&& mouse_report
.y
> 0) mouse_report
.y
= 0;
153 else if (code
== KC_MS_LEFT
&& mouse_report
.x
< 0) mouse_report
.x
= 0;
154 else if (code
== KC_MS_RIGHT
&& mouse_report
.x
> 0) mouse_report
.x
= 0;
155 else if (code
== KC_MS_WH_UP
&& mouse_report
.v
> 0) mouse_report
.v
= 0;
156 else if (code
== KC_MS_WH_DOWN
&& mouse_report
.v
< 0) mouse_report
.v
= 0;
157 else if (code
== KC_MS_WH_LEFT
&& mouse_report
.h
< 0) mouse_report
.h
= 0;
158 else if (code
== KC_MS_WH_RIGHT
&& mouse_report
.h
> 0) mouse_report
.h
= 0;
159 else if (code
== KC_MS_BTN1
) mouse_report
.buttons
&= ~MOUSE_BTN1
;
160 else if (code
== KC_MS_BTN2
) mouse_report
.buttons
&= ~MOUSE_BTN2
;
161 else if (code
== KC_MS_BTN3
) mouse_report
.buttons
&= ~MOUSE_BTN3
;
162 else if (code
== KC_MS_BTN4
) mouse_report
.buttons
&= ~MOUSE_BTN4
;
163 else if (code
== KC_MS_BTN5
) mouse_report
.buttons
&= ~MOUSE_BTN5
;
164 else if (code
== KC_MS_ACCEL0
) mousekey_accel
&= ~(1<<0);
165 else if (code
== KC_MS_ACCEL1
) mousekey_accel
&= ~(1<<1);
166 else if (code
== KC_MS_ACCEL2
) mousekey_accel
&= ~(1<<2);
168 if (mouse_report
.x
== 0 && mouse_report
.y
== 0 && mouse_report
.v
== 0 && mouse_report
.h
== 0)
172 void mousekey_send(void)
175 host_mouse_send(&mouse_report
);
176 last_timer
= timer_read();
179 void mousekey_clear(void)
181 mouse_report
= (report_mouse_t
){};
186 static void mousekey_debug(void)
188 if (!debug_mouse
) return;
189 print("mousekey [btn|x y v h](rep/acl): [");
190 phex(mouse_report
.buttons
); print("|");
191 print_decs(mouse_report
.x
); print(" ");
192 print_decs(mouse_report
.y
); print(" ");
193 print_decs(mouse_report
.v
); print(" ");
194 print_decs(mouse_report
.h
); print("](");
195 print_dec(mousekey_repeat
); print("/");
196 print_dec(mousekey_accel
); print(")\n");