]> git.gir.st - tmk_keyboard.git/blob - protocol/serial_mouse_mousesystems.c
factored out serial_mouse_init() into serial_mouse.h
[tmk_keyboard.git] / protocol / serial_mouse_mousesystems.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 <stdint.h>
19 #include <avr/io.h>
20 #include <util/delay.h>
21
22 #include "serial.h"
23 #include "serial_mouse.h"
24 #include "report.h"
25 #include "host.h"
26 #include "timer.h"
27 #include "print.h"
28 #include "debug.h"
29
30 //#define SERIAL_MOUSE_CENTER_SCROLL
31
32 static void print_usb_data(const report_mouse_t *report);
33
34 void serial_mouse_task(void)
35 {
36 /* 5 byte ring buffer */
37 static uint8_t buffer[5];
38 static int buffer_cur = 0;
39
40 int16_t rcv;
41
42 report_mouse_t report = {0, 0, 0, 0, 0};
43
44 rcv = serial_recv2();
45 if (rcv < 0)
46 /* no new data */
47 return;
48
49 if (debug_mouse)
50 xprintf("serial_mouse: byte: %04X\n", rcv);
51
52 /*
53 * Synchronization: mouse(4) says that all
54 * bytes but the first one in the packet have
55 * bit 7 == 0, but this is untrue.
56 * Therefore we discard all bytes up to the
57 * first one with the characteristic bit pattern.
58 */
59 if (buffer_cur == 0 && (rcv >> 3) != 0x10)
60 return;
61
62 buffer[buffer_cur++] = (uint8_t)rcv;
63
64 if (buffer_cur < 5)
65 return;
66 buffer_cur = 0;
67
68 #ifdef SERIAL_MOUSE_CENTER_SCROLL
69 if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) {
70 report.h = (int8_t)buffer[1];
71 /* USB HID uses only values from -127 to 127 */
72 report.h = report.h < -127 ? -127 : report.h;
73 report.v = (int8_t)buffer[2];
74 report.v = report.v < -127 ? -127 : report.v;
75
76 print_usb_data(&report);
77 host_mouse_send(&report);
78
79 if (buffer[3] || buffer[4]) {
80 report.h = (int8_t)buffer[3];
81 report.h = report.h < -127 ? -127 : report.h;
82 report.v = (int8_t)buffer[4];
83 report.v = report.v < -127 ? -127 : report.v;
84
85 print_usb_data(&report);
86 host_mouse_send(&report);
87 }
88
89 return;
90 }
91 #endif
92
93 /*
94 * parse 5 byte packet.
95 * NOTE: We only get a complete packet
96 * if the mouse moved or the button states
97 * change.
98 */
99 if (!(buffer[0] & (1 << 2)))
100 report.buttons |= MOUSE_BTN1;
101 if (!(buffer[0] & (1 << 1)))
102 report.buttons |= MOUSE_BTN3;
103 if (!(buffer[0] & (1 << 0)))
104 report.buttons |= MOUSE_BTN2;
105
106 report.x = (int8_t)buffer[1];
107 /* USB HID uses only values from -127 to 127 */
108 report.x = report.x < -127 ? -127 : report.x;
109 report.y = -(int8_t)buffer[2];
110 report.y = report.y < -127 ? -127 : report.y;
111
112 print_usb_data(&report);
113 host_mouse_send(&report);
114
115 if (buffer[3] || buffer[4]) {
116 report.x = (int8_t)buffer[3];
117 report.x = report.x < -127 ? -127 : report.x;
118 report.y = -(int8_t)buffer[4];
119 report.y = report.y < -127 ? -127 : report.y;
120
121 print_usb_data(&report);
122 host_mouse_send(&report);
123 }
124 }
125
126 static void print_usb_data(const report_mouse_t *report)
127 {
128 if (!debug_mouse)
129 return;
130
131 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
132 report->buttons, report->x, report->y,
133 report->v, report->h);
134 }
Imprint / Impressum