]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - ps2_vusb/mousekey.c
added protocol stack: pjrc, vusb
[max/tmk_keyboard.git] / ps2_vusb / mousekey.c
1 #include <stdint.h>
2 #include <util/delay.h>
3 #include "usb_keycodes.h"
4 #include "host.h"
5 #include "timer.h"
6 #include "print.h"
7 #include "mousekey.h"
8
9
10 static report_mouse_t report;
11 static report_mouse_t report_prev;
12
13 static uint8_t mousekey_repeat =  0;
14
15
16 /*
17  * TODO: fix acceleration algorithm
18  * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
19  */
20 #ifndef MOUSEKEY_DELAY_TIME
21 #   define MOUSEKEY_DELAY_TIME 255
22 #endif
23
24
25 static inline uint8_t move_unit(void)
26 {
27     uint8_t unit = (10 + (mousekey_repeat));
28     return unit > 127 ? 127 : unit;
29 }
30
31 void mousekey_decode(uint8_t code)
32 {
33     if      (code == KB_MS_UP)      report.y -= move_unit();
34     else if (code == KB_MS_DOWN)    report.y += move_unit();
35     else if (code == KB_MS_LEFT)    report.x -= move_unit();
36     else if (code == KB_MS_RIGHT)   report.x += move_unit();
37     else if (code == KB_MS_BTN1)    report.buttons |= MOUSE_BTN1;
38     else if (code == KB_MS_BTN2)    report.buttons |= MOUSE_BTN2;
39     else if (code == KB_MS_BTN3)    report.buttons |= MOUSE_BTN3;
40 /*
41     else if (code == KB_MS_BTN4)    report.buttons |= MOUSE_BTN4;
42     else if (code == KB_MS_BTN5)    report.buttons |= MOUSE_BTN5;
43     else if (code == KB_MS_WH_UP)   report.v += 1;
44     else if (code == KB_MS_WH_DOWN) report.v -= 1;
45     else if (code == KB_MS_WH_LEFT) report.h -= 1;
46     else if (code == KB_MS_WH_RIGHT)report.h += 1;
47 */
48 }
49
50 bool mousekey_changed(void)
51 {
52     return (report.buttons != report_prev.buttons ||
53             report.x != report_prev.x ||
54             report.y != report_prev.y ||
55             report.x || report.y);
56     //return (report.buttons != report_prev.buttons || report.x || report.y);
57 }
58
59 void mousekey_send(void)
60 {
61     static uint16_t last_timer = 0;
62
63     if (!mousekey_changed()) {
64         mousekey_repeat = 0;
65         return;
66     }
67
68     // send immediately when buttun state is changed
69     if (report.buttons == report_prev.buttons) {
70         // TODO: delay parameter setting
71         if ((timer_elapsed(last_timer) < (mousekey_repeat == 1 ? 20 : 5))) {
72             return;
73         }
74     }
75
76     if (report.x && report.y) {
77         report.x *= 0.7;
78         report.y *= 0.7;
79     }
80
81     /*
82     print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
83     print("timer: "); phex16(timer_read()); print("\n");
84     print("last_timer: "); phex16(last_timer); print("\n");
85     print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
86     */
87
88     host_mouse_send(&report);
89     report_prev.buttons = report.buttons;
90     report_prev.x = report.x;
91     report_prev.y = report.y;
92     if (mousekey_repeat != 0xFF) mousekey_repeat++;
93     last_timer = timer_read();
94     mousekey_clear_report();
95 }
96
97 void mousekey_clear_report(void)
98 {
99     report.buttons = 0;
100     report.x = 0;
101     report.y = 0;
102 }