]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/serial_mouse_microsoft.c
factored out serial_mouse_init() into serial_mouse.h
[tmk_firmware.git] / protocol / serial_mouse_microsoft.c
1 /*
2 Copyright 2011,2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
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
30 static void print_usb_data(const report_mouse_t *report);
31
32 void serial_mouse_task(void)
33 {
34     /* 3 byte ring buffer */
35     static uint8_t buffer[3];
36     static int buffer_cur = 0;
37
38     static report_mouse_t report = {};
39
40     int16_t rcv;
41
42     rcv = serial_recv2();
43     if (rcv < 0)
44         /* no new data */
45         return;
46
47     if (debug_mouse)
48         xprintf("serial_mouse: byte: %04X\n", rcv);
49
50     /*
51      * If bit 6 is one, this signals the beginning
52      * of a 3 byte sequence/packet.
53      */
54     if (rcv & (1 << 6))
55         buffer_cur = 0;
56
57     buffer[buffer_cur] = (uint8_t)rcv;
58
59     if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) {
60         /*
61          * Logitech extension: This must be a follow-up on
62          * the last 3-byte packet signaling a middle button click
63          */
64         report.buttons |= MOUSE_BTN3;
65         report.x = report.y = 0;
66
67         print_usb_data(&report);
68         host_mouse_send(&report);
69         return;
70     }
71
72     buffer_cur++;
73
74     if (buffer_cur < 3)
75         return;
76     buffer_cur = 0;
77
78     /*
79      * parse 3 byte packet.
80      * NOTE: We only get a complete packet
81      * if the mouse moved or the button states
82      * change.
83      */
84     report.buttons = 0;
85     if (buffer[0] & (1 << 5))
86         report.buttons |= MOUSE_BTN1;
87     if (buffer[0] & (1 << 4))
88         report.buttons |= MOUSE_BTN2;
89
90     report.x = (buffer[0] << 6) | buffer[1];
91     report.y = ((buffer[0] << 4) & 0xC0) | buffer[2];
92
93     /* USB HID uses values from -127 to 127 only */
94     report.x = report.x < -127 ? -127 : report.x;
95     report.y = report.y < -127 ? -127 : report.y;
96
97 #if 0
98     if (!report.buttons && !report.x && !report.y) {
99         /*
100          * Microsoft extension: Middle mouse button pressed
101          * FIXME: I don't know how exactly this extension works.
102          */
103         report.buttons |= MOUSE_BTN3;
104     }
105 #endif
106
107     print_usb_data(&report);
108     host_mouse_send(&report);
109 }
110
111 static void print_usb_data(const report_mouse_t *report)
112 {
113     if (!debug_mouse)
114         return;
115
116     xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
117             report->buttons, report->x, report->y,
118             report->v, report->h);
119 }