8000 Merge branch 'master' into ipv4ipv6 · earlephilhower/arduino-pico@3443768 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3443768

Browse files
Merge branch 'master' into ipv4ipv6
2 parents 37b53a0 + 40f4fdf commit 3443768

File tree

6 files changed

+90
-37
lines changed

6 files changed

+90
-37
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
[submodule "libraries/Mouse"]
2020
path = libraries/Mouse
2121
url = https://github.com/earlephilhower/Mouse
22+
[submodule "libraries/Joystick"]
23+
path = libraries/Joystick
24+
url = https://github.com/benjaminaigner/Joystick
2225
[submodule "libraries/Adafruit_TinyUSB_Arduino"]
2326
path = libraries/Adafruit_TinyUSB_Arduino
2427
url = https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git

cores/rp2040/RP2040USB.cpp

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const uint8_t *tud_descriptor_device_cb(void) {
9191
.iSerialNumber = USBD_STR_SERIAL,
9292
.bNumConfigurations = 1
9393
};
94-
if (__USBInstallSerial && !__USBInstallKeyboard && !__USBInstallMouse && !__USBInstallMIDI) {
94+
if (__USBInstallSerial && !__USBInstallKeyboard && !__USBInstallMouse && !__USBInstallJoystick && !__USBInstallMIDI) {
9595
// Can use as-is, this is the default USB case
9696
return (const uint8_t *)&usbd_desc_device;
9797
}
@@ -102,6 +102,9 @@ const uint8_t *tud_descriptor_device_cb(void) {
102102
if (__USBInstallMouse) {
103103
usbd_desc_device.idProduct |= 0x4000;
104104
}
105+
if (__USBInstallJoystick) {
106+
usbd_desc_device.idProduct |= 0x0100;
107+
}
105108
if (__USBInstallMIDI) {
106109
usbd_desc_device.idProduct |= 0x2000;
107110
}
@@ -120,6 +123,17 @@ int __USBGetMouseReportID() {
120123
return __USBInstallKeyboard ? 2 : 1;
121124
}
122125

126+
int __USBGetJoystickReportID() {
127+
int i = 1;
128+
if (__USBInstallKeyboard) {
129+
i++;
130+
}
131+
if (__USBInstallMouse) {
132+
i++;
133+
}
134+
return i;
135+
}
136+
123137
static int __hid_report_len = 0;
124138
static uint8_t *__hid_report = nullptr;
125139

@@ -131,37 +145,68 @@ static uint8_t *GetDescHIDReport(int *len) {
131145
}
132146

133147
void __SetupDescHIDReport() {
134-
if (__USBInstallKeyboard && __USBInstallMouse) {
135-
uint8_t desc_hid_report[] = {
136-
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)),
137-
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2))
138-
};
139-
__hid_report = (uint8_t *)malloc(sizeof(desc_hid_report));
140-
if (__hid_report) {
141-
__hid_report_len = sizeof(desc_hid_report);
142-
memcpy(__hid_report, desc_hid_report, __hid_report_len);
148+
//allocate memory for the HID report descriptors. We don't use them, but need the size here.
149+
uint8_t desc_hid_report_mouse[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1)) };
150+
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1)) };
151+
uint8_t desc_hid_report_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)) };
152+
int size = 0;
153+
154+
//accumulate the size of all used HID report descriptors
155+
if (__USBInstallKeyboard) {
156+
size += sizeof(desc_hid_report_keyboard);
157+
}
158+
if (__USBInstallMouse) {
159+
size += sizeof(desc_hid_report_mouse);
160+
}
161+
if (__USBInstallJoystick) {
162+
size += sizeof(desc_hid_report_joystick);
163+
}
164+
165+
//no HID used at all
166+
if (size == 0) {
167+
__hid_report = nullptr;
168+
__hid_report_len = 0;
169+
return;
170+
}
171+
172+
//allocate the "real" HID report descriptor
173+
__hid_report = (uint8_t *)malloc(size);
174+
if (__hid_report) {
175+
__hid_report_len = size;
176+
177+
//now copy the descriptors
178+
179+
//1.) keyboard descriptor, if requested
180+
if (__USBInstallKeyboard) {
181+
memcpy(__hid_report, desc_hid_report_keyboard, sizeof(desc_hid_report_keyboard));
143182
}
144-
} else if (__USBInstallKeyboard && ! __USBInstallMouse) {
145-
uint8_t desc_hid_report[] = {
146-
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1))
147-
};
148-
__hid_report = (uint8_t *)malloc(sizeof(desc_hid_report));
149-
if (__hid_report) {
150-
__hid_report_len = sizeof(desc_hid_report);
151-
memcpy(__hid_report, desc_hid_report, __hid_report_len);
183+
184+
//2.) mouse descriptor, if necessary. Additional offset & new array is necessary if there is a keyboard.
185+
if (__USBInstallMouse) {
186+
//determine if we need an offset (USB keyboard is installed)
187+
if (__USBInstallKeyboard) {
188+
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2)) };
189+
memcpy(__hid_report + sizeof(desc_hid_report_keyboard), desc_local, sizeof(desc_local));
190+
} else {
191+
memcpy(__hid_report, desc_hid_report_mouse, sizeof(desc_hid_report_mouse));
192+
}
152193
}
153-
} else if (! __USBInstallKeyboard && __USBInstallMouse) {
154-
uint8_t desc_hid_report[] = {
155-
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1))
156-
};
157-
__hid_report = (uint8_t *)malloc(sizeof(desc_hid_report));
158-
if (__hid_report) {
159-
__hid_report_len = sizeof(desc_hid_report);
160-
memcpy(__hid_report, desc_hid_report, __hid_report_len);
194+
195+
//3.) joystick descriptor. 2 additional checks are necessary for mouse and/or keyboard
196+
if (__USBInstallJoystick) {
197+
uint8_t reportid = 1;
198+
int offset = 0;
199+
if (__USBInstallKeyboard) {
200+
reportid++;
201+
offset += sizeof(desc_hid_report_keyboard);
202+
}
203+
if (__USBInstallMouse) {
204+
reportid++;
205+
offset += sizeof(desc_hid_report_mouse);
206+
}
207+
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(reportid)) };
208+
memcpy(__hid_report + offset, desc_local, sizeof(desc_local));
161209
}
162-
} else {
163-
__hid_report = nullptr;
164-
__hid_report_len = 0;
165210
}
166211
}
167212

