Last active
May 21, 2022 11:27
-
-
Save petejohanson/e2c079132cc141f88f19b128e8933c59 to your computer and use it in GitHub Desktop.
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
/* | |
* Copyright (c) 2020 The ZMK Contributors | |
* | |
* SPDX-License-Identifier: MIT | |
*/ | |
#pragma once | |
#include <usb/usb_device.h> | |
#include <usb/class/usb_hid.h> | |
#include <zmk/keys.h> | |
#include <dt-bindings/zmk/hid_usage.h> | |
#include <dt-bindings/zmk/hid_usage_pages.h> | |
#ifndef HID_USAGE_PAGE16 | |
#define HID_USAGE_PAGE16(page, page2) \ | |
HID_ITEM(HID_ITEM_TAG_USAGE_PAGE, HID_ITEM_TYPE_GLOBAL, 2), page, page2 | |
#endif | |
#ifndef HID_USAGE6 | |
#define HID_USAGE16(page, page2) \ | |
HID_ITEM(HID_ITEM_TAG_USAGE, HID_ITEM_TYPE_LOCAL, 2), page, page2 | |
#endif | |
#define KEYMAP_HID_MAX_BYTES 64 | |
static const uint8_t zmk_keymap_hid_report_desc[] = { | |
HID_USAGE_PAGE16(0x0B, 0xFF), | |
HID_COLLECTION(HID_COLLECTION_APPLICATION), | |
HID_USAGE(0x00), | |
HID_REPORT_ID(0x01), | |
HID_LOGICAL_MIN8(0x00), | |
HID_LOGICAL_MAX16(0xFF, 0x00), | |
HID_REPORT_SIZE(0x08), | |
HID_REPORT_COUNT(KEYMAP_HID_MAX_BYTES), | |
/* INPUT (Data,Var,Abs) */ | |
HID_FEATURE(0x02), | |
HID_END_COLLECTION, | |
HID_COLLECTION(HID_COLLECTION_APPLICATION), | |
HID_USAGE(0x00), | |
HID_REPORT_ID(0x02), | |
HID_LOGICAL_MIN8(0x00), | |
HID_LOGICAL_MAX16(0xFF, 0x00), | |
HID_REPORT_SIZE(0x08), | |
HID_REPORT_COUNT(KEYMAP_HID_MAX_BYTES), | |
/* INPUT (Data,Var,Abs) */ | |
HID_FEATURE(0x02), | |
HID_END_COLLECTION, | |
}; | |
struct zmk_keymap_hid_sample_report_body { | |
uint8_t data[KEYMAP_HID_MAX_BYTES]; | |
} __packed; | |
struct zmk_keymap_hid_sample_report { | |
uint8_t report_id; | |
struct zmk_keymap_hid_sample_report_body body; | |
} __packed; |
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
/* | |
* Copyright (c) 2020 The ZMK Contributors | |
* | |
* SPDX-License-Identifier: MIT | |
*/ | |
#include <device.h> | |
#include <init.h> | |
#include <usb/usb_device.h> | |
#include <usb/class/usb_hid.h> | |
#include <zmk/usb.h> | |
#include <zmk/hid.h> | |
#include <zmk/keymap_hid.h> | |
#include <zmk/keymap.h> | |
#include <zmk/event_manager.h> | |
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); | |
static const struct device *hid_dev; | |
static K_SEM_DEFINE(hid_sem, 1, 1); | |
static void in_ready_cb(const struct device *dev) { | |
LOG_DBG(""); | |
k_sem_give(&hid_sem); | |
} | |
static void out_ready_cb(const struct device *dev) { LOG_DBG(""); } | |
static uint8_t payload[] = {0x10, 0x02}; | |
static int get_report_cb(const struct device *dev, struct usb_setup_packet *setup, int32_t *len, | |
uint8_t **data) { | |
LOG_DBG(""); | |
*data = payload; | |
*len = sizeof(payload); | |
return 0; | |
} | |
static int set_report_cb(const struct device *dev, struct usb_setup_packet *setup, int32_t *len, | |
uint8_t **data) { | |
LOG_DBG(""); | |
return 0; | |
} | |
static const struct hid_ops ops = {.get_report = get_report_cb, | |
.set_report = set_report_cb, | |
.int_in_ready = in_ready_cb, | |
.int_out_ready = out_ready_cb}; | |
int zmk_keymap_hid_usb_hid_send_report(const uint8_t *report, size_t len) { | |
switch (zmk_usb_get_status()) { | |
case USB_DC_SUSPEND: | |
return usb_wakeup_request(); | |
case USB_DC_ERROR: | |
case USB_DC_RESET: | |
case USB_DC_DISCONNECTED: | |
case USB_DC_UNKNOWN: | |
return -ENODEV; | |
default: | |
k_sem_take(&hid_sem, K_MSEC(30)); | |
int err = hid_int_ep_write(hid_dev, report, len, NULL); | |
if (err) { | |
k_sem_give(&hid_sem); | |
} | |
return err; | |
} | |
} | |
static int zmk_keymap_hid_usb_init(const struct device *_arg) { | |
int ret; | |
hid_dev = device_get_binding("HID_1"); | |
if (hid_dev == NULL) { | |
LOG_ERR("Unable to locate HID device"); | |
return -EINVAL; | |
} | |
usb_hid_register_device(hid_dev, zmk_keymap_hid_report_desc, sizeof(zmk_keymap_hid_report_desc), | |
&ops); | |
ret = usb_hid_init(hid_dev); | |
if (ret < 0) { | |
LOG_ERR("Failed to init hid device: %d", ret); | |
return ret; | |
} | |
return 0; | |
} | |
SYS_INIT(zmk_keymap_hid_usb_init, APPLICATION, CONFIG_ZMK_USB_INIT_PRIORITY); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@petejohanson I think
#define KEYMAP_HID_MAX_BYTES 64
should be 63 if you have the report ID using the first bytewhy is everything 16 bit in the report descriptor instead of 8 bit?