From 5675174fc97a273393fcb328f1adad841b0e89eb Mon Sep 17 00:00:00 2001 From: Lasse Rosenow Date: Fri, 12 Apr 2024 15:06:38 +0200 Subject: [PATCH] fixup! examples: add registry_core example Add more comments and address review --- examples/registry_core/Makefile | 2 ++ examples/registry_core/README.md | 16 ++++++++++++++ examples/registry_core/main.c | 36 +++++++++++++------------------- 3 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 examples/registry_core/README.md diff --git a/examples/registry_core/Makefile b/examples/registry_core/Makefile index f8b0afe4e247..d3c76e6394c3 100644 --- a/examples/registry_core/Makefile +++ b/examples/registry_core/Makefile @@ -8,7 +8,9 @@ BOARD ?= native RIOTBASE ?= $(CURDIR)/../.. # required modules +# enable board_led schema (sys/board_led) of the riot registry USEMODULE += registry_namespace_sys_board_led +# enable the ztimer to turn the led on and off every second USEMODULE += ztimer_msec # Comment this out to disable code in RIOT that does safety checking diff --git a/examples/registry_core/README.md b/examples/registry_core/README.md new file mode 100644 index 000000000000..0317fb298292 --- /dev/null +++ b/examples/registry_core/README.md @@ -0,0 +1,16 @@ +examples/registry_core +================ + +This application demonstrates the most basic usage of the RIOT Registry. +It implements a RGB LED schema and continuously changes its value every second. + +Usage +===== + +Simply build and flash the application for your target board: + +``` +BOARD=YOUR_BOARD_NAME_HERE make flash term +``` + +Now you should see the terminal continuously print out different LED states. diff --git a/examples/registry_core/main.c b/examples/registry_core/main.c index 7fc2bcfeb9cb..bc6cb88ada8a 100644 --- a/examples/registry_core/main.c +++ b/examples/registry_core/main.c @@ -11,7 +11,8 @@ * @{ * * @file - * @brief RIOT Registry example application + * @brief RIOT registry core minimal example application to demonstrate + * how to use the riot registry without any of its extensions. * * @author Lasse Rosenow * @@ -30,7 +31,7 @@ #include "registry/namespace/sys/board_led.h" #include "ztimer.h" -int board_led_instance_0_commit_cb(const registry_commit_cb_scope_t scope, +int board_led_instance_commit_cb(const registry_commit_cb_scope_t scope, const registry_group_or_parameter_id_t *group_or_parameter_id, const void *context); @@ -41,35 +42,26 @@ registry_sys_board_led_instance_t board_led_instance_0_data = { registry_instance_t board_led_instance = { .data = &board_led_instance_0_data, - .commit_cb = &board_led_instance_0_commit_cb, + .commit_cb = &board_led_instance_commit_cb, }; -int board_led_instance_0_commit_cb(const registry_commit_cb_scope_t scope, +/* this callback is usually implemented drivers such as an RGB LED driver */ +int board_led_instance_commit_cb(const registry_commit_cb_scope_t scope, const registry_group_or_parameter_id_t *group_or_parameter_id, const void *context) { (void)scope; (void)context; - if (group_or_parameter_id != NULL) { - if (*group_or_parameter_id == REGISTRY_SYS_BOARD_LED_ENABLED) { - /* The Driver owns the board_led data instance, so we can just get our value from there */ - bool led_state = board_led_instance_0_data.enabled; - - /* Turn the LED on or off depending on the led_state */ - if (led_state == true) { - /* This is the commit_cb function of instance 0, so we toggle LED 0 as well */ - LED_ON(0); - } else { - LED_OFF(0); - } - } - } - else { - /* The whole instance got committed in one go, so apply all parameters (BOARD_LED has only one anyways)*/ - + /* Either commit all parameters of the instance or only the given parameter. + * For a single LED there is no difference as it only has one parameter. */ + if ((group_or_parameter_id == NULL) || + (*group_or_parameter_id == REGISTRY_SYS_BOARD_LED_ENABLED)) { + /* The Driver owns the board_led data instance, so we can just get our value from there */ bool led_state = board_led_instance_0_data.enabled; + /* Turn the LED on or off depending on the led_state */ if (led_state == true) { + /* This is the commit_cb function of instance 0, so we toggle LED 0 as well */ LED_ON(0); } else { LED_OFF(0); @@ -103,7 +95,7 @@ int main(void) /* Set new registry value */ registry_set(¶meter_node, &board_led_enabled, sizeof(board_led_enabled)); - /* Apply the registry value to change the LED state (calls the commit_cb function implemented by the BOARD for example)*/ + /* Apply the registry value to change the LED state (in this case calls the commit_cb function: "board_led_instance_commit_cb")*/ registry_commit(¶meter_node); /* Sleep for 1 second and then do it again*/