Skip to content

Commit

Permalink
uart_comm: Report FN state to Dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Jan 29, 2023
1 parent d96b724 commit f22bf7f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
# report - ping

zephyr_library_sources(report_ping.c)

# report - fn-state

zephyr_library_sources(report_fn_state.c)
zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022-2023 XiNGRZ
* SPDX-License-Identifier: MIT
*/

#include <kernel.h>
#include <device.h>

#include <string.h>

#include <zmk/keymap.h>
#include <zmk/event_manager.h>
#include <zmk/events/layer_state_changed.h>

#include "report.h"

#define KEYMAP_NODE DT_INST(0, zmk_keymap)
#define LAYER_LABEL(node) COND_CODE_0(DT_NODE_HAS_PROP(node, label), (NULL), (DT_LABEL(node))),

static const char *layer_names[] = { DT_FOREACH_CHILD(KEYMAP_NODE, LAYER_LABEL) };

static bool fn_pressed = false;

static void report_fn_state_changed(bool pressed)
{
uart_comm_MessageK2D k2d = uart_comm_MessageK2D_init_zero;
k2d.action = uart_comm_Action_FN_STATE_CHANGED;
k2d.which_payload = uart_comm_MessageK2D_fn_state_tag;
k2d.payload.fn_state.pressed = pressed;

uart_comm_report(&k2d);
}

static int report_fn_state_event_listener(const zmk_event_t *eh)
{
if (as_zmk_layer_state_changed(eh)) {
uint8_t index = zmk_keymap_highest_layer_active();
bool current = strncmp(layer_names[index], "FN", 2) == 0;
if (!fn_pressed && current) {
report_fn_state_changed(true);
} else if (fn_pressed && !current) {
report_fn_state_changed(false);
}
fn_pressed = current;
return 0;
}

return -ENOTSUP;
}

ZMK_LISTENER(report_fn_state, report_fn_state_event_listener);
ZMK_SUBSCRIPTION(report_fn_state, zmk_layer_state_changed);
2 changes: 2 additions & 0 deletions config/hw75_keyboard.keymap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
keymap {
compatible = "zmk,keymap";
base {
label = "BASE";
bindings = <
&kp ESC &kp F1 &kp F2 &kp F3 &kp F4 &kp F5 &kp F6 &kp F7 &kp F8 &kp F9 &kp F10 &kp F11 &kp F12 &kp PAUSE_BREAK
&kp GRAVE &kp N1 &kp N2 &kp N3 &kp N4 &kp N5 &kp N6 &kp N7 &kp N8 &kp N9 &kp N0 &kp MINUS &kp EQUAL &kp BSPC &kp INSERT
Expand All @@ -26,6 +27,7 @@
};

fn {
label = "FN";
bindings = <
&trans &kp C_BRI_DEC &kp C_BRI_INC &none &none &none &none &kp C_PREV &kp C_PP &kp C_NEXT &kp C_MUTE &kp C_VOL_DN &kp C_VOL_UP &trans
&trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans
Expand Down
7 changes: 7 additions & 0 deletions config/proto/uart_comm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package uart.comm;
enum Action {
NOP = 0;
PING = 1;
FN_STATE_CHANGED = 2;
}

message MessageK2D
Expand All @@ -18,9 +19,15 @@ message MessageK2D
oneof payload
{
Nop nop = 2;
FnState fn_state = 3;
}
}

message Nop
{
}

message FnState
{
required bool pressed = 1;
}

0 comments on commit f22bf7f

Please sign in to comment.