diff options
author | tmk <nobody@nowhere> | 2014-08-26 05:02:37 +0900 |
---|---|---|
committer | tmk <nobody@nowhere> | 2014-08-26 05:02:37 +0900 |
commit | b316f19871ab39e6a75146d74a3b5cca37165f3c (patch) | |
tree | 66d06b895fea2e85da612d06604ab59234dc46b7 | |
parent | 10eb70acb4c8a03fc5bda32d9c23fc41266aef7a (diff) | |
parent | 03fd4a6ff0fcccf8d7683b7fac3d9e6ae4fb635e (diff) |
Merge branch 'serial-mouse' of git://github.com/rhaberkorn/tmk_keyboard into rhaberkorn-serial-mouse
-rw-r--r-- | converter/ps2_usb/Makefile | 13 | ||||
-rw-r--r-- | converter/ps2_usb/config.h | 40 | ||||
-rw-r--r-- | protocol.mk | 20 | ||||
-rw-r--r-- | protocol/lufa/lufa.c | 12 | ||||
-rw-r--r-- | protocol/serial_mouse.h | 33 | ||||
-rw-r--r-- | protocol/serial_mouse_microsoft.c | 124 | ||||
-rw-r--r-- | protocol/serial_mouse_mousesystems.c | 131 |
7 files changed, 372 insertions, 1 deletions
diff --git a/converter/ps2_usb/Makefile b/converter/ps2_usb/Makefile index 1dd23c157e..f20039c6f0 100644 --- a/converter/ps2_usb/Makefile +++ b/converter/ps2_usb/Makefile @@ -91,6 +91,19 @@ PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomen #PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin #PS2_USE_BUSYWAIT = yes # uses primitive reference code +# Serial Mouse Options +# You can choose a mouse protocol and the implementation of +# the underlying serial connection. +# +#SERIAL_MOUSE_MICROSOFT_ENABLE = yes # Enable support for Microsoft-compatible mice +#SERIAL_MOUSE_MOUSESYSTEMS_ENABLE = yes # Enable support for Mousesystems-compatible mice +#SERIAL_MOUSE_USE_UART = yes # use hardware UART for serial connection +#SERIAL_MOUSE_USE_SOFT = yes # use software serial implementation + +# Optional serial mouse driver features +# Support scrolling while holding the middle mouse button +# (currently only supported for Mousesystems mice): +#OPT_DEFS += -DSERIAL_MOUSE_CENTER_SCROLL # Optimize size but this may cause error "relocation truncated to fit" #EXTRALDFLAGS = -Wl,--relax diff --git a/converter/ps2_usb/config.h b/converter/ps2_usb/config.h index c9bab1b072..5b644002dc 100644 --- a/converter/ps2_usb/config.h +++ b/converter/ps2_usb/config.h @@ -69,7 +69,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define PS2_DATA_PORT PORTD #define PS2_DATA_PIN PIND #define PS2_DATA_DDR DDRD -#define PS2_DATA_BIT 2 +#define PS2_DATA_BIT 0 #define PS2_INT_INIT() do { \ EICRA |= ((1<<ISC11) | \ (0<<ISC10)); \ @@ -170,4 +170,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #endif #endif +#ifdef SERIAL_MOUSE_MICROSOFT + /* + * Serial(USART) configuration (for Microsoft serial mice) + * asynchronous, positive logic, 1200baud, bit order: LSB first + * 1-start bit, 7-data bit, no parity, 1-stop bit + */ + #define SERIAL_UART_BAUD 1200 + #define SERIAL_UART_DATA UDR1 + #define SERIAL_UART_UBRR ((F_CPU/(16UL*SERIAL_UART_BAUD))-1) + #define SERIAL_UART_RXD_VECT USART1_RX_vect + #define SERIAL_UART_TXD_READY (UCSR1A&(1<<UDRE1)) + #define SERIAL_UART_INIT() do { \ + UBRR1L = (uint8_t) SERIAL_UART_UBRR; /* baud rate */ \ + UBRR1H = (uint8_t) (SERIAL_UART_UBRR>>8); /* baud rate */ \ + UCSR1B |= (1<<RXCIE1) | (1<<RXEN1); /* RX interrupt, RX: enable */ \ + UCSR1C = (1<<UCSZ11) | (0<<UCSZ10); /* no parity, 1 stop bit, 7-bit characters */ \ + sei(); \ + } while(0) +#elif defined(SERIAL_MOUSE_MOUSESYSTEMS) + /* + * Serial(USART) configuration (for Mousesystems serial mice) + * asynchronous, positive logic, 1200baud, bit order: LSB first + * 1-start bit, 8-data bit, no parity, 1-stop bit + */ + #define SERIAL_UART_BAUD 1200 + #define SERIAL_UART_DATA UDR1 + #define SERIAL_UART_UBRR ((F_CPU/(16UL*SERIAL_UART_BAUD))-1) + #define SERIAL_UART_RXD_VECT USART1_RX_vect + #define SERIAL_UART_TXD_READY (UCSR1A&(1<<UDRE1)) + #define SERIAL_UART_INIT() do { \ + UBRR1L = (uint8_t) SERIAL_UART_UBRR; /* baud rate */ \ + UBRR1H = (uint8_t) (SERIAL_UART_UBRR>>8); /* baud rate */ \ + UCSR1B |= (1<<RXCIE1) | (1<<RXEN1); /* RX interrupt, RX: enable */ \ + UCSR1C = (1<<UCSZ11) | (1<<UCSZ10); /* no parity, 1 stop bit, 8-bit characters */ \ + sei(); \ + } while(0) +#endif + #endif diff --git a/protocol.mk b/protocol.mk index 7f561e62d6..de7014e866 100644 --- a/protocol.mk +++ b/protocol.mk @@ -23,5 +23,25 @@ ifdef PS2_USE_USART endif +ifdef SERIAL_MOUSE_MICROSOFT_ENABLE + SRC += $(PROTOCOL_DIR)/serial_mouse_microsoft.c + OPT_DEFS += -DSERIAL_MOUSE_ENABLE -DSERIAL_MOUSE_MICROSOFT \ + -DMOUSE_ENABLE +endif + +ifdef SERIAL_MOUSE_MOUSESYSTEMS_ENABLE + SRC += $(PROTOCOL_DIR)/serial_mouse_mousesystems.c + OPT_DEFS += -DSERIAL_MOUSE_ENABLE -DSERIAL_MOUSE_MOUSESYSTEMS \ + -DMOUSE_ENABLE +endif + +ifdef SERIAL_MOUSE_USE_SOFT + SRC += $(PROTOCOL_DIR)/serial_soft.c +endif + +ifdef SERIAL_MOUSE_USE_UART + SRC += $(PROTOCOL_DIR)/serial_uart.c +endif + # Search Path VPATH += $(TOP_DIR)/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.h b/protocol/serial_mouse.h new file mode 100644 index 0000000000..226314fc0e --- /dev/null +++ b/protocol/serial_mouse.h @@ -0,0 +1,33 @@ +/* +Copyright 2014 Robin Haberkorn <robin.haberkorn@googlemail.com> + +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 <http://www.gnu.org/licenses/>. +*/ + +#ifndef SERIAL_MOUSE_H +#define SERIAL_MOUSE_H + +#include <stdint.h> + +#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 new file mode 100644 index 0000000000..ab74b7cdd3 --- /dev/null +++ b/protocol/serial_mouse_microsoft.c @@ -0,0 +1,124 @@ +/* +Copyright 2014 Robin Haberkorn <robin.haberkorn@googlemail.com> + +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 <http://www.gnu.org/licenses/>. +*/ + +#include <stdint.h> +#include <avr/io.h> +#include <util/delay.h> + +#include "serial.h" +#include "serial_mouse.h" +#include "report.h" +#include "host.h" +#include "timer.h" +#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) +{ + /* 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 = MAX(report.x, -127); + report.y = MAX(report.y, -127); + +#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..cfe8996216 --- /dev/null +++ b/protocol/serial_mouse_mousesystems.c @@ -0,0 +1,131 @@ +/* +Copyright 2014 Robin Haberkorn <robin.haberkorn@googlemail.com> + +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 <http://www.gnu.org/licenses/>. +*/ + +#include <stdint.h> +#include <avr/io.h> +#include <util/delay.h> + +#include "serial.h" +#include "serial_mouse.h" +#include "report.h" +#include "host.h" +#include "timer.h" +#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); + +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])) { + /* USB HID uses only values from -127 to 127 */ + 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 = MAX((int8_t)buffer[3], -127); + report.v = MAX((int8_t)buffer[4], -127); + + 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; + + /* USB HID uses only values from -127 to 127 */ + 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 = MAX((int8_t)buffer[3], -127); + report.y = MAX(-(int8_t)buffer[4], -127); + + 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); +} |