summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPete Johanson <peter@peterjohanson.com>2020-06-02 11:23:22 -0400
committerPete Johanson <peter@peterjohanson.com>2020-06-02 11:23:22 -0400
commit8de6c85b7464fdc3518648242ce16142e30ed73d (patch)
tree22fb6a1b87520e8472142003d093edfab7db616b /src
parentf1afbb8e49383d429096bc84fc5855c3929acc50 (diff)
Implement a basic set of consumer page keycodes.
Diffstat (limited to 'src')
-rw-r--r--src/endpoints.c58
-rw-r--r--src/handlers.c2
-rw-r--r--src/hid.c93
-rw-r--r--src/hog.c32
-rw-r--r--src/keymap.c7
-rw-r--r--src/usb_hid.c4
6 files changed, 145 insertions, 51 deletions
diff --git a/src/endpoints.c b/src/endpoints.c
index 1f2c94d..f46d42d 100644
--- a/src/endpoints.c
+++ b/src/endpoints.c
@@ -35,41 +35,71 @@ int zmk_endpoints_init()
return 0;
}
-int zmk_endpoints_send_report()
+int zmk_endpoints_send_report(enum zmk_hid_report_changes report_type)
{
int err;
- struct zmk_hid_report *report = zmk_hid_get_report();
+ struct zmk_hid_keypad_report *keypad_report;
+ struct zmk_hid_consumer_report *consumer_report;
+ switch (report_type)
+ {
+ case Keypad:
+ keypad_report = zmk_hid_get_keypad_report();
+#ifdef CONFIG_ZMK_USB
+ if (zmk_usb_hid_send_report((u8_t *)keypad_report, sizeof(struct zmk_hid_keypad_report)) != 0)
+ {
+ LOG_DBG("USB Send Failed");
+ }
+#endif /* CONFIG_ZMK_USB */
+#ifdef CONFIG_ZMK_BLE
+ err = zmk_hog_send_keypad_report(&keypad_report->body);
+ if (err)
+ {
+ LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
+ }
+#endif /* CONFIG_ZMK_BLE */
+
+ break;
+ case Consumer:
+ consumer_report = zmk_hid_get_consumer_report();
#ifdef CONFIG_ZMK_USB
- if (zmk_usb_hid_send_report(report) != 0)
- {
- LOG_DBG("USB Send Failed");
- }
+ if (zmk_usb_hid_send_report((u8_t *)consumer_report, sizeof(struct zmk_hid_consumer_report)) != 0)
+ {
+ LOG_DBG("USB Send Failed");
+ }
#endif /* CONFIG_ZMK_USB */
#ifdef CONFIG_ZMK_BLE
- err = zmk_hog_send_report(report);
- if (err)
- {
- LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
- }
+ err = zmk_hog_send_consumer_report(&consumer_report->body);
+ if (err)
+ {
+ LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
+ }
#endif /* CONFIG_ZMK_BLE */
+ break;
+ default:
+ LOG_ERR("Unknown report change type %d", report_type);
+ return -EINVAL;
+ }
+
return 0;
}
int zmk_endpoints_send_key_event(struct zmk_key_event key_event)
{
+ enum zmk_hid_report_changes changes;
+
LOG_DBG("key %d, state %d\n", key_event.key, key_event.pressed);
if (key_event.pressed)
{
- zmk_hid_press_key(key_event.key);
+ changes = zmk_hid_press_key(key_event.key);
}
else
{
- zmk_hid_release_key(key_event.key);
+ changes = zmk_hid_release_key(key_event.key);
}
- return zmk_endpoints_send_report();
+ return zmk_endpoints_send_report(changes);
}
diff --git a/src/handlers.c b/src/handlers.c
index ff969b1..90a0867 100644
--- a/src/handlers.c
+++ b/src/handlers.c
@@ -46,7 +46,7 @@ bool zmk_handle_action(zmk_action action, struct zmk_key_event *key_event)
else
{
// Since not sending a keycode, at least send the report w/ the mod removed
- zmk_endpoints_send_report();
+ zmk_endpoints_send_report(Keypad);
}
}
break;
diff --git a/src/hid.c b/src/hid.c
index 35dcdac..b3d47cf 100644
--- a/src/hid.c
+++ b/src/hid.c
@@ -1,15 +1,25 @@
-#include <zmk/hid.h>
+#include <logging/log.h>
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
-static struct zmk_hid_report report = {
- .modifiers = 0,
- .keys = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
+#include <zmk/hid.h>
-#define _TOGGLE_MOD(mod, state) \
- if (modifier > MOD_RGUI) \
- { \
- return -EINVAL; \
- } \
- WRITE_BIT(report.modifiers, mod, state); \
+static struct zmk_hid_keypad_report kp_report = {
+ .report_id = 1,
+ .body = {
+ .modifiers = 0,
+ .keys = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
+
+static struct zmk_hid_consumer_report consumer_report = {
+ .report_id = 2,
+ .body = {
+ .keys = 0x00}};
+
+#define _TOGGLE_MOD(mod, state) \
+ if (modifier > MOD_RGUI) \
+ { \
+ return -EINVAL; \
+ } \
+ WRITE_BIT(kp_report.body.modifiers, mod, state); \
return 0;
int zmk_hid_register_mod(zmk_mod modifier)
@@ -23,13 +33,13 @@ int zmk_hid_unregister_mod(zmk_mod modifier)
int zmk_hid_register_mods(zmk_mod_flags modifiers)
{
- report.modifiers |= modifiers;
+ kp_report.body.modifiers |= modifiers;
return 0;
}
int zmk_hid_unregister_mods(zmk_mod_flags modifiers)
{
- report.modifiers &= ~modifiers;
+ kp_report.body.modifiers &= ~modifiers;
return 0;
}
@@ -40,56 +50,81 @@ int zmk_hid_unregister_mods(zmk_mod_flags modifiers)
#define TOGGLE_BOOT_KEY(match, val) \
for (int idx = 0; idx < MAX_KEYS; idx++) \
{ \
- if (report.boot.keys[idx + KEY_OFFSET] != match) \
+ if (kp_report.boot.keys[idx + KEY_OFFSET] != match) \
{ \
continue; \
} \
- report.boot.keys[idx + KEY_OFFSET] = val; \
+ kp_report.boot.keys[idx + KEY_OFFSET] = val; \
break; \
}
*/
-#define TOGGLE_KEY(code, val) WRITE_BIT(report.keys[code / 8], code % 8, val)
+#define TOGGLE_KEY(code, val) WRITE_BIT(kp_report.body.keys[code / 8], code % 8, val)
+
+#define TOGGLE_CONSUMER(key, state) \
+ WRITE_BIT(consumer_report.body.keys, (key - 0x100), state);
-int zmk_hid_press_key(zmk_key code)
+enum zmk_hid_report_changes zmk_hid_press_key(zmk_key code)
{
if (code >= KC_LCTL && code <= KC_RGUI)
{
return zmk_hid_register_mod(code - KC_LCTL);
}
- if (code > ZMK_HID_MAX_KEYCODE)
+ if (ZK_IS_CONSUMER(code))
{
- return -EINVAL;
+ LOG_DBG("Toggling a consumer key!");
+ TOGGLE_CONSUMER(code, true);
+ return Consumer;
}
+ else
+ {
+ if (code > ZMK_HID_MAX_KEYCODE)
+ {
+ return -EINVAL;
+ }
- // TOGGLE_BOOT_KEY(0U, code);
+ // TOGGLE_BOOT_KEY(0U, code);
- TOGGLE_KEY(code, true);
+ TOGGLE_KEY(code, true);
- return 0;
+ return Keypad;
+ }
};
-int zmk_hid_release_key(zmk_key code)
+enum zmk_hid_report_changes zmk_hid_release_key(zmk_key code)
{
if (code >= KC_LCTL && code <= KC_RGUI)
{
return zmk_hid_unregister_mod(code - KC_LCTL);
}
- if (code > ZMK_HID_MAX_KEYCODE)
+ if (ZK_IS_CONSUMER(code))
{
- return -EINVAL;
+ TOGGLE_CONSUMER(code, false);
+ return Consumer;
}
+ else
+ {
+ if (code > ZMK_HID_MAX_KEYCODE)
+ {
+ return -EINVAL;
+ }
- // TOGGLE_BOOT_KEY(code, 0U);
+ // TOGGLE_BOOT_KEY(0U, code);
- TOGGLE_KEY(code, false);
+ TOGGLE_KEY(code, false);
- return 0;
+ return Keypad;
+ }
};
-struct zmk_hid_report *zmk_hid_get_report()
+struct zmk_hid_keypad_report *zmk_hid_get_keypad_report()
+{
+ return &kp_report;
+}
+
+struct zmk_hid_consumer_report *zmk_hid_get_consumer_report()
{
- return &report;
+ return &consumer_report;
}
diff --git a/src/hog.c b/src/hog.c
index 67ccf5b..087af42 100644
--- a/src/hog.c
+++ b/src/hog.c
@@ -49,6 +49,11 @@ static struct hids_report input = {
.type = HIDS_INPUT,
};
+static struct hids_report consumer_input = {
+ .id = 0x02,
+ .type = HIDS_INPUT,
+};
+
static bool host_requests_notification = false;
static u8_t ctrl_point;
// static u8_t proto_mode;
@@ -70,8 +75,14 @@ static ssize_t read_hids_report_map(struct bt_conn *conn, const struct bt_gatt_a
static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset)
{
- struct zmk_hid_report *report = zmk_hid_get_report();
- return bt_gatt_attr_read(conn, attr, buf, len, offset, report, sizeof(struct zmk_hid_report));
+ struct zmk_hid_keypad_report_body *report_body = &zmk_hid_get_keypad_report()->body;
+ return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body, sizeof(struct zmk_hid_keypad_report_body));
+}
+
+static ssize_t read_hids_consumer_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset)
+{
+ struct zmk_hid_consumer_report_body *report_body = &zmk_hid_get_consumer_report()->body;
+ return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body, sizeof(struct zmk_hid_consumer_report_body));
}
// static ssize_t write_proto_mode(struct bt_conn *conn,
@@ -123,12 +134,25 @@ BT_GATT_SERVICE_DEFINE(hog_svc,
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ,
read_hids_report_ref, NULL, &input),
+ 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),
+ BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ,
+ read_hids_report_ref, NULL, &consumer_input),
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT,
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE,
NULL, write_ctrl_point, &ctrl_point));
-int zmk_hog_send_report(struct zmk_hid_report *report)
+int zmk_hog_send_keypad_report(struct zmk_hid_keypad_report_body *report)
+{
+ return bt_gatt_notify(NULL, &hog_svc.attrs[5], report, sizeof(struct zmk_hid_keypad_report_body));
+};
+
+int zmk_hog_send_consumer_report(struct zmk_hid_consumer_report_body *report)
{
- return bt_gatt_notify(NULL, &hog_svc.attrs[5], report, sizeof(struct zmk_hid_report));
+ return bt_gatt_notify(NULL, &hog_svc.attrs[10], report, sizeof(struct zmk_hid_consumer_report_body));
};
diff --git a/src/keymap.c b/src/keymap.c
index df7e715..569a2cc 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -1,4 +1,6 @@
+#include <logging/log.h>
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/keymap.h>
static u32_t zmk_keymap_layer_state = 0;
@@ -55,7 +57,10 @@ zmk_key zmk_keymap_keycode_from_position(u32_t row, u32_t column)
{
if ((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default)
{
- zmk_key key = zmk_keymap[layer][(row * ZMK_MATRIX_COLS) + column];
+ u8_t key_index = (row * ZMK_MATRIX_COLS) + column;
+ LOG_DBG("Getting key at index %d", key_index);
+
+ zmk_key key = zmk_keymap[layer][key_index];
if (key == ZC_TRNS)
{
continue;
diff --git a/src/usb_hid.c b/src/usb_hid.c
index d7594a1..2862d56 100644
--- a/src/usb_hid.c
+++ b/src/usb_hid.c
@@ -14,14 +14,14 @@ static enum usb_dc_status_code usb_status;
static struct device *hid_dev;
-int zmk_usb_hid_send_report(const struct zmk_hid_report *report)
+int zmk_usb_hid_send_report(const u8_t *report, size_t len)
{
if (usb_status == USB_DC_SUSPEND)
{
return usb_wakeup_request();
}
- return hid_int_ep_write(hid_dev, (u8_t *)report, sizeof(struct zmk_hid_report), NULL);
+ return hid_int_ep_write(hid_dev, report, len, NULL);
}
void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params)