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.
This commit is contained in:
parent
9d5de4e922
commit
ed5532bf94
12 changed files with 228 additions and 12 deletions
|
@ -22,6 +22,7 @@ target_sources(app PRIVATE src/kscan.c)
|
||||||
target_sources(app PRIVATE src/keymap.c)
|
target_sources(app PRIVATE src/keymap.c)
|
||||||
target_sources(app PRIVATE src/hid.c)
|
target_sources(app PRIVATE src/hid.c)
|
||||||
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.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(app PRIVATE src/usb_hid.c)
|
||||||
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c)
|
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c)
|
||||||
target_sources(app PRIVATE src/endpoints.c)
|
target_sources(app PRIVATE src/endpoints.c)
|
||||||
|
|
11
Kconfig
11
Kconfig
|
@ -21,13 +21,12 @@ config ZMK_BLE_PASSKEY_ENTRY
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
module = ZMK_KSCAN
|
config ZMK_KSCAN_MOCK_DRIVER
|
||||||
module-str = zmk_kscan
|
bool "Enable mock kscan driver to simulate key presses"
|
||||||
source "subsys/logging/Kconfig.template.log_config"
|
default n
|
||||||
|
|
||||||
module = ZMK_USB_HID
|
module = ZMK
|
||||||
module-str = zmk_usb_hid
|
module-str = zmk
|
||||||
source "subsys/logging/Kconfig.template.log_config"
|
source "subsys/logging/Kconfig.template.log_config"
|
||||||
|
|
||||||
|
|
||||||
source "Kconfig.zephyr"
|
source "Kconfig.zephyr"
|
||||||
|
|
8
boards/native_posix.conf
Normal file
8
boards/native_posix.conf
Normal file
|
@ -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
|
47
boards/native_posix.overlay
Normal file
47
boards/native_posix.overlay
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#include <dt-bindings/zmk/keys.h>
|
||||||
|
#include <zmk/kscan-mock.h>
|
||||||
|
|
||||||
|
/ {
|
||||||
|
chosen {
|
||||||
|
zmk,kscan = &kscan0;
|
||||||
|
zmk,keymap = &keymap0;
|
||||||
|
};
|
||||||
|
|
||||||
|
kscan0: kscan {
|
||||||
|
compatible = "zmk,kscan-mock";
|
||||||
|
label = "KSCAN_MOCK0";
|
||||||
|
|
||||||
|
rows = <2>;
|
||||||
|
columns = <2>;
|
||||||
|
events = <ZMK_MOCK_PRESS(0,0,800) ZMK_MOCK_RELEASE(0,0,800)>;
|
||||||
|
};
|
||||||
|
|
||||||
|
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 = <KC_D KC_C ZC_TRNS ZC_TRNS>;
|
||||||
|
};
|
||||||
|
|
||||||
|
raise: layer_2 {
|
||||||
|
label = "RAISE";
|
||||||
|
keys = <KC_C KC_D ZC_TRNS ZC_TRNS>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
17
dts/bindings/zmk,kscan-mock.yaml
Normal file
17
dts/bindings/zmk,kscan-mock.yaml
Normal file
|
@ -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
|
8
include/zmk/kscan-mock.h
Normal file
8
include/zmk/kscan-mock.h
Normal file
|
@ -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)
|
|
@ -1,6 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define ZMK_MATRIX_NODE_ID DT_CHOSEN(zmk_kscan)
|
#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_ROWS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,row_gpios)
|
||||||
#define ZMK_MATRIX_COLS DT_PROP_LEN(ZMK_MATRIX_NODE_ID,col_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
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,19 @@
|
||||||
#include <zmk/usb_hid.h>
|
#include <zmk/usb_hid.h>
|
||||||
#include <zmk/hog.h>
|
#include <zmk/hog.h>
|
||||||
|
|
||||||
|
#include <logging/log.h>
|
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
int zmk_endpoints_init()
|
int zmk_endpoints_init()
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
LOG_DBG("");
|
||||||
|
|
||||||
err = zmk_usb_hid_init();
|
err = zmk_usb_hid_init();
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
printk("USB HID Init Failed\n");
|
LOG_ERR("USB HID Init Failed\n");
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +24,7 @@ int zmk_endpoints_init()
|
||||||
err = zmk_hog_init();
|
err = zmk_hog_init();
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
printk("HOG Init Failed\n");
|
LOG_ERR("HOG Init Failed\n");
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +38,8 @@ int zmk_endpoints_send_key_event(struct zmk_key_event key_event)
|
||||||
struct zmk_hid_report *report;
|
struct zmk_hid_report *report;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
LOG_DBG("key %lld, state %d\n", key_event.key, key_event.pressed);
|
||||||
|
|
||||||
|
|
||||||
if (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);
|
err = zmk_hog_send_report(report);
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
printk("FAILED TO SEND OVER HOG: %d\n", err);
|
LOG_ERR("FAILED TO SEND OVER HOG: %d", err);
|
||||||
// LOG_DBG("HID Over GATTP Send Failed");
|
|
||||||
}
|
}
|
||||||
#endif /* CONFIG_ZMK_BLE */
|
#endif /* CONFIG_ZMK_BLE */
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <drivers/kscan.h>
|
#include <drivers/kscan.h>
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(zmk_kscan, CONFIG_ZMK_KSCAN_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
#include <zmk/keymap.h>
|
#include <zmk/keymap.h>
|
||||||
#include <zmk/handlers.h>
|
#include <zmk/handlers.h>
|
||||||
|
|
121
src/kscan_mock.c
Normal file
121
src/kscan_mock.c
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DT_DRV_COMPAT zmk_kscan_mock
|
||||||
|
|
||||||
|
#include <device.h>
|
||||||
|
#include <drivers/kscan.h>
|
||||||
|
#include <logging/log.h>
|
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
#include <zmk/kscan-mock.h>
|
||||||
|
|
||||||
|
#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);
|
|
@ -9,6 +9,9 @@
|
||||||
#include <devicetree.h>
|
#include <devicetree.h>
|
||||||
#include <settings/settings.h>
|
#include <settings/settings.h>
|
||||||
|
|
||||||
|
#include <logging/log.h>
|
||||||
|
LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
#include <zmk/matrix.h>
|
#include <zmk/matrix.h>
|
||||||
#include <zmk/kscan.h>
|
#include <zmk/kscan.h>
|
||||||
#include <zmk/endpoints.h>
|
#include <zmk/endpoints.h>
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include <zmk/hid.h>
|
#include <zmk/hid.h>
|
||||||
#include <zmk/keymap.h>
|
#include <zmk/keymap.h>
|
||||||
|
|
||||||
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;
|
static enum usb_dc_status_code usb_status;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue