Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions include/zephyr/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
extern "C" {
#endif


/**
* @brief Input event structure.
*
Expand All @@ -33,8 +34,12 @@ extern "C" {
struct input_event {
/** Device generating the event or NULL. */
const struct device *dev;
/** Event type (see @ref INPUT_EV_CODES). */
uint16_t type;
struct {
/** Event sync. */
uint16_t sync: 1;
/** Event type (see @ref INPUT_EV_CODES). */
uint16_t type: 15;
};
/**
* Event code (see @ref INPUT_KEY_CODES, @ref INPUT_BTN_CODES,
* @ref INPUT_ABS_CODES, @ref INPUT_REL_CODES, @ref INPUT_MSC_CODES).
Expand Down Expand Up @@ -113,14 +118,16 @@ static inline int input_report_abs(const struct device *dev,
*/
bool input_queue_empty(void);

#ifndef CONFIG_INPUT_ZBUS

/**
* @brief Input listener callback structure.
*/
struct input_listener {
/** `struct device` pointer or NULL. */
const struct device *dev;
/** The callback function. */
void (*callback)(struct input_event *evt, bool sync);
void (*callback)(struct input_event *evt);
};

/**
Expand All @@ -140,6 +147,8 @@ struct input_listener {
.callback = _callback, \
}

#endif /* CONFIG_INPUT_ZBUS */

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions samples/subsys/input/input_dump/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include <zephyr/input/input.h>
#include <zephyr/sys/printk.h>

static void input_cb(struct input_event *evt, bool sync)
static void input_cb(struct input_event *evt)
{
printk("input event: dev=%-16s %3s type=%2x code=%3d value=%d\n",
evt->dev->name,
sync ? "SYN" : "",
evt->sync ? "SYN" : "",
evt->type,
evt->code,
evt->value);
Expand Down
7 changes: 7 additions & 0 deletions samples/subsys/input/input_dump_zbus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(input_dump_zbus)

target_sources(app PRIVATE src/main.c)
27 changes: 27 additions & 0 deletions samples/subsys/input/input_dump_zbus/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.. _input-dump-zbus-sample:

Input Dump Zbus
###############

Overview
********

The Input Dump Zbus sample prints any input event using the :ref:`input` APIs.

Requirements
************

The samples works on any board with an input driver defined in the board devicetree.

Building and Running
********************

Build and flash as follows, changing ``nrf52dk_nrf52832`` for your board:

.. zephyr-app-commands::
:zephyr-app: samples/subsys/input/input_dump
:board: nrf52dk_nrf52832
:goals: build flash
:compact:

After starting, the sample will print any input event in the console.
3 changes: 3 additions & 0 deletions samples/subsys/input/input_dump_zbus/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_INPUT=y
CONFIG_INPUT_ZBUS=y
CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE=1
6 changes: 6 additions & 0 deletions samples/subsys/input/input_dump_zbus/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sample:
name: Input Dump Zbus
tests:
sample.input.input_dump_zbus:
tags: input
build_only: true
28 changes: 28 additions & 0 deletions samples/subsys/input/input_dump_zbus/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022 Rodrigo Peixoto <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/input/input.h>
#include <zephyr/sys/printk.h>
#include <zephyr/zbus/zbus.h>

ZBUS_CHAN_DECLARE(sys_chan_input);

void input_listener(const struct zbus_channel *chan)
{
const struct input_event *evt = zbus_chan_const_msg(chan);

printk("input event: dev=%-16s %3s type=%2x code=%3d value=%d\n", evt->dev->name,
evt->sync ? "SYN" : "", evt->type, evt->code, evt->value);
}

ZBUS_LISTENER_DEFINE(input_sample_lis, input_listener);

void main(void)
{
zbus_chan_add_obs(&sys_chan_input, &input_sample_lis, K_FOREVER);

printk("Input zbus sample started\n");
}
7 changes: 7 additions & 0 deletions subsys/input/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ config INPUT_INIT_PRIORITY
help
Input subsystem and drivers initialization priority.

config INPUT_ZBUS
bool "Input subsystem uses zbus to distribute events"
select ZBUS
default n
help
Input subsystem uses zbus to distribute events.

choice INPUT_MODE
prompt "Input event processing mode"
default INPUT_MODE_THREAD
Expand Down
45 changes: 31 additions & 14 deletions subsys/input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(input, CONFIG_INPUT_LOG_LEVEL);
#ifdef CONFIG_INPUT_ZBUS

#define INPUT_TYPE_MASK 0x7fff
#include <zephyr/zbus/zbus.h>

#define INPUT_TYPE(t) (t & INPUT_TYPE_MASK)
#endif /* CONFIG_INPUT_ZBUS */

LOG_MODULE_REGISTER(input, CONFIG_INPUT_LOG_LEVEL);

#define INPUT_SYN_IS_SET(v) (v & INPUT_EV_SYN)
#define INPUT_TYPE_MASK 0x7fff

#ifdef CONFIG_INPUT_MODE_THREAD

Expand All @@ -23,19 +25,37 @@ K_MSGQ_DEFINE(input_msgq, sizeof(struct input_event),

#endif

static void input_process(struct input_event *evt)

#if CONFIG_INPUT_ZBUS

ZBUS_CHAN_DEFINE(sys_chan_input,
struct input_event,

NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(0));

static void input_process(struct input_event *evt, k_timeout_t timeout)
{
bool sync = INPUT_SYN_IS_SET(evt->type);
zbus_chan_pub(&sys_chan_input, evt, timeout);
}

evt->type = INPUT_TYPE(evt->type);
#else /* CONFIG_INPUT_ZBUS */

static void input_process(struct input_event *evt, k_timeout_t timeout)
{
ARG_UNUSED(timeout);

STRUCT_SECTION_FOREACH(input_listener, listener) {
if (listener->dev == NULL || listener->dev == evt->dev) {
listener->callback(evt, sync);
listener->callback(evt);
}
}
}

#endif /* CONFIG_INPUT_ZBUS */

bool input_queue_empty(void)
{
#ifdef CONFIG_INPUT_MODE_THREAD
Expand All @@ -52,6 +72,7 @@ int input_report(const struct device *dev,
{
struct input_event evt = {
.dev = dev,
.sync = (uint16_t) sync,
.type = type,
.code = code,
.value = value,
Expand All @@ -61,14 +82,10 @@ int input_report(const struct device *dev,
return -EINVAL;
}

if (sync) {
evt.type |= INPUT_EV_SYN;
}

#ifdef CONFIG_INPUT_MODE_THREAD
return k_msgq_put(&input_msgq, &evt, timeout);
#else
input_process(&evt);
input_process(&evt, timeout);
return 0;
#endif
}
Expand All @@ -87,7 +104,7 @@ static void input_thread(void)
continue;
}

input_process(&evt);
input_process(&evt, K_MSEC(250));
}
}

Expand Down
Loading