]>
git.gir.st - tmk_keyboard.git/blob - protocol/ps2_mouse.c
2 Copyright 2011,2013 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/>.
20 #include<util/delay.h>
22 #include "ps2_mouse.h"
23 #include "usb_mouse.h"
25 #define PS2_MOUSE_DEBUG
26 #ifdef PS2_MOUSE_DEBUG
36 bool ps2_mouse_enable
= true;
37 uint8_t ps2_mouse_x
= 0;
38 uint8_t ps2_mouse_y
= 0;
39 uint8_t ps2_mouse_btn
= 0;
40 uint8_t ps2_mouse_error_count
= 0;
42 static uint8_t ps2_mouse_btn_prev
= 0;
45 uint8_t ps2_mouse_init(void) {
48 if (!ps2_mouse_enable
) return 1;
52 _delay_ms(1000); // wait for powering up
55 rcv
= ps2_host_send(0xFF);
56 print("ps2_mouse_init: send Reset: ");
57 phex(rcv
); phex(ps2_error
); print("\n");
59 // read completion code of BAT
60 rcv
= ps2_host_recv();
61 print("ps2_mouse_init: read BAT: ");
62 phex(rcv
); phex(ps2_error
); print("\n");
65 rcv
= ps2_host_recv();
66 print("ps2_mouse_init: read DevID: ");
67 phex(rcv
); phex(ps2_error
); print("\n");
69 // send Enable Data Reporting
70 rcv
= ps2_host_send(0xF4);
71 print("ps2_mouse_init: send 0xF4: ");
72 phex(rcv
); phex(ps2_error
); print("\n");
74 // send Set Remote mode
75 rcv
= ps2_host_send(0xF0);
76 print("ps2_mouse_init: send 0xF0: ");
77 phex(rcv
); phex(ps2_error
); print("\n");
82 uint8_t ps2_mouse_read(void)
86 if (!ps2_mouse_enable
) return 1;
88 rcv
= ps2_host_send(0xEB);
91 ps2_mouse_btn
= ps2_host_recv_response();
92 ps2_mouse_x
= ps2_host_recv_response();
93 ps2_mouse_y
= ps2_host_recv_response();
98 bool ps2_mouse_changed(void)
100 return (ps2_mouse_x
|| ps2_mouse_y
|| (ps2_mouse_btn
& PS2_MOUSE_BTN_MASK
) != ps2_mouse_btn_prev
);
103 #define PS2_MOUSE_SCROLL_BUTTON 0x04
104 void ps2_mouse_usb_send(void)
106 static bool scrolled
= false;
108 if (!ps2_mouse_enable
) return;
110 if (ps2_mouse_changed()) {
114 // convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
115 if (ps2_mouse_btn
& (1<<PS2_MOUSE_X_SIGN
))
116 x
= ps2_mouse_x
> 128 ? (int8_t)ps2_mouse_x
: -127;
118 x
= ps2_mouse_x
< 128 ? (int8_t)ps2_mouse_x
: 127;
120 if (ps2_mouse_btn
& (1<<PS2_MOUSE_Y_SIGN
))
121 y
= ps2_mouse_y
> 128 ? (int8_t)ps2_mouse_y
: -127;
123 y
= ps2_mouse_y
< 128 ? (int8_t)ps2_mouse_y
: 127;
125 // Y is needed to reverse
128 if (ps2_mouse_btn
& PS2_MOUSE_SCROLL_BUTTON
) {
130 if (x
> 0 || x
< 0) h
= (x
> 64 ? 64 : (x
< -64 ? -64 :x
));
131 if (y
> 0 || y
< 0) v
= (y
> 64 ? 64 : (y
< -64 ? -64 :y
));
134 usb_mouse_send(0,0, -v
/16, h
/16, 0);
137 } else if (!scrolled
&& (ps2_mouse_btn_prev
& PS2_MOUSE_SCROLL_BUTTON
)) {
138 usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON
);
140 usb_mouse_send(0,0,0,0, 0);
143 usb_mouse_send(x
, y
, 0, 0, ps2_mouse_btn
& PS2_MOUSE_BTN_MASK
);
146 ps2_mouse_btn_prev
= (ps2_mouse_btn
& PS2_MOUSE_BTN_MASK
);
154 void ps2_mouse_print(void)
156 if (!debug_mouse
) return;
157 print("ps2_mouse[btn|x y]: ");
158 phex(ps2_mouse_btn
); print("|");
159 phex(ps2_mouse_x
); print(" ");
160 phex(ps2_mouse_y
); print("\n");
164 /* PS/2 Mouse Synopsis
165 * http://www.computer-engineering.org/ps2mouse/
169 * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
170 * 0xF5: Disable Data Reporting
171 * 0xF4: Enable Data Reporting
172 * 0xF3: Set Sample Rate
173 * 0xF2: Get Device ID
174 * 0xF0: Set Remote Mode
176 * 0xEA: Set Stream Mode
177 * 0xE9: Status Request
178 * 0xE8: Set Resolution
179 * 0xE7: Set Scaling 2:1
180 * 0xE6: Set Scaling 1:1
183 * Stream Mode: devices sends the data when it changs its state
184 * Remote Mode: host polls the data periodically
186 * This code uses Remote Mode and polls the data with Read Data(0xEB).
189 * byte|7 6 5 4 3 2 1 0
190 * ----+--------------------------------------------------------------
191 * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
192 * 1| X movement(0-255)
193 * 2| Y movement(0-255)