X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=tmk_core%2Fprotocol%2Fps2_mouse.c;h=cf1055eb802c30f3f434df6ba55aeb50c8c466b2;hb=e717dcaa09143615ae0b46bf625621f67a7b55ce;hp=82f6966e8ed7697011dd738c280f2f26c4f6bfc8;hpb=d6d0b7eadaa568853b21d74057db306a33b7a847;p=qmk_firmware.git diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 82f6966e8..cf1055eb8 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -18,206 +18,242 @@ along with this program. If not, see . #include #include #include -#include "ps2.h" #include "ps2_mouse.h" -#include "report.h" #include "host.h" #include "timer.h" #include "print.h" +#include "report.h" #include "debug.h" +#include "ps2.h" -#ifndef PS2_INIT_DELAY -#define PS2_INIT_DELAY 1000 -#endif +/* ============================= MACROS ============================ */ static report_mouse_t mouse_report = {}; +static inline void ps2_mouse_print_report(report_mouse_t *mouse_report); +static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report); +static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report); +static inline void ps2_mouse_enable_scrolling(void); +static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report); -static void print_usb_data(void); - +/* ============================= IMPLEMENTATION ============================ */ /* supports only 3 button mouse at this time */ -uint8_t ps2_mouse_init(void) { - uint8_t rcv; - +void ps2_mouse_init(void) { ps2_host_init(); - _delay_ms(PS2_INIT_DELAY); // wait for powering up + _delay_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up - // send Reset - rcv = ps2_host_send(0xFF); - print("ps2_mouse_init: send Reset: "); - phex(rcv); phex(ps2_error); print("\n"); + PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset"); - // read completion code of BAT - rcv = ps2_host_recv_response(); - print("ps2_mouse_init: read BAT: "); - phex(rcv); phex(ps2_error); print("\n"); + PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT"); + PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID"); - // read Device ID - rcv = ps2_host_recv_response(); - print("ps2_mouse_init: read DevID: "); - phex(rcv); phex(ps2_error); print("\n"); +#ifdef PS2_MOUSE_USE_REMOTE_MODE + ps2_mouse_set_remote_mode(); +#else + ps2_mouse_enable_data_reporting(); +#endif + +#ifdef PS2_MOUSE_ENABLE_SCROLLING + ps2_mouse_enable_scrolling(); +#endif - // send Set Remote mode - rcv = ps2_host_send(0xF0); - print("ps2_mouse_init: send 0xF0: "); - phex(rcv); phex(ps2_error); print("\n"); +#ifdef PS2_MOUSE_USE_2_1_SCALING + ps2_mouse_set_scaling_2_1(); +#endif - return 0; + ps2_mouse_init_user(); } -#define X_IS_NEG (mouse_report.buttons & (1<buttons & (1<buttons & (1<buttons & (1<buttons & (1<x = X_IS_NEG ? + ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : + ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); + mouse_report->y = Y_IS_NEG ? + ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : + ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); + + // remove sign and overflow flags + mouse_report->buttons &= PS2_MOUSE_BTN_MASK; + +#ifdef PS2_MOUSE_INVERT_X + mouse_report->x = -mouse_report->x; #endif - scroll_state = SCROLL_NONE; - } - // doesn't send Scroll Button - mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK); +#ifndef PS2_MOUSE_INVERT_Y // NOTE if not! + // invert coordinate of y to conform to USB HID mouse + mouse_report->y = -mouse_report->y; #endif +} - host_mouse_send(&mouse_report); - print_usb_data(); - } - // clear report - mouse_report.x = 0; - mouse_report.y = 0; - mouse_report.v = 0; - mouse_report.h = 0; - mouse_report.buttons = 0; +static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) { + mouse_report->x = 0; + mouse_report->y = 0; + mouse_report->v = 0; + mouse_report->h = 0; + mouse_report->buttons = 0; } -static void print_usb_data(void) -{ +static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) { if (!debug_mouse) return; - print("ps2_mouse usb: ["); - phex(mouse_report.buttons); print("|"); - print_hex8((uint8_t)mouse_report.x); print(" "); - print_hex8((uint8_t)mouse_report.y); print(" "); - print_hex8((uint8_t)mouse_report.v); print(" "); - print_hex8((uint8_t)mouse_report.h); print("]\n"); -} - - -/* PS/2 Mouse Synopsis - * http://www.computer-engineering.org/ps2mouse/ - * - * Command: - * 0xFF: Reset - * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled - * 0xF5: Disable Data Reporting - * 0xF4: Enable Data Reporting - * 0xF3: Set Sample Rate - * 0xF2: Get Device ID - * 0xF0: Set Remote Mode - * 0xEB: Read Data - * 0xEA: Set Stream Mode - * 0xE9: Status Request - * 0xE8: Set Resolution - * 0xE7: Set Scaling 2:1 - * 0xE6: Set Scaling 1:1 - * - * Mode: - * Stream Mode: devices sends the data when it changs its state - * Remote Mode: host polls the data periodically - * - * This code uses Remote Mode and polls the data with Read Data(0xEB). - * - * Data format: - * byte|7 6 5 4 3 2 1 0 - * ----+-------------------------------------------------------------- - * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left - * 1| X movement - * 2| Y movement - */ + print("ps2_mouse: ["); + phex(mouse_report->buttons); print("|"); + print_hex8((uint8_t)mouse_report->x); print(" "); + print_hex8((uint8_t)mouse_report->y); print(" "); + print_hex8((uint8_t)mouse_report->v); print(" "); + print_hex8((uint8_t)mouse_report->h); print("]\n"); +} + +static inline void ps2_mouse_enable_scrolling(void) { + PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate"); + PS2_MOUSE_SEND(200, "200"); + PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate"); + PS2_MOUSE_SEND(100, "100"); + PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate"); + PS2_MOUSE_SEND(80, "80"); + PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel"); + _delay_ms(20); +} + +#define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK) +#define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK) +static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) { + static enum { + SCROLL_NONE, + SCROLL_BTN, + SCROLL_SENT, + } scroll_state = SCROLL_NONE; + static uint16_t scroll_button_time = 0; + + if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) { + // All scroll buttons are pressed + + if (scroll_state == SCROLL_NONE) { + scroll_button_time = timer_read(); + scroll_state = SCROLL_BTN; + } + + // If the mouse has moved, update the report to scroll instead of move the mouse + if (mouse_report->x || mouse_report->y) { + scroll_state = SCROLL_SENT; + mouse_report->v = -mouse_report->y/(PS2_MOUSE_SCROLL_DIVISOR_V); + mouse_report->h = mouse_report->x/(PS2_MOUSE_SCROLL_DIVISOR_H); + mouse_report->x = 0; + mouse_report->y = 0; +#ifdef PS2_MOUSE_INVERT_H + mouse_report->h = -mouse_report->h; +#endif +#ifdef PS2_MOUSE_INVERT_V + mouse_report->v = -mouse_report->v; +#endif + } + } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) { + // None of the scroll buttons are pressed + +#if PS2_MOUSE_SCROLL_BTN_SEND + if (scroll_state == SCROLL_BTN + && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) { + PRESS_SCROLL_BUTTONS; + host_mouse_send(mouse_report); + _delay_ms(100); + RELEASE_SCROLL_BUTTONS; + } +#endif + scroll_state = SCROLL_NONE; + } + + RELEASE_SCROLL_BUTTONS; +}