2020-06-20 05:32:33 +10:00
|
|
|
/*
|
2020-09-10 13:57:44 +10:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-06-20 05:32:33 +10:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT zmk_behavior_reset
|
|
|
|
|
|
|
|
#include <device.h>
|
2021-12-12 14:39:41 +11:00
|
|
|
#include <sys/reboot.h>
|
2020-06-20 05:32:33 +10:00
|
|
|
#include <drivers/behavior.h>
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
2020-10-11 08:32:53 +11:00
|
|
|
#include <zmk/behavior.h>
|
|
|
|
|
2020-06-20 05:32:33 +10:00
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|
|
|
|
2021-01-18 17:09:51 +11:00
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
|
2020-08-21 14:33:48 +10:00
|
|
|
struct behavior_reset_config {
|
2020-09-14 12:53:24 +10:00
|
|
|
int type;
|
2020-08-21 14:33:48 +10:00
|
|
|
};
|
2020-06-20 05:32:33 +10:00
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int behavior_reset_init(const struct device *dev) { return 0; };
|
2020-06-20 05:32:33 +10:00
|
|
|
|
2020-10-11 08:32:53 +11:00
|
|
|
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
|
|
|
struct zmk_behavior_binding_event event) {
|
2020-12-11 06:31:51 +11:00
|
|
|
const struct device *dev = device_get_binding(binding->behavior_dev);
|
2020-11-29 07:03:37 +11:00
|
|
|
const struct behavior_reset_config *cfg = dev->config;
|
2020-08-21 14:33:48 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
// TODO: Correct magic code for going into DFU?
|
|
|
|
// See
|
|
|
|
// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107
|
|
|
|
sys_reboot(cfg->type);
|
2020-12-19 00:24:28 +11:00
|
|
|
return ZMK_BEHAVIOR_OPAQUE;
|
2020-06-20 05:32:33 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct behavior_driver_api behavior_reset_driver_api = {
|
2020-09-14 12:53:24 +10:00
|
|
|
.binding_pressed = on_keymap_binding_pressed,
|
2020-12-21 01:49:42 +11:00
|
|
|
.locality = BEHAVIOR_LOCALITY_EVENT_SOURCE,
|
2020-06-20 05:32:33 +10:00
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define RST_INST(n) \
|
|
|
|
static const struct behavior_reset_config behavior_reset_config_##n = { \
|
|
|
|
.type = DT_INST_PROP(n, type)}; \
|
2021-11-06 07:44:14 +11:00
|
|
|
DEVICE_DT_INST_DEFINE(n, behavior_reset_init, NULL, NULL, &behavior_reset_config_##n, \
|
|
|
|
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
|
|
|
|
&behavior_reset_driver_api);
|
2020-06-20 05:32:33 +10:00
|
|
|
|
2021-01-18 17:09:51 +11:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(RST_INST)
|
|
|
|
|
|
|
|
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
|