2020-07-22 16:13:52 +10:00
|
|
|
/*
|
2020-09-10 13:57:44 +10:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-07-22 16:13:52 +10:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <drivers/sensor.h>
|
|
|
|
#include <devicetree.h>
|
|
|
|
#include <init.h>
|
|
|
|
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|
|
|
|
|
|
|
#include <zmk/sensors.h>
|
2020-12-23 02:56:00 +11:00
|
|
|
#include <zmk/event_manager.h>
|
|
|
|
#include <zmk/events/sensor_event.h>
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-07-23 01:08:52 +10:00
|
|
|
#if ZMK_KEYMAP_HAS_SENSORS
|
|
|
|
|
2020-07-22 16:13:52 +10:00
|
|
|
struct sensors_data_item {
|
2020-12-03 03:41:57 +11:00
|
|
|
uint8_t sensor_number;
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *dev;
|
2020-07-22 16:13:52 +10:00
|
|
|
struct sensor_trigger trigger;
|
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define _SENSOR_ITEM(node) \
|
|
|
|
{.dev = NULL, .trigger = {.type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION}},
|
|
|
|
#define SENSOR_ITEM(idx, _) \
|
|
|
|
COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \
|
|
|
|
(_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), ())
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static struct sensors_data_item sensors[] = {UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, 0)};
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static void zmk_sensors_trigger_handler(const struct device *dev, struct sensor_trigger *trigger) {
|
2020-07-22 16:13:52 +10:00
|
|
|
int err;
|
2020-09-14 12:53:24 +10:00
|
|
|
struct sensors_data_item *item = CONTAINER_OF(trigger, struct sensors_data_item, trigger);
|
|
|
|
|
2020-07-22 16:13:52 +10:00
|
|
|
LOG_DBG("sensor %d", item->sensor_number);
|
|
|
|
|
|
|
|
err = sensor_sample_fetch(dev);
|
|
|
|
if (err) {
|
|
|
|
LOG_WRN("Failed to fetch sample from device %d", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:35:56 +11:00
|
|
|
ZMK_EVENT_RAISE(new_zmk_sensor_event((struct zmk_sensor_event){
|
|
|
|
.sensor_number = item->sensor_number, .sensor = dev, .timestamp = k_uptime_get()}));
|
2020-07-22 16:13:52 +10:00
|
|
|
}
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
static void zmk_sensors_init_item(const char *node, uint8_t i, uint8_t abs_i) {
|
2020-07-22 16:13:52 +10:00
|
|
|
LOG_DBG("Init %s at index %d with sensor_number %d", node, i, abs_i);
|
|
|
|
|
|
|
|
sensors[i].dev = device_get_binding(node);
|
|
|
|
sensors[i].sensor_number = abs_i;
|
|
|
|
|
|
|
|
if (!sensors[i].dev) {
|
2020-07-25 12:47:26 +10:00
|
|
|
LOG_WRN("Failed to find device for %s", node);
|
|
|
|
return;
|
2020-07-22 16:13:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
sensor_trigger_set(sensors[i].dev, &sensors[i].trigger, zmk_sensors_trigger_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _SENSOR_INIT(node) zmk_sensors_init_item(DT_LABEL(node), local_index++, absolute_index++);
|
2020-09-14 12:53:24 +10:00
|
|
|
#define SENSOR_INIT(idx, _i) \
|
|
|
|
COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \
|
|
|
|
(_SENSOR_INIT(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), (absolute_index++;))
|
2020-07-22 16:13:52 +10:00
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int zmk_sensors_init(const struct device *_arg) {
|
2020-07-22 16:13:52 +10:00
|
|
|
int local_index = 0;
|
|
|
|
int absolute_index = 0;
|
|
|
|
|
|
|
|
UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_INIT, 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
SYS_INIT(zmk_sensors_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|
2020-07-23 01:08:52 +10:00
|
|
|
|
|
|
|
#endif /* ZMK_KEYMAP_HAS_SENSORS */
|