]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_mouse.c
Adding basic mouse button support
[kiibohd-controller.git] / Output / pjrcUSB / arm / usb_mouse.c
1 /* Teensyduino Core Library
2  * http://www.pjrc.com/teensy/
3  * Copyright (c) 2013 PJRC.COM, LLC.
4  * Modified by Jacob Alexander (2015-2016)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * 1. The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * 2. If the Software is incorporated into a build system that allows
18  * selection among a list of target devices, then similar target
19  * devices manufactured by PJRC.COM must be included in the list of
20  * target devices and selectable in the same manner.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  * SOFTWARE.
30  */
31
32 // ----- Includes -----
33
34 // Compiler Includes
35 #include <string.h> // for memcpy()
36
37 // Project Includes
38 #include <Lib/OutputLib.h>
39 #include <print.h>
40
41 // Local Includes
42 #include "usb_dev.h"
43 #include "usb_mouse.h"
44
45
46
47 // ----- Defines -----
48
49 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
50 #define TX_PACKET_LIMIT 3
51
52 // When the PC isn't listening, how long do we wait before discarding data?
53 #define TX_TIMEOUT_MSEC 30
54
55 #if F_CPU == 168000000
56         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
57 #elif F_CPU == 144000000
58         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
59 #elif F_CPU == 120000000
60         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
61 #elif F_CPU == 96000000
62         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
63 #elif F_CPU == 72000000
64         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
65 #elif F_CPU == 48000000
66         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
67 #elif F_CPU == 24000000
68         #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
69 #endif
70
71 //#define DEFAULT_XRES 640
72 //#define DEFAULT_YRES 480
73
74 //#define DEFAULT_XRES 800
75 //#define DEFAULT_YRES 600
76
77 //#define DEFAULT_XRES 1024
78 //#define DEFAULT_YRES 768
79
80 //#define DEFAULT_XRES 1280
81 //#define DEFAULT_YRES 720
82
83 //#define DEFAULT_XRES 1280
84 //#define DEFAULT_YRES 800
85
86 #define DEFAULT_XRES 1366
87 #define DEFAULT_YRES 768
88
89 //#define DEFAULT_XRES 1440
90 //#define DEFAULT_YRES 900
91
92 //#define DEFAULT_XRES 1920
93 //#define DEFAULT_YRES 1080
94
95 //#define DEFAULT_XRES 2560
96 //#define DEFAULT_YRES 1440
97
98 //#define DEFAULT_XRES 2560
99 //#define DEFAULT_YRES 1600
100
101 //#define DEFAULT_XRES 2880
102 //#define DEFAULT_YRES 1800
103
104 //#define DEFAULT_XRES 3840
105 //#define DEFAULT_YRES 2160
106
107 //#define DEFAULT_XRES 7680
108 //#define DEFAULT_YRES 4320
109
110
111 #define DEFAULT_XSCALE ((0x80000000ul+DEFAULT_XRES/2)/DEFAULT_XRES)
112 #define DEFAULT_YSCALE ((0x80000000ul+DEFAULT_YRES/2)/DEFAULT_YRES)
113
114
115
116 // ----- Variables -----
117
118 static uint8_t transmit_previous_timeout = 0;
119
120 // which buttons are currently pressed
121 uint8_t usb_mouse_buttons_state = 0;
122
123 static uint16_t usb_mouse_resolution_x = DEFAULT_XRES;
124 static uint16_t usb_mouse_resolution_y = DEFAULT_YRES;
125 static uint16_t usb_mouse_position_x   = DEFAULT_XRES / 2;
126 static uint16_t usb_mouse_position_y   = DEFAULT_YRES / 2;
127 static uint32_t usb_mouse_scale_x      = DEFAULT_XSCALE;
128 static uint32_t usb_mouse_scale_y      = DEFAULT_YSCALE;
129 static uint32_t usb_mouse_offset_x     = DEFAULT_XSCALE / 2 - 1;
130 static uint32_t usb_mouse_offset_y     = DEFAULT_YSCALE / 2 - 1;
131
132
133
134 // ----- Functions -----
135
136 // Process pending mouse commands
137 // XXX Missing mouse movement and wheels
138 //     Proper support will require KLL generation of the USB descriptors
139 //     Similar support will be required for joystick control
140 void usb_mouse_send()
141 {
142         uint32_t wait_count = 0;
143         usb_packet_t *tx_packet;
144
145         // Wait till ready
146         while ( 1 )
147         {
148                 if ( !usb_configuration )
149                 {
150                         erro_print("USB not configured...");
151                         return;
152                 }
153
154                 // Attempt to acquire a USB packet for the mouse endpoint
155                 if ( usb_tx_packet_count( MOUSE_ENDPOINT ) < TX_PACKET_LIMIT )
156                 {
157                         tx_packet = usb_malloc();
158                         if ( tx_packet )
159                                 break;
160                 }
161
162                 if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
163                 {
164                         transmit_previous_timeout = 1;
165                         warn_print("USB Transmit Timeout...");
166                         return;
167                 }
168                 yield();
169         }
170
171         transmit_previous_timeout = 0;
172
173         // Prepare USB Mouse Packet
174         // TODO Document each byte
175         // TODO Dynamically generate this code based on KLL requirements
176         *(tx_packet->buf + 0) = USBMouse_Buttons;
177         *(tx_packet->buf + 1) = 0;
178         *(tx_packet->buf + 2) = 0;
179         *(tx_packet->buf + 3) = 0;
180         *(tx_packet->buf + 4) = 0;
181         tx_packet->len = 5;
182         usb_tx( MOUSE_ENDPOINT, tx_packet );
183
184         // Clear status and state
185         USBMouse_Buttons = 0;
186         USBMouse_Changed = 0;
187 }
188
189
190 // Move the mouse.  x, y and wheel are -127 to 127.  Use 0 for no movement.
191 int usb_mouse_move(int8_t x, int8_t y, int8_t wheel)
192 {
193         uint32_t wait_count=0;
194         usb_packet_t *tx_packet;
195
196         //serial_print("move");
197         //serial_print("\n");
198         if (x == -128) x = -127;
199         if (y == -128) y = -127;
200         if (wheel == -128) wheel = -127;
201
202         while (1) {
203                 if (!usb_configuration) {
204                         return -1;
205                 }
206                 if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
207                         tx_packet = usb_malloc();
208                         if (tx_packet) break;
209                 }
210                 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
211                         transmit_previous_timeout = 1;
212                         return -1;
213                 }
214                 yield();
215         }
216         transmit_previous_timeout = 0;
217         *(tx_packet->buf + 0) = 1;
218         *(tx_packet->buf + 1) = usb_mouse_buttons_state;
219         *(tx_packet->buf + 2) = x;
220         *(tx_packet->buf + 3) = y;
221         *(tx_packet->buf + 4) = wheel;
222         tx_packet->len = 5;
223         usb_tx(MOUSE_ENDPOINT, tx_packet);
224         return 0;
225 }
226
227 int usb_mouse_position(uint16_t x, uint16_t y)
228 {
229         uint32_t wait_count=0, val32;
230         usb_packet_t *tx_packet;
231
232         if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
233         usb_mouse_position_x = x;
234         if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
235         usb_mouse_position_y = y;
236
237         while (1) {
238                 if (!usb_configuration) {
239                         return -1;
240                 }
241                 if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
242                         tx_packet = usb_malloc();
243                         if (tx_packet) break;
244                 }
245                 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
246                         transmit_previous_timeout = 1;
247                         return -1;
248                 }
249                 yield();
250         }
251         transmit_previous_timeout = 0;
252         *(tx_packet->buf + 0) = 2;
253         val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
254          //serial_print("position:");
255          //serial_phex16(usb_mouse_position_x);
256          //serial_print("->");
257          //serial_phex32(val32);
258         *(tx_packet->buf + 1) = val32 >> 16;
259         *(tx_packet->buf + 2) = val32 >> 24;
260         val32 = usb_mouse_position_y * usb_mouse_scale_y + usb_mouse_offset_y;
261          //serial_print(",");
262          //serial_phex16(usb_mouse_position_y);
263          //serial_print("->");
264          //serial_phex32(val32);
265          //serial_print("\n");
266         *(tx_packet->buf + 3) = val32 >> 16;
267         *(tx_packet->buf + 4) = val32 >> 24;
268         tx_packet->len = 5;
269         usb_tx(MOUSE_ENDPOINT, tx_packet);
270         return 0;
271 }
272
273 void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac)
274 {
275         if (width < 128) width = 128;
276         else if (width > 7680) width = 7680;
277         if (height < 128) height = 128;
278         else if (height > 7680) height = 7680;
279         usb_mouse_resolution_x = width;
280         usb_mouse_resolution_y = height;
281         usb_mouse_position_x = width / 2;
282         usb_mouse_position_y = height / 2;
283         usb_mouse_scale_x = (0x80000000ul + (width >> 1)) / width;
284         usb_mouse_scale_y = (0x80000000ul + (height >> 1)) / height;
285         usb_mouse_offset_x = (usb_mouse_scale_x >> 1) - 1;
286         usb_mouse_offset_y = (usb_mouse_scale_y >> 1) - 1;
287         if (mac) {
288                 // ugly workaround for Mac's HID coordinate scaling:
289                 // http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
290                 usb_mouse_offset_x += 161061273ul;
291                 usb_mouse_offset_y += 161061273ul;
292                 usb_mouse_scale_x = (1825361101ul + (width >> 1)) / width;
293                 usb_mouse_scale_y = (1825361101ul + (height >> 1)) / height;
294         }
295 }
296