2020-05-27 14:48:03 +10:00
|
|
|
/*
|
2020-09-10 13:57:44 +10:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-05-27 14:48:03 +10:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct kscan_composite_child_config {
|
2020-05-27 14:48:03 +10:00
|
|
|
char *label;
|
2020-12-03 03:41:57 +11:00
|
|
|
uint8_t row_offset;
|
|
|
|
uint8_t column_offset;
|
2020-05-27 14:48:03 +10:00
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#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)},
|
2020-05-27 14:48:03 +10:00
|
|
|
|
|
|
|
const struct kscan_composite_child_config kscan_composite_children[] = {
|
|
|
|
DT_FOREACH_CHILD(MATRIX_NODE_ID, CHILD_CONFIG)};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct kscan_composite_config {};
|
2020-05-27 14:48:03 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct kscan_composite_data {
|
2020-05-27 14:48:03 +10:00
|
|
|
kscan_callback_t callback;
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *dev;
|
2020-05-27 14:48:03 +10:00
|
|
|
};
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_composite_enable_callback(const struct device *dev) {
|
2020-10-05 04:33:12 +11:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
|
2020-05-27 14:48:03 +10:00
|
|
|
const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
|
|
|
|
|
|
|
|
kscan_enable_callback(device_get_binding(cfg->label));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_composite_disable_callback(const struct device *dev) {
|
2020-10-05 04:33:12 +11:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
|
2020-05-27 14:48:03 +10:00
|
|
|
const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
|
|
|
|
|
|
|
|
kscan_disable_callback(device_get_binding(cfg->label));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static void kscan_composite_child_callback(const struct device *child_dev, uint32_t row,
|
|
|
|
uint32_t column, bool pressed) {
|
2020-05-27 14:48:03 +10:00
|
|
|
// TODO: Ideally we can get this passed into our callback!
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *dev = device_get_binding(DT_INST_LABEL(0));
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_composite_data *data = dev->data;
|
2020-05-27 14:48:03 +10:00
|
|
|
|
2020-10-05 04:33:12 +11:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
|
2020-05-27 14:48:03 +10:00
|
|
|
const struct kscan_composite_child_config *cfg = &kscan_composite_children[i];
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (device_get_binding(cfg->label) != child_dev) {
|
2020-05-27 14:48:03 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->callback(dev, row + cfg->row_offset, column + cfg->column_offset, pressed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_composite_configure(const struct device *dev, kscan_callback_t callback) {
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_composite_data *data = dev->data;
|
2020-05-27 14:48:03 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (!callback) {
|
2020-05-27 14:48:03 +10:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-10-05 04:33:12 +11:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(kscan_composite_children); i++) {
|
2020-05-27 14:48:03 +10:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int kscan_composite_init(const struct device *dev) {
|
2020-11-29 07:03:12 +11:00
|
|
|
struct kscan_composite_data *data = dev->data;
|
2020-05-27 14:48:03 +10:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-03-23 01:31:55 +11:00
|
|
|
DEVICE_DT_INST_DEFINE(0, kscan_composite_init, device_pm_control_nop, &kscan_composite_data,
|
|
|
|
&kscan_composite_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
|
|
|
|
&mock_driver_api);
|