Skip to content

Commit

Permalink
modules: lvgl: input: add zephyr,lvgl-encoder-input device binding
Browse files Browse the repository at this point in the history
Add a pseudo device which can be used to hook into qdec events and
optionally a button and relay the input_event to lvgl.

Signed-off-by: Fabian Blatz <[email protected]>
  • Loading branch information
faxe1008 authored and carlescufi committed Sep 13, 2023
1 parent 1f51a0e commit 0943428
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
32 changes: 32 additions & 0 deletions dts/bindings/input/zephyr,lvgl-encoder-input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Fabian Blatz <[email protected]>
# SPDX-License-Identifier: Apache-2.0

description: |
LVGL encoder indev pseudo-device
Listens for button/encoder input events and routes the
lv_indev_data_t to the underlying encoder lv_indev_t managed by LVGL.
Example configuration:
encoder {
compatible = "zephyr,lvgl-encoder-input";
rotation-input-code = <INPUT_REL_Y>;
button-input-code = <INPUT_KEY_0>;
};
compatible: "zephyr,lvgl-encoder-input"

include: zephyr,lvgl-common-input.yaml

properties:
rotation-input-code:
type: int
required: true
description: |
Input event code associated with rotation (INPUT_REL_*).
button-input-code:
type: int
description: |
Input event key code for encoder button (INPUT_KEY_* or INPUT_BTN_*).
2 changes: 2 additions & 0 deletions modules/lvgl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ zephyr_library()

zephyr_include_directories(${LVGL_DIR}/src/)
zephyr_include_directories(.)
zephyr_include_directories(include)

zephyr_compile_definitions(LV_CONF_INCLUDE_SIMPLE=1)
zephyr_compile_definitions(LV_CONF_PATH=${CMAKE_CURRENT_SOURCE_DIR}/lv_conf.h)
Expand Down Expand Up @@ -227,6 +228,7 @@ zephyr_library_sources(input/lvgl_common_input.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_KSCAN input/lvgl_pointer_kscan.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_INPUT input/lvgl_pointer_input.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_BUTTON_INPUT input/lvgl_button_input.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_ENCODER_INPUT input/lvgl_encoder_input.c)

zephyr_library_link_libraries(LVGL)
target_link_libraries(LVGL INTERFACE zephyr_interface)
Expand Down
13 changes: 13 additions & 0 deletions modules/lvgl/Kconfig.input
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ config LV_Z_BUTTON_INPUT_MSGQ_COUNT
help
Size of the button message queue buffering input events.

config LV_Z_ENCODER_INPUT
bool "Input lvgl encoder"
default y
depends on INPUT
depends on DT_HAS_ZEPHYR_LVGL_ENCODER_INPUT_ENABLED

config LV_Z_ENCODER_INPUT_MSGQ_COUNT
int "Input encoder queue message count"
default 4
depends on LV_Z_ENCODER_INPUT
help
Size of the encoder message queue buffering input events.

endmenu
23 changes: 23 additions & 0 deletions modules/lvgl/include/lvgl_input_device.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 Fabian Blatz <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_MODULES_LVGL_INPUT_DEVICE_H_
#define ZEPHYR_MODULES_LVGL_INPUT_DEVICE_H_

#include <zephyr/device.h>
#include <lvgl.h>

#ifdef __cplusplus
extern "C" {
#endif

lv_indev_t *lvgl_input_get_indev(const struct device *dev);

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_MODULES_LVGL_INPUT_DEVICE_H_ */
8 changes: 8 additions & 0 deletions modules/lvgl/input/lvgl_common_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <lvgl_input_device.h>

LOG_MODULE_DECLARE(lvgl);

lv_indev_t *lvgl_input_get_indev(const struct device *dev)
{
struct lvgl_common_input_data *common_data = dev->data;

return common_data->indev;
}

static void lvgl_input_read_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
const struct device *dev = drv->user_data;
Expand Down
71 changes: 71 additions & 0 deletions modules/lvgl/input/lvgl_encoder_input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023 Fabian Blatz <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT zephyr_lvgl_encoder_input

#include "lvgl_common_input.h"

#include <zephyr/logging/log.h>

LOG_MODULE_DECLARE(lvgl);

struct lvgl_encoder_input_config {
struct lvgl_common_input_config common_config; /* Needs to be first member */
int rotation_input_code;
int button_input_code;
};

static void lvgl_encoder_process_event(const struct device *dev, struct input_event *evt)
{
struct lvgl_common_input_data *data = dev->data;
const struct lvgl_encoder_input_config *cfg = dev->config;

if (evt->code == cfg->rotation_input_code) {
data->pending_event.enc_diff = evt->value;
} else if (evt->code == cfg->button_input_code) {
data->pending_event.state = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
} else {
LOG_DBG("Ignored input event: %u", evt->code);
return;
}

if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
LOG_WRN("Could not put input data into queue");
}
}

static int lvgl_encoder_input_init(const struct device *dev)
{
return lvgl_input_register_driver(LV_INDEV_TYPE_ENCODER, dev);
}

#define BUTTON_CODE(inst) DT_INST_PROP_OR(inst, button_input_code, -1)
#define ROTATION_CODE(inst) DT_INST_PROP(inst, rotation_input_code)

#define ASSERT_PROPERTIES(inst) \
BUILD_ASSERT(IN_RANGE(ROTATION_CODE(inst), 0, 65536), \
"Property rotation-input-code needs to be between 0 and 65536."); \
BUILD_ASSERT(!DT_INST_NODE_HAS_PROP(inst, button_input_code) || \
IN_RANGE(BUTTON_CODE(inst), 0, 65536), \
"Property button-input-code needs to be between 0 and 65536."); \
BUILD_ASSERT(ROTATION_CODE(inst) != BUTTON_CODE(inst), \
"Property rotation-input-code and button-input-code should not be equal.")

#define LVGL_ENCODER_INPUT_DEFINE(inst) \
ASSERT_PROPERTIES(inst); \
LVGL_INPUT_DEFINE(inst, encoder, CONFIG_LV_Z_ENCODER_INPUT_MSGQ_COUNT, \
lvgl_encoder_process_event); \
static const struct lvgl_encoder_input_config lvgl_encoder_input_config_##inst = { \
.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, encoder), \
.rotation_input_code = ROTATION_CODE(inst), \
.button_input_code = BUTTON_CODE(inst), \
}; \
static struct lvgl_common_input_data lvgl_common_input_data_##inst; \
DEVICE_DT_INST_DEFINE(inst, lvgl_encoder_input_init, NULL, &lvgl_common_input_data_##inst, \
&lvgl_encoder_input_config_##inst, APPLICATION, \
CONFIG_LV_Z_INPUT_INIT_PRIORITY, NULL);

DT_INST_FOREACH_STATUS_OKAY(LVGL_ENCODER_INPUT_DEFINE)

0 comments on commit 0943428

Please sign in to comment.