aboutsummaryrefslogtreecommitdiffstats
path: root/src/hid.c
blob: 678e7447fddff83f7d426e3f5865dc30549740e9 (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "hid.h"
#include "split.h"
#include "keymat.h"
#include "keysym.h"
#include "keymap.h"

#include "bsp/board.h"
#include "pico/types.h"

void
hid_init(void)
{
	
}

bool
hid_gen_report(uint8_t *report)
{
	int keycnt;
	uint x, y, p;

	keycnt = 0;
	for (y = 0; y < KEY_ROWS * 2; y++) {
		for (x = 0; x < KEY_COLS; x++) {
			if (!keymat[y][x])
				continue;
			if (keycnt >= 6) break;
			DEBUG("PRESS %i %", x, y);
			p = y * KEY_COLS + x;
			report[keycnt] = TO_CODE(keymap_layers[0][p]);
			keycnt++;
		}
	}

	return keycnt > 0;
}

bool
send_keyboard_report(void)
{
	static bool cleared = true;
	uint8_t report[6] = { 0 };
	bool any;

	any = hid_gen_report(report);

	if (any) {
		tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, report);
		cleared = false;
		return true;
	} else if (!cleared) {
		tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
		cleared = true;
		return true;
	}

	return false;
}

bool
send_mouse_report(bool state)
{
	if (state) {
		tud_hid_mouse_report(REPORT_ID_MOUSE, 0, 10, 10, 0, 0);
		return true;
	}

	return false;
}

bool
send_consumer_control_report(bool state)
{
	static bool cleared = true;
	uint16_t report;

	if (state) {
		report = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
		tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &report, 2);
		cleared = false;
		return true;
	} else if (!cleared) {
		report = 0;
		tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &report, 2);
		cleared = true;
		return true;
	}

	return false;
}

bool
send_hid_report(int id)
{
	if (!tud_hid_ready()) return false;

	switch (id) {
	case REPORT_ID_KEYBOARD:
		return send_keyboard_report();
	case REPORT_ID_MOUSE:
		return send_mouse_report(false);
	case REPORT_ID_CONSUMER_CONTROL:
		return send_consumer_control_report(false);
	}

	return false;
}

void
tud_hid_report_complete_cb(uint8_t instance,
	uint8_t const *report, uint8_t len)
{
	uint8_t id;

	for (id = report[0] + 1; id < REPORT_ID_MAX; id++) {
		if (send_hid_report(id))
			break;
	}
}

void
hid_task(void)
{
	send_hid_report(REPORT_ID_MIN);
}