]> git.donarmstrong.com Git - qmk_firmware.git/blob - docs/feature_ps2_mouse.md
[Keyboard] leds in default keymap (#6357)
[qmk_firmware.git] / docs / feature_ps2_mouse.md
1 ## PS/2 Mouse Support
2
3 Its possible to hook up a PS/2 mouse (for example touchpads or trackpoints) to your keyboard as a composite device.
4
5 To hook up a Trackpoint, you need to obtain a Trackpoint module (i.e. harvest from a Thinkpad keyboard), identify the function of each pin of the module, and make the necessary circuitry between controller and Trackpoint module. For more information, please refer to [Trackpoint Hardware](https://deskthority.net/wiki/TrackPoint_Hardware) page on Deskthority Wiki.
6
7 There are three available modes for hooking up PS/2 devices: USART (best), interrupts (better) or busywait (not recommended).
8
9 ### The Cirtuitry between Trackpoint and Controller
10
11 To get the things working, a 4.7K drag is needed between the two lines DATA and CLK and the line 5+. 
12
13 ```
14
15           DATA ----------+--------- PIN
16                          |
17                         4.7K
18                          |
19 MODULE    5+  --------+--+--------- PWR   CONTROLLER
20                       |
21                      4.7K
22                       |    
23           CLK   ------+------------ PIN
24 ```
25
26
27 ### Busywait Version
28
29 Note: This is not recommended, you may encounter jerky movement or unsent inputs. Please use interrupt or USART version if possible.
30
31 In rules.mk:
32
33 ```
34 PS2_MOUSE_ENABLE = yes
35 PS2_USE_BUSYWAIT = yes
36 ```
37
38 In your keyboard config.h:
39
40 ```
41 #ifdef PS2_USE_BUSYWAIT
42 #   define PS2_CLOCK_PORT  PORTD
43 #   define PS2_CLOCK_PIN   PIND
44 #   define PS2_CLOCK_DDR   DDRD
45 #   define PS2_CLOCK_BIT   1
46 #   define PS2_DATA_PORT   PORTD
47 #   define PS2_DATA_PIN    PIND
48 #   define PS2_DATA_DDR    DDRD
49 #   define PS2_DATA_BIT    2
50 #endif
51 ```
52
53 ### Interrupt Version
54
55 The following example uses D2 for clock and D5 for data. You can use any INT or PCINT pin for clock, and any pin for data.
56
57 In rules.mk:
58
59 ```
60 PS2_MOUSE_ENABLE = yes
61 PS2_USE_INT = yes
62 ```
63
64 In your keyboard config.h:
65
66 ```
67 #ifdef PS2_USE_INT
68 #define PS2_CLOCK_PORT  PORTD
69 #define PS2_CLOCK_PIN   PIND
70 #define PS2_CLOCK_DDR   DDRD
71 #define PS2_CLOCK_BIT   2
72 #define PS2_DATA_PORT   PORTD
73 #define PS2_DATA_PIN    PIND
74 #define PS2_DATA_DDR    DDRD
75 #define PS2_DATA_BIT    5
76
77 #define PS2_INT_INIT()  do {    \
78     EICRA |= ((1<<ISC21) |      \
79               (0<<ISC20));      \
80 } while (0)
81 #define PS2_INT_ON()  do {      \
82     EIMSK |= (1<<INT2);         \
83 } while (0)
84 #define PS2_INT_OFF() do {      \
85     EIMSK &= ~(1<<INT2);        \
86 } while (0)
87 #define PS2_INT_VECT   INT2_vect
88 #endif
89 ```
90
91 ### USART Version
92
93 To use USART on the ATMega32u4, you have to use PD5 for clock and PD2 for data. If one of those are unavailable, you need to use interrupt version.
94
95 In rules.mk:
96
97 ```
98 PS2_MOUSE_ENABLE = yes
99 PS2_USE_USART = yes
100 ```
101
102 In your keyboard config.h:
103
104 ```
105 #ifdef PS2_USE_USART
106 #define PS2_CLOCK_PORT  PORTD
107 #define PS2_CLOCK_PIN   PIND
108 #define PS2_CLOCK_DDR   DDRD
109 #define PS2_CLOCK_BIT   5
110 #define PS2_DATA_PORT   PORTD
111 #define PS2_DATA_PIN    PIND
112 #define PS2_DATA_DDR    DDRD
113 #define PS2_DATA_BIT    2
114
115 /* synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge */
116 /* set DDR of CLOCK as input to be slave */
117 #define PS2_USART_INIT() do {   \
118     PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);   \
119     PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);     \
120     UCSR1C = ((1 << UMSEL10) |  \
121               (3 << UPM10)   |  \
122               (0 << USBS1)   |  \
123               (3 << UCSZ10)  |  \
124               (0 << UCPOL1));   \
125     UCSR1A = 0;                 \
126     UBRR1H = 0;                 \
127     UBRR1L = 0;                 \
128 } while (0)
129 #define PS2_USART_RX_INT_ON() do {  \
130     UCSR1B = ((1 << RXCIE1) |       \
131               (1 << RXEN1));        \
132 } while (0)
133 #define PS2_USART_RX_POLL_ON() do { \
134     UCSR1B = (1 << RXEN1);          \
135 } while (0)
136 #define PS2_USART_OFF() do {    \
137     UCSR1C = 0;                 \
138     UCSR1B &= ~((1 << RXEN1) |  \
139                 (1 << TXEN1));  \
140 } while (0)
141 #define PS2_USART_RX_READY      (UCSR1A & (1<<RXC1))
142 #define PS2_USART_RX_DATA       UDR1
143 #define PS2_USART_ERROR         (UCSR1A & ((1<<FE1) | (1<<DOR1) | (1<<UPE1)))
144 #define PS2_USART_RX_VECT       USART1_RX_vect
145 #endif
146 ```
147
148 ### Additional Settings
149
150 #### PS/2 Mouse Features
151
152 These enable settings supported by the PS/2 mouse protocol: http://www.computer-engineering.org/ps2mouse/
153
154 ```
155 /* Use remote mode instead of the default stream mode (see link) */
156 #define PS2_MOUSE_USE_REMOTE_MODE
157
158 /* Enable the scrollwheel or scroll gesture on your mouse or touchpad */
159 #define PS2_MOUSE_ENABLE_SCROLLING
160
161 /* Some mice will need a scroll mask to be configured. The default is 0xFF. */
162 #define PS2_MOUSE_SCROLL_MASK 0x0F
163
164 /* Applies a transformation to the movement before sending to the host (see link) */
165 #define PS2_MOUSE_USE_2_1_SCALING
166
167 /* The time to wait after initializing the ps2 host */
168 #define PS2_MOUSE_INIT_DELAY 1000 /* Default */
169 ```
170
171 You can also call the following functions from ps2_mouse.h
172
173 ```
174 void ps2_mouse_disable_data_reporting(void);
175
176 void ps2_mouse_enable_data_reporting(void);
177
178 void ps2_mouse_set_remote_mode(void);
179
180 void ps2_mouse_set_stream_mode(void);
181
182 void ps2_mouse_set_scaling_2_1(void);
183
184 void ps2_mouse_set_scaling_1_1(void);
185
186 void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution);
187
188 void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate);
189 ```
190
191 #### Fine Control
192
193 Use the following defines to change the sensitivity and speed of the mouse.
194 Note: you can also use `ps2_mouse_set_resolution` for the same effect (not supported on most touchpads).
195
196 ```
197 #define PS2_MOUSE_X_MULTIPLIER 3
198 #define PS2_MOUSE_Y_MULTIPLIER 3
199 #define PS2_MOUSE_V_MULTIPLIER 1
200 ```
201
202 #### Scroll Button
203
204 If you're using a trackpoint, you will likely want to be able to use it for scrolling.
205 Its possible to enable a "scroll button/s" that when pressed will cause the mouse to scroll instead of moving.
206 To enable the feature, you must set a scroll button mask as follows:
207
208 ```
209 #define PS2_MOUSE_SCROLL_BTN_MASK (1<<PS2_MOUSE_BUTTON_MIDDLE) /* Default */
210 ```
211
212 To disable the scroll button feature:
213
214 ```
215 #define PS2_MOUSE_SCROLL_BTN_MASK 0
216 ```
217
218 The available buttons are:
219
220 ```
221 #define PS2_MOUSE_BTN_LEFT      0
222 #define PS2_MOUSE_BTN_RIGHT     1
223 #define PS2_MOUSE_BTN_MIDDLE    2
224 ```
225
226 You can also combine buttons in the mask by `|`ing them together.
227
228 Once you've configured your scroll button mask, you must configure the scroll button send interval.
229 This is the interval before which if the scroll buttons were released they would be sent to the host.
230 After this interval, they will cause the mouse to scroll and will not be sent.
231
232 ```
233 #define PS2_MOUSE_SCROLL_BTN_SEND 300 /* Default */
234 ```
235
236 To disable sending the scroll buttons:
237 ```
238 #define PS2_MOUSE_SCROLL_BTN_SEND 0
239 ```
240
241 Fine control over the scrolling is supported with the following defines:
242
243 ```
244 #define PS2_MOUSE_SCROLL_DIVISOR_H 2
245 #define PS2_MOUSE_SCROLL_DIVISOR_V 2
246 ```
247
248 #### Invert Mouse and Scroll Axes
249
250 To invert the X and Y axes you can put:
251
252 ```
253 #define PS2_MOUSE_INVERT_X
254 #define PS2_MOUSE_INVERT_Y
255 ```
256
257 into config.h.
258
259 To reverse the scroll axes you can put:
260
261 ```
262 #define PS2_MOUSE_INVERT_H
263 #define PS2_MOUSE_INVERT_V
264 ```
265
266 into config.h.
267
268 #### Debug Settings
269
270 To debug the mouse, add `debug_mouse = true` or enable via bootmagic.
271
272 ```
273 /* To debug the mouse reports */
274 #define PS2_MOUSE_DEBUG_HID
275 #define PS2_MOUSE_DEBUG_RAW
276 ```