Skip to content

Commit

Permalink
Improve HID descriptors to work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
WiseLord committed May 14, 2019
1 parent abab5ef commit 53a0d9f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 71 deletions.
20 changes: 10 additions & 10 deletions actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ static void actionNavigateCommon(RcCmd cmd)
actionSet(ACTION_DISP_EXPIRED, 0);
break;
case RC_CMD_NAV_RIGHT:
actionSet(ACTION_MEDIA, HIDKEY_MEDIA_FORWARD);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_FFD);
break;
case RC_CMD_NAV_LEFT:
actionSet(ACTION_MEDIA, HIDKEY_MEDIA_BACK);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_REWIND);
break;
case RC_CMD_NAV_UP:
case RC_CMD_CHAN_NEXT:
Expand Down Expand Up @@ -494,19 +494,19 @@ static void actionRemapRemote(void)
break;

case RC_CMD_STOP:
actionSet(ACTION_MEDIA, HIDKEY_MEDIA_STOPCD);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_STOP);
break;
case RC_CMD_PLAY:
actionSet(ACTION_MEDIA, HIDKEY_MEDIA_PLAYPAUSE);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_PLAY);
break;
case RC_CMD_PAUSE:
actionSet(ACTION_MEDIA, HIDKEY_PAUSE);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_PAUSE);
break;
case RC_CMD_REW:
actionSet(ACTION_MEDIA, HIDKEY_MEDIA_BACK);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_REWIND);
break;
case RC_CMD_FWD:
actionSet(ACTION_MEDIA, HIDKEY_MEDIA_FORWARD);
actionSet(ACTION_MEDIA, HIDMEDIAKEY_FFD);
break;
case RC_CMD_TIMER:
actionSet(ACTION_TIMER, 0);
Expand Down Expand Up @@ -794,7 +794,7 @@ void actionHandle(bool visible)
case ACTION_MEDIA:
switch (inType) {
case IN_PC:
usbHidSendKey((HidKey)action.value);
usbHidSendMediaKey((HidKey)action.value);
break;
}
break;
Expand All @@ -810,9 +810,9 @@ void actionHandle(bool visible)
break;
case IN_PC:
if (action.value > 0) {
usbHidSendKey(HIDKEY_MEDIA_NEXTSONG);
usbHidSendMediaKey(HIDMEDIAKEY_NEXT_TRACK);
} else if (action.value < 0) {
usbHidSendKey(HIDKEY_MEDIA_PREVIOUSSONG);
usbHidSendMediaKey(HIDMEDIAKEY_PREV_TRACK);
}
break;
}
Expand Down
5 changes: 5 additions & 0 deletions files/utils/emuldisp/emuldisp_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,8 @@ void usbHidSendKey(HidKey key)
{
(void)key;
}

