Add system keyboard interface and add sleep, power and wake keys.

This commit is contained in:
László Monda
2017-03-06 01:58:02 +01:00
parent c7f101b67f
commit 62a3f6b3a4
14 changed files with 277 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
#include "action.h"
#include "arduino_hid/ConsumerAPI.h"
#include "arduino_hid/SystemAPI.h"
key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] = {
// Base layer
@@ -210,7 +211,7 @@ key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] =
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_KEYSTROKE, .keystroke = { .keystrokeType = KEYSTROKE_SYSTEM, .scancode = SYSTEM_WAKE_UP }},
{ .type = KEY_ACTION_NONE },
// Row 2
@@ -219,8 +220,8 @@ key_action_t CurrentKeymap[LAYER_COUNT][SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE] =
{ .type = KEY_ACTION_KEYSTROKE, .keystroke = { .keystrokeType = KEYSTROKE_MEDIA, .scancode = MEDIA_STOP }},
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_NONE },
{ .type = KEY_ACTION_KEYSTROKE, .keystroke = { .keystrokeType = KEYSTROKE_MEDIA, .scancode = HID_CONSUMER_SLEEP_MODE }},
{ .type = KEY_ACTION_KEYSTROKE, .keystroke = { .keystrokeType = KEYSTROKE_SYSTEM, .scancode = SYSTEM_SLEEP }},
{ .type = KEY_ACTION_KEYSTROKE, .keystroke = { .keystrokeType = KEYSTROKE_SYSTEM, .scancode = SYSTEM_POWER_DOWN }},
{ .type = KEY_ACTION_NONE },
// Row 3

View File

@@ -70,11 +70,15 @@ void UpdateUsbReports()
ResetActiveUsbBasicKeyboardReport();
ResetActiveUsbMediaKeyboardReport();
ResetActiveUsbSystemKeyboardReport();
KeyMatrix_Scan(&KeyMatrix);
memcpy(CurrentKeyStates[SLOT_ID_RIGHT_KEYBOARD_HALF], KeyMatrix.keyStates, MAX_KEY_COUNT_PER_MODULE);
UpdateActiveUsbReports();
SwitchActiveUsbBasicKeyboardReport();
SwitchActiveUsbMediaKeyboardReport();
SwitchActiveUsbSystemKeyboardReport();
IsUsbBasicKeyboardReportSent = false;
}

View File

