summaryrefslogtreecommitdiff
path: root/app/drivers
diff options
context:
space:
mode:
authorPete Johanson <peter@peterjohanson.com>2020-12-08 22:06:56 -0500
committerPete Johanson <peter@peterjohanson.com>2020-12-08 22:20:12 -0500
commite6a27b15c1d8e1a14549e01af770fa9273a4f23a (patch)
treecea3fa5a0ad5396610c7f34bfc06b10a0035ec23 /app/drivers
parent02673e2f1d0b6c2ab349b14cf0ab3fcf9b82df51 (diff)
fix(kscan): Fix building kscan for tests.
Diffstat (limited to 'app/drivers')
-rw-r--r--app/drivers/kscan/CMakeLists.txt7
-rw-r--r--app/drivers/kscan/Kconfig2
-rw-r--r--app/drivers/kscan/kscan_composite.c113
-rw-r--r--app/drivers/kscan/kscan_mock.c95
4 files changed, 214 insertions, 3 deletions
diff --git a/app/drivers/kscan/CMakeLists.txt b/app/drivers/kscan/CMakeLists.txt
index 19933aa..b5f86ab 100644
--- a/app/drivers/kscan/CMakeLists.txt
+++ b/app/drivers/kscan/CMakeLists.txt
@@ -1,8 +1,11 @@
# Copyright (c) 2020 The ZMK Contributors
# SPDX-License-Identifier: MIT
-zephyr_library()
+zephyr_library_named(zmk__drivers__kscan)
+zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_matrix.c)
zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_direct.c)
-zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_demux.c) \ No newline at end of file
+zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_GPIO_DRIVER kscan_gpio_demux.c)
+zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER kscan_mock.c)
+zephyr_library_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER kscan_composite.c)
diff --git a/app/drivers/kscan/Kconfig b/app/drivers/kscan/Kconfig
index 5b913f3..654e0ee 100644
--- a/app/drivers/kscan/Kconfig
+++ b/app/drivers/kscan/Kconfig
@@ -22,4 +22,4 @@ config ZMK_KSCAN_INIT_PRIORITY
int "Keyboard scan driver init priority"
default 40
help
- Keyboard scan device driver initialization priority. \ No newline at end of file
+ Keyboard scan device driver initialization priority.
diff --git a/app/drivers/kscan/kscan_composite.c b/app/drivers/kscan/kscan_composite.c
new file mode 100644
index 0000000..f8e8d60
--- /dev/null
+++ b/app/drivers/kscan/kscan_composite.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#define DT_DRV_COMPAT zmk_kscan_composite
+
+#include <device.h>
+#include <drivers/kscan.h>
+#include <logging/log.h>
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+
+#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)
+
+struct kscan_composite_child_config {
+ char *label;
+ u8_t row_offset;
+ u8_t column_offset;
+};
+
+#define CHILD_CONFIG(inst) \
+ {.label = DT_LABEL(DT_PHANDLE(inst, kscan)), \
+ .row_offset = DT_PROP(inst, row_offset), \
+ .column_offset = DT_PROP(inst, column_offset)},
+
+const struct kscan_composite_child_config kscan_composite_children[] = {
+ DT_FOREACH_CHILD(MATRIX_NODE_ID, CHILD_CONFIG)};
+
+struct kscan_composite_config {};
+
+struct kscan_composite_data {
+ kscan_callback_t callback;
+
+ struct device *dev;
+};
+
+static int kscan_composite_enable_callback(struct device *dev) {
+ for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
+ const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
+
+ kscan_enable_callback(device_get_binding(cfg->label));
+ }
+ return 0;
+}
+
+static int kscan_composite_disable_callback(struct device *dev) {
+ for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
+ const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
+
+ kscan_disable_callback(device_get_binding(cfg->label));
+ }
+ return 0;
+}
+
+static void kscan_composite_child_callback(struct device *child_dev, u32_t row, u32_t column,
+ bool pressed) {
+ // TODO: Ideally we can get this passed into our callback!
+ struct device *dev = device_get_binding(DT_INST_LABEL(0));
+ struct kscan_composite_data *data = dev->driver_data;
+
+ for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
+ const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
+
+ if (device_get_binding(cfg->label) != child_dev) {
+ continue;
+ }
+
+ data->callback(dev, row + cfg->row_offset, column + cfg->column_offset, pressed);
+ }
+}
+
+static int kscan_composite_configure(struct device *dev, kscan_callback_t callback) {
+ struct kscan_composite_data *data = dev->driver_data;
+
+ if (!callback) {
+ return -EINVAL;
+ }
+
+ for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
+ const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
+
+ kscan_config(device_get_binding(cfg->label), &kscan_composite_child_callback);
+ }
+
+ data->callback = callback;
+
+ return 0;
+}
+
+static int kscan_composite_init(struct device *dev) {
+ struct kscan_composite_data *data = dev->driver_data;
+
+ data->dev = dev;
+
+ return 0;
+}
+
+static const struct kscan_driver_api mock_driver_api = {
+ .config = kscan_composite_configure,
+ .enable_callback = kscan_composite_enable_callback,
+ .disable_callback = kscan_composite_disable_callback,
+};
+
+static const struct kscan_composite_config kscan_composite_config = {};
+
+static struct kscan_composite_data kscan_composite_data;
+
+DEVICE_AND_API_INIT(kscan_composite, DT_INST_LABEL(0), kscan_composite_init, &kscan_composite_data,
+ &kscan_composite_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
+ &mock_driver_api);
diff --git a/app/drivers/kscan/kscan_mock.c b/app/drivers/kscan/kscan_mock.c
new file mode 100644
index 0000000..f1614f1
--- /dev/null
+++ b/app/drivers/kscan/kscan_mock.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#define DT_DRV_COMPAT zmk_kscan_mock
+
+#include <stdlib.h>
+#include <device.h>
+#include <drivers/kscan.h>
+#include <logging/log.h>
+
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+
+#include <dt-bindings/zmk/kscan-mock.h>
+
+struct kscan_mock_data {
+ kscan_callback_t callback;
+
+ u32_t event_index;
+ struct k_delayed_work work;
+ struct device *dev;
+};
+
+static int kscan_mock_disable_callback(struct device *dev) {
+ struct kscan_mock_data *data = dev->driver_data;
+
+ k_delayed_work_cancel(&data->work);
+ return 0;
+}
+
+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;
+}
+
+#define MOCK_INST_INIT(n) \
+ struct kscan_mock_config_##n { \
+ u32_t events[DT_INST_PROP_LEN(n, events)]; \
+ bool exit_after; \
+ }; \
+ static void kscan_mock_schedule_next_event_##n(struct device *dev) { \
+ struct kscan_mock_data *data = dev->driver_data; \
+ const struct kscan_mock_config_##n *cfg = dev->config_info; \
+ if (data->event_index < DT_INST_PROP_LEN(n, events)) { \
+ 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))); \
+ } else if (cfg->exit_after) { \
+ LOG_DBG("Exiting"); \
+ exit(0); \
+ } \
+ } \
+ static void kscan_mock_work_handler_##n(struct k_work *work) { \
+ struct kscan_mock_data *data = CONTAINER_OF(work, struct kscan_mock_data, work); \
+ const struct kscan_mock_config_##n *cfg = data->dev->config_info; \
+ u32_t ev = cfg->events[data->event_index]; \
+ LOG_DBG("ev %u row %d column %d state %d\n", ev, ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), \
+ ZMK_MOCK_IS_PRESS(ev)); \
+ data->callback(data->dev, ZMK_MOCK_ROW(ev), ZMK_MOCK_COL(ev), ZMK_MOCK_IS_PRESS(ev)); \
+ kscan_mock_schedule_next_event_##n(data->dev); \
+ data->event_index++; \
+ } \
+ static int kscan_mock_init_##n(struct device *dev) { \
+ struct kscan_mock_data *data = dev->driver_data; \
+ data->dev = dev; \
+ k_delayed_work_init(&data->work, kscan_mock_work_handler_##n); \
+ return 0; \
+ } \
+ static int kscan_mock_enable_callback_##n(struct device *dev) { \
+ kscan_mock_schedule_next_event_##n(dev); \
+ return 0; \
+ } \
+ static const struct kscan_driver_api mock_driver_api_##n = { \
+ .config = kscan_mock_configure, \
+ .enable_callback = kscan_mock_enable_callback_##n, \
+ .disable_callback = kscan_mock_disable_callback, \
+ }; \
+ static struct kscan_mock_data kscan_mock_data_##n; \
+ static const struct kscan_mock_config_##n kscan_mock_config_##n = { \
+ .events = DT_INST_PROP(n, events), .exit_after = DT_INST_PROP(n, exit_after)}; \
+ DEVICE_AND_API_INIT(kscan_mock_##n, DT_INST_LABEL(n), kscan_mock_init_##n, \
+ &kscan_mock_data_##n, &kscan_mock_config_##n, APPLICATION, \
+ CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &mock_driver_api_##n);
+
+DT_INST_FOREACH_STATUS_OKAY(MOCK_INST_INIT)