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>
23 #include "serial_mouse.h"
30 #define SERIAL_MOUSE_CENTER_SCROLL
32 static void print_usb_data(const report_mouse_t *report);
34 uint8_t serial_mouse_init(void)
40 void serial_mouse_task(void)
42 /* 5 byte ring buffer */
43 static uint8_t buffer[5];
44 static int buffer_cur = 0;
48 report_mouse_t report = {0, 0, 0, 0, 0};
56 xprintf("serial_mouse: byte: %04X\n", rcv);
59 * Synchronization: mouse(4) says that all
60 * bytes but the first one in the packet have
61 * bit 7 == 0, but this is untrue.
62 * Therefore we discard all bytes up to the
63 * first one with the characteristic bit pattern.
65 if (buffer_cur == 0 && (rcv >> 3) != 0x10)
68 buffer[buffer_cur++] = (uint8_t)rcv;
74 #ifdef SERIAL_MOUSE_CENTER_SCROLL
75 if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) {
76 report.h = (int8_t)buffer[1];
77 /* USB HID uses only values from -127 to 127 */
78 report.h = report.h < -127 ? -127 : report.h;
79 report.v = (int8_t)buffer[2];
80 report.v = report.v < -127 ? -127 : report.v;
82 print_usb_data(&report);
83 host_mouse_send(&report);
85 if (buffer[3] || buffer[4]) {
86 report.h = (int8_t)buffer[3];
87 report.h = report.h < -127 ? -127 : report.h;
88 report.v = (int8_t)buffer[4];
89 report.v = report.v < -127 ? -127 : report.v;
91 print_usb_data(&report);
92 host_mouse_send(&report);
100 * parse 5 byte packet.
101 * NOTE: We only get a complete packet
102 * if the mouse moved or the button states
105 if (!(buffer[0] & (1 << 2)))
106 report.buttons |= MOUSE_BTN1;
107 if (!(buffer[0] & (1 << 1)))
108 report.buttons |= MOUSE_BTN3;
109 if (!(buffer[0] & (1 << 0)))
110 report.buttons |= MOUSE_BTN2;
112 report.x = (int8_t)buffer[1];
113 /* USB HID uses only values from -127 to 127 */
114 report.x = report.x < -127 ? -127 : report.x;
115 report.y = -(int8_t)buffer[2];
116 report.y = report.y < -127 ? -127 : report.y;
118 print_usb_data(&report);
119 host_mouse_send(&report);
121 if (buffer[3] || buffer[4]) {
122 report.x = (int8_t)buffer[3];
123 report.x = report.x < -127 ? -127 : report.x;
124 report.y = -(int8_t)buffer[4];
125 report.y = report.y < -127 ? -127 : report.y;
127 print_usb_data(&report);
128 host_mouse_send(&report);
132 static void print_usb_data(const report_mouse_t *report)
137 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
138 report->buttons, report->x, report->y,
139 report->v, report->h);