Skip to content

Commit

Permalink
feat: handle LED indicators report
Browse files Browse the repository at this point in the history
  • Loading branch information
bortoz committed Feb 24, 2022
1 parent 1d5b48c commit 1e33ee7
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ target_sources(app PRIVATE src/events/layer_state_changed.c)
target_sources(app PRIVATE src/events/keycode_state_changed.c)
target_sources(app PRIVATE src/events/modifiers_state_changed.c)
target_sources(app PRIVATE src/events/endpoint_selection_changed.c)
target_sources(app PRIVATE src/events/led_indicator_changed.c)
target_sources(app PRIVATE src/events/sensor_event.c)
target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/events/wpm_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/ble_active_profile_changed.c)
Expand Down Expand Up @@ -81,6 +82,7 @@ target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c)
target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/backlight.c)
target_sources(app PRIVATE src/endpoints.c)
target_sources(app PRIVATE src/hid_listener.c)
target_sources(app PRIVATE src/led_indicators.c)
target_sources(app PRIVATE src/main.c)

add_subdirectory(src/display/)
Expand Down
1 change: 1 addition & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ config ZMK_USB
select USB
select USB_DEVICE_STACK
select USB_DEVICE_HID
select ENABLE_HID_INT_OUT_EP

if ZMK_USB

Expand Down
27 changes: 27 additions & 0 deletions app/include/dt-bindings/zmk/led_indicators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

/* Led Num Lock */
#define LED_NUMLOCK (0)
#define LED_NUM (LED_NUMLOCK)
#define LED_NLCK (LED_NUMLOCK)

/* Led Caps Lock */
#define LED_CAPSLOCK (1)
#define LED_CAPS (LED_CAPSLOCK)
#define LED_CLCK (LED_CAPSLOCK)

/* Led Scroll Lock */
#define LED_SCROLLLOCK (2)
#define LED_SLCK (LED_SCROLLLOCK)

/* Led Compose */
#define LED_COMPOSE (3)

/* Led Kana */
#define LED_KANA (4)
1 change: 1 addition & 0 deletions app/include/zmk/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ int zmk_ble_prof_prev();
int zmk_ble_prof_select(uint8_t index);

int zmk_ble_active_profile_index();
int zmk_ble_profile_index(const bt_addr_le_t *addr);
bt_addr_le_t *zmk_ble_active_profile_addr();
bool zmk_ble_active_profile_is_open();
bool zmk_ble_active_profile_is_connected();
Expand Down
16 changes: 16 additions & 0 deletions app/include/zmk/events/led_indicator_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zmk/led_indicators_types.h>
#include <zmk/event_manager.h>

struct zmk_led_changed {
zmk_leds_flags_t leds;
};

ZMK_EVENT_DECLARE(zmk_led_changed);
52 changes: 50 additions & 2 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#define COLLECTION_REPORT 0x03

#define HID_REPORT_ID_KEYBOARD 0x01
#define HID_REPORT_ID_LEDS 0x01
#define HID_REPORT_ID_CONSUMER 0x02

