Browse Source

Modifier event, tweaks for linker script.

xmkb
Pete Johanson 4 years ago
parent
commit
96ec16da92
  1. 3
      app/CMakeLists.txt
  2. 6
      app/include/linker/zmk-events.ld
  3. 6
      app/include/zmk/events.h
  4. 12
      app/include/zmk/events/keycode-state-changed.h
  5. 22
      app/include/zmk/events/modifiers-state-changed.h
  6. 46
      app/src/behaviors/behavior_hid.c
  7. 13
      app/src/behaviors/behavior_mod_tap.c
  8. 6
      app/src/events/modifiers_state_changed.c

3
app/CMakeLists.txt

@ -33,8 +33,9 @@ target_sources(app PRIVATE src/events.c) @@ -33,8 +33,9 @@ target_sources(app PRIVATE src/events.c)
target_sources(app PRIVATE src/keymap.c)
target_sources(app PRIVATE src/hid.c)
target_sources(app PRIVATE src/event_manager.c)
target_sources(app PRIVATE src/events/keycode_state_changed.c)
target_sources(app PRIVATE src/events/position_state_changed.c)
target_sources(app PRIVATE src/events/keycode_state_changed.c)
target_sources(app PRIVATE src/events/modifiers_state_changed.c)
target_sources(app PRIVATE src/behaviors/behavior_hid.c)
target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
target_sources(app PRIVATE src/behaviors/behavior_reset.c)

6
app/include/linker/zmk-events.ld

@ -1,16 +1,10 @@ @@ -1,16 +1,10 @@
#include <linker/linker-defs.h>
SECTION_PROLOGUE(event_types,,)
{
__event_type_start = .; \
KEEP(*(".event_type")); \
__event_type_end = .; \
} GROUP_LINK_IN(ROMABLE_REGION)
SECTION_PROLOGUE(event_subscriptions,,)
{
__event_subscriptions_start = .; \
KEEP(*(".event_subscription")); \
__event_subscriptions_end = .; \
} GROUP_LINK_IN(ROMABLE_REGION)

6
app/include/zmk/events.h

@ -2,14 +2,8 @@ @@ -2,14 +2,8 @@
#include <zmk/keys.h>
int zmk_events_position_pressed(u32_t position);
int zmk_events_position_released(u32_t position);
int zmk_events_keycode_pressed(u8_t usage_page, u32_t keycode);
int zmk_events_keycode_released(u8_t usage_page, u32_t keycode);
int zmk_events_modifiers_pressed(zmk_mod_flags modifiers);
int zmk_events_modifiers_released(zmk_mod_flags modifiers);
int zmk_events_consumer_key_pressed(u32_t usage);
int zmk_events_consumer_key_released(u32_t usage);
// TODO: Encoders?
// TODO: Sensors?

12
app/include/zmk/events/keycode-state-changed.h

