Skip to content

Commit 18d1654

Browse files
committed
usbcomposite library
1 parent f56c37e commit 18d1654

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+8676
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "USBHID.h"
2+
3+
//================================================================================
4+
//================================================================================
5+
// Mouse
6+
7+
void HIDAbsMouse::begin(void){
8+
}
9+
10+
void HIDAbsMouse::end(void){
11+
}
12+
13+
void HIDAbsMouse::click(uint8_t b)
14+
{
15+
report.wheel = 0;
16+
report.buttons = b;
17+
sendReport();
18+
report.buttons = 0;
19+
sendReport();
20+
}
21+
22+
void HIDAbsMouse::move(int16 x, int16 y, int8 wheel)
23+
{
24+
report.x = x;
25+
report.y = y;
26+
report.wheel = wheel;
27+
28+
sendReport();
29+
}
30+
31+
void HIDAbsMouse::buttons(uint8_t b)
32+
{
33+
if (b != report.buttons)
34+
{
35+
report.wheel = 0;
36+
report.buttons = b;
37+
sendReport();
38+
}
39+
}
40+
41+
void HIDAbsMouse::press(uint8_t b)
42+
{
43+
buttons(report.buttons | b);
44+
}
45+
46+
void HIDAbsMouse::release(uint8_t b)
47+
{
48+
buttons(report.buttons & ~b);
49+
}
50+
51+
bool HIDAbsMouse::isPressed(uint8_t b)
52+
{
53+
if ((b & report.buttons) != 0)
54+
return true;
55+
return false;
56+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "USBHID.h"
2+
3+
//================================================================================
4+
//================================================================================
5+
// Keyboard
6+
7+
HIDKeyboard BootKeyboard(0);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "USBHID.h"
2+
3+
void HIDConsumer::begin(void) {}
4+
void HIDConsumer::end(void) {}
5+
void HIDConsumer::press(uint16_t button) {
6+
report.button = button;
7+
sendReport();
8+
}
9+
10+
void HIDConsumer::release() {
11+
report.button = 0;
12+
sendReport();
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "USBHID.h"
2+
3+
#define REPORT(name, ...) \
4+
static uint8_t raw_ ## name[] = { __VA_ARGS__ }; \
5+
static const HIDReportDescriptor desc_ ## name = { raw_ ##name, sizeof(raw_ ##name) }; \
6+
const HIDReportDescriptor* hidReport ## name = & desc_ ## name;
7+
8+
REPORT(KeyboardMouseJoystick, HID_MOUSE_REPORT_DESCRIPTOR(), HID_KEYBOARD_REPORT_DESCRIPTOR(), HID_JOYSTICK_REPORT_DESCRIPTOR());
9+
REPORT(KeyboardMouse, HID_MOUSE_REPORT_DESCRIPTOR(), HID_KEYBOARD_REPORT_DESCRIPTOR());
10+
REPORT(Keyboard, HID_KEYBOARD_REPORT_DESCRIPTOR());
11+
REPORT(Mouse, HID_MOUSE_REPORT_DESCRIPTOR());
12+
REPORT(KeyboardJoystick, HID_KEYBOARD_REPORT_DESCRIPTOR(), HID_JOYSTICK_REPORT_DESCRIPTOR());
13+
REPORT(Joystick, HID_JOYSTICK_REPORT_DESCRIPTOR());
14+
REPORT(BootKeyboard, HID_BOOT_KEYBOARD_REPORT_DESCRIPTOR());
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "USBHID.h"
2+
3+
// This code requires gcc on low-endian devices.
4+
5+
//================================================================================
6+
//================================================================================
7+
// Joystick
8+
9+
void HIDJoystick::begin(void){
10+
}
11+
12+
void HIDJoystick::end(void){
13+
}
14+
15+
void HIDJoystick::setManualReportMode(bool mode) {
16+
manualReport = mode;
17+
}
18+
19+
bool HIDJoystick::getManualReportMode() {
20+
return manualReport;
21+
}
22+
23+
void HIDJoystick::safeSendReport() {
24+
if (!manualReport) {
25+
sendReport();
26+
}
27+
}
28+
29+
void HIDJoystick::button(uint8_t button, bool val){
30+
uint32_t mask = ((uint32_t)1 << (button-1));
31+
32+
if (val) {
33+
joyReport.buttons |= mask;
34+
} else {
35+
joyReport.buttons &= ~mask;
36+
}
37+
38+
safeSendReport();
39+
}
40+
41+
void HIDJoystick::X(uint16_t val){
42+
if (val > 1023) val = 1023;
43+
joyReport.x = val;
44+
45+
safeSendReport();
46+
}
47+
48+
void HIDJoystick::Y(uint16_t val){
49+
if (val > 1023) val = 1023;
50+
joyReport.y = val;
51+
52+
safeSendReport();
53+
}
54+
55+
void HIDJoystick::position(uint16_t x, uint16_t y){
56+
if (x > 1023) x = 1023;
57+
if (y > 1023) y = 1023;
58+
joyReport.x = x;
59+
joyReport.y = y;
60+
61+
safeSendReport();
62+
}
63+
64+
void HIDJoystick::Xrotate(uint16_t val){
65+
if (val > 1023) val = 1023;
66+
joyReport.rx = val;
67+
68+
safeSendReport();
69+
}
70+
71+
void HIDJoystick::Yrotate(uint16_t val){
72+
if (val > 1023) val = 1023;
73+
joyReport.ry = val;
74+
75+
safeSendReport();
76+
}
77+
78+
void HIDJoystick::sliderLeft(uint16_t val){
79+
if (val > 1023) val = 1023;
80+
joyReport.sliderLeft = val;
81+
82+
safeSendReport();
83+
}
84+
85+
void HIDJoystick::sliderRight(uint16_t val){
86+
if (val > 1023) val = 1023;
87+
joyReport.sliderRight = val;
88+
89+
safeSendReport();
90+
}
91+
92+
void HIDJoystick::slider(uint16_t val){
93+
if (val > 1023) val = 1023;
94+
joyReport.sliderLeft = val;
95+
joyReport.sliderRight = val;
96+
97+
safeSendReport();
98+
}
99+
100+
void HIDJoystick::hat(int16_t dir){
101+
uint8_t val;
102+
if (dir < 0) val = 15;
103+
else if (dir < 23) val = 0;
104+
else if (dir < 68) val = 1;
105+
else if (dir < 113) val = 2;
106+
else if (dir < 158) val = 3;
107+
else if (dir < 203) val = 4;
108+
else if (dir < 245) val = 5;
109+
else if (dir < 293) val = 6;
110+
else if (dir < 338) val = 7;
111+
else val = 15;
112+
113+
joyReport.hat = val;
114+
115+
safeSendReport();
116+
}
117+
118+
HIDJoystick Joystick;

0 commit comments

Comments
 (0)