static const uint8_t zmk_hid_report_desc[] = {
/* USAGE_PAGE (Generic Desktop) */
HID_GI_USAGE_PAGE,
Expand All @@ -29,7 +33,7 @@ static const uint8_t zmk_hid_report_desc[] = {
COLLECTION_APPLICATION,
/* REPORT ID (1) */
HID_GI_REPORT_ID,
0x01,
HID_REPORT_ID_KEYBOARD,
/* USAGE_PAGE (Keyboard/Keypad) */
HID_GI_USAGE_PAGE,
HID_USAGE_KEY,
Expand Down Expand Up @@ -69,6 +73,41 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_MI_INPUT,
0x03,

/* REPORT ID (1) */
HID_GI_REPORT_ID,
HID_REPORT_ID_LEDS,
/* USAGE_PAGE (Led) */
HID_GI_USAGE_PAGE,
HID_USAGE_LED,
/* USAGE_MINIMUM (Num Lock) */
HID_LI_USAGE_MIN(1),
HID_USAGE_LED_NUM_LOCK,
/* USAGE_MAXIMUM (Kana) */
HID_LI_USAGE_MAX(1),
HID_USAGE_LED_KANA,
/* REPORT_SIZE (1) */
HID_GI_REPORT_SIZE,
0x01,
/* REPORT_COUNT (5) */
HID_GI_REPORT_COUNT,
0x05,
/* OUTPUT (Data,Var,Abs) */
HID_MI_OUTPUT,
0x02,

/* USAGE_PAGE (Led) */
HID_GI_USAGE_PAGE,
HID_USAGE_LED,
/* REPORT_SIZE (3) */
HID_GI_REPORT_SIZE,
0x03,
/* REPORT_COUNT (1) */
HID_GI_REPORT_COUNT,
0x01,
/* OUTPUT (Cnst,Var,Abs) */
HID_MI_OUTPUT,
0x03,

/* USAGE_PAGE (Keyboard/Keypad) */
HID_GI_USAGE_PAGE,
HID_USAGE_KEY,
Expand Down Expand Up @@ -134,7 +173,7 @@ static const uint8_t zmk_hid_report_desc[] = {
COLLECTION_APPLICATION,
/* REPORT ID (1) */
HID_GI_REPORT_ID,
0x02,
HID_REPORT_ID_CONSUMER,
/* USAGE_PAGE (Consumer) */
HID_GI_USAGE_PAGE,
HID_USAGE_CONSUMER,
Expand Down Expand Up @@ -209,6 +248,15 @@ struct zmk_hid_keyboard_report {
struct zmk_hid_keyboard_report_body body;
} __packed;

struct zmk_hid_led_report_body {
uint8_t leds;
} __packed;

struct zmk_hid_led_report {
uint8_t report_id;
struct zmk_hid_led_report_body body;
} __packed;

struct zmk_hid_consumer_report_body {
#if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC)
uint8_t keys[CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE];
Expand Down
18 changes: 18 additions & 0 deletions app/include/zmk/led_indicators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zmk/endpoints_types.h>
#include <zmk/hid.h>
#include <zmk/led_indicators_types.h>

zmk_leds_flags_t zmk_leds_get_current_flags();
zmk_leds_flags_t zmk_leds_get_flags(enum zmk_endpoint endpoint, uint8_t profile);
void zmk_leds_update_flags(zmk_leds_flags_t leds, enum zmk_endpoint endpoint, uint8_t profile);

void zmk_leds_process_report(struct zmk_hid_led_report_body *report, enum zmk_endpoint endpoint,
uint8_t profile);
9 changes: 9 additions & 0 deletions app/include/zmk/led_indicators_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

typedef uint8_t zmk_leds_flags_t;
9 changes: 9 additions & 0 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ int zmk_ble_clear_bonds() {

int zmk_ble_active_profile_index() { return active_profile; }

int zmk_ble_profile_index(const bt_addr_le_t *addr) {
for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) {
if (bt_addr_le_cmp(addr, &profiles[i].peer) == 0) {
return i;
}
}
return -ENODEV;
}

#if IS_ENABLED(CONFIG_SETTINGS)
static void ble_save_profile_work(struct k_work *work) {
settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile));
Expand Down
10 changes: 10 additions & 0 deletions app/src/events/led_indicator_changed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <kernel.h>
#include <zmk/events/led_indicator_changed.h>

ZMK_EVENT_IMPL(zmk_led_changed);
5 changes: 3 additions & 2 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <dt-bindings/zmk/modifiers.h>

static struct zmk_hid_keyboard_report keyboard_report = {
.report_id = 1, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}};
.report_id = HID_REPORT_ID_KEYBOARD, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}};

static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}};
static struct zmk_hid_consumer_report consumer_report = {.report_id = HID_REPORT_ID_CONSUMER,
.body = {.keys = {0}}};

// Keep track of how often a modifier was pressed.
// Only release the modifier if the count is 0.
Expand Down
38 changes: 36 additions & 2 deletions app/src/hog.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <bluetooth/gatt.h>

#include <zmk/ble.h>
#include <zmk/endpoints_types.h>
#include <zmk/hog.h>
#include <zmk/hid.h>
#include <zmk/led_indicators.h>

