Browse Source

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.
xmkb
Pete Johanson 5 years ago
parent
commit
ed5532bf94
  1. 1
      CMakeLists.txt
  2. 11
      Kconfig
  3. 8
      boards/native_posix.conf
  4. 47
      boards/native_posix.overlay
  5. 17
      dts/bindings/zmk,kscan-mock.yaml
  6. 8
      include/zmk/kscan-mock.h
  7. 6
      include/zmk/matrix.h
  8. 14
      src/endpoints.c
  9. 2
      src/kscan.c
  10. 121
      src/kscan_mock.c
  11. 3
      src/main.c
  12. 2
      src/usb_hid.c

1
CMakeLists.txt

@ -22,6 +22,7 @@ target_sources(app PRIVATE src/kscan.c) @@ -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)

11
Kconfig

@ -21,13 +21,12 @@ config ZMK_BLE_PASSKEY_ENTRY @@ -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"

8
boards/native_posix.conf

@ -0,0 +1,8 @@ @@ -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

@ -0,0 +1,47 @@ @@ -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

@ -0,0 +1,17 @@ @@ -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

@ -0,0 +1,8 @@ @@ -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)

6
include/zmk/matrix.h

@ -1,6 +1,12 @@ @@ -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

14
src/endpoints.c

@ -4,14 +4,19 @@ @@ -4,14 +4,19 @@
#include <zmk/usb_hid.h>
#include <zmk/hog.h>
#include <logging/log.h>
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() @@ -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) @@ -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) @@ -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 */

2
src/kscan.c

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
#include <drivers/kscan.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/handlers.h>

121
src/kscan_mock.c

@ -0,0 +1,121 @@ @@ -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);

3
src/main.c

@ -9,6 +9,9 @@ @@ -9,6 +9,9 @@
#include <devicetree.h>
#include <settings/settings.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/matrix.h>
#include <zmk/kscan.h>
#include <zmk/endpoints.h>

2
src/usb_hid.c

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
#include <zmk/hid.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;

Loading…
Cancel
Save