Created
October 23, 2016 09:15
-
-
Save lborg019/ced50000dd114fa6b44b3065c08c1ff4 to your computer and use it in GitHub Desktop.
libusb steamcontroller (windows)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <libusb.h> | |
#include <cassert> | |
using namespace std; | |
#define RT 0x000001; //Right Trigger press | |
#define LT 0x000002; //Left Trigger press | |
#define RB 0x000004; //Right Bumper | |
#define LB 0x000008; //Left Bumper | |
#define Y 0x000010; | |
#define B 0x000020; | |
#define X 0x000040; | |
#define A 0x000080; | |
#define SELECT 0x10 //on data[9] | |
#define LOGO 0x20 | |
#define START 0x40 | |
#define BL 0x80 //Back Left | |
//data[10] | |
#define BR 0x01 //Back Right | |
#define LS 0x02 //Left Stick press | |
struct buttonData | |
{ | |
unsigned int rt : 1; | |
unsigned int lt : 1; | |
unsigned int rb : 1; | |
unsigned int lb : 1; | |
unsigned int y : 1; | |
unsigned int b : 1; | |
unsigned int x : 1; | |
unsigned int a : 1; | |
unsigned int select : 1; | |
unsigned int logo : 1; | |
unsigned int start : 1; | |
unsigned int bl : 1; | |
unsigned int br : 1; | |
unsigned int ls : 1; | |
}; | |
void printButtons(uint8_t data[]) | |
{ | |
if (data[9] & SELECT) | |
{ | |
printf("Select Button pressed\n"); | |
} | |
if (data[9] & START) | |
{ | |
printf("Start Button pressed\n"); | |
} | |
if (data[9] & LOGO) | |
{ | |
printf("Big button in the middle pressed\n"); | |
} | |
} | |
int getFirmwareDate(libusb_device_handle* handle) | |
{ | |
int i; | |
int time = 0; | |
uint8_t data[64] = { 0 }; | |
uint8_t offset = 23; | |
libusb_control_transfer(handle, 0xa1, 0x01, 0x0300, 0x0002, data, 64, 0); | |
//printf ("Data: \n"); | |
for (i = 0; i<4; i++) | |
{ | |
time += data[i + offset] << (8 * (i)); | |
} | |
//time = data[26] << 24 | data[25] << 16 | data[24] << 8 | data[23]; | |
return time; | |
} | |
int main() | |
{ | |
const struct libusb_interface *inter = new libusb_interface(); | |
const struct libusb_interface_descriptor *interdesc = new libusb_interface_descriptor(); | |
const struct libusb_endpoint_descriptor *endpointdesc = new libusb_endpoint_descriptor(); | |
struct libusb_config_descriptor *config = new libusb_config_descriptor(); | |
libusb_context *context = NULL; //libusb requires a context to work on | |
libusb_device **list = NULL; //device list | |
libusb_device_handle *handle = NULL; //handle | |
int rc = 0; | |
ssize_t count = 0; | |
//int j, k; | |
int* bytesRead = new int(); | |
//bytesRead=malloc(sizeof(int)); | |
uint8_t data[64] = { 0 }; | |
data[0] = 0x8f;//Also needs to be this specific value | |
data[1] = 0x07;//Can be changed | |
data[2] = 0x00; | |
data[3] = 0x58; | |
data[4] = 0x02; | |
data[5] = 0x00; | |
data[6] = 0x00; | |
data[7] = 0x01;//Has to be 1 for Vibration | |
rc = libusb_init(&context); | |
assert(rc == 0); | |
libusb_set_debug(context, 3); | |
count = libusb_get_device_list(context, &list); | |
if (count <= 0) | |
cout << "No devices in the list" << endl; | |
else | |
cout << count << " devices in list" << endl; | |
for (int i = 0; i < count; i++) { | |
libusb_device *device = list[i]; | |
libusb_device_descriptor desc = { 0 }; | |
rc = libusb_get_device_descriptor(device, &desc); | |
if (rc < 0) | |
{ | |
fprintf(stderr, "error in getting device description\n"); | |
return 1; | |
} | |
printf("Number of pos configs is %d\n", desc.bNumConfigurations); | |
printf("Vendor (device class): %04x\n", desc.idVendor); | |
printf("Product ID: %04x\n", desc.idProduct); | |
libusb_get_config_descriptor(device, 0, &config);//assign value to config pointer | |
printf("Interface: %d\n", config->bNumInterfaces); | |
for (int i=0; i<config->bNumInterfaces; i++) | |
{ | |
inter = &config->interface[i]; | |
printf(" number of alt settings:%d\n", inter->num_altsetting); | |
for (int j=0; j<inter->num_altsetting; j++) | |
{ | |
interdesc = &inter->altsetting[j]; | |
if (interdesc == NULL) | |
{ | |
printf("Interface number is null\n"); | |
continue; | |
} | |
else | |
{ | |
printf(" Interface number: %d, ", interdesc->bInterfaceNumber); | |
printf(" Num of endpoints: %d\n", interdesc->bNumEndpoints); | |
} | |
for (int k=0; k<interdesc->bNumEndpoints; k++) | |
{ | |
endpointdesc = &interdesc->endpoint[k]; | |
printf(" Desc type: %d", endpointdesc->bDescriptorType); | |
printf(" EP Addr: %d\n", endpointdesc->bEndpointAddress); | |
} | |
} | |
} | |
printf("\n\n"); | |
//libusb_free_config_descriptor(config); | |
//printf("Vendor:Device:SerialNumber = %04x:%04x:%04x\n", desc.idVendor, desc.idProduct, desc.iSerialNumber); | |
} | |
libusb_set_debug(NULL, 3);//not sure if necessary | |
handle = libusb_open_device_with_vid_pid(0, 0x28de, 0x1142); | |
if (!handle) | |
{ | |
fprintf(stderr, "Unable to open steam controller.\n"); | |
system("PAUSE"); | |
return 1; | |
} | |
else | |
{ | |
printf("Device opened successfully!\n"); | |
} | |
printf("Handle 0x%x\n", (unsigned int)handle); | |
int err = libusb_claim_interface(handle, 2); | |
if (err){ cout << "error claiming interface" << endl; } | |
rc = libusb_interrupt_transfer(handle, 0x83, data, 64, bytesRead, 0); //figure out arguments for this function | |
//rc = libusb_interrupt_transfer(d_udh, 0x81, data, 64, &numBytes, 5000); | |
printButtons(data); | |
libusb_close(handle); | |
libusb_exit(NULL); | |
system("PAUSE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment