-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
caf: modules: added shell module to common application framework
Added a shell module to the Common Application Framework, and added a custom command to that shell to simulate button press event. Signed-off-by: Tony Le <tony.le@nordicsemi.no>
- Loading branch information
1 parent
c2b7e6b
commit a5cb384
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. _caf_shell: | ||
|
||
CAF: Shell module | ||
################# | ||
|
||
.. contents:: | ||
:local: | ||
:depth: 2 | ||
|
||
The shell module of the :ref:`lib_caf` (CAF) allows interactions with CAF modules and events using a predefined set of Zephyr's :ref:`zephyr:shell_api` commands. | ||
The module introduces a shell command for submitting events informing about button press or release (:c:struct:`button_event`). | ||
Submitting :c:struct:`button_event` related to the hardware button presses can be handled by the :ref:`caf_buttons`. | ||
|
||
Configuration | ||
************* | ||
|
||
To use the module, enable the following Kconfig options: | ||
|
||
* :kconfig:option:`CONFIG_CAF_SHELL` - This option is mandatory as it enables this module. | ||
* :kconfig:option:`CONFIG_SHELL` - This option enables Zephyr shell that is used by the module to receive commands from user. | ||
* :kconfig:option:`CONFIG_CAF` - This option enables the Common Application Framework. | ||
|
||
This module selects the Kconfig option :kconfig:option:`CONFIG_CAF_BUTTON_EVENTS`. | ||
|
||
Usage | ||
***** | ||
|
||
All shell commands of this module are subcommands of the :command:`caf_events` command. | ||
To send a :c:struct:`button_event`, use the following syntax: | ||
|
||
.. code-block:: console | ||
caf_events button_event [button_id] [pressed] | ||
The :command:`button_event` subcommand has two required arguments: | ||
|
||
* ``button_id`` - A decimal number represents the ID of a button. | ||
* ``pressed`` - Button press state, can only be either ``y`` or ``n``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# Copyright (c) 2023 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
|
||
menuconfig CAF_SHELL | ||
bool "Shell interface for triggering CAF events" | ||
depends on SHELL | ||
select CAF_BUTTON_EVENTS | ||
|
||
if CAF_SHELL | ||
|
||
module = CAF_SHELL | ||
module-str = caf shell | ||
source "subsys/logging/Kconfig.template.log_config" | ||
|
||
endif # CAF_SHELL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <zephyr/shell/shell.h> | ||
#include <caf/events/button_event.h> | ||
|
||
static const char button_event_cmd_help_str[] = | ||
"Submit a button_event with user-defined key ID and pressed state\n" | ||
" Key ID Decimal numeric ID of the button\n" | ||
" Pressed state 'y' or 'n'\n"; | ||
|
||
static int button_event_handler(const struct shell *shell, size_t argc, char **argv) | ||
{ | ||
/* The string part of button_id param, for invalidation check */ | ||
char *str_part; | ||
|
||
/* Convert button_id from string to decimal number */ | ||
long button_id = strtol(argv[1], &str_part, 10); | ||
|
||
if (strlen(str_part) > 0 || button_id < 0 || button_id > UINT16_MAX) { | ||
shell_error(shell, "Invalid key ID"); | ||
return -EINVAL; | ||
} | ||
|
||
if ((strlen(argv[2]) > 1) || (*argv[2] != 'y' && *argv[2] != 'n')) { | ||
shell_error(shell, "Invalid button press state"); | ||
return -EINVAL; | ||
} | ||
|
||
struct button_event *event = new_button_event(); | ||
|
||
event->key_id = (uint16_t)button_id; | ||
event->pressed = *argv[2] != 'n'; | ||
APP_EVENT_SUBMIT(event); | ||
|
||
return 0; | ||
} | ||
|
||
SHELL_STATIC_SUBCMD_SET_CREATE(sub_caf_shell_command, | ||
SHELL_CMD_ARG(button_event, NULL, button_event_cmd_help_str, button_event_handler, 3, 0), | ||
SHELL_SUBCMD_SET_END | ||
); | ||
|
||
SHELL_CMD_REGISTER(caf_events, &sub_caf_shell_command, | ||
"Submit a CAF event with user-defined parameters", NULL); |