refactor(encoder): use behavior queue to archieve key taps for encoder
Remove k_msleep() call used in sensor trigger callback that's meant to simulate a key tap (press and release), instead we can use the existing behavior queue mechanism, where a press and a release events are queued up separately. The trick is to add .binging_pressed and .binding_released callbacks for the sensor driver api, which will gets called when the queued events are processed accordingly.
This commit is contained in:
parent
c065d451cb
commit
fd995f8058
2 changed files with 45 additions and 12 deletions
|
@ -51,7 +51,7 @@ static void behavior_queue_process_next(struct k_work *work) {
|
|||
|
||||
int zmk_behavior_queue_add(uint32_t position, const struct zmk_behavior_binding binding, bool press,
|
||||
uint32_t wait) {
|
||||
struct q_item item = {.press = press, .binding = binding, .wait = wait};
|
||||
struct q_item item = {.position = position, .press = press, .binding = binding, .wait = wait};
|
||||
|
||||
const int ret = k_msgq_put(&zmk_behavior_queue_msgq, &item, K_NO_WAIT);
|
||||
if (ret < 0) {
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <logging/log.h>
|
||||
|
||||
#include <drivers/sensor.h>
|
||||
#include <zmk/behavior_queue.h>
|
||||
#include <zmk/event_manager.h>
|
||||
#include <zmk/events/keycode_state_changed.h>
|
||||
|
||||
|
@ -20,11 +21,47 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|||
|
||||
static int behavior_sensor_rotate_key_press_init(const struct device *dev) { return 0; };
|
||||
|
||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||
struct zmk_behavior_binding_event event) {
|
||||
uint32_t keycode;
|
||||
|
||||
switch (event.position) {
|
||||
case 1:
|
||||
keycode = binding->param1;
|
||||
break;
|
||||
case 2:
|
||||
keycode = binding->param2;
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, true, event.timestamp));
|
||||
}
|
||||
|
||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
||||
struct zmk_behavior_binding_event event) {
|
||||
uint32_t keycode;
|
||||
|
||||
switch (event.position) {
|
||||
case 1:
|
||||
keycode = binding->param1;
|
||||
break;
|
||||
case 2:
|
||||
keycode = binding->param2;
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, false, event.timestamp));
|
||||
}
|
||||
|
||||
static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
|
||||
const struct device *sensor, int64_t timestamp) {
|
||||
struct sensor_value value;
|
||||
int err;
|
||||
uint32_t keycode;
|
||||
uint32_t keycode_position;
|
||||
LOG_DBG("inc keycode 0x%02X dec keycode 0x%02X", binding->param1, binding->param2);
|
||||
|
||||
err = sensor_channel_get(sensor, SENSOR_CHAN_ROTATION, &value);
|
||||
|
@ -36,26 +73,22 @@ static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
|
|||
|
||||
switch (value.val1) {
|
||||
case 1:
|
||||
keycode = binding->param1;
|
||||
keycode_position = 1;
|
||||
break;
|
||||
case -1:
|
||||
keycode = binding->param2;
|
||||
keycode_position = 2;
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
LOG_DBG("SEND %d", keycode);
|
||||
|
||||
ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, true, timestamp));
|
||||
|
||||
// TODO: Better way to do this?
|
||||
k_msleep(5);
|
||||
|
||||
return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, false, timestamp));
|
||||
zmk_behavior_queue_add(keycode_position, *binding, true, 20);
|
||||
return zmk_behavior_queue_add(keycode_position, *binding, false, 20);
|
||||
}
|
||||
|
||||
static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_api = {
|
||||
.binding_pressed = on_keymap_binding_pressed,
|
||||
.binding_released = on_keymap_binding_released,
|
||||
.sensor_binding_triggered = on_sensor_binding_triggered};
|
||||
|
||||
#define KP_INST(n) \
|
||||
|
|
Loading…
Add table
Reference in a new issue