USB Composite (HID / MIDI / Mass Storage) library
arpruss Sun Dec 03, 2017 8:32 pm
So, here is a USB library compatible with the main libmaple-based core:
(The library is also bundled with Roger’s core.)
As of February 2018, the library supports:
- USB HID
- Serial (use CompositeSerial, not Serial)
- USB MIDI
- XBox 360 controller
- Mass storage
You can do various composite combinations, e.g., HID+Serial, Mass storage+HID, etc.
Here’s some example code: an adapter from the IBM PC game port to USB joystick:
#include <GameControllers.h> // https://github.com/arpruss/GameControllersSTM32
#include <USBComposite.h>
USBHID HID; // declare HID plugin instance
HIDJoystick Joystick(HID); // declare Joystick profile instance
GamePortController controller(PA0,PA1,PA2,PA3,PA4,PA5,PA6,PA7);
void setup()
{
// to composite serial, use USBHID_begin_with_serial() instead
USBHID.begin(HID_JOYSTICK); // other options: HID_KEYBOARD, HID_MOUSE, HID_KEYBOARD_JOYSTICK, HID_KEYBOARD_MOUSE, HID_KEYBOARD_MOUSE_JOYSTICK
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1);
controller.begin();
Joystick.setManualReportMode(true); // aggregate data before sending
}
void loop()
{
GameControllerData_t data;
if (controller.read(&data)) {
Joystick.X(data.joystickX);
Joystick.Y(data.joystickY);
Joystick.Xrotate(data.cX);
Joystick.sliderRight(data.shoulderRight);
uint8_t mask = 1;
for (int i=1; i<=8; i++, mask <<= 1)
Joystick.button(i, (data.buttons & mask) != 0);
Joystick.send();
}
delay(10);
}