void usbHidSendMediaKey(HidMediaKey key)
{
(void)key;
}
Binary file added usb/amp.hid
Binary file not shown.
38 changes: 12 additions & 26 deletions usb/hidkeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern "C" {

typedef uint8_t HidKey;
enum {
// key codes
HIDKEY_A = 4,
HIDKEY_B = 5,
HIDKEY_C = 6,
Expand Down Expand Up @@ -139,31 +138,6 @@ enum {
HIDKEY_FIND = 0x7e,
HIDKEY_MUTE = 0x7f,

HIDKEY_VOLUME_UP = 0x80,
HIDKEY_VOLUME_DOWN = 0x81,


HIDKEY_MEDIA_PLAYPAUSE = 0xe8,
HIDKEY_MEDIA_STOPCD = 0xe9,
HIDKEY_MEDIA_PREVIOUSSONG = 0xea,
HIDKEY_MEDIA_NEXTSONG = 0xeb,
HIDKEY_MEDIA_EJECTCD = 0xec,
HIDKEY_MEDIA_VOLUMEUP = 0xed,
HIDKEY_MEDIA_VOLUMEDOWN = 0xee,
HIDKEY_MEDIA_MUTE = 0xef,
HIDKEY_MEDIA_WWW = 0xf0,
HIDKEY_MEDIA_BACK = 0xf1,
HIDKEY_MEDIA_FORWARD = 0xf2,
HIDKEY_MEDIA_STOP = 0xf3,
HIDKEY_MEDIA_FIND = 0xf4,
HIDKEY_MEDIA_SCROLLUP = 0xf5,
HIDKEY_MEDIA_SCROLLDOWN = 0xf6,
HIDKEY_MEDIA_EDIT = 0xf7,
HIDKEY_MEDIA_SLEEP = 0xf8,
HIDKEY_MEDIA_COFFEE = 0xf9,
HIDKEY_MEDIA_REFRESH = 0xfa,
HIDKEY_MEDIA_CALC = 0xfb,

// Modifiers bit sets
HIDKEY_MODIFIER_NONE = 0x00,
HIDKEY_MODIFIER_LEFT_CTRL = 0x01,
Expand All @@ -176,6 +150,18 @@ enum {
HIDKEY_MODIFIER_RIGHT_UI = 0x80,
};

typedef uint8_t HidMediaKey;
enum {
HIDMEDIAKEY_NONE = 0,
HIDMEDIAKEY_PREV_TRACK ,
HIDMEDIAKEY_NEXT_TRACK,
HIDMEDIAKEY_STOP,
HIDMEDIAKEY_PLAY,
HIDMEDIAKEY_PAUSE,
HIDMEDIAKEY_REWIND,
HIDMEDIAKEY_FFD,
};

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion usb/usbd_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" {
#define USBD_SUPPORT_USER_STRING 0
#define USBD_SELF_POWERED 1
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 2
#define USBD_AMP_REPORT_DESC_SIZE 41
#define USBD_AMP_REPORT_DESC_SIZE 78

#define DEVICE_FS 0

Expand Down
8 changes: 4 additions & 4 deletions usb/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#define USBD_LANGID_STRING 0x0409
#define USBD_MANUFACTURER_STRING "WiseLord"
#define USBD_PID_FS 0x5750
#define USBD_PRODUCT_STRING_FS "Ampcontrol HID"
#define USBD_SERIALNUMBER_STRING_FS "00000000001A"
#define USBD_CONFIGURATION_STRING_FS "Amp HID Config"
#define USBD_INTERFACE_STRING_FS "Amp HID Interface"
#define USBD_PRODUCT_STRING_FS "Ampcontrol"
#define USBD_SERIALNUMBER_STRING_FS "000032F103CB"
#define USBD_CONFIGURATION_STRING_FS "Amp Config"
#define USBD_INTERFACE_STRING_FS "Amp Interface"

// Internal string descriptor buffer
static uint8_t USBD_StrDescBuf[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
Expand Down
86 changes: 56 additions & 30 deletions usb/usbhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include "usbd_amp.h"

#include <stm32f1xx_ll_utils.h>
#include <string.h>

#define REPORT_ID_KEYBOARD 0x01
#define REPORT_ID_MOUSE 0x02
#define REPORT_ID_KEYBOARD 0x01
#define REPORT_ID_MEDIAKEYS 0x02

static int8_t usbHidInitFS(void);
static int8_t usbHidDeInitFS(void);
Expand All @@ -15,33 +16,51 @@ static int8_t usbHidOutEventFS(uint8_t event_idx, uint8_t state);
static USBD_HandleTypeDef hUsbDeviceFS;

static HidKeyboard keyboardHID;
static HidMediaKeys mediakeysHID;

static uint8_t usbHidReportDescFS[USBD_AMP_REPORT_DESC_SIZE] __ALIGN_END = {
// 41 bytes
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x06, // Usage (Keyboard)
0xA1, 0x01, // Collection (Application)
0x85, REPORT_ID_KEYBOARD, // Report ID (1)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x75, 0x01, // Report Size (1)
0x95, 0x08, // Report Count (8)
0x19, 0xE0, // Usage Minimum (0xE0)
0x29, 0xE7, // Usage Maximum (0xE7)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, KBD_KEYS, // Report Count (KBD_KEYS)
0x75, 0x08, // Report Size (8)
0x15, 0x00, // Logical Minimum (0)
0x25, 0xff, // Logical Maximum (255)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x19, 0x00, // Usage Minimum (0)
0x29, 0xff, // Usage Maximum (255)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x01, // REPORT_ID (1)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x02, // REPORT_COUNT (2)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x7f, // USAGE_MAXIMUM (Keyboard Mute)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0, // END_COLLECTION
0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
0x09, 0x01, // USAGE (Consumer Control)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x02, // REPORT_ID (2)
0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x15, 0x01, // LOGICAL_MINIMUM (1)
0x25, 0x08, // LOGICAL_MAXIMUM (8)
0x09, 0xb6, // USAGE (Scan Previous Track)
0x09, 0xb5, // USAGE (Scan Next Track)
0x09, 0xb7, // USAGE (Stop)
0x09, 0xb0, // USAGE (Play)
0x09, 0xb1, // USAGE (Pause)
0x09, 0xb4, // USAGE (Rewind)
0x09, 0xb3, // USAGE (Fast Forward)
0x09, 0xe2, // USAGE (Mute)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};


static USBD_AMP_ItfTypeDef USBD_AmpHidFopsFS = {
usbHidReportDescFS,
usbHidInitFS,
Expand Down Expand Up @@ -80,12 +99,11 @@ static void usbHidSend(void *data, uint16_t size)

void usbHidInit(void)
{
keyboardHID.id = REPORT_ID_KEYBOARD;
keyboardHID.modifiers = 0;
memset(&keyboardHID, 0, sizeof (keyboardHID));
memset(&mediakeysHID, 0, sizeof (mediakeysHID));

for (uint8_t i = 0; i < KBD_KEYS; i++) {
keyboardHID.key[i] = 0;
}
keyboardHID.id = REPORT_ID_KEYBOARD;
mediakeysHID.id = REPORT_ID_MEDIAKEYS;

USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_AMP);
Expand All @@ -103,3 +121,11 @@ void usbHidSendKey(HidKey key)
keyboardHID.key[0] = 0;
usbHidSend(&keyboardHID, sizeof(keyboardHID));
}

void usbHidSendMediaKey(HidMediaKey key)
{
mediakeysHID.key = key;
usbHidSend(&mediakeysHID, sizeof(mediakeysHID));
mediakeysHID.key = HIDMEDIAKEY_NONE;
usbHidSend(&mediakeysHID, sizeof(mediakeysHID));
}
6 changes: 6 additions & 0 deletions usb/usbhid.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ typedef struct {
uint8_t key[KBD_KEYS];
} HidKeyboard;

typedef struct {
uint8_t id;
uint8_t key;
} HidMediaKeys;

void usbHidInit(void);

void usbHidSendKey(HidKey key);
void usbHidSendMediaKey(HidMediaKey key);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 53a0d9f

Please sign in to comment.