@ -10,4 +10,14 @@ struct keycode_state_changed { @@ -10,4 +10,14 @@ struct keycode_state_changed {
bool state;
};
ZMK_EVENT_DECLARE(keycode_state_changed);
ZMK_EVENT_DECLARE(keycode_state_changed);
inline struct keycode_state_changed* create_keycode_state_changed(u8_t usage_page, u32_t keycode, bool state)
{
struct keycode_state_changed* ev = new_keycode_state_changed();
ev->usage_page = usage_page;
ev->keycode = keycode;
ev->state = state;
return ev;
}

22
app/include/zmk/events/modifiers-state-changed.h

@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
#pragma once
#include <zephyr.h>
#include <zmk/keys.h>
#include <zmk/event-manager.h>
struct modifiers_state_changed {
struct zmk_event_header header;
zmk_mod_flags modifiers;
bool state;
};
ZMK_EVENT_DECLARE(modifiers_state_changed);
inline struct modifiers_state_changed* create_modifiers_state_changed(zmk_mod_flags modifiers, bool state)
{
struct modifiers_state_changed* ev = new_modifiers_state_changed();
ev->modifiers = modifiers;
ev->state = state;
return ev;
}

46
app/src/behaviors/behavior_hid.c

@ -15,6 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -15,6 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h>
#include <zmk/events/keycode-state-changed.h>
#include <zmk/events/modifiers-state-changed.h>
#include <zmk/hid.h>
#include <zmk/endpoints.h>
@ -56,14 +57,14 @@ static int behaviour_hid_keycode_released(u8_t usage_page, u32_t keycode) @@ -56,14 +57,14 @@ static int behaviour_hid_keycode_released(u8_t usage_page, u32_t keycode)
case USAGE_KEYPAD:
err = zmk_hid_keypad_release(keycode);
if (err) {
LOG_ERR("Unable to press keycode");
LOG_ERR("Unable to release keycode");
return err;
}
break;
case USAGE_CONSUMER:
err = zmk_hid_consumer_release(keycode);
if (err) {
LOG_ERR("Unable to press keycode");
LOG_ERR("Unable to release keycode");
return err;
}
break;
@ -71,6 +72,21 @@ static int behaviour_hid_keycode_released(u8_t usage_page, u32_t keycode) @@ -71,6 +72,21 @@ static int behaviour_hid_keycode_released(u8_t usage_page, u32_t keycode)
return zmk_endpoints_send_report(usage_page);
}
static int behavior_hid_modifiers_pressed(zmk_mod_flags modifiers)
{
LOG_DBG("modifiers %d", modifiers);
zmk_hid_register_mods(modifiers);
return zmk_endpoints_send_report(USAGE_KEYPAD);
}
static int behavior_hid_modifiers_released(zmk_mod_flags modifiers)
{
LOG_DBG("modifiers %d", modifiers);
zmk_hid_unregister_mods(modifiers);
return zmk_endpoints_send_report(USAGE_KEYPAD);
}
int behavior_hid_listener(const struct zmk_event_header *eh)
{
@ -81,37 +97,27 @@ int behavior_hid_listener(const struct zmk_event_header *eh) @@ -81,37 +97,27 @@ int behavior_hid_listener(const struct zmk_event_header *eh)
} else {
behaviour_hid_keycode_released(ev->usage_page, ev->keycode);
}
} else if (is_modifiers_state_changed(eh)) {
const struct modifiers_state_changed *ev = cast_modifiers_state_changed(eh);
if (ev->state) {
behavior_hid_modifiers_pressed(ev->modifiers);
} else {
behavior_hid_modifiers_released(ev->modifiers);
}
}
return 0;
}
ZMK_LISTENER(behavior_hid, behavior_hid_listener);
ZMK_SUBSCRIPTION(behavior_hid, keycode_state_changed);
ZMK_SUBSCRIPTION(behavior_hid, modifiers_state_changed);
static int behavior_hid_init(struct device *dev)
{
return 0;
};
static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers)
{
LOG_DBG("modifiers %d", modifiers);
zmk_hid_register_mods(modifiers);
return zmk_endpoints_send_report(USAGE_KEYPAD);
}
static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers)
{
LOG_DBG("modifiers %d", modifiers);
zmk_hid_unregister_mods(modifiers);
return zmk_endpoints_send_report(USAGE_KEYPAD);
}
static const struct behavior_driver_api behavior_hid_driver_api = {
.modifiers_pressed = on_modifiers_pressed,
.modifiers_released = on_modifiers_released
};
static const struct behavior_hid_config behavior_hid_config = {};

13
app/src/behaviors/behavior_mod_tap.c

@ -12,6 +12,7 @@ @@ -12,6 +12,7 @@
#include <zmk/event-manager.h>
#include <zmk/events/keycode-state-changed.h>
#include <zmk/events/modifiers-state-changed.h>
#include <zmk/events.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -48,21 +49,20 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t m @@ -48,21 +49,20 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t m
struct behavior_mod_tap_data *data = dev->driver_data;
LOG_DBG("mods: %d, keycode: %d", mods, keycode);
WRITE_BIT(data->pending_press_positions, position, true);
return zmk_events_modifiers_pressed(mods);
return ZMK_EVENT_RAISE(create_modifiers_state_changed(mods, true));
}
// They keycode is passed by the "keymap" based on the parameter created as part of the assignment.
static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t mods, u32_t keycode)
{
struct behavior_mod_tap_data *data = dev->driver_data;
LOG_DBG("mods: %d, keycode: %d", mods, keycode);
zmk_events_modifiers_released(mods);
ZMK_EVENT_RAISE(create_modifiers_state_changed(mods, false));
k_msleep(10); // TODO: Better approach than k_msleep to avoid USB send failures? Retries in the USB endpoint layer?
if (data->pending_press_positions & BIT(position)) {
zmk_events_keycode_pressed(USAGE_KEYPAD, keycode);
ZMK_EVENT_RAISE(create_keycode_state_changed(USAGE_KEYPAD, keycode, true));
k_msleep(10);
zmk_events_keycode_released(USAGE_KEYPAD, keycode);
ZMK_EVENT_RAISE(create_keycode_state_changed(USAGE_KEYPAD, keycode, false));
}
return 0;
@ -73,7 +73,6 @@ static const struct behavior_driver_api behavior_mod_tap_driver_api = { @@ -73,7 +73,6 @@ static const struct behavior_driver_api behavior_mod_tap_driver_api = {
.binding_released = on_keymap_binding_released,
};
static const struct behavior_mod_tap_config behavior_mod_tap_config = {};
static struct behavior_mod_tap_data behavior_mod_tap_data;

6
app/src/events/modifiers_state_changed.c

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
#include <kernel.h>
#include <zmk/events/modifiers-state-changed.h>
ZMK_EVENT_IMPL(modifiers_state_changed);
Loading…
Cancel
Save