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