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 static void print_usb_data(const report_mouse_t *report);
37 void serial_mouse_task(void)
39 /* 3 byte ring buffer */
40 static uint8_t buffer[3];
41 static int buffer_cur = 0;
43 static report_mouse_t report = {};
53 xprintf("serial_mouse: byte: %04X\n", rcv);
56 * If bit 6 is one, this signals the beginning
57 * of a 3 byte sequence/packet.
62 buffer[buffer_cur] = (uint8_t)rcv;
64 if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) {
66 * Logitech extension: This must be a follow-up on
67 * the last 3-byte packet signaling a middle button click
69 report.buttons |= MOUSE_BTN3;
70 report.x = report.y = 0;
72 print_usb_data(&report);
73 host_mouse_send(&report);
84 * parse 3 byte packet.
85 * NOTE: We only get a complete packet
86 * if the mouse moved or the button states
90 if (buffer[0] & (1 << 5))
91 report.buttons |= MOUSE_BTN1;
92 if (buffer[0] & (1 << 4))
93 report.buttons |= MOUSE_BTN2;
95 report.x = (buffer[0] << 6) | buffer[1];
96 report.y = ((buffer[0] << 4) & 0xC0) | buffer[2];
98 /* USB HID uses values from -127 to 127 only */
99 report.x = MAX(report.x, -127);
100 report.y = MAX(report.y, -127);
103 if (!report.buttons && !report.x && !report.y) {
105 * Microsoft extension: Middle mouse button pressed
106 * FIXME: I don't know how exactly this extension works.
108 report.buttons |= MOUSE_BTN3;
112 print_usb_data(&report);
113 host_mouse_send(&report);
116 static void print_usb_data(const report_mouse_t *report)
121 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
122 report->buttons, report->x, report->y,
123 report->v, report->h);