-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhid_report_buffer.h
79 lines (61 loc) · 2.06 KB
/
hid_report_buffer.h
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
//---------------------------------------------------------------------------
#pragma once
#include <stddef.h>
#include <stdint.h>
//---------------------------------------------------------------------------
class HidReportBufferBase {
public:
void SendReport(uint8_t reportId, const uint8_t *data, size_t length);
void SendNextReport();
void Print(const char *p);
void Reset() {
startIndex = 0;
endIndex = 0;
}
bool IsEmpty() const { return startIndex == endIndex; }
bool IsFull() const { return endIndex - startIndex >= NUMBER_OF_ENTRIES; }
size_t GetAvailableBufferCount() const {
size_t used = endIndex - startIndex;
return NUMBER_OF_ENTRIES - used;
}
static void PrintInfo();
static uint32_t reportsSentCount[];
protected:
HidReportBufferBase(uint8_t entrySize, uint8_t instanceNumber)
: entrySize(entrySize), instanceNumber(instanceNumber) {}
static constexpr size_t NUMBER_OF_ENTRIES = 16;
private:
const uint8_t entrySize;
const uint8_t instanceNumber;
size_t startIndex = 0;
size_t endIndex = 0;
struct Entry {
uint8_t length;
uint8_t reportId;
uint8_t data[1];
};
Entry *GetEntry(size_t index) {
return (Entry *)(entryData + entrySize * index);
}
void PumpUntilNotFull();
public:
uint8_t entryData[0];
};
//---------------------------------------------------------------------------
template <size_t DATA_SIZE>
struct HidReportBuffer : public HidReportBufferBase {
public:
HidReportBuffer(uint8_t instanceNumber)
: HidReportBufferBase(DATA_SIZE + 2, instanceNumber) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
static_assert(offsetof(HidReportBuffer, buffers) ==
offsetof(HidReportBufferBase, entryData),
"Data is not positioned where expected");
#pragma GCC diagnostic pop
}
private:
// Each entry has one byte prefix which is the length, followed by the data.
uint8_t buffers[(DATA_SIZE + 2) * NUMBER_OF_ENTRIES];
};
//---------------------------------------------------------------------------