Skip to content

Instantly share code, notes, and snippets.

@OrhanYigitDurmaz
Last active December 1, 2023 18:02
Show Gist options
  • Save OrhanYigitDurmaz/a37b410e4875a625a515a4def4397046 to your computer and use it in GitHub Desktop.
Save OrhanYigitDurmaz/a37b410e4875a625a515a4def4397046 to your computer and use it in GitHub Desktop.
16 Bit Test with TinyUSB_Arduino
#include <Adafruit_TinyUSB.h>
/*
* Orhan Yiğit Durmaz Copyright 2023
*
* This example shows how to use custom usb
* descriptors with TinyUSB HID devices.
*/
//http://janaxelson.com/hidpage.htm <-- Excellent Website for USB communication!
//Descriptor is made with HID Decriptor Tool (DT).
//Get it here: https://usb.org/document-library/hid-descriptor-tool
uint8_t const sixteen_bit_desc[] =
{
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x05, // USAGE (Joystick)
0xa1, 0x01, // COLLECTION (Application)
0xa1, 0x00, // COLLECTION (Physical)
//0x85, 0x01, // REPORT_ID (1)
HID_REPORT_ID(1) // DYNAMIC REPORT ID WITH TINYUSB
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x20, // USAGE_MAXIMUM (Button 32)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x40, // REPORT_COUNT (64)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x32, // USAGE (Z)
0x09, 0x33, // USAGE (Rx)
0x09, 0x34, // USAGE (Ry)
0x09, 0x35, // USAGE (Rz)
0x09, 0x37, // USAGE (Dial)
0x09, 0x36, // USAGE (Slider)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x27, 0xff, 0xff, 0x00, 0x00, // LOGICAL_MAXIMUM (65535)
0x75, 0x10, // REPORT_SIZE (16)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0, // END_COLLECTION
0xc0 // END_COLLECTION
};
struct __attribute__((__packed__)) reportHID_t {
//uint8_t id = 1;
uint64_t buttons = 0;
int16_t X = 0;
int16_t Y = 0;
int16_t Z = 0;
int16_t RX = 0;
int16_t RY = 0;
int16_t RZ = 0;
int16_t Dial = 0;
int16_t Slider = 0;
};
reportHID_t reportHID;
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval (1= 1000hz, 2= 500hz, 3= 333hz), use out endpoint
Adafruit_USBD_HID HID(sixteen_bit_desc, sizeof(sixteen_bit_desc), HID_ITF_PROTOCOL_NONE, 1, false);
void setup() {
//analogReadResolution(12); //should be used with 12 bit adc supported boards
HID.begin();
Serial.begin(115200);
while( !TinyUSBDevice.mounted() ) delay(1);
Serial.println("USB and Serial Started");
}
void loop() {
if ( HID.ready() )
{
for (int i = 1; i <= 32; i++) {
int analogValue = analogRead(A0);
reportHID.X = map(analogValue, 0, 1023, 0, 65535);
button(i, 1);
HID.sendReport(1, &reportHID, sizeof(reportHID_t));
delay(500);
}
}
}
void button(uint8_t button, bool val)
{
//for data consistency, we are using 2 dword's
static uint32_t buttons_local = 0;
if(button >= 1 && button <= 32) {
if(val){
buttons_local |= (1UL << (button-1));
} else {
buttons_local &= ~(1UL << (button-1));
}
reportHID.buttons = buttons_local;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment