diff --git a/config/boards/arm/hw75_dynamic/README.md b/config/boards/arm/hw75_dynamic/README.md index b70637d2..c93616ce 100644 --- a/config/boards/arm/hw75_dynamic/README.md +++ b/config/boards/arm/hw75_dynamic/README.md @@ -16,7 +16,7 @@ HW-75 Dynamic - [x] 旋钮力反馈 (FOC) - [x] 墨水屏 - [x] 上位机 -- [ ] 键盘联动 +- [x] 键盘联动 ## 烧录 diff --git a/config/boards/arm/hw75_dynamic/app/uart_comm/CMakeLists.txt b/config/boards/arm/hw75_dynamic/app/uart_comm/CMakeLists.txt index d355405f..087831cd 100644 --- a/config/boards/arm/hw75_dynamic/app/uart_comm/CMakeLists.txt +++ b/config/boards/arm/hw75_dynamic/app/uart_comm/CMakeLists.txt @@ -6,3 +6,5 @@ zephyr_library() zephyr_library_link_libraries(hw75_proto) zephyr_library_sources(uart_comm.c) + +add_subdirectory(handler) diff --git a/config/boards/arm/hw75_dynamic/app/uart_comm/handler/CMakeLists.txt b/config/boards/arm/hw75_dynamic/app/uart_comm/handler/CMakeLists.txt new file mode 100644 index 00000000..219e0319 --- /dev/null +++ b/config/boards/arm/hw75_dynamic/app/uart_comm/handler/CMakeLists.txt @@ -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) diff --git a/config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler.h b/config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler.h new file mode 100644 index 00000000..ecc9bab5 --- /dev/null +++ b/config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler.h @@ -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); diff --git a/config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler_fn_state.c b/config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler_fn_state.c new file mode 100644 index 00000000..62b243ca --- /dev/null +++ b/config/boards/arm/hw75_dynamic/app/uart_comm/handler/handler_fn_state.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022-2023 XiNGRZ + * SPDX-License-Identifier: MIT + */ + +#include "handler.h" + +#include + +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; +} diff --git a/config/boards/arm/hw75_dynamic/app/uart_comm/uart_comm.c b/config/boards/arm/hw75_dynamic/app/uart_comm/uart_comm.c index cef7b51a..3c4943ab 100644 --- a/config/boards/arm/hw75_dynamic/app/uart_comm/uart_comm.c +++ b/config/boards/arm/hw75_dynamic/app/uart_comm/uart_comm.c @@ -11,6 +11,10 @@ #include LOG_MODULE_REGISTER(uart_comm, CONFIG_HW75_UART_COMM_LOG_LEVEL); +#include + +#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]; @@ -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); @@ -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); } }