enum {
HIDS_REMOTE_WAKE = BIT(0),
Expand Down Expand Up @@ -47,12 +49,17 @@ enum {
};

static struct hids_report input = {
.id = 0x01,
.id = HID_REPORT_ID_KEYBOARD,
.type = HIDS_INPUT,
};

static struct hids_report led_indicators = {
.id = HID_REPORT_ID_LEDS,
.type = HIDS_OUTPUT,
};

static struct hids_report consumer_input = {
.id = 0x02,
.id = HID_REPORT_ID_CONSUMER,
.type = HIDS_INPUT,
};

Expand Down Expand Up @@ -85,6 +92,27 @@ static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt
sizeof(struct zmk_hid_keyboard_report_body));
}

static ssize_t write_hids_leds_report(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset,
uint8_t flags) {
if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
return 0;
}

if (len != sizeof(struct zmk_hid_led_report_body)) {
LOG_ERR("LED report is malformed: length=%d", len);
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}

struct zmk_hid_led_report_body *report =
(struct zmk_hid_led_report_body *)((uint8_t *)buf + offset);
uint8_t profile = zmk_ble_profile_index(bt_conn_get_dst(conn));

zmk_leds_process_report(report, ZMK_ENDPOINT_BLE, profile);

return len;
}

static ssize_t read_hids_consumer_input_report(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset) {
Expand Down Expand Up @@ -134,6 +162,12 @@ BT_GATT_SERVICE_DEFINE(
BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
NULL, &input),

BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE_ENCRYPT, NULL, write_hids_leds_report, NULL),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
NULL, &led_indicators),

BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_READ_ENCRYPT, read_hids_consumer_input_report, NULL, NULL),
BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
Expand Down
83 changes: 83 additions & 0 deletions app/src/led_indicators.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <kernel.h>
#include <device.h>
#include <init.h>

#include <logging/log.h>
#include <drivers/led.h>

#include <zmk/ble.h>
#include <zmk/endpoints.h>
#include <zmk/hid.h>
#include <zmk/led_indicators.h>
#include <zmk/events/led_indicator_changed.h>
#include <dt-bindings/zmk/led_indicators.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#define NUM_USB_PROFILES (COND_CODE_1(IS_ENABLED(CONFIG_ZMK_USB), (1), (0)))
#define NUM_BLE_PROFILES (COND_CODE_1(IS_ENABLED(CONFIG_ZMK_BLE), (CONFIG_BT_MAX_CONN), (0)))
#define NUM_PROFILES (NUM_USB_PROFILES + NUM_BLE_PROFILES)

static zmk_leds_flags_t led_flags[NUM_PROFILES];

static size_t profile_index(enum zmk_endpoint endpoint, uint8_t profile) {
switch (endpoint) {
case ZMK_ENDPOINT_USB:
return 0;
case ZMK_ENDPOINT_BLE:
return NUM_USB_PROFILES + profile;
}

CODE_UNREACHABLE;
}

zmk_leds_flags_t zmk_leds_get_current_flags() {
enum zmk_endpoint endpoint = zmk_endpoints_selected();

uint8_t profile;

switch (endpoint) {
#if IS_ENABLED(CONFIG_ZMK_BLE)
case ZMK_ENDPOINT_USB:
profile = zmk_ble_active_profile_index();
break;
#endif
default:
profile = 0;
}

return zmk_leds_get_flags(endpoint, profile);
}

zmk_leds_flags_t zmk_leds_get_flags(enum zmk_endpoint endpoint, uint8_t profile) {
size_t index = profile_index(endpoint, profile);
return led_flags[index];
}

static void raise_led_changed_event(struct k_work *_work) {
ZMK_EVENT_RAISE(
new_zmk_led_changed((struct zmk_led_changed){.leds = zmk_leds_get_current_flags()}));
}

static K_WORK_DEFINE(led_changed_work, raise_led_changed_event);

void zmk_leds_update_flags(zmk_leds_flags_t leds, enum zmk_endpoint endpoint, uint8_t profile) {
size_t index = profile_index(endpoint, profile);
led_flags[index] = leds;

k_work_submit(&led_changed_work);
}

void zmk_leds_process_report(struct zmk_hid_led_report_body *report, enum zmk_endpoint endpoint,
uint8_t profile) {
zmk_leds_flags_t leds = report->leds;
zmk_leds_update_flags(leds, endpoint, profile);

LOG_DBG("Update LED indicators: endpoint=%d, profile=%d, flags=%x", endpoint, profile, leds);
}
Loading

0 comments on commit 1e33ee7

Please sign in to comment.