-
Notifications
You must be signed in to change notification settings - Fork 4
/
mouse.c
78 lines (69 loc) · 1.94 KB
/
mouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bsp/board.h>
#include <tusb.h>
#include <pico/stdlib.h>
#include <hardware/uart.h>
static bool serial_data_in_tail = false;
static bool updated = false;
static int32_t delta_x = 0;
static int32_t delta_y = 0;
#define NO_BUTTONS 0x7
static char btns = NO_BUTTONS;
static uint32_t interval = 40;
#define UART_MOUSE_ID uart0
#define UART_MOUSE_TX_PIN 0
void mouse_uart_init() {
uart_init(UART_MOUSE_ID, 1200);
gpio_set_function(UART_MOUSE_TX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART_MOUSE_ID, false, false);
uart_set_format(UART_MOUSE_ID, 8, 1, UART_PARITY_NONE);
gpio_set_outover(UART_MOUSE_TX_PIN, GPIO_OVERRIDE_INVERT);
}
static inline int32_t clamp(int32_t value, int32_t min, int32_t max) {
if (value < min) return min;
else if (value > max) return max;
return value;
}
static uint32_t push_head_packet() {
uart_putc_raw(UART_MOUSE_ID, btns | 0x80);
uart_putc_raw(UART_MOUSE_ID, delta_x);
uart_putc_raw(UART_MOUSE_ID, delta_y);
btns = NO_BUTTONS;
delta_x = 0;
delta_y = 0;
serial_data_in_tail = true;
return 25;
}
static uint32_t push_tail_packet() {
uart_putc_raw(UART_MOUSE_ID, delta_x);
uart_putc_raw(UART_MOUSE_ID, delta_y);
delta_x = 0;
delta_y = 0;
serial_data_in_tail = false;
return 15;
}
void mouse_tx() {
static uint32_t start_ms = 0;
if ((board_millis() - start_ms) < interval) {
return;
}
start_ms += interval;
if (updated) {
if (serial_data_in_tail) {
interval = push_tail_packet();
updated = (btns != NO_BUTTONS);
} else {
interval = push_head_packet();
}
}
}
void process_mouse_report(hid_mouse_report_t const * report) {
btns = ((report->buttons & MOUSE_BUTTON_LEFT) ? 0 : 4)
| ((report->buttons & MOUSE_BUTTON_MIDDLE) ? 0 : 2)
| ((report->buttons & MOUSE_BUTTON_RIGHT) ? 0 : 1)
;
delta_x += report->x;
delta_y += -report->y;
delta_x = clamp(delta_x, -127, 127);
delta_y = clamp(delta_y, -127, 127);
updated = true;
}