]>
git.gir.st - tmk_keyboard.git/blob - protocol/ps2_mouse.c
701d97213c5938e7dfe444e5b3f4f36d3e502dd7
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 // Not reliable: sometime fail to initialize mouse
55 _delay_ms(1000); // wait for powering up
56 rcv
= ps2_host_send(0xFF);
57 print("ps2_mouse_init: send Reset: ");
58 phex(rcv
); phex(ps2_error
); print("\n");
60 // read completion code of BAT
61 //_delay_ms(1000); // wait for Basic Assurance Test
62 rcv
= ps2_host_recv();
63 print("ps2_mouse_init: read BAT: ");
64 phex(rcv
); phex(ps2_error
); print("\n");
67 rcv
= ps2_host_recv();
68 print("ps2_mouse_init: read DevID: ");
69 phex(rcv
); phex(ps2_error
); print("\n");
71 // send Enable Data Reporting
72 rcv
= ps2_host_send(0xF4);
73 print("ps2_mouse_init: send 0xF4: ");
74 phex(rcv
); phex(ps2_error
); print("\n");
76 // send Set Remote mode
77 rcv
= ps2_host_send(0xF0);
78 print("ps2_mouse_init: send 0xF0: ");
79 phex(rcv
); phex(ps2_error
); print("\n");
84 uint8_t ps2_mouse_read(void)
88 if (!ps2_mouse_enable
) return 1;
90 rcv
= ps2_host_send(0xEB);
93 ps2_mouse_btn
= ps2_host_recv_response();
94 ps2_mouse_x
= ps2_host_recv_response();
95 ps2_mouse_y
= ps2_host_recv_response();
100 bool ps2_mouse_changed(void)
102 return (ps2_mouse_x
|| ps2_mouse_y
|| (ps2_mouse_btn
& PS2_MOUSE_BTN_MASK
) != ps2_mouse_btn_prev
);
105 #define PS2_MOUSE_SCROLL_BUTTON 0x04
106 void ps2_mouse_usb_send(void)
108 static bool scrolled
= false;
110 if (!ps2_mouse_enable
) return;
112 if (ps2_mouse_changed()) {
116 // convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
117 if (ps2_mouse_btn
& (1<<PS2_MOUSE_X_SIGN
))
118 x
= ps2_mouse_x
> 128 ? (int8_t)ps2_mouse_x
: -127;
120 x
= ps2_mouse_x
< 128 ? (int8_t)ps2_mouse_x
: 127;
122 if (ps2_mouse_btn
& (1<<PS2_MOUSE_Y_SIGN
))
123 y
= ps2_mouse_y
> 128 ? (int8_t)ps2_mouse_y
: -127;
125 y
= ps2_mouse_y
< 128 ? (int8_t)ps2_mouse_y
: 127;
127 // Y is needed to reverse
130 if (ps2_mouse_btn
& PS2_MOUSE_SCROLL_BUTTON
) {
132 if (x
> 0 || x
< 0) h
= (x
> 64 ? 64 : (x
< -64 ? -64 :x
));
133 if (y
> 0 || y
< 0) v
= (y
> 64 ? 64 : (y
< -64 ? -64 :y
));
136 usb_mouse_send(0,0, -v
/16, h
/16, 0);
139 } else if (!scrolled
&& (ps2_mouse_btn_prev
& PS2_MOUSE_SCROLL_BUTTON
)) {
140 usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON
);
142 usb_mouse_send(0,0,0,0, 0);
145 usb_mouse_send(x
, y
, 0, 0, ps2_mouse_btn
& PS2_MOUSE_BTN_MASK
);
148 ps2_mouse_btn_prev
= (ps2_mouse_btn
& PS2_MOUSE_BTN_MASK
);
156 void ps2_mouse_print(void)
158 if (!debug_mouse
) return;
159 print("ps2_mouse[btn|x y]: ");
160 phex(ps2_mouse_btn
); print("|");
161 phex(ps2_mouse_x
); print(" ");
162 phex(ps2_mouse_y
); print("\n");
166 /* PS/2 Mouse Synopsis
167 * http://www.computer-engineering.org/ps2mouse/
171 * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
172 * 0xF5: Disable Data Reporting
173 * 0xF4: Enable Data Reporting
174 * 0xF3: Set Sample Rate
175 * 0xF2: Get Device ID
176 * 0xF0: Set Remote Mode
178 * 0xEA: Set Stream Mode
179 * 0xE9: Status Request
180 * 0xE8: Set Resolution
181 * 0xE7: Set Scaling 2:1
182 * 0xE6: Set Scaling 1:1
185 * Stream Mode: devices sends the data when it changs its state
186 * Remote Mode: host polls the data periodically
188 * This code uses Remote Mode and polls the data with Read Data(0xEB).
191 * byte|7 6 5 4 3 2 1 0
192 * ----+--------------------------------------------------------------
193 * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
194 * 1| X movement(0-255)
195 * 2| Y movement(0-255)