aboutsummaryrefslogtreecommitdiff
path: root/protocol
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2014-07-27 16:23:23 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2014-07-27 16:23:23 +0200
commit6bf8000e6322da88f91f7eb53f510c2ba72743e5 (patch)
treec60d1659baa463fd12335ea229174a5f9ccd0398 /protocol
parent79840c678e13f9a737f80048bc3b9c9c55e3fc77 (diff)
downloadqmk_firmware-6bf8000e6322da88f91f7eb53f510c2ba72743e5.tar.gz
qmk_firmware-6bf8000e6322da88f91f7eb53f510c2ba72743e5.zip
added serial mouse driver for Microsoft and Mousesystems mice
* Both drivers use the interface defined in serial_mouse.h * They should work with any UART implementation (hardware/software UART) * The Microsoft driver is currently untested. The Mousesystems driver is confirmed to work.
Diffstat (limited to 'protocol')
-rw-r--r--protocol/serial_mouse.h26
-rw-r--r--protocol/serial_mouse_microsoft.c125
-rw-r--r--protocol/serial_mouse_mousesystems.c140
3 files changed, 291 insertions, 0 deletions
diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h
new file mode 100644
index 000000000..c3c19d769
--- /dev/null
+++ b/protocol/serial_mouse.h
@@ -0,0 +1,26 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
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#ifndef SERIAL_MOUSE_H
19#define SERIAL_MOUSE_H
20
21#include <stdint.h>
22
23uint8_t serial_mouse_init(void);
24void serial_mouse_task(void);
25
26#endif
diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c
new file mode 100644
index 000000000..6b3f80648
--- /dev/null
+++ b/protocol/serial_mouse_microsoft.c
@@ -0,0 +1,125 @@
1/*
2Copyright 2011,2013 Jun Wako <wakojun@gmail.com>
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
30static void print_usb_data(const report_mouse_t *report);
31
32uint8_t serial_mouse_init(void)
33{
34 serial_init();
35 return 0;
36}
37
38void serial_mouse_task(void)
39{
40 /* 3 byte ring buffer */
41 static uint8_t buffer[3];
42 static int buffer_cur = 0;
43
44 static report_mouse_t report = {};
45
46 int16_t rcv;
47
48 rcv = serial_recv2();
49 if (rcv < 0)
50 /* no new data */
51 return;
52
53 if (debug_mouse)
54 xprintf("serial_mouse: byte: %04X\n", rcv);
55
56 /*
57 * If bit 6 is one, this signals the beginning
58 * of a 3 byte sequence/packet.
59 */
60 if (rcv & (1 << 6))
61 buffer_cur = 0;
62
63 buffer[buffer_cur] = (uint8_t)rcv;
64
65 if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) {
66 /*
67 * Logitech extension: This must be a follow-up on
68 * the last 3-byte packet signaling a middle button click
69 */
70 report.buttons |= MOUSE_BTN3;
71 report.x = report.y = 0;
72
73 print_usb_data(&report);
74 host_mouse_send(&report);
75 return;
76 }
77
78 buffer_cur++;
79
80 if (buffer_cur < 3)
81 return;
82 buffer_cur = 0;
83
84 /*
85 * parse 3 byte packet.
86 * NOTE: We only get a complete packet
87 * if the mouse moved or the button states
88 * change.
89 */
90 report.buttons = 0;
91 if (buffer[0] & (1 << 5))
92 report.buttons |= MOUSE_BTN1;
93 if (buffer[0] & (1 << 4))
94 report.buttons |= MOUSE_BTN2;
95
96 report.x = (buffer[0] << 6) | buffer[1];
97 report.y = ((buffer[0] << 4) & 0xC0) | buffer[2];
98
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;
102
103#if 0
104 if (!report.buttons && !report.x && !report.y) {
105 /*
106 * Microsoft extension: Middle mouse button pressed
107 * FIXME: I don't know how exactly this extension works.
108 */
109 report.buttons |= MOUSE_BTN3;
110 }
111#endif
112
113 print_usb_data(&report);
114 host_mouse_send(&report);
115}
116
117static void print_usb_data(const report_mouse_t *report)
118{
119 if (!debug_mouse)
120 return;
121
122 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
123 report->buttons, report->x, report->y,
124 report->v, report->h);
125}
diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c
new file mode 100644
index 000000000..ec708c911
--- /dev/null
+++ b/protocol/serial_mouse_mousesystems.c
@@ -0,0 +1,140 @@
1/*
2Copyright 2011,2013 Jun Wako <wakojun@gmail.com>
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
30#define SERIAL_MOUSE_CENTER_SCROLL
31
32static void print_usb_data(const report_mouse_t *report);
33
34uint8_t serial_mouse_init(void)
35{
36 serial_init();
37 return 0;
38}
39
40void serial_mouse_task(void)
41{
42 /* 5 byte ring buffer */
43 static uint8_t buffer[5];
44 static int buffer_cur = 0;
45
46 int16_t rcv;
47
48 report_mouse_t report = {0, 0, 0, 0, 0};
49
50 rcv = serial_recv2();
51 if (rcv < 0)
52 /* no new data */
53 return;
54
55 if (debug_mouse)
56 xprintf("serial_mouse: byte: %04X\n", rcv);
57
58 /*
59 * Synchronization: mouse(4) says that all
60 * bytes but the first one in the packet have
61 * bit 7 == 0, but this is untrue.
62 * Therefore we discard all bytes up to the
63 * first one with the characteristic bit pattern.
64 */
65 if (buffer_cur == 0 && (rcv >> 3) != 0x10)
66 return;
67
68 buffer[buffer_cur++] = (uint8_t)rcv;
69
70 if (buffer_cur < 5)
71 return;
72 buffer_cur = 0;
73
74#ifdef SERIAL_MOUSE_CENTER_SCROLL
75 if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) {
76 report.h = (int8_t)buffer[1];
77 /* USB HID uses only values from -127 to 127 */
78 report.h = report.h < -127 ? -127 : report.h;
79 report.v = (int8_t)buffer[2];
80 report.v = report.v < -127 ? -127 : report.v;
81
82 print_usb_data(&report);
83 host_mouse_send(&report);
84
85 if (buffer[3] || buffer[4]) {
86 report.h = (int8_t)buffer[3];
87 report.h = report.h < -127 ? -127 : report.h;
88 report.v = (int8_t)buffer[4];
89 report.v = report.v < -127 ? -127 : report.v;
90
91 print_usb_data(&report);
92 host_mouse_send(&report);
93 }
94
95 return;
96 }
97#endif
98
99 /*
100 * parse 5 byte packet.
101 * NOTE: We only get a complete packet
102 * if the mouse moved or the button states
103 * change.
104 */
105 if (!(buffer[0] & (1 << 2)))
106 report.buttons |= MOUSE_BTN1;
107 if (!(buffer[0] & (1 << 1)))
108 report.buttons |= MOUSE_BTN3;
109 if (!(buffer[0] & (1 << 0)))
110 report.buttons |= MOUSE_BTN2;
111
112 report.x = (int8_t)buffer[1];
113 /* USB HID uses only values from -127 to 127 */
114 report.x = report.x < -127 ? -127 : report.x;
115 report.y = -(int8_t)buffer[2];
116 report.y = report.y < -127 ? -127 : report.y;
117
118 print_usb_data(&report);
119 host_mouse_send(&report);
120
121 if (buffer[3] || buffer[4]) {
122 report.x = (int8_t)buffer[3];
123 report.x = report.x < -127 ? -127 : report.x;
124 report.y = -(int8_t)buffer[4];
125 report.y = report.y < -127 ? -127 : report.y;
126
127 print_usb_data(&report);
128 host_mouse_send(&report);
129 }
130}
131
132static void print_usb_data(const report_mouse_t *report)
133{
134 if (!debug_mouse)
135 return;
136
137 xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
138 report->buttons, report->x, report->y,
139 report->v, report->h);
140}