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>
28 static report_mouse_t report
;
30 static uint8_t mousekey_repeat
= 0;
32 static void mousekey_debug(void);
35 /* max value on report descriptor */
36 #define MOUSEKEY_MOVE_MAX 127
37 #define MOUSEKEY_WHEEL_MAX 15
39 #ifndef MOUSEKEY_MOVE_DELTA
40 #define MOUSEKEY_MOVE_DELTA 5
42 #ifndef MOUSEKEY_WHEEL_DELTA
43 #define MOUSEKEY_WHEEL_DELTA 1
45 #ifndef MOUSEKEY_DELAY
46 #define MOUSEKEY_DELAY 300
48 #ifndef MOUSEKEY_INTERVAL
49 #define MOUSEKEY_INTERVAL 50
51 #ifndef MOUSEKEY_MAX_SPEED
52 #define MOUSEKEY_MAX_SPEED 10
54 #ifndef MOUSEKEY_TIME_TO_MAX
55 #define MOUSEKEY_TIME_TO_MAX 20
57 #ifndef MOUSEKEY_WHEEL_MAX_SPEED
58 #define MOUSEKEY_WHEEL_MAX_SPEED 8
60 #ifndef MOUSEKEY_WHEEL_TIME_TO_MAX
61 #define MOUSEKEY_WHEEL_TIME_TO_MAX 40
66 * Mouse keys acceleration algorithm
67 * http://en.wikipedia.org/wiki/Mouse_keys
69 * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
71 /* milliseconds between the initial key press and first repeated motion event (0-2550) */
72 static uint8_t mk_delay
= MOUSEKEY_DELAY
/10;
73 /* milliseconds between repeated motion events (0-255) */
74 static uint8_t mk_interval
= MOUSEKEY_INTERVAL
;
75 /* steady speed (in action_delta units) applied each event (0-255) */
76 static uint8_t mk_max_speed
= MOUSEKEY_MAX_SPEED
;
77 /* number of events (count) accelerating to steady speed (0-255) */
78 static uint8_t mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
79 /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
80 //static int8_t mk_curve = 0;
82 static uint8_t mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
83 static uint8_t mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
86 static uint16_t last_timer
= 0;
89 static uint8_t move_unit(void)
92 if (mousekey_repeat
> mk_time_to_max
) {
93 unit
= MOUSEKEY_MOVE_DELTA
* mk_max_speed
;
95 unit
= (MOUSEKEY_MOVE_DELTA
* mk_max_speed
* mousekey_repeat
) / mk_time_to_max
;
97 if (unit
== 0) return 1;
98 return (unit
> MOUSEKEY_MOVE_MAX
? MOUSEKEY_MOVE_MAX
: unit
);
101 static uint8_t wheel_unit(void)
104 if (mousekey_repeat
> mk_time_to_max
) {
105 unit
= MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
;
107 unit
= (MOUSEKEY_WHEEL_DELTA
* mk_wheel_max_speed
* mousekey_repeat
) / mk_time_to_max
;
109 if (unit
== 0) return 1;
110 return (unit
> MOUSEKEY_WHEEL_MAX
? MOUSEKEY_WHEEL_MAX
: unit
);
113 void mousekey_task(void)
115 if (timer_elapsed(last_timer
) < (mousekey_repeat
? mk_interval
: mk_delay
*10))
118 if (report
.x
== 0 && report
.y
== 0 && report
.v
== 0 && report
.h
== 0)
121 if (mousekey_repeat
!= UINT8_MAX
)
125 if (report
.x
> 0) report
.x
= move_unit();
126 if (report
.x
< 0) report
.x
= move_unit() * -1;
127 if (report
.y
> 0) report
.y
= move_unit();
128 if (report
.y
< 0) report
.y
= move_unit() * -1;
130 if (report
.x
&& report
.y
) {
135 if (report
.v
> 0) report
.v
= wheel_unit();
136 if (report
.v
< 0) report
.v
= wheel_unit() * -1;
137 if (report
.h
> 0) report
.h
= wheel_unit();
138 if (report
.h
< 0) report
.h
= wheel_unit() * -1;
143 void mousekey_on(uint8_t code
)
145 if (code
== KC_MS_UP
) report
.y
= MOUSEKEY_MOVE_DELTA
* -1;
146 else if (code
== KC_MS_DOWN
) report
.y
= MOUSEKEY_MOVE_DELTA
;
147 else if (code
== KC_MS_LEFT
) report
.x
= MOUSEKEY_MOVE_DELTA
* -1;
148 else if (code
== KC_MS_RIGHT
) report
.x
= MOUSEKEY_MOVE_DELTA
;
149 else if (code
== KC_MS_WH_UP
) report
.v
= MOUSEKEY_WHEEL_DELTA
;
150 else if (code
== KC_MS_WH_DOWN
) report
.v
= MOUSEKEY_WHEEL_DELTA
* -1;
151 else if (code
== KC_MS_WH_LEFT
) report
.h
= MOUSEKEY_WHEEL_DELTA
* -1;
152 else if (code
== KC_MS_WH_RIGHT
) report
.h
= MOUSEKEY_WHEEL_DELTA
;
153 else if (code
== KC_MS_BTN1
) report
.buttons
|= MOUSE_BTN1
;
154 else if (code
== KC_MS_BTN2
) report
.buttons
|= MOUSE_BTN2
;
155 else if (code
== KC_MS_BTN3
) report
.buttons
|= MOUSE_BTN3
;
156 else if (code
== KC_MS_BTN4
) report
.buttons
|= MOUSE_BTN4
;
157 else if (code
== KC_MS_BTN5
) report
.buttons
|= MOUSE_BTN5
;
160 void mousekey_off(uint8_t code
)
162 if (code
== KC_MS_UP
&& report
.y
< 0) report
.y
= 0;
163 else if (code
== KC_MS_DOWN
&& report
.y
> 0) report
.y
= 0;
164 else if (code
== KC_MS_LEFT
&& report
.x
< 0) report
.x
= 0;
165 else if (code
== KC_MS_RIGHT
&& report
.x
> 0) report
.x
= 0;
166 else if (code
== KC_MS_WH_UP
&& report
.v
> 0) report
.v
= 0;
167 else if (code
== KC_MS_WH_DOWN
&& report
.v
< 0) report
.v
= 0;
168 else if (code
== KC_MS_WH_LEFT
&& report
.h
< 0) report
.h
= 0;
169 else if (code
== KC_MS_WH_RIGHT
&& report
.h
> 0) report
.h
= 0;
170 else if (code
== KC_MS_BTN1
) report
.buttons
&= ~MOUSE_BTN1
;
171 else if (code
== KC_MS_BTN2
) report
.buttons
&= ~MOUSE_BTN2
;
172 else if (code
== KC_MS_BTN3
) report
.buttons
&= ~MOUSE_BTN3
;
173 else if (code
== KC_MS_BTN4
) report
.buttons
&= ~MOUSE_BTN4
;
174 else if (code
== KC_MS_BTN5
) report
.buttons
&= ~MOUSE_BTN5
;
176 if (report
.x
== 0 && report
.y
== 0 && report
.v
== 0 && report
.h
== 0)
180 void mousekey_send(void)
183 host_mouse_send(&report
);
184 last_timer
= timer_read();
187 void mousekey_clear(void)
189 report
= (report_mouse_t
){};
192 static void mousekey_debug(void)
194 if (!debug_mouse
) return;
195 print("mousekey [btn|x y v h]rep: [");
196 phex(report
.buttons
); print("|");
197 phex(report
.x
); print(" ");
198 phex(report
.y
); print(" ");
199 phex(report
.v
); print(" ");
200 phex(report
.h
); print("]");
201 phex(mousekey_repeat
);