summaryrefslogtreecommitdiff
path: root/hid/src/main.cpp
blob: 93a90b8bfea360d321db7548c8985410a5c505ea (plain)
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
#include <Arduino.h>
#include <HID-Project.h>

#include "inline.h"
#include "keymap.h"


#define CMD_SERIAL			Serial1
#define CMD_SERIAL_SPEED	115200

#define CMD_MOUSE_LEFT			0b10000000
#define CMD_MOUSE_LEFT_STATE	0b00001000
#define CMD_MOUSE_RIGHT			0b01000000
#define CMD_MOUSE_RIGHT_STATE	0b00000100

#define REPORT_INTERVAL 100


// -----------------------------------------------------------------------------
INLINE void readNoop() {
	for (int count = 0; count < 4; ++count) {
		CMD_SERIAL.read();
	}
}

INLINE void cmdResetHid() { // 0 bytes
	readNoop();
	BootKeyboard.releaseAll();
	SingleAbsoluteMouse.releaseAll();
}

INLINE void cmdKeyEvent() { // 2 bytes
	KeyboardKeycode code = keymap((uint8_t)CMD_SERIAL.read());
	uint8_t state = CMD_SERIAL.read();
	CMD_SERIAL.read(); // unused
	CMD_SERIAL.read(); // unused
	if (code != KEY_ERROR_UNDEFINED) {
		if (state) {
			BootKeyboard.press(code);
		} else {
			BootKeyboard.release(code);
		}
	}
}

INLINE void cmdMouseMoveEvent() { // 4 bytes
	int x = (int)CMD_SERIAL.read() << 8;
	x |= (int)CMD_SERIAL.read();
	int y = (int)CMD_SERIAL.read() << 8;
	y |= (int)CMD_SERIAL.read();
	SingleAbsoluteMouse.moveTo(x, y);
}

INLINE void cmdMouseButtonEvent() { // 1 byte
	uint8_t state = CMD_SERIAL.read();
	CMD_SERIAL.read(); // unused
	CMD_SERIAL.read(); // unused
	CMD_SERIAL.read(); // unused
	if (state & CMD_MOUSE_LEFT) {
		if (state & CMD_MOUSE_LEFT_STATE) {
			SingleAbsoluteMouse.press(MOUSE_LEFT);
		} else {
			SingleAbsoluteMouse.release(MOUSE_LEFT);
		}
	}
	if (state & CMD_MOUSE_RIGHT) {
		if (state & CMD_MOUSE_RIGHT_STATE) {
			SingleAbsoluteMouse.press(MOUSE_RIGHT);
		} else {
			SingleAbsoluteMouse.release(MOUSE_RIGHT);
		}
	}
}

INLINE void cmdMouseWheelEvent() { // 2 bytes
	CMD_SERIAL.read(); // delta_x is not supported by hid-project now
	signed char delta_y = CMD_SERIAL.read();
	CMD_SERIAL.read(); // unused
	CMD_SERIAL.read(); // unused
	SingleAbsoluteMouse.move(0, 0, delta_y);
}


// -----------------------------------------------------------------------------
void setup() {
	CMD_SERIAL.begin(CMD_SERIAL_SPEED);
	BootKeyboard.begin();
	SingleAbsoluteMouse.begin();
}

void loop() {
	static unsigned long last_report = 0;
	bool cmd_processed = false;

	if (CMD_SERIAL.available() >= 5) {
		switch ((uint8_t)CMD_SERIAL.read()) {
			case 0: cmdResetHid(); break;
			case 1: cmdKeyEvent(); break;
			case 2: cmdMouseMoveEvent(); break;
			case 3: cmdMouseButtonEvent(); break;
			case 4: cmdMouseWheelEvent(); break;
			default: readNoop(); break;
		}
		cmd_processed = true;
	}

	unsigned long now = millis();
	if (
		cmd_processed
		|| (now >= last_report && now - last_report >= REPORT_INTERVAL)
		|| (now < last_report && ((unsigned long) -1) - last_report + now >= REPORT_INTERVAL)
	) {
		CMD_SERIAL.write(0);
		last_report = now;
	}
}