From ed5532bf94e93d25e35498cfd7a9040b0c69597b Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 24 May 2020 18:22:16 -0400 Subject: [PATCH] Add a mock kscan driver, working native_posix. * Unified logging to one module. * Mock kscan driver that simulates events. * Working native_posix board target for testing. --- CMakeLists.txt | 1 + Kconfig | 11 ++- boards/native_posix.conf | 8 ++ boards/native_posix.overlay | 47 ++++++++++++ dts/bindings/zmk,kscan-mock.yaml | 17 +++++ include/zmk/kscan-mock.h | 8 ++ include/zmk/matrix.h | 6 ++ src/endpoints.c | 14 +++- src/kscan.c | 2 +- src/kscan_mock.c | 121 +++++++++++++++++++++++++++++++ src/main.c | 3 + src/usb_hid.c | 2 +- 12 files changed, 228 insertions(+), 12 deletions(-) create mode 100644 boards/native_posix.conf create mode 100644 boards/native_posix.overlay create mode 100644 dts/bindings/zmk,kscan-mock.yaml create mode 100644 include/zmk/kscan-mock.h create mode 100644 src/kscan_mock.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bc1b289..e81b0dcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ target_sources(app PRIVATE src/kscan.c) target_sources(app PRIVATE src/keymap.c) target_sources(app PRIVATE src/hid.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) +target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c) target_sources(app PRIVATE src/usb_hid.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c) target_sources(app PRIVATE src/endpoints.c) diff --git a/Kconfig b/Kconfig index 7d06654d..d778b64e 100644 --- a/Kconfig +++ b/Kconfig @@ -21,13 +21,12 @@ config ZMK_BLE_PASSKEY_ENTRY endif -module = ZMK_KSCAN -module-str = zmk_kscan -source "subsys/logging/Kconfig.template.log_config" +config ZMK_KSCAN_MOCK_DRIVER + bool "Enable mock kscan driver to simulate key presses" + default n -module = ZMK_USB_HID -module-str = zmk_usb_hid +module = ZMK +module-str = zmk source "subsys/logging/Kconfig.template.log_config" - source "Kconfig.zephyr" diff --git a/boards/native_posix.conf b/boards/native_posix.conf new file mode 100644 index 00000000..acccd99f --- /dev/null +++ b/boards/native_posix.conf @@ -0,0 +1,8 @@ +CONFIG_ZMK_KSCAN_MOCK_DRIVER=y +CONFIG_KSCAN=n +CONFIG_KSCAN_GPIO=n +CONFIG_GPIO=n +CONFIG_ZMK_BLE=n +CONFIG_LOG=y +CONFIG_KSCAN_LOG_LEVEL_DBG=y +CONFIG_ZMK_LOG_LEVEL_DBG=y diff --git a/boards/native_posix.overlay b/boards/native_posix.overlay new file mode 100644 index 00000000..cd71191c --- /dev/null +++ b/boards/native_posix.overlay @@ -0,0 +1,47 @@ +#include +#include + +/ { + chosen { + zmk,kscan = &kscan0; + zmk,keymap = &keymap0; + }; + + kscan0: kscan { + compatible = "zmk,kscan-mock"; + label = "KSCAN_MOCK0"; + + rows = <2>; + columns = <2>; + events = ; + }; + + keymap0: keymap { + compatible = "zmk,keymap"; + label ="Default keymap"; + layers = <&default &lower &raise>; + }; + + layers { + compatible = "zmk,layers"; + + default: layer_0 { + label = "DEFAULT"; + keys = + < + KC_A KC_B + KC_C KC_D + >; + }; + + lower: layer_1 { + label = "LOWER"; + keys = ; + }; + + raise: layer_2 { + label = "RAISE"; + keys = ; + }; + }; +}; diff --git a/dts/bindings/zmk,kscan-mock.yaml b/dts/bindings/zmk,kscan-mock.yaml new file mode 100644 index 00000000..41dd5b81 --- /dev/null +++ b/dts/bindings/zmk,kscan-mock.yaml @@ -0,0 +1,17 @@ +description: | + Allows defining a mock keyboard scan driver that simulates periodic events. + +compatible: "zmk,kscan-mock" + +properties: + label: + type: string + event-period: + type: int + description: Milliseconds between each generated event + events: + type: array + rows: + type: int + columns: + type: int diff --git a/include/zmk/kscan-mock.h b/include/zmk/kscan-mock.h new file mode 100644 index 00000000..b19cafb9 --- /dev/null +++ b/include/zmk/kscan-mock.h @@ -0,0 +1,8 @@ +#pragma once + +#define ZMK_MOCK_IS_PRESS(v) (v & (0x01 << 31)) +#define ZMK_MOCK_PRESS(row, col, msec) (row + (col << 8) + (msec << 16) + (0x01 << 31)) +#define ZMK_MOCK_RELEASE(row, col, msec) (row + (col << 8) + (msec << 16)) +#define ZMK_MOCK_ROW(v) (v & 0xFF) +#define ZMK_MOCK_COL(v) ((v >> 8) & 0xFF) +#define ZMK_MOCK_MSEC(v) ((v & ~(0x01 << 31)) >> 16) diff --git a/include/zmk/matrix.h b/include/zmk/matrix.h index 83a8e2df..783c98e2 100644 --- a/include/zmk/matrix.h +++ b/include/zmk/matrix.h @@ -1,6 +1,12 @@ #pragma once #define ZMK_MATRIX_NODE_ID DT_CHOSEN(zmk_kscan) + +#if DT_NODE_HAS_PROP(ZMK_MATRIX_NODE_ID,row_gpios) #define ZMK_MATRIX_ROWS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,row_gpios) #define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,col_gpios) +#else +#define ZMK_MATRIX_ROWS DT_PROP(ZMK_MATRIX_NODE_ID,rows) +#define ZMK_MATRIX_COLS DT_PROP(ZMK_MATRIX_NODE_ID,columns) +#endif diff --git a/src/endpoints.c b/src/endpoints.c index b73e7ed9..5e9fcdfb 100644 --- a/src/endpoints.c +++ b/src/endpoints.c @@ -4,14 +4,19 @@ #include #include +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + int zmk_endpoints_init() { int err; + LOG_DBG(""); + err = zmk_usb_hid_init(); if (err) { - printk("USB HID Init Failed\n"); + LOG_ERR("USB HID Init Failed\n"); return err; } @@ -19,7 +24,7 @@ int zmk_endpoints_init() err = zmk_hog_init(); if (err) { - printk("HOG Init Failed\n"); + LOG_ERR("HOG Init Failed\n"); return err; } @@ -33,6 +38,8 @@ int zmk_endpoints_send_key_event(struct zmk_key_event key_event) struct zmk_hid_report *report; int err; + LOG_DBG("key %lld, state %d\n", key_event.key, key_event.pressed); + if (key_event.pressed) { @@ -54,8 +61,7 @@ int zmk_endpoints_send_key_event(struct zmk_key_event key_event) err = zmk_hog_send_report(report); if (err) { - printk("FAILED TO SEND OVER HOG: %d\n", err); - // LOG_DBG("HID Over GATTP Send Failed"); + LOG_ERR("FAILED TO SEND OVER HOG: %d", err); } #endif /* CONFIG_ZMK_BLE */ diff --git a/src/kscan.c b/src/kscan.c index 09ca3872..83e8ca66 100644 --- a/src/kscan.c +++ b/src/kscan.c @@ -9,7 +9,7 @@ #include #include -LOG_MODULE_REGISTER(zmk_kscan, CONFIG_ZMK_KSCAN_LOG_LEVEL); +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include diff --git a/src/kscan_mock.c b/src/kscan_mock.c new file mode 100644 index 00000000..09a01617 --- /dev/null +++ b/src/kscan_mock.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_kscan_mock + +#include +#include +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include + +#define MATRIX_NODE_ID DT_DRV_INST(0) +#define MATRIX_ROWS DT_PROP(MATRIX_NODE_ID, rows) +#define MATRIX_COLS DT_PROP(MATRIX_NODE_ID, columns) +#define MATRIX_MOCK_EVENT_COUNT DT_PROP_LEN(MATRIX_NODE_ID, events) + +struct kscan_mock_config +{ + u32_t events[MATRIX_MOCK_EVENT_COUNT]; +}; + +struct kscan_mock_data +{ + kscan_callback_t callback; + + u8_t event_index; + struct k_delayed_work work; + struct device *dev; +}; + +static void kscan_mock_schedule_next_event(struct device *dev) +{ + struct kscan_mock_data *data = dev->driver_data; + const struct kscan_mock_config *cfg = dev->config_info; + + if (data->event_index < MATRIX_MOCK_EVENT_COUNT) + { + u32_t ev = cfg->events[data->event_index]; + LOG_DBG("delaying next keypress: %d", ZMK_MOCK_MSEC(ev)); + k_delayed_work_submit(&data->work, K_MSEC(ZMK_MOCK_MSEC(ev))); + } +} + +static int kscan_mock_enable_callback(struct device *dev) +{ + struct kscan_mock_data *data = dev->driver_data; + kscan_mock_schedule_next_event(dev); + return 0; +} + +static int kscan_mock_disable_callback(struct device *dev) +{ + struct kscan_mock_data *data = dev->driver_data; + const struct kscan_mock_config *cfg = dev->config_info; + + k_delayed_work_cancel(&data->work); + return 0; +} + +static void kscan_mock_work_handler(struct k_work *work) +{ + struct kscan_mock_data *data = + CONTAINER_OF(work, struct kscan_mock_data, work); + struct kscan_mock_config *cfg = data->dev->config_info; + + u32_t ev = cfg->events[data->event_index++]; + LOG_DBG("Triggering ev %d\n", ev); + data->callback(data->dev, ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), ZMK_MOCK_IS_PRESS(ev)); + kscan_mock_schedule_next_event(data->dev); +} + +static int kscan_mock_configure(struct device *dev, kscan_callback_t callback) +{ + struct kscan_mock_data *data = dev->driver_data; + + if (!callback) + { + return -EINVAL; + } + + data->event_index = 0; + data->callback = callback; + + return 0; +} + +static int kscan_mock_init(struct device *dev) +{ + struct kscan_mock_data *data = dev->driver_data; + const struct kscan_mock_config *cfg = dev->config_info; + + printk("Init first event: %d\n", cfg->events[0]); + + int err; + + data->dev = dev; + k_delayed_work_init(&data->work, kscan_mock_work_handler); + + return 0; +} + +static const struct kscan_driver_api mock_driver_api = { + .config = kscan_mock_configure, + .enable_callback = kscan_mock_enable_callback, + .disable_callback = kscan_mock_disable_callback, +}; + +static const struct kscan_mock_config kscan_mock_config = { + .events = DT_PROP(MATRIX_NODE_ID, events)}; + +static struct kscan_mock_data kscan_mock_data; + +DEVICE_AND_API_INIT(kscan_mock, DT_INST_LABEL(0), kscan_mock_init, + &kscan_mock_data, + &kscan_mock_config, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &mock_driver_api); diff --git a/src/main.c b/src/main.c index ecfbd56a..1ced310d 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,9 @@ #include #include +#include +LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL); + #include #include #include diff --git a/src/usb_hid.c b/src/usb_hid.c index ee8ee130..d7594a12 100644 --- a/src/usb_hid.c +++ b/src/usb_hid.c @@ -8,7 +8,7 @@ #include #include -LOG_MODULE_REGISTER(zmk_usb_hid, CONFIG_ZMK_USB_HID_LOG_LEVEL); +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static enum usb_dc_status_code usb_status;