Skip to content

Commit

Permalink
uart_comm: Handle FN reports from Keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Jan 29, 2023
1 parent f22bf7f commit 1877e07
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/boards/arm/hw75_dynamic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ HW-75 Dynamic
- [x] 旋钮力反馈 (FOC)
- [x] 墨水屏
- [x] 上位机
- [ ] 键盘联动
- [x] 键盘联动

## 烧录

Expand Down
2 changes: 2 additions & 0 deletions config/boards/arm/hw75_dynamic/app/uart_comm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ zephyr_library()
zephyr_library_link_libraries(hw75_proto)

zephyr_library_sources(uart_comm.c)

add_subdirectory(handler)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

# handler - fn-state

zephyr_library_sources(handler_fn_state.c)
zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include)
12 changes: 12 additions & 0 deletions config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2022-2023 XiNGRZ
* SPDX-License-Identifier: MIT
*/

#pragma once

#include "uart_comm.pb.h"

typedef bool (*uart_comm_handler_t)(const uart_comm_MessageK2D *k2d);

bool handle_fn_state(const uart_comm_MessageK2D *k2d);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2022-2023 XiNGRZ
* SPDX-License-Identifier: MIT
*/

#include "handler.h"

#include <zmk/keymap.h>

static uint8_t current_layer = 0;

bool handle_fn_state(const uart_comm_MessageK2D *k2d)
{
const uart_comm_FnState *report = &k2d->payload.fn_state;

if (report->pressed) {
current_layer = zmk_keymap_highest_layer_active();
zmk_keymap_layer_to(1);
} else {
zmk_keymap_layer_to(current_layer);
}

return true;
}
35 changes: 34 additions & 1 deletion config/boards/arm/hw75_dynamic/app/uart_comm/uart_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(uart_comm, CONFIG_HW75_UART_COMM_LOG_LEVEL);

#include <pb_decode.h>

#include "handler/handler.h"

#define SLIP_LABEL DT_LABEL(DT_NODELABEL(slip))

static uint8_t uart_rx_buf[CONFIG_HW75_UART_COMM_MAX_RX_MESSAGE_SIZE];
Expand All @@ -20,6 +24,35 @@ struct k_thread thread;

static const struct device *slip;

static struct {
uart_comm_Action action;
uart_comm_handler_t handler;
} handlers[] = {
{ uart_comm_Action_FN_STATE_CHANGED, handle_fn_state },
};

static void uart_comm_handle(uint32_t len)
{
LOG_HEXDUMP_DBG(uart_rx_buf, len, "RX");

pb_istream_t k2d_stream = pb_istream_from_buffer(uart_rx_buf, len);

uart_comm_MessageK2D k2d = uart_comm_MessageK2D_init_zero;
if (!pb_decode_delimited(&k2d_stream, uart_comm_MessageK2D_fields, &k2d)) {
LOG_ERR("Failed decoding k2d message: %s", k2d_stream.errmsg);
return;
}

LOG_DBG("report action: %d", k2d.action);

for (size_t i = 0; i < ARRAY_SIZE(handlers); i++) {
if (handlers[i].action == k2d.action) {
handlers[i].handler(&k2d);
break;
}
}
}

static void uart_comm_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
Expand All @@ -30,7 +63,7 @@ static void uart_comm_thread(void *p1, void *p2, void *p3)

while (1) {
uart_slip_receive(slip, uart_rx_buf, sizeof(uart_rx_buf), &len);
LOG_HEXDUMP_DBG(uart_rx_buf, len, "RX");
uart_comm_handle(len);
}
}

Expand Down

0 comments on commit 1877e07

Please sign in to comment.