]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/ps2_mouse.c
Fix delay of PS/2 mouse init
[tmk_firmware.git] / 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.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
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;
41
42 static uint8_t ps2_mouse_btn_prev = 0;
43
44
45 uint8_t ps2_mouse_init(void) {
46     uint8_t rcv;
47
48     if (!ps2_mouse_enable) return 1;
49
50     ps2_host_init();
51
52     _delay_ms(1000);    // wait for powering up
53
54     // send Reset
55     rcv = ps2_host_send(0xFF);
56     print("ps2_mouse_init: send Reset: ");
57     phex(rcv); phex(ps2_error); print("\n");
58
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");
63
64     // read Device ID
65     rcv = ps2_host_recv();
66     print("ps2_mouse_init: read DevID: ");
67     phex(rcv); phex(ps2_error); print("\n");
68
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");
73
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");
78
79     return 0;
80 }
81
82 uint8_t ps2_mouse_read(void)
83 {
84     uint8_t rcv;
85
86     if (!ps2_mouse_enable) return 1;
87
88     rcv = ps2_host_send(0xEB);
89
90     if(rcv==0xFA) {
91         ps2_mouse_btn = ps2_host_recv_response();
92         ps2_mouse_x = ps2_host_recv_response();
93         ps2_mouse_y = ps2_host_recv_response();
94     }
95     return 0;
96 }
97
98 bool ps2_mouse_changed(void)
99 {
100     return (ps2_mouse_x || ps2_mouse_y || (ps2_mouse_btn & PS2_MOUSE_BTN_MASK) != ps2_mouse_btn_prev);
101 }
102
103 #define PS2_MOUSE_SCROLL_BUTTON 0x04
104 void ps2_mouse_usb_send(void)
105 {
106     static bool scrolled = false;
107
108     if (!ps2_mouse_enable) return;
109
110     if (ps2_mouse_changed()) {
111         int8_t x, y, v, h;
112         x = y = v = h = 0;
113
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;
117         else
118             x = ps2_mouse_x < 128 ? (int8_t)ps2_mouse_x : 127;
119
120         if (ps2_mouse_btn & (1<<PS2_MOUSE_Y_SIGN))
121             y = ps2_mouse_y > 128 ? (int8_t)ps2_mouse_y : -127;
122         else
123             y = ps2_mouse_y < 128 ? (int8_t)ps2_mouse_y : 127;
124
125         // Y is needed to reverse
126         y = -y;
127
128         if (ps2_mouse_btn & PS2_MOUSE_SCROLL_BUTTON) {
129             // scroll
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));
132             if (h || v) {
133                 scrolled = true;
134                 usb_mouse_send(0,0, -v/16, h/16, 0);
135                 _delay_ms(100);
136             }
137         } else if (!scrolled && (ps2_mouse_btn_prev & PS2_MOUSE_SCROLL_BUTTON)) {
138             usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON);
139             _delay_ms(100);
140             usb_mouse_send(0,0,0,0, 0);
141         } else { 
142             scrolled = false;
143             usb_mouse_send(x, y, 0, 0, ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
144         }
145
146         ps2_mouse_btn_prev = (ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
147         ps2_mouse_print();
148     }
149     ps2_mouse_x = 0;
150     ps2_mouse_y = 0;
151     ps2_mouse_btn = 0;
152 }
153
154 void ps2_mouse_print(void)
155 {
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");
161 }
162
163
164 /* PS/2 Mouse Synopsis
165  * http://www.computer-engineering.org/ps2mouse/
166  *
167  * Command:
168  * 0xFF: Reset
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
175  * 0xEB: Read Data
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
181  *
182  * Mode:
183  * Stream Mode: devices sends the data when it changs its state
184  * Remote Mode: host polls the data periodically
185  *
186  * This code uses Remote Mode and polls the data with Read Data(0xEB).
187  *
188  * Data format:
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)
194  */