@@ -181,7 +226,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
181226

182227
void __SetupUSBDescriptor() {
183228
if (!usbd_desc_cfg) {
184-
bool hasHID = __USBInstallKeyboard || __USBInstallMouse;
229+
bool hasHID = __USBInstallKeyboard || __USBInstallMouse || __USBInstallJoystick;
185230

186231
uint8_t interface_count = (__USBInstallSerial ? 2 : 0) + (hasHID ? 1 : 0) + (__USBInstallMIDI ? 2 : 0);
187232

cores/rp2040/RP2040USB.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// Weak function definitions for each type of endpoint
2525
extern void __USBInstallSerial() __attribute__((weak));
2626
extern void __USBInstallKeyboard() __attribute__((weak));
27+
extern void __USBInstallJoystick() __attribute__((weak));
2728
extern void __USBInstallMouse() __attribute__((weak));
2829
extern void __USBInstallMIDI() __attribute__((weak));
2930

@@ -34,6 +35,7 @@ extern mutex_t __usb_mutex;
3435
// HID report ID inquiry (report ID will vary depending on the number/type of other HID)
3536
int __USBGetKeyboardReportID();
3637
int __USBGetMouseReportID();
38+
int __USBGetJoystickReportID();
3739

3840
// Called by main() to init the USB HW/SW.
3941
void __USBStart();

docs/usb.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ serial port, ``Serial`` as well as supporting automatic reset-to-upload
1212
from the IDE.
1313

1414
The Arduino-Pico core includes ported versions of the basic Arduino
15-
``Keyboard`` and ``Mouse`` libraries. These libraries allow you to
16-
emulate a keyboard or mouse with the Pico in your sketches.
15+
``Keyboard``, ``Mouse`` and ``Joystick`` libraries. These libraries
16+
allow you to emulate a keyboard, a gamepad or mouse (or all together)
17+
with the Pico in your sketches.
1718

1819
See the examples and Arduino Reference at
1920
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/

libraries/Joystick

Submodule Joystick added at af745f3

tools/platformio-build.py

Copy file name to clipboard
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def configure_network_flags(cpp_defines):
235235
("LWIP_IGMP", 1),
236236
("LWIP_CHECKSUM_CTRL_PER_NETIF", 1)
237237
])
238-
if "PIO_FRAMEWORK_ARDUINO_ENABLE_IPV6" in cppdefines:
238+
if "PIO_FRAMEWORK_ARDUINO_ENABLE_IPV6" in cpp_defines:
239239
env.Append(CPPDEFINES=[("LWIP_IPV6", 1)])
240240
else:
241241
env.Append(CPPDEFINES=[("LWIP_IPV6", 0)])
@@ -298,10 +298,11 @@ def configure_network_flags(cpp_defines):
298298
os.path.join(FRAMEWORK_DIR, "variants", variant)
299299
])
300300

301-
libs.append(
302-
env.BuildLibrary(
303-
os.path.join("$BUILD_DIR", "FrameworkArduinoVariant"),
304-
os.path.join(FRAMEWORK_DIR, "variants", variant)))
301+
# link variant's source files as object files into the binary.
302+
# otherwise weak function overriding won't work in the linking stage.
303+
env.BuildSources(
304+
os.path.join("$BUILD_DIR", "FrameworkArduinoVariant"),
305+
os.path.join(FRAMEWORK_DIR, "variants", variant))
305306

306307
libs.append(
307308
env.BuildLibrary(

0 commit comments

Comments
 (0)
0