2 Copyright 2014 Robin Haberkorn <robin.haberkorn@googlemail.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"
33 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
35 //#define SERIAL_MOUSE_CENTER_SCROLL
37 static void print_usb_data(const report_mouse_t *report);
39 void serial_mouse_task(void)
41 /* 5 byte ring buffer */
42 static uint8_t buffer[5];
43 static int buffer_cur = 0;
47 report_mouse_t report = {0, 0, 0, 0, 0};
55 xprintf("serial_mouse: byte: %04X\n", rcv);
58 * Synchronization: mouse(4) says that all
59 * bytes but the first one in the packet have
60 * bit 7 == 0, but this is untrue.
61 * Therefore we discard all bytes up to the
62 * first one with the characteristic bit pattern.
64 if (buffer_cur == 0 && (rcv >> 3) != 0x10)
67 buffer[buffer_cur++] = (uint8_t)rcv;
73 #ifdef SERIAL_MOUSE_CENTER_SCROLL
74 if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) {
75 /* USB HID uses only values from -127 to 127 */
76 report.h = MAX((int8_t)buffer[1], -127);
77 report.v = MAX((int8_t)buffer[2], -127);
79 print_usb_data(&report);
80 host_mouse_send(&report);
82 if (buffer[3] || buffer[4]) {
83 report.h = MAX((int8_t)buffer[3], -127);
84 report.v = MAX((int8_t)buffer[4], -127);
86 print_usb_data(&report);
87 host_mouse_send(&report);
95 * parse 5 byte packet.
96 * NOTE: We only get a complete packet
97 * if the mouse moved or the button states
100 if (!(buffer[0] & (1 << 2)))
101 report.buttons |= MOUSE_BTN1;
102 if (!(buffer[0] & (1 << 1)))
103 report.buttons |= MOUSE_BTN3;
104 if (!(buffer[0] & (1 << 0)))
105 report.buttons |= MOUSE_BTN2;
107 /* USB HID uses only values from -127 to 127 */
108 report.x = MAX((int8_t)buffer[1], -127);
109 report.y = MAX(-(int8_t)buffer[2], -127);
111 print_usb_data(&report);
112 host_mouse_send(&report);
114 if (buffer[3] || buffer[4]) {
115 report.x = MAX((int8_t)buffer[3], -127);
116 report.y = MAX(-(int8_t)buffer[4], -127);
118 print_usb_data(&report);
119 host_mouse_send(&report);
123 static void print_usb_data(const report_mouse_t *report)
128 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
129 report->buttons, report->x, report->y,
130 report->v, report->h);