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 static void print_usb_data(const report_mouse_t *report);
32 uint8_t serial_mouse_init(void)
38 void serial_mouse_task(void)
40 /* 3 byte ring buffer */
41 static uint8_t buffer[3];
42 static int buffer_cur = 0;
44 static report_mouse_t report = {};
54 xprintf("serial_mouse: byte: %04X\n", rcv);
57 * If bit 6 is one, this signals the beginning
58 * of a 3 byte sequence/packet.
63 buffer[buffer_cur] = (uint8_t)rcv;
65 if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) {
67 * Logitech extension: This must be a follow-up on
68 * the last 3-byte packet signaling a middle button click
70 report.buttons |= MOUSE_BTN3;
71 report.x = report.y = 0;
73 print_usb_data(&report);
74 host_mouse_send(&report);
85 * parse 3 byte packet.
86 * NOTE: We only get a complete packet
87 * if the mouse moved or the button states
91 if (buffer[0] & (1 << 5))
92 report.buttons |= MOUSE_BTN1;
93 if (buffer[0] & (1 << 4))
94 report.buttons |= MOUSE_BTN2;
96 report.x = (buffer[0] << 6) | buffer[1];
97 report.y = ((buffer[0] << 4) & 0xC0) | buffer[2];
99 /* USB HID uses values from -127 to 127 only */
100 report.x = report.x < -127 ? -127 : report.x;
101 report.y = report.y < -127 ? -127 : report.y;
104 if (!report.buttons && !report.x && !report.y) {
106 * Microsoft extension: Middle mouse button pressed
107 * FIXME: I don't know how exactly this extension works.
109 report.buttons |= MOUSE_BTN3;
113 print_usb_data(&report);
114 host_mouse_send(&report);
117 static void print_usb_data(const report_mouse_t *report)
122 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
123 report->buttons, report->x, report->y,
124 report->v, report->h);