Skip to content

Instantly share code, notes, and snippets.

@argonism
Last active December 1, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save argonism/90dfee5bd253eb5583dab3f7765cd3ee to your computer and use it in GitHub Desktop.
Save argonism/90dfee5bd253eb5583dab3f7765cd3ee to your computer and use it in GitHub Desktop.
#define JOYCON_L_PRODUCT_ID 8198
#define JOYCON_R_PRODUCT_ID 8199
#include <hidapi/hidapi.h>
#include <stdint.h>
#include <string.h>
void SendSubcommand(hid_device *dev, uint8_t command, uint8_t data[], int len, int* globalCount) {
uint8_t buf[0x40]; memset(buf, 0x0, size_t(0x40));
buf[0] = 1; // 0x10 for rumble only
buf[1] = *globalCount; // Increment by 1 for each packet sent. It loops in 0x0 - 0xF range.
if (*globalCount == 0xf0) {
*globalCount = 0x00;
} else {
*globalCount++;
}
buf[10] = command;
memcpy(buf + 11, data, len);
hid_write(dev, buf, 0x40);
}
int main()
{
int globalCount = 0;
// 接続されているHIDデバイスの連結リストを取得。
hid_device_info *device = hid_enumerate(0, 0);
while (device)
{
if ( device->product_id == JOYCON_L_PRODUCT_ID || device->product_id == JOYCON_R_PRODUCT_ID)
{
// プロダクトID等を指定して、HID deviceをopenする。そうすると、そのhidデバイスの情報が載ったhid_deviceが帰ってくる。
hid_device *dev = hid_open(device->vendor_id, device->product_id, device->serial_number);
// 今開いているデバイスのプロダクト名の取得。
printf("\nproduct_id: %ls", device->product_string);
uint8_t data[0x01];
data[0] = 0x01;
// 0x03番のサブコマンドに、0x01を送信します。
// ランプはビットフラグで、4桁。ランプの一番上から10進数で 1, 2, 4, 8 と対応しています。
SendSubcommand(dev, 0x30, data, 1, &globalCount);
}
// 次のデバイスへ。  
device = device->next;
}
hid_free_enumeration(device);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment