fix(keymap): move the toggle layer logic to keymap.c

This commit is contained in:
Cody McGinnis 2020-08-18 14:02:25 -04:00
parent ae104374c0
commit 7facb6eee8
4 changed files with 17 additions and 7 deletions

View file

@ -1,4 +1,10 @@
/ { /*
* Copyright (c) 2020 Cody McGinnis <brainwart@gmail.com>
*
* SPDX-License-Identifier: MIT
*/
/ {
behaviors { behaviors {
tog: behavior_toggle_layer { tog: behavior_toggle_layer {
compatible = "zmk,behavior-toggle-layer"; compatible = "zmk,behavior-toggle-layer";

View file

@ -3,5 +3,6 @@
bool zmk_keymap_layer_active(u8_t layer); bool zmk_keymap_layer_active(u8_t layer);
int zmk_keymap_layer_activate(u8_t layer); int zmk_keymap_layer_activate(u8_t layer);
int zmk_keymap_layer_deactivate(u8_t layer); int zmk_keymap_layer_deactivate(u8_t layer);
int zmk_keymap_layer_toggle(u8_t layer);
int zmk_keymap_position_state_changed(u32_t position, bool pressed); int zmk_keymap_position_state_changed(u32_t position, bool pressed);

View file

@ -25,12 +25,7 @@ static int behavior_tog_init(struct device *dev)
static int tog_keymap_binding_pressed(struct device *dev, u32_t position, u32_t layer, u32_t _) static int tog_keymap_binding_pressed(struct device *dev, u32_t position, u32_t layer, u32_t _)
{ {
LOG_DBG("position %d layer %d current %d", position, layer, zmk_keymap_layer_active(layer)); return zmk_keymap_layer_toggle(layer);
if (zmk_keymap_layer_active(layer))
return zmk_keymap_layer_deactivate(layer);
return zmk_keymap_layer_activate(layer);
} }
static int tog_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _) static int tog_keymap_binding_released(struct device *dev, u32_t position, u32_t layer, u32_t _)

View file

@ -91,6 +91,14 @@ int zmk_keymap_layer_deactivate(u8_t layer)
SET_LAYER_STATE(layer, false); SET_LAYER_STATE(layer, false);
}; };
int zmk_keymap_layer_toggle(u8_t layer)
{
if (zmk_keymap_layer_active(layer))
return zmk_keymap_layer_deactivate(layer);
return zmk_keymap_layer_activate(layer);
};
bool is_active_position(u32_t position, u8_t layer) bool is_active_position(u32_t position, u8_t layer)
{ {
return (zmk_keymap_layer_state & BIT(layer)) == BIT(layer) return (zmk_keymap_layer_state & BIT(layer)) == BIT(layer)