Db9_zu_USB/Db9_zu_USB.ino

89 lines
2.1 KiB
Arduino
Raw Normal View History

2024-10-15 09:59:04 +02:00
#include <Adafruit_TinyUSB.h>
int up = 10;
int down = 11;
int left = 12;
int right = 13;
int button = 14;
uint8_t const desc_hid_report[] =
{
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x05, // Usage (Game Pad)
0xA1, 0x01, // Collection (Application)
// X und Y Achsen
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x02, // Report Count (2)
0x81, 0x02, // Input (Data,Var,Abs)
// Buttons (nur 1 Button)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (Button 1)
0x29, 0x01, // Usage Maximum (Button 1)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x01, // Report Count (1)
0x81, 0x02, // Input (Data,Var,Abs)
// Padding auf Bytegröße
0x75, 0x07, // Report Size (7)
0x95, 0x01, // Report Count (1)
0x81, 0x03, // Input (Const,Var,Abs)
0xC0 // End Collection
};
Adafruit_USBD_HID usb_hid;
typedef struct
{
int8_t x;
int8_t y;
uint8_t buttons;
} gamepad_report_t;
gamepad_report_t gp;
void setup()
{
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
pinMode(left, INPUT_PULLUP);
pinMode(right, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);
if(!TinyUSBDevice.isInitialized()) TinyUSBDevice.begin(0);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.begin();
}
void loop()
{
if(!TinyUSBDevice.mounted())
{
return;
}
gp.x = 0;
gp.y = 0;
gp.buttons = 0;
if(digitalRead(up) == LOW) gp.y = -127;
if(digitalRead(down) == LOW) gp.y = 127;
if(digitalRead(left) == LOW) gp.x = -127;
if(digitalRead(right) == LOW) gp.x = 127;
if(digitalRead(button) == LOW) gp.buttons = 1;
usb_hid.sendReport(0, &gp, sizeof(gp));
delay(10);
}