]> git.gir.st - tmk_keyboard.git/blob - protocol/ps2_mouse.c
changed tabs to spaces in bluefruit.c
[tmk_keyboard.git] / protocol / ps2_mouse.c
1 /*
2 Copyright 2011 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.h"
22 #include "ps2_mouse.h"
23 #include "usb_mouse.h"
24
25 #define PS2_MOUSE_DEBUG
26 #ifdef PS2_MOUSE_DEBUG
27 # include "print.h"
28 # include "debug.h"
29 #else
30 # define print(s)
31 # define phex(h)
32 # define phex16(h)
33 #endif
34
35 // disable when errors occur 255 times.
36 #define ERROR_RETURN() do { \
37 if (ps2_error) { \
38 if (ps2_mouse_error_count < 255) { \
39 ps2_mouse_error_count++; \
40 } else { \
41 ps2_mouse_error_count = 0; \
42 ps2_mouse_enable = false; \
43 } \
44 return ps2_error; \
45 } \
46 } while (0)
47
48
49 /*
50 TODO
51 ----
52 - Stream mode
53 - Tracpoint command support: needed
54 - Middle button + move = Wheel traslation
55 */
56 bool ps2_mouse_enable = true;
57 uint8_t ps2_mouse_x = 0;
58 uint8_t ps2_mouse_y = 0;
59 uint8_t ps2_mouse_btn = 0;
60 uint8_t ps2_mouse_error_count = 0;
61
62 static uint8_t ps2_mouse_btn_prev = 0;
63
64
65 uint8_t ps2_mouse_init(void) {
66 uint8_t rcv;
67
68 if (!ps2_mouse_enable) return 1;
69
70 ps2_host_init();
71
72 // Reset
73 rcv = ps2_host_send(0xFF);
74 print("ps2_mouse_init: send 0xFF: ");
75 phex(ps2_error); print("\n");
76 ERROR_RETURN();
77
78 // ACK
79 rcv = ps2_host_recv();
80 print("ps2_mouse_init: read ACK: ");
81 phex(rcv); phex(ps2_error); print("\n");
82 ERROR_RETURN();
83
84 // BAT takes some time
85 _delay_ms(100);
86 rcv = ps2_host_recv();
87 print("ps2_mouse_init: read BAT: ");
88 phex(rcv); phex(ps2_error); print("\n");
89 ERROR_RETURN();
90
91 // Device ID
92 rcv = ps2_host_recv();
93 print("ps2_mouse_init: read DevID: ");
94 phex(rcv); phex(ps2_error); print("\n");
95 ERROR_RETURN();
96
97 // Enable data reporting
98 ps2_host_send(0xF4);
99 print("ps2_mouse_init: send 0xF4: ");
100 phex(ps2_error); print("\n");
101 ERROR_RETURN();
102
103 // ACK
104 rcv = ps2_host_recv();
105 print("ps2_mouse_init: read ACK: ");
106 phex(rcv); phex(ps2_error); print("\n");
107 ERROR_RETURN();
108
109 // Set Remote mode
110 ps2_host_send(0xF0);
111 print("ps2_mouse_init: send 0xF0: ");
112 phex(ps2_error); print("\n");
113 ERROR_RETURN();
114
115 // ACK
116 rcv = ps2_host_recv();
117 print("ps2_mouse_init: read ACK: ");
118 phex(rcv); phex(ps2_error); print("\n");
119 ERROR_RETURN();
120
121 return 0;
122 }
123
124 /*
125 Data format:
126 bit: 7 6 5 4 3 2 1 0
127 -----------------------------------------------------------------------
128 0 btn: Yovflw Xovflw Ysign Xsign 1 Middle Right Left
129 1 x: X movement(0-255)
130 2 y: Y movement(0-255)
131 */
132 uint8_t ps2_mouse_read(void)
133 {
134 uint8_t rcv;
135
136 if (!ps2_mouse_enable) return 1;
137
138 ps2_host_send(0xEB);
139 ERROR_RETURN();
140
141 rcv=ps2_host_recv();
142 ERROR_RETURN();
143
144 if(rcv==0xFA) {
145 ps2_mouse_btn = ps2_host_recv();
146 ERROR_RETURN();
147 ps2_mouse_x = ps2_host_recv();
148 ERROR_RETURN();
149 ps2_mouse_y = ps2_host_recv();
150 ERROR_RETURN();
151 }
152 return 0;
153 }
154
155 bool ps2_mouse_changed(void)
156 {
157 return (ps2_mouse_x || ps2_mouse_y || (ps2_mouse_btn & PS2_MOUSE_BTN_MASK) != ps2_mouse_btn_prev);
158 }
159
160 #define PS2_MOUSE_SCROLL_BUTTON 0x04
161 void ps2_mouse_usb_send(void)
162 {
163 static bool scrolled = false;
164
165 if (!ps2_mouse_enable) return;
166
167 if (ps2_mouse_changed()) {
168 int8_t x, y, v, h;
169 x = y = v = h = 0;
170
171 // convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
172 if (ps2_mouse_btn & (1<<PS2_MOUSE_X_SIGN))
173 x = ps2_mouse_x > 128 ? (int8_t)ps2_mouse_x : -127;
174 else
175 x = ps2_mouse_x < 128 ? (int8_t)ps2_mouse_x : 127;
176
177 if (ps2_mouse_btn & (1<<PS2_MOUSE_Y_SIGN))
178 y = ps2_mouse_y > 128 ? (int8_t)ps2_mouse_y : -127;
179 else
180 y = ps2_mouse_y < 128 ? (int8_t)ps2_mouse_y : 127;
181
182 // Y is needed to reverse
183 y = -y;
184
185 if (ps2_mouse_btn & PS2_MOUSE_SCROLL_BUTTON) {
186 // scroll
187 if (x > 0 || x < 0) h = (x > 64 ? 64 : (x < -64 ? -64 :x));
188 if (y > 0 || y < 0) v = (y > 64 ? 64 : (y < -64 ? -64 :y));
189 if (h || v) {
190 scrolled = true;
191 usb_mouse_send(0,0, -v/16, h/16, 0);
192 _delay_ms(100);
193 }
194 } else if (!scrolled && (ps2_mouse_btn_prev & PS2_MOUSE_SCROLL_BUTTON)) {
195 usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON);
196 _delay_ms(100);
197 usb_mouse_send(0,0,0,0, 0);
198 } else {
199 scrolled = false;
200 usb_mouse_send(x, y, 0, 0, ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
201 }
202
203 ps2_mouse_btn_prev = (ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
204 ps2_mouse_print();
205 }
206 ps2_mouse_x = 0;
207 ps2_mouse_y = 0;
208 ps2_mouse_btn = 0;
209 }
210
211 void ps2_mouse_print(void)
212 {
213 if (!debug_mouse) return;
214 print("ps2_mouse[btn|x y]: ");
215 phex(ps2_mouse_btn); print("|");
216 phex(ps2_mouse_x); print(" ");
217 phex(ps2_mouse_y); print("\n");
218 }
Imprint / Impressum