updated copyrights: serial_mouse.h, serial_mouse_microsoft.c and serial_mouse_mousesy...
[tmk_keyboard.git] / protocol / serial_mouse_microsoft.c
CommitLineData
6bf8000e 1/*
03fd4a6f 2Copyright 2014 Robin Haberkorn <robin.haberkorn@googlemail.com>
6bf8000e
RH
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
eb902844
RH
30#ifdef MAX
31#undef MAX
32#endif
33#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
34
6bf8000e
RH
35static void print_usb_data(const report_mouse_t *report);
36
6bf8000e
RH
37void serial_mouse_task(void)
38{
39 /* 3 byte ring buffer */
40 static uint8_t buffer[3];
41 static int buffer_cur = 0;
42
43 static report_mouse_t report = {};
44
45 int16_t rcv;
46
47 rcv = serial_recv2();
48 if (rcv < 0)
49 /* no new data */
50 return;
51
52 if (debug_mouse)
53 xprintf("serial_mouse: byte: %04X\n", rcv);
54
55 /*
56 * If bit 6 is one, this signals the beginning
57 * of a 3 byte sequence/packet.
58 */
59 if (rcv & (1 << 6))
60 buffer_cur = 0;
61
62 buffer[buffer_cur] = (uint8_t)rcv;
63
64 if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) {
65 /*
66 * Logitech extension: This must be a follow-up on
67 * the last 3-byte packet signaling a middle button click
68 */
69 report.buttons |= MOUSE_BTN3;
70 report.x = report.y = 0;
71
72 print_usb_data(&report);
73 host_mouse_send(&report);
74 return;
75 }
76
77 buffer_cur++;
78
79 if (buffer_cur < 3)
80 return;
81 buffer_cur = 0;
82
83 /*
84 * parse 3 byte packet.
85 * NOTE: We only get a complete packet
86 * if the mouse moved or the button states
87 * change.
88 */
89 report.buttons = 0;
90 if (buffer[0] & (1 << 5))
91 report.buttons |= MOUSE_BTN1;
92 if (buffer[0] & (1 << 4))
93 report.buttons |= MOUSE_BTN2;
94
95 report.x = (buffer[0] << 6) | buffer[1];
96 report.y = ((buffer[0] << 4) & 0xC0) | buffer[2];
97
98 /* USB HID uses values from -127 to 127 only */
eb902844
RH
99 report.x = MAX(report.x, -127);
100 report.y = MAX(report.y, -127);
6bf8000e
RH
101
102#if 0
103 if (!report.buttons && !report.x && !report.y) {
104 /*
105 * Microsoft extension: Middle mouse button pressed
106 * FIXME: I don't know how exactly this extension works.
107 */
108 report.buttons |= MOUSE_BTN3;
109 }
110#endif
111
112 print_usb_data(&report);
113 host_mouse_send(&report);
114}
115
116static void print_usb_data(const report_mouse_t *report)
117{
118 if (!debug_mouse)
119 return;
120
121 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
122 report->buttons, report->x, report->y,
123 report->v, report->h);
124}
Imprint / Impressum