2020-05-25 08:22:16 +10:00
|
|
|
/*
|
2020-09-10 13:57:44 +10:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-05-25 08:22:16 +10:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT zmk_kscan_mock
|
|
|
|
|
2020-08-29 06:21:31 +10:00
|
|
|
#include <stdlib.h>
|
2020-05-25 08:22:16 +10:00
|
|
|
#include <device.h>
|
|
|
|
#include <drivers/kscan.h>
|
|
|
|
#include <logging/log.h>
|
2020-06-20 14:11:39 +10:00
|
|
|
|
2020-05-25 08:22:16 +10:00
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|
|
|
|
2020-12-23 02:56:00 +11:00
|
|
|
#include <dt-bindings/zmk/kscan_mock.h>
|
2020-05-25 08:22:16 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct kscan_mock_data {
|
2020-05-25 08:22:16 +10:00
|
|
|
kscan_callback_t callback;
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
uint32_t event_index;
|
2020-05-25 08:22:16 +10:00
|
|
|
struct k_delayed_work work;
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *dev;
|
2020-05-25 08:22:16 +10:00
|
|
|
};
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_mock_disable_callback(const struct device *dev) {
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_mock_data *data = dev->data;
|
2020-05-25 08:22:16 +10:00
|
|
|
|
|
|
|
k_delayed_work_cancel(&data->work);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_mock_configure(const struct device *dev, kscan_callback_t callback) {
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_mock_data *data = dev->data;
|
2020-05-25 08:22:16 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (!callback) {
|
2020-05-25 08:22:16 +10:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->event_index = 0;
|
|
|
|
data->callback = callback;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define MOCK_INST_INIT(n) \
|
|
|
|
struct kscan_mock_config_##n { \
|
2020-12-03 03:41:57 +11:00
|
|
|
uint32_t events[DT_INST_PROP_LEN(n, events)]; \
|
2020-09-14 12:53:24 +10:00
|
|
|
bool exit_after; \
|
|
|
|
}; \
|
2020-12-11 06:31:51 +11:00
|
|
|
static void kscan_mock_schedule_next_event_##n(const struct device *dev) { \
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_mock_data *data = dev->data; \
|
2020-11-29 07:03:37 +11:00
|
|
|
const struct kscan_mock_config_##n *cfg = dev->config; \
|
2020-09-14 12:53:24 +10:00
|
|
|
if (data->event_index < DT_INST_PROP_LEN(n, events)) { \
|
2020-12-03 03:41:57 +11:00
|
|
|
uint32_t ev = cfg->events[data->event_index]; \
|
2020-09-14 12:53:24 +10:00
|
|
|
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); \
|
2020-11-29 07:03:37 +11:00
|
|
|
const struct kscan_mock_config_##n *cfg = data->dev->config; \
|
2020-12-03 03:41:57 +11:00
|
|
|
uint32_t ev = cfg->events[data->event_index]; \
|
2020-09-14 12:53:24 +10:00
|
|
|
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++; \
|
|
|
|
} \
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_mock_init_##n(const struct device *dev) { \
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_mock_data *data = dev->data; \
|
2020-09-14 12:53:24 +10:00
|
|
|
data->dev = dev; \
|
|
|
|
k_delayed_work_init(&data->work, kscan_mock_work_handler_##n); \
|
|
|
|
return 0; \
|
|
|
|
} \
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_mock_enable_callback_##n(const struct device *dev) { \
|
2020-09-14 12:53:24 +10:00
|
|
|
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);
|
2020-05-27 14:48:03 +10:00
|
|
|
|
2020-08-29 06:21:31 +10:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(MOCK_INST_INIT)
|