-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUsbKeyboard.cpp
More file actions
119 lines (110 loc) · 2.81 KB
/
UsbKeyboard.cpp
File metadata and controls
119 lines (110 loc) · 2.81 KB
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
#include "UsbKeyboard.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bsp/board.h"
#include "tusb.h"
#include "usb_descriptors.h"
UsbKeyboard::UsbKeyboard(uint8_t interfaceId, uint8_t reportId) :
interfaceId(interfaceId),
reportId(reportId),
currentKeycodes(),
keycodesUpdated(false),
keyPressed(false)
{}
bool UsbKeyboard::isButtonPressed()
{
return keyPressed;
}
//--------------------------------------------------------------------+
// EXTERNAL API
//--------------------------------------------------------------------+
bool UsbKeyboard::updatePressed(uint8_t keycode)
{
bool foundDuplicate = false;
int32_t slot = -1;
uint8_t modifier = 0; // This class doesn't allow modifier keys
for (uint32_t i = 0; i < sizeof(currentKeycodes) / sizeof(currentKeycodes[0]); ++i)
{
if (slot == -1 && currentKeycodes[i] == 0)
{
slot = i;
}
else if (currentKeycodes[i] == keycode)
{
foundDuplicate = true;
}
}
if (slot != -1 && !foundDuplicate)
{
keyPressed = true;
currentKeycodes[slot] = keycode;
keycodesUpdated = true;
}
return (slot != -1 || foundDuplicate);
}
bool UsbKeyboard::updateReleased(uint8_t keycode)
{
bool found = false;
uint8_t modifier = 0; // This class doesn't allow modifier keys
for (uint32_t i = 0; i < sizeof(currentKeycodes) / sizeof(currentKeycodes[0]); ++i)
{
if (found)
{
// Shift everything 1 to the left once key is found
currentKeycodes[i - 1] = currentKeycodes[i];
currentKeycodes[i] = 0;
}
else if (currentKeycodes[i] == keycode)
{
// Remove this key
found = true;
currentKeycodes[i] = 0;
keycodesUpdated = true;
// If this is the first position and the next position is empty, assume no more keys pressed
if (i == 0 && currentKeycodes[1] == 0)
{
keyPressed = false;
}
}
}
return found;
}
void UsbKeyboard::updateAllReleased()
{
uint8_t modifier = 0; // This class doesn't allow modifier keys
memset(currentKeycodes, 0, sizeof(currentKeycodes));
keyPressed = false;
keycodesUpdated = true;
}
bool UsbKeyboard::send(bool force)
{
if (keycodesUpdated || force)
{
bool sent = sendReport(interfaceId, reportId);
if (sent)
{
keycodesUpdated = false;
}
return sent;
}
else
{
return true;
}
}
uint8_t UsbKeyboard::getReportSize()
{
return sizeof(hid_keyboard_report_t);
}
void UsbKeyboard::getReport(uint8_t *buffer, uint16_t reqlen)
{
// Build the report
hid_keyboard_report_t report;
report.modifier = 0; // This class doesn't allow modifier keys
report.reserved = 0;
memcpy(report.keycode, currentKeycodes, sizeof(report.keycode));
// Copy report into buffer
uint16_t setLen = (sizeof(report) <= reqlen) ? sizeof(report) : reqlen;
memcpy(buffer, &report, setLen);
}