From 6bf8000e6322da88f91f7eb53f510c2ba72743e5 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 16:23:23 +0200 Subject: 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. --- protocol/serial_mouse.h | 26 +++++++ protocol/serial_mouse_microsoft.c | 125 +++++++++++++++++++++++++++++++ protocol/serial_mouse_mousesystems.c | 140 +++++++++++++++++++++++++++++++++++ 3 files changed, 291 insertions(+) create mode 100644 protocol/serial_mouse.h create mode 100644 protocol/serial_mouse_microsoft.c create mode 100644 protocol/serial_mouse_mousesystems.c (limited to 'protocol') diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h new file mode 100644 index 0000000000..c3c19d7696 --- /dev/null +++ b/protocol/serial_mouse.h @@ -0,0 +1,26 @@ +/* +Copyright 2011 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef SERIAL_MOUSE_H +#define SERIAL_MOUSE_H + +#include + +uint8_t serial_mouse_init(void); +void serial_mouse_task(void); + +#endif diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c new file mode 100644 index 0000000000..6b3f80648b --- /dev/null +++ b/protocol/serial_mouse_microsoft.c @@ -0,0 +1,125 @@ +/* +Copyright 2011,2013 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include +#include + +#include "serial.h" +#include "serial_mouse.h" +#include "report.h" +#include "host.h" +#include "timer.h" +#include "print.h" +#include "debug.h" + +static void print_usb_data(const report_mouse_t *report); + +uint8_t serial_mouse_init(void) +{ + serial_init(); + return 0; +} + +void serial_mouse_task(void) +{ + /* 3 byte ring buffer */ + static uint8_t buffer[3]; + static int buffer_cur = 0; + + static report_mouse_t report = {}; + + int16_t rcv; + + rcv = serial_recv2(); + if (rcv < 0) + /* no new data */ + return; + + if (debug_mouse) + xprintf("serial_mouse: byte: %04X\n", rcv); + + /* + * If bit 6 is one, this signals the beginning + * of a 3 byte sequence/packet. + */ + if (rcv & (1 << 6)) + buffer_cur = 0; + + buffer[buffer_cur] = (uint8_t)rcv; + + if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) { + /* + * Logitech extension: This must be a follow-up on + * the last 3-byte packet signaling a middle button click + */ + report.buttons |= MOUSE_BTN3; + report.x = report.y = 0; + + print_usb_data(&report); + host_mouse_send(&report); + return; + } + + buffer_cur++; + + if (buffer_cur < 3) + return; + buffer_cur = 0; + + /* + * parse 3 byte packet. + * NOTE: We only get a complete packet + * if the mouse moved or the button states + * change. + */ + report.buttons = 0; + if (buffer[0] & (1 << 5)) + report.buttons |= MOUSE_BTN1; + if (buffer[0] & (1 << 4)) + report.buttons |= MOUSE_BTN2; + + report.x = (buffer[0] << 6) | buffer[1]; + report.y = ((buffer[0] << 4) & 0xC0) | buffer[2]; + + /* USB HID uses values from -127 to 127 only */ + report.x = report.x < -127 ? -127 : report.x; + report.y = report.y < -127 ? -127 : report.y; + +#if 0 + if (!report.buttons && !report.x && !report.y) { + /* + * Microsoft extension: Middle mouse button pressed + * FIXME: I don't know how exactly this extension works. + */ + report.buttons |= MOUSE_BTN3; + } +#endif + + print_usb_data(&report); + host_mouse_send(&report); +} + +static void print_usb_data(const report_mouse_t *report) +{ + if (!debug_mouse) + return; + + xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", + report->buttons, report->x, report->y, + report->v, report->h); +} diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c new file mode 100644 index 0000000000..ec708c911f --- /dev/null +++ b/protocol/serial_mouse_mousesystems.c @@ -0,0 +1,140 @@ +/* +Copyright 2011,2013 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include +#include + +#include "serial.h" +#include "serial_mouse.h" +#include "report.h" +#include "host.h" +#include "timer.h" +#include "print.h" +#include "debug.h" + +#define SERIAL_MOUSE_CENTER_SCROLL + +static void print_usb_data(const report_mouse_t *report); + +uint8_t serial_mouse_init(void) +{ + serial_init(); + return 0; +} + +void serial_mouse_task(void) +{ + /* 5 byte ring buffer */ + static uint8_t buffer[5]; + static int buffer_cur = 0; + + int16_t rcv; + + report_mouse_t report = {0, 0, 0, 0, 0}; + + rcv = serial_recv2(); + if (rcv < 0) + /* no new data */ + return; + + if (debug_mouse) + xprintf("serial_mouse: byte: %04X\n", rcv); + + /* + * Synchronization: mouse(4) says that all + * bytes but the first one in the packet have + * bit 7 == 0, but this is untrue. + * Therefore we discard all bytes up to the + * first one with the characteristic bit pattern. + */ + if (buffer_cur == 0 && (rcv >> 3) != 0x10) + return; + + buffer[buffer_cur++] = (uint8_t)rcv; + + if (buffer_cur < 5) + return; + buffer_cur = 0; + +#ifdef SERIAL_MOUSE_CENTER_SCROLL + if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) { + report.h = (int8_t)buffer[1]; + /* USB HID uses only values from -127 to 127 */ + report.h = report.h < -127 ? -127 : report.h; + report.v = (int8_t)buffer[2]; + report.v = report.v < -127 ? -127 : report.v; + + print_usb_data(&report); + host_mouse_send(&report); + + if (buffer[3] || buffer[4]) { + report.h = (int8_t)buffer[3]; + report.h = report.h < -127 ? -127 : report.h; + report.v = (int8_t)buffer[4]; + report.v = report.v < -127 ? -127 : report.v; + + print_usb_data(&report); + host_mouse_send(&report); + } + + return; + } +#endif + + /* + * parse 5 byte packet. + * NOTE: We only get a complete packet + * if the mouse moved or the button states + * change. + */ + if (!(buffer[0] & (1 << 2))) + report.buttons |= MOUSE_BTN1; + if (!(buffer[0] & (1 << 1))) + report.buttons |= MOUSE_BTN3; + if (!(buffer[0] & (1 << 0))) + report.buttons |= MOUSE_BTN2; + + report.x = (int8_t)buffer[1]; + /* USB HID uses only values from -127 to 127 */ + report.x = report.x < -127 ? -127 : report.x; + report.y = -(int8_t)buffer[2]; + report.y = report.y < -127 ? -127 : report.y; + + print_usb_data(&report); + host_mouse_send(&report); + + if (buffer[3] || buffer[4]) { + report.x = (int8_t)buffer[3]; + report.x = report.x < -127 ? -127 : report.x; + report.y = -(int8_t)buffer[4]; + report.y = report.y < -127 ? -127 : report.y; + + print_usb_data(&report); + host_mouse_send(&report); + } +} + +static void print_usb_data(const report_mouse_t *report) +{ + if (!debug_mouse) + return; + + xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", + report->buttons, report->x, report->y, + report->v, report->h); +} -- cgit v1.2.3 From 7dde35d4f7cc3cc1569a31e752ea4043133b1a72 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:07:26 +0200 Subject: integrated serial mouse drivers as a feature into the firmware architecture * can be enabled by defining Makefile macro SERIAL_MOUSE_MICROSOFT_ENABLE or SERIAL_MOUSE_MOUSESYSTEMS_ENABLE. * Serial implementation can be chosen via SERIAL_MOUSE_USE_SOFT and SERIAL_MOUSE_USE_UART macros * UART configuration still has to be done in config.h: I added working clauses for both mouse protocols to ps2_usb's config.h --- protocol/lufa/lufa.c | 12 ++++++++++++ protocol/serial_mouse_mousesystems.c | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'protocol') diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c index 16a602df13..58201e5c98 100644 --- a/protocol/lufa/lufa.c +++ b/protocol/lufa/lufa.c @@ -49,6 +49,10 @@ #endif #include "suspend.h" +#ifdef SERIAL_MOUSE_ENABLE +#include "serial_mouse.h" +#endif + #include "descriptor.h" #include "lufa.h" @@ -571,6 +575,10 @@ int main(void) sleep_led_init(); #endif +#ifdef SERIAL_MOUSE_ENABLE + serial_mouse_init(); +#endif + print("Keyboard start.\n"); while (1) { while (USB_DeviceState == DEVICE_STATE_Suspended) { @@ -582,6 +590,10 @@ int main(void) keyboard_task(); +#ifdef SERIAL_MOUSE_ENABLE + serial_mouse_task(); +#endif + #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index ec708c911f..68b2b5b359 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -27,7 +27,7 @@ along with this program. If not, see . #include "print.h" #include "debug.h" -#define SERIAL_MOUSE_CENTER_SCROLL +//#define SERIAL_MOUSE_CENTER_SCROLL static void print_usb_data(const report_mouse_t *report); -- cgit v1.2.3 From 0bfba7acc4e05e66c8ab448286fc51bc94d03a57 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:18:14 +0200 Subject: factored out serial_mouse_init() into serial_mouse.h --- protocol/serial_mouse.h | 9 ++++++++- protocol/serial_mouse_microsoft.c | 6 ------ protocol/serial_mouse_mousesystems.c | 6 ------ 3 files changed, 8 insertions(+), 13 deletions(-) (limited to 'protocol') diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h index c3c19d7696..2ccd3d900c 100644 --- a/protocol/serial_mouse.h +++ b/protocol/serial_mouse.h @@ -20,7 +20,14 @@ along with this program. If not, see . #include -uint8_t serial_mouse_init(void); +#include "serial.h" + +static inline uint8_t serial_mouse_init(void) +{ + serial_init(); + return 0; +} + void serial_mouse_task(void); #endif diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c index 6b3f80648b..f83036a317 100644 --- a/protocol/serial_mouse_microsoft.c +++ b/protocol/serial_mouse_microsoft.c @@ -29,12 +29,6 @@ along with this program. If not, see . static void print_usb_data(const report_mouse_t *report); -uint8_t serial_mouse_init(void) -{ - serial_init(); - return 0; -} - void serial_mouse_task(void) { /* 3 byte ring buffer */ diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index 68b2b5b359..36c67386f7 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -31,12 +31,6 @@ along with this program. If not, see . static void print_usb_data(const report_mouse_t *report); -uint8_t serial_mouse_init(void) -{ - serial_init(); - return 0; -} - void serial_mouse_task(void) { /* 5 byte ring buffer */ -- cgit v1.2.3 From eb902844946f0bda7da76cdb1e9aafae4881b63c Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:26:44 +0200 Subject: serial_mouse: simplified clipping of X/Y/V/H changes below -127 using a MAX macro --- protocol/serial_mouse_microsoft.c | 9 +++++++-- protocol/serial_mouse_mousesystems.c | 29 +++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) (limited to 'protocol') diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c index f83036a317..54fedae778 100644 --- a/protocol/serial_mouse_microsoft.c +++ b/protocol/serial_mouse_microsoft.c @@ -27,6 +27,11 @@ along with this program. If not, see . #include "print.h" #include "debug.h" +#ifdef MAX +#undef MAX +#endif +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) + static void print_usb_data(const report_mouse_t *report); void serial_mouse_task(void) @@ -91,8 +96,8 @@ void serial_mouse_task(void) report.y = ((buffer[0] << 4) & 0xC0) | buffer[2]; /* USB HID uses values from -127 to 127 only */ - report.x = report.x < -127 ? -127 : report.x; - report.y = report.y < -127 ? -127 : report.y; + report.x = MAX(report.x, -127); + report.y = MAX(report.y, -127); #if 0 if (!report.buttons && !report.x && !report.y) { diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index 36c67386f7..c4ddbb8452 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -27,6 +27,11 @@ along with this program. If not, see . #include "print.h" #include "debug.h" +#ifdef MAX +#undef MAX +#endif +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) + //#define SERIAL_MOUSE_CENTER_SCROLL static void print_usb_data(const report_mouse_t *report); @@ -67,20 +72,16 @@ void serial_mouse_task(void) #ifdef SERIAL_MOUSE_CENTER_SCROLL if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) { - report.h = (int8_t)buffer[1]; /* USB HID uses only values from -127 to 127 */ - report.h = report.h < -127 ? -127 : report.h; - report.v = (int8_t)buffer[2]; - report.v = report.v < -127 ? -127 : report.v; + report.h = MAX((int8_t)buffer[1], -127); + report.v = MAX((int8_t)buffer[2], -127); print_usb_data(&report); host_mouse_send(&report); if (buffer[3] || buffer[4]) { - report.h = (int8_t)buffer[3]; - report.h = report.h < -127 ? -127 : report.h; - report.v = (int8_t)buffer[4]; - report.v = report.v < -127 ? -127 : report.v; + report.h = MAX((int8_t)buffer[3], -127); + report.v = MAX((int8_t)buffer[4], -127); print_usb_data(&report); host_mouse_send(&report); @@ -103,20 +104,16 @@ void serial_mouse_task(void) if (!(buffer[0] & (1 << 0))) report.buttons |= MOUSE_BTN2; - report.x = (int8_t)buffer[1]; /* USB HID uses only values from -127 to 127 */ - report.x = report.x < -127 ? -127 : report.x; - report.y = -(int8_t)buffer[2]; - report.y = report.y < -127 ? -127 : report.y; + report.x = MAX((int8_t)buffer[1], -127); + report.y = MAX(-(int8_t)buffer[2], -127); print_usb_data(&report); host_mouse_send(&report); if (buffer[3] || buffer[4]) { - report.x = (int8_t)buffer[3]; - report.x = report.x < -127 ? -127 : report.x; - report.y = -(int8_t)buffer[4]; - report.y = report.y < -127 ? -127 : report.y; + report.x = MAX((int8_t)buffer[3], -127); + report.y = MAX(-(int8_t)buffer[4], -127); print_usb_data(&report); host_mouse_send(&report); -- cgit v1.2.3 From 03fd4a6ff0fcccf8d7683b7fac3d9e6ae4fb635e Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 27 Jul 2014 17:58:41 +0200 Subject: updated copyrights: serial_mouse.h, serial_mouse_microsoft.c and serial_mouse_mousesystems.c are new --- protocol/serial_mouse.h | 2 +- protocol/serial_mouse_microsoft.c | 2 +- protocol/serial_mouse_mousesystems.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'protocol') diff --git a/protocol/serial_mouse.h b/protocol/serial_mouse.h index 2ccd3d900c..226314fc0e 100644 --- a/protocol/serial_mouse.h +++ b/protocol/serial_mouse.h @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako +Copyright 2014 Robin Haberkorn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/protocol/serial_mouse_microsoft.c b/protocol/serial_mouse_microsoft.c index 54fedae778..ab74b7cdd3 100644 --- a/protocol/serial_mouse_microsoft.c +++ b/protocol/serial_mouse_microsoft.c @@ -1,5 +1,5 @@ /* -Copyright 2011,2013 Jun Wako +Copyright 2014 Robin Haberkorn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/protocol/serial_mouse_mousesystems.c b/protocol/serial_mouse_mousesystems.c index c4ddbb8452..cfe8996216 100644 --- a/protocol/serial_mouse_mousesystems.c +++ b/protocol/serial_mouse_mousesystems.c @@ -1,5 +1,5 @@ /* -Copyright 2011,2013 Jun Wako +Copyright 2014 Robin Haberkorn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by -- cgit v1.2.3