]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/ps2_mouse.c
Merge branch 'master' into promethium
[qmk_firmware.git] / tmk_core / protocol / ps2_mouse.c
1 /*
2 Copyright 2011,2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdbool.h>
19 #include<avr/io.h>
20 #include<util/delay.h>
21 #include "ps2_mouse.h"
22 #include "host.h"
23 #include "timer.h"
24 #include "print.h"
25 #include "report.h"
26 #include "debug.h"
27 #include "ps2.h"
28
29 /* ============================= MACROS ============================ */
30
31 #define PS2_MOUSE_SEND(command, message) \
32 do { \
33    uint8_t rcv = ps2_host_send(command); \
34    if (debug_mouse) { \
35         print((message)); \
36         xprintf(" command: %X, result: %X, error: %X \n", command, rcv, ps2_error); \
37     } \
38 } while(0)
39
40 #define PS2_MOUSE_SEND_SAFE(command, message) \
41 do { \
42     if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
43         ps2_mouse_disable_data_reporting(); \
44     } \
45     PS2_MOUSE_SEND(command, message); \
46     if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
47         ps2_mouse_enable_data_reporting(); \
48     } \
49 } while(0)
50
51 #define PS2_MOUSE_SET_SAFE(command, value, message) \
52 do { \
53     if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
54         ps2_mouse_disable_data_reporting(); \
55     } \
56     PS2_MOUSE_SEND(command, message); \
57     PS2_MOUSE_SEND(value, "Sending value"); \
58     if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
59         ps2_mouse_enable_data_reporting(); \
60     } \
61 } while(0)
62
63 #define PS2_MOUSE_RECEIVE(message) \
64 do { \
65    uint8_t rcv = ps2_host_recv_response(); \
66    if (debug_mouse) { \
67         print((message)); \
68         xprintf(" result: %X, error: %X \n", rcv, ps2_error); \
69     } \
70 } while(0)
71
72 static enum ps2_mouse_mode_e {
73     PS2_MOUSE_STREAM_MODE,
74     PS2_MOUSE_REMOTE_MODE,
75 } ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
76
77 static report_mouse_t mouse_report = {};
78
79 static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
80 static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
81 static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
82 static inline void ps2_mouse_enable_scrolling(void);
83 static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
84
85 /* ============================= IMPLEMENTATION ============================ */
86
87 /* supports only 3 button mouse at this time */
88 void ps2_mouse_init(void) {
89     ps2_host_init();
90
91     _delay_ms(PS2_MOUSE_INIT_DELAY);    // wait for powering up
92
93     PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
94
95     PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
96     PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
97
98 #ifdef PS2_MOUSE_USE_REMOTE_MODE
99     ps2_mouse_set_remote_mode();
100 #else
101     ps2_mouse_enable_data_reporting();
102 #endif
103
104 #ifdef PS2_MOUSE_ENABLE_SCROLLING
105     ps2_mouse_enable_scrolling();
106 #endif
107
108 #ifdef PS2_MOUSE_USE_2_1_SCALING
109     ps2_mouse_set_scaling_2_1();
110 #endif
111 }
112
113 void ps2_mouse_task(void) {
114     static uint8_t buttons_prev = 0;
115
116     /* receives packet from mouse */
117     uint8_t rcv;
118     rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
119     if (rcv == PS2_ACK) {
120         mouse_report.buttons = ps2_host_recv_response();
121         mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
122         mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
123 #ifdef PS2_MOUSE_ENABLE_SCROLLING
124         mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
125 #endif
126     } else {
127         if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
128         return;
129     }
130
131     /* if mouse moves or buttons state changes */
132     if (mouse_report.x || mouse_report.y || mouse_report.v ||
133             ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
134 #ifdef PS2_MOUSE_DEBUG_RAW
135         // Used to debug raw ps2 bytes from mouse
136         ps2_mouse_print_report(&mouse_report);
137 #endif
138         buttons_prev = mouse_report.buttons;
139         ps2_mouse_convert_report_to_hid(&mouse_report);
140 #if PS2_MOUSE_SCROLL_BTN_MASK
141         ps2_mouse_scroll_button_task(&mouse_report);
142 #endif
143 #ifdef PS2_MOUSE_DEBUG_HID
144         // Used to debug the bytes sent to the host
145         ps2_mouse_print_report(&mouse_report);
146 #endif
147         host_mouse_send(&mouse_report);
148     }
149     
150     ps2_mouse_clear_report(&mouse_report);
151 }
152
153 void ps2_mouse_disable_data_reporting(void) {
154     PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting"); 
155 }
156
157 void ps2_mouse_enable_data_reporting(void) {
158     PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting");
159 }
160
161 void ps2_mouse_set_remote_mode(void) { 
162     PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode"); 
163     ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
164 }
165
166 void ps2_mouse_set_stream_mode(void) { 
167     PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode"); 
168     ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
169 }
170
171 void ps2_mouse_set_scaling_2_1(void) {
172     PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1"); 
173 }
174
175 void ps2_mouse_set_scaling_1_1(void) {
176     PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1"); 
177 }
178
179 void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) {
180     PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution");
181 }
182
183 void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) {
184     PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate");
185 }
186
187 /* ============================= HELPERS ============================ */
188
189 #define X_IS_NEG  (mouse_report->buttons & (1<<PS2_MOUSE_X_SIGN))
190 #define Y_IS_NEG  (mouse_report->buttons & (1<<PS2_MOUSE_Y_SIGN))
191 #define X_IS_OVF  (mouse_report->buttons & (1<<PS2_MOUSE_X_OVFLW))
192 #define Y_IS_OVF  (mouse_report->buttons & (1<<PS2_MOUSE_Y_OVFLW))
193 static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
194     // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
195     // bit: 8    7 ... 0
196     //      sign \8-bit/
197     //
198     // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
199     //
200     // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
201     mouse_report->x = X_IS_NEG ?
202         ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ?  mouse_report->x : -127) :
203         ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
204     mouse_report->y = Y_IS_NEG ?
205         ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ?  mouse_report->y : -127) :
206         ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
207
208     // remove sign and overflow flags
209     mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
210
211     // invert coordinate of y to conform to USB HID mouse
212     mouse_report->y = -mouse_report->y;
213 }
214
215 static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
216     mouse_report->x = 0;
217     mouse_report->y = 0;
218     mouse_report->v = 0;
219     mouse_report->h = 0;
220     mouse_report->buttons = 0;
221 }
222
223 static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
224     if (!debug_mouse) return;
225     print("ps2_mouse: [");
226     phex(mouse_report->buttons); print("|");
227     print_hex8((uint8_t)mouse_report->x); print(" ");
228     print_hex8((uint8_t)mouse_report->y); print(" ");
229     print_hex8((uint8_t)mouse_report->v); print(" ");
230     print_hex8((uint8_t)mouse_report->h); print("]\n");
231 }
232
233 static inline void ps2_mouse_enable_scrolling(void) {
234     PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
235     PS2_MOUSE_SEND(200, "200");
236     PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
237     PS2_MOUSE_SEND(100, "100");
238     PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
239     PS2_MOUSE_SEND(80, "80");
240     PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
241     _delay_ms(20);
242 }
243
244 #define PRESS_SCROLL_BUTTONS    mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
245 #define RELEASE_SCROLL_BUTTONS  mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
246 static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
247     static enum { 
248         SCROLL_NONE, 
249         SCROLL_BTN, 
250         SCROLL_SENT,
251     } scroll_state = SCROLL_NONE;
252     static uint16_t scroll_button_time = 0;
253
254     if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
255         // All scroll buttons are pressed
256
257         if (scroll_state == SCROLL_NONE) {
258             scroll_button_time = timer_read();
259             scroll_state = SCROLL_BTN;
260         }
261
262         // If the mouse has moved, update the report to scroll instead of move the mouse
263         if (mouse_report->x || mouse_report->y) {
264             scroll_state = SCROLL_SENT;
265             mouse_report->v = -mouse_report->y/(PS2_MOUSE_SCROLL_DIVISOR_V);
266             mouse_report->h =  mouse_report->x/(PS2_MOUSE_SCROLL_DIVISOR_H);
267             mouse_report->x = 0;
268             mouse_report->y = 0;
269         }
270     } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
271         // None of the scroll buttons are pressed 
272
273 #if PS2_MOUSE_SCROLL_BTN_SEND
274         if (scroll_state == SCROLL_BTN 
275                 && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
276             PRESS_SCROLL_BUTTONS;
277             host_mouse_send(mouse_report);
278             _delay_ms(100);
279             RELEASE_SCROLL_BUTTONS;
280         }
281 #endif
282         scroll_state = SCROLL_NONE;
283     }
284
285     RELEASE_SCROLL_BUTTONS;
286 }