@@ -11,6 +11,7 @@ usb_device_class_config_struct_t UsbDeviceCompositeClassConfig[USB_DEVICE_CONFIG
{UsbBasicKeyboardCallback, (class_handle_t)NULL, &UsbBasicKeyboardClass},
{UsbMouseCallback, (class_handle_t)NULL, &UsbMouseClass},
{UsbMediaKeyboardCallback, (class_handle_t)NULL, &UsbMediaKeyboardClass},
{UsbSystemKeyboardCallback, (class_handle_t)NULL, &UsbSystemKeyboardClass},
};
usb_device_class_config_list_struct_t UsbDeviceCompositeConfigList = {
@@ -41,6 +42,7 @@ static usb_status_t UsbDeviceCallback(usb_device_handle handle, uint32_t event,
UsbBasicKeyboardSetConfiguration(UsbCompositeDevice.basicKeyboardHandle, *temp8);
UsbMouseSetConfiguration(UsbCompositeDevice.mouseHandle, *temp8);
UsbMediaKeyboardSetConfiguration(UsbCompositeDevice.mediaKeyboardHandle, *temp8);
UsbSystemKeyboardSetConfiguration(UsbCompositeDevice.systemKeyboardHandle, *temp8);
error = kStatus_USB_Success;
break;
case kUSB_DeviceEventGetConfiguration:
@@ -57,6 +59,7 @@ static usb_status_t UsbDeviceCallback(usb_device_handle handle, uint32_t event,
UsbBasicKeyboardSetInterface(UsbCompositeDevice.basicKeyboardHandle, interface, alternateSetting);
UsbMouseSetInterface(UsbCompositeDevice.mouseHandle, interface, alternateSetting);
UsbMediaKeyboardSetInterface(UsbCompositeDevice.mediaKeyboardHandle, interface, alternateSetting);
UsbSystemKeyboardSetInterface(UsbCompositeDevice.systemKeyboardHandle, interface, alternateSetting);
error = kStatus_USB_Success;
}
}
@@ -112,6 +115,7 @@ void InitUsb()
UsbCompositeDevice.basicKeyboardHandle = UsbDeviceCompositeConfigList.config[USB_BASIC_KEYBOARD_INTERFACE_INDEX].classHandle;
UsbCompositeDevice.mouseHandle = UsbDeviceCompositeConfigList.config[USB_MOUSE_INTERFACE_INDEX].classHandle;
UsbCompositeDevice.mediaKeyboardHandle = UsbDeviceCompositeConfigList.config[USB_MEDIA_KEYBOARD_INTERFACE_INDEX].classHandle;
UsbCompositeDevice.systemKeyboardHandle = UsbDeviceCompositeConfigList.config[USB_SYSTEM_KEYBOARD_INTERFACE_INDEX].classHandle;
NVIC_SetPriority((IRQn_Type)irqNumber, USB_DEVICE_INTERRUPT_PRIORITY);
NVIC_EnableIRQ((IRQn_Type)irqNumber);

View File

@@ -20,6 +20,7 @@
class_handle_t basicKeyboardHandle;
class_handle_t genericHidHandle;
class_handle_t mediaKeyboardHandle;
class_handle_t systemKeyboardHandle;
uint8_t attach;
uint8_t currentConfiguration;
uint8_t currentInterfaceAlternateSetting[USB_DEVICE_CONFIG_HID];

View File

@@ -139,10 +139,8 @@ uint8_t UsbConfigurationDescriptor[USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH] =
USB_INTERFACE_ALTERNATE_SETTING_NONE,
USB_MEDIA_KEYBOARD_ENDPOINT_COUNT,
USB_CLASS_HID,
// USB_HID_SUBCLASS_NONE, ////
// USB_HID_PROTOCOL_NONE, ////
USB_HID_SUBCLASS_BOOT,
USB_HID_PROTOCOL_KEYBOARD,
USB_HID_SUBCLASS_NONE,
USB_HID_PROTOCOL_NONE,
USB_STRING_DESCRIPTOR_NONE,
// Media keyboard HID descriptor
@@ -166,6 +164,40 @@ uint8_t UsbConfigurationDescriptor[USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH] =
USB_SHORT_GET_LOW(USB_MEDIA_KEYBOARD_INTERRUPT_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(USB_MEDIA_KEYBOARD_INTERRUPT_IN_PACKET_SIZE),
USB_MEDIA_KEYBOARD_INTERRUPT_IN_INTERVAL,
// System keyboard interface descriptor
USB_DESCRIPTOR_LENGTH_INTERFACE,
USB_DESCRIPTOR_TYPE_INTERFACE,
USB_SYSTEM_KEYBOARD_INTERFACE_INDEX,
USB_INTERFACE_ALTERNATE_SETTING_NONE,
USB_SYSTEM_KEYBOARD_ENDPOINT_COUNT,
USB_CLASS_HID,
USB_HID_SUBCLASS_NONE,
USB_HID_PROTOCOL_NONE,
USB_STRING_DESCRIPTOR_NONE,
// System keyboard HID descriptor
USB_DESCRIPTOR_LENGTH_HID,
USB_DESCRIPTOR_TYPE_HID,
USB_SHORT_GET_LOW(USB_HID_VERSION),
USB_SHORT_GET_HIGH(USB_HID_VERSION),
USB_HID_COUNTRY_CODE_NOT_SUPPORTED,
USB_REPORT_DESCRIPTOR_COUNT_PER_HID_DEVICE,
USB_DESCRIPTOR_TYPE_HID_REPORT,
USB_SHORT_GET_LOW(USB_SYSTEM_KEYBOARD_REPORT_DESCRIPTOR_LENGTH),
USB_SHORT_GET_HIGH(USB_SYSTEM_KEYBOARD_REPORT_DESCRIPTOR_LENGTH),
// System keyboard endpoint descriptor
USB_DESCRIPTOR_LENGTH_ENDPOINT,
USB_DESCRIPTOR_TYPE_ENDPOINT,
USB_SYSTEM_KEYBOARD_ENDPOINT_INDEX | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
USB_ENDPOINT_INTERRUPT,
USB_SHORT_GET_LOW(USB_SYSTEM_KEYBOARD_INTERRUPT_IN_PACKET_SIZE),
USB_SHORT_GET_HIGH(USB_SYSTEM_KEYBOARD_INTERRUPT_IN_PACKET_SIZE),
USB_SYSTEM_KEYBOARD_INTERRUPT_IN_INTERVAL,
};
usb_status_t USB_DeviceGetConfigurationDescriptor(

View File

@@ -5,13 +5,14 @@
#include "usb_interfaces/usb_interface_basic_keyboard.h"
#include "usb_interfaces/usb_interface_media_keyboard.h"
#include "usb_interfaces/usb_interface_system_keyboard.h"
#include "usb_interfaces/usb_interface_mouse.h"
#include "usb_interfaces/usb_interface_generic_hid.h"
// Macros:
#define USB_COMPOSITE_CONFIGURATION_INDEX 1
#define USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH 116
#define USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH 141
// Functions:

View File

@@ -21,6 +21,9 @@ usb_status_t USB_DeviceGetHidReportDescriptor(
} else if (USB_MEDIA_KEYBOARD_INTERFACE_INDEX == hidReportDescriptor->interfaceNumber) {
hidReportDescriptor->buffer = UsbMediaKeyboardReportDescriptor;
hidReportDescriptor->length = USB_MEDIA_KEYBOARD_REPORT_DESCRIPTOR_LENGTH;
} else if (USB_SYSTEM_KEYBOARD_INTERFACE_INDEX == hidReportDescriptor->interfaceNumber) {
hidReportDescriptor->buffer = UsbSystemKeyboardReportDescriptor;
hidReportDescriptor->length = USB_MEDIA_KEYBOARD_REPORT_DESCRIPTOR_LENGTH;
} else if (USB_GENERIC_HID_INTERFACE_INDEX == hidReportDescriptor->interfaceNumber) {
hidReportDescriptor->buffer = UsbGenericHidReportDescriptor;
hidReportDescriptor->length = USB_GENERIC_HID_REPORT_DESCRIPTOR_LENGTH;

View File

@@ -5,6 +5,7 @@
#include "usb_interfaces/usb_interface_basic_keyboard.h"
#include "usb_interfaces/usb_interface_media_keyboard.h"
#include "usb_interfaces/usb_interface_system_keyboard.h"
#include "usb_interfaces/usb_interface_mouse.h"
#include "usb_interfaces/usb_interface_generic_hid.h"

View File

@@ -0,0 +1,38 @@
#include "usb_api.h"
#include "usb_descriptor_system_keyboard_report.h"
uint8_t UsbSystemKeyboardReportDescriptor[USB_SYSTEM_KEYBOARD_REPORT_DESCRIPTOR_LENGTH] = {
//TODO limit to system keys only?
// System Control (Power Down, Sleep, Wakeup, ...)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x80, // USAGE (System Control)
0xa1, 0x01, // COLLECTION (Application)
// 1 system key
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x19, 0x00, // USAGE_MINIMUM (Undefined)
0x29, 0xff, // USAGE_MAXIMUM (System Menu Down)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/*
uint8_t UsbMediaKeyboardReportDescriptor[USB_MEDIA_KEYBOARD_REPORT_DESCRIPTOR_LENGTH] = {
HID_RI_USAGE_PAGE(8, HID_RI_USAGE_PAGE_CONSUMER),
HID_RI_USAGE(8, 0x01),
HID_RI_COLLECTION(8, HID_RI_COLLECTION_APPLICATION),
// Scancodes
HID_RI_LOGICAL_MINIMUM(8, 0x00),
HID_RI_LOGICAL_MAXIMUM(8, 0xFF),
HID_RI_USAGE_PAGE(8, HID_RI_USAGE_PAGE_KEY_CODES),
HID_RI_USAGE_MINIMUM(8, 0x00),
HID_RI_USAGE_MAXIMUM(8, 0xFF),
HID_RI_REPORT_COUNT(8, USB_MEDIA_KEYBOARD_MAX_KEYS),
HID_RI_REPORT_SIZE(8, 0x08),
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
HID_RI_END_COLLECTION(0),
};
*/

View File

@@ -0,0 +1,13 @@
#ifndef __USB_DESCRIPTOR_SYSTEM_KEYBOARD_REPORT_H__
#define __USB_DESCRIPTOR_SYSTEM_KEYBOARD_REPORT_H__
// Macros:
#define USB_SYSTEM_KEYBOARD_REPORT_DESCRIPTOR_LENGTH 22
#define USB_SYSTEM_KEYBOARD_MAX_KEYS 1
// Variables:
extern uint8_t UsbSystemKeyboardReportDescriptor[USB_SYSTEM_KEYBOARD_REPORT_DESCRIPTOR_LENGTH];
#endif

View File

@@ -6,6 +6,7 @@
#include "usb_interfaces/usb_interface_basic_keyboard.h"
#include "usb_interfaces/usb_interface_media_keyboard.h"
#include "usb_interfaces/usb_interface_system_keyboard.h"
#include "usb_interfaces/usb_interface_mouse.h"
#include "usb_interfaces/usb_interface_generic_hid.h"
@@ -16,6 +17,7 @@
#define USB_DEVICE_CONFIG_HID ( \
USB_BASIC_KEYBOARD_INTERFACE_COUNT + \
USB_MEDIA_KEYBOARD_INTERFACE_COUNT + \
USB_SYSTEM_KEYBOARD_INTERFACE_COUNT + \
USB_MOUSE_INTERFACE_COUNT + \
USB_GENERIC_HID_INTERFACE_COUNT \
)
@@ -34,6 +36,7 @@
USB_CONTROL_ENDPOINT_COUNT + \
USB_BASIC_KEYBOARD_ENDPOINT_COUNT + \
USB_MEDIA_KEYBOARD_ENDPOINT_COUNT + \
USB_SYSTEM_KEYBOARD_ENDPOINT_COUNT + \
USB_MOUSE_ENDPOINT_COUNT + \
USB_GENERIC_HID_ENDPOINT_COUNT \
)

View File

@@ -0,0 +1,112 @@
#include "main.h"
#include "action.h"
#include "fsl_port.h"
#include "usb_api.h"
#include "usb_composite_device.h"
#include "peripherials/test_led.h"
#include "fsl_i2c.h"
#include "i2c.h"
#include "i2c_addresses.h"
static usb_device_endpoint_struct_t UsbSystemKeyboardEndpoints[USB_SYSTEM_KEYBOARD_ENDPOINT_COUNT] = {{
USB_SYSTEM_KEYBOARD_ENDPOINT_INDEX | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
USB_ENDPOINT_INTERRUPT,
USB_SYSTEM_KEYBOARD_INTERRUPT_IN_PACKET_SIZE,
}};
static usb_device_interface_struct_t UsbSystemKeyboardInterface[] = {{
USB_INTERFACE_ALTERNATE_SETTING_NONE,
{USB_SYSTEM_KEYBOARD_ENDPOINT_COUNT, UsbSystemKeyboardEndpoints},
NULL,
}};
static usb_device_interfaces_struct_t UsbSystemKeyboardInterfaces[USB_SYSTEM_KEYBOARD_INTERFACE_COUNT] = {{
USB_CLASS_HID,
USB_HID_SUBCLASS_BOOT,
USB_HID_PROTOCOL_KEYBOARD,
USB_SYSTEM_KEYBOARD_INTERFACE_INDEX,
UsbSystemKeyboardInterface,
sizeof(UsbSystemKeyboardInterface) / sizeof(usb_device_interfaces_struct_t),
}};
static usb_device_interface_list_t UsbSystemKeyboardInterfaceList[USB_DEVICE_CONFIGURATION_COUNT] = {{
USB_SYSTEM_KEYBOARD_INTERFACE_COUNT,
UsbSystemKeyboardInterfaces,
}};
usb_device_class_struct_t UsbSystemKeyboardClass = {
UsbSystemKeyboardInterfaceList,
kUSB_DeviceClassTypeHid,
USB_DEVICE_CONFIGURATION_COUNT,
};
static usb_system_keyboard_report_t usbSystemKeyboardReports[2];
usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport = usbSystemKeyboardReports;
bool IsUsbSystemKeyboardReportSent = false;
usb_system_keyboard_report_t* getInactiveUsbSystemKeyboardReport()
{
return ActiveUsbSystemKeyboardReport == usbSystemKeyboardReports ? usbSystemKeyboardReports+1 : usbSystemKeyboardReports;
}
void SwitchActiveUsbSystemKeyboardReport()
{
ActiveUsbSystemKeyboardReport = getInactiveUsbSystemKeyboardReport();
}
void ResetActiveUsbSystemKeyboardReport()
{
bzero(ActiveUsbSystemKeyboardReport, USB_SYSTEM_KEYBOARD_REPORT_LENGTH);
}
static usb_status_t UsbSystemKeyboardAction(void)
{
usb_status_t status = USB_DeviceHidSend(
UsbCompositeDevice.systemKeyboardHandle, USB_SYSTEM_KEYBOARD_ENDPOINT_INDEX,
(uint8_t*)getInactiveUsbSystemKeyboardReport(), USB_SYSTEM_KEYBOARD_REPORT_LENGTH);
IsUsbSystemKeyboardReportSent = true;
return status;
}
usb_status_t UsbSystemKeyboardCallback(class_handle_t handle, uint32_t event, void *param)
{
usb_status_t error = kStatus_USB_Error;
switch (event) {
case kUSB_DeviceHidEventSendResponse:
if (UsbCompositeDevice.attach) {
return UsbSystemKeyboardAction();
}
break;
case kUSB_DeviceHidEventGetReport:
case kUSB_DeviceHidEventSetReport:
case kUSB_DeviceHidEventRequestReportBuffer:
error = kStatus_USB_InvalidRequest;
break;
case kUSB_DeviceHidEventGetIdle:
case kUSB_DeviceHidEventGetProtocol:
case kUSB_DeviceHidEventSetIdle:
case kUSB_DeviceHidEventSetProtocol:
break;
default:
break;
}
return error;
}
usb_status_t UsbSystemKeyboardSetConfiguration(class_handle_t handle, uint8_t configuration)
{
if (USB_COMPOSITE_CONFIGURATION_INDEX == configuration) {
return UsbSystemKeyboardAction();
}
return kStatus_USB_Error;
}
usb_status_t UsbSystemKeyboardSetInterface(class_handle_t handle, uint8_t interface, uint8_t alternateSetting)
{
if (USB_SYSTEM_KEYBOARD_INTERFACE_INDEX == interface) {
return UsbSystemKeyboardAction();
}
return kStatus_USB_Error;
}

View File

@@ -0,0 +1,44 @@
#ifndef __USB_INTERFACE_SYSTEM_KEYBOARD_H__
#define __USB_INTERFACE_SYSTEM_KEYBOARD_H__
// Includes:
#include "fsl_common.h"
#include "usb_api.h"
#include "usb_descriptors/usb_descriptor_system_keyboard_report.h"
// Macros:
#define USB_SYSTEM_KEYBOARD_INTERFACE_INDEX 4
#define USB_SYSTEM_KEYBOARD_INTERFACE_COUNT 1
#define USB_SYSTEM_KEYBOARD_ENDPOINT_INDEX 6
#define USB_SYSTEM_KEYBOARD_ENDPOINT_COUNT 1
#define USB_SYSTEM_KEYBOARD_INTERRUPT_IN_PACKET_SIZE 1
#define USB_SYSTEM_KEYBOARD_INTERRUPT_IN_INTERVAL 4
#define USB_SYSTEM_KEYBOARD_REPORT_LENGTH 1
// Typedefs:
typedef struct {
uint8_t scancodes[USB_SYSTEM_KEYBOARD_MAX_KEYS];
} __attribute__ ((packed)) usb_system_keyboard_report_t;
// Variables:
extern bool IsUsbSystemKeyboardReportSent;
extern usb_device_class_struct_t UsbSystemKeyboardClass;
extern usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport;
// Functions:
extern usb_status_t UsbSystemKeyboardCallback(class_handle_t handle, uint32_t event, void *param);
extern usb_status_t UsbSystemKeyboardSetConfiguration(class_handle_t handle, uint8_t configuration);
extern usb_status_t UsbSystemKeyboardSetInterface(class_handle_t handle, uint8_t interface, uint8_t alternateSetting);
extern void ResetActiveUsbSystemKeyboardReport();
extern void SwitchActiveUsbSystemKeyboardReport();
#endif

View File

@@ -74,6 +74,7 @@ void UpdateActiveUsbReports() {
uint8_t basicScancodeIndex = 0;
uint8_t mediaScancodeIndex = 0;
uint8_t systemScancodeIndex = 0;
activeLayer = LAYER_ID_BASE;
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
@@ -112,6 +113,12 @@ void UpdateActiveUsbReports() {
}
ActiveUsbMediaKeyboardReport->scancodes[mediaScancodeIndex++] = action.keystroke.scancode;
break;
case KEYSTROKE_SYSTEM:
if (systemScancodeIndex >= USB_SYSTEM_KEYBOARD_MAX_KEYS) {
break;
}
ActiveUsbSystemKeyboardReport->scancodes[systemScancodeIndex++] = action.keystroke.scancode;
break;
}
break;
case KEY_ACTION_MOUSE: