Skip to content

Commit 5a0f76a

Browse files
committed
examples: add registry_core example
1 parent d1132b4 commit 5a0f76a

4 files changed

Lines changed: 152 additions & 0 deletions

File tree

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Here is a quick overview of the examples available in the RIOT:
144144
| [senml_saul](./advanced/senml_saul/README.md) | This example demonstrates the usage of the SAUL (Sensor Actuator Uber Layer) module with the SenML (Sensor Measurement Lists) format. |
145145
| [opendsme](./advanced/opendsme/README.md) | This example demonstrates the usage of the OpenDSME module in RIOT. |
146146
| [xipfs](./advanced/xipfs/README.md) | This example demonstrates the usage of XIPFS for creating and executing an executable file. |
147+
| [registry_core](./advanced/registry/registry_core/README.md) | This example demonstrates the most basic usage of the RIOT Registry. |
147148

148149
## Examples from Guides
149150

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# name of the application
2+
APPLICATION = registry_example_core
3+
4+
# If no BOARD is found in the environment, use this default:
5+
BOARD ?= native
6+
7+
# This has to be the absolute path to the RIOT base directory:
8+
RIOTBASE ?= $(CURDIR)/../../../..
9+
10+
# required modules
11+
# enable board_led schema (sys/board_led) of the riot registry
12+
USEMODULE += registry_namespace_sys_board_led
13+
# enable the ztimer to turn the led on and off every second
14+
USEMODULE += ztimer_msec
15+
16+
# Comment this out to disable code in RIOT that does safety checking
17+
# which is not needed in a production environment but helps in the
18+
# development process:
19+
DEVELHELP ?= 1
20+
21+
# Change this to 0 show compiler invocation lines by default:
22+
QUIET ?= 1
23+
24+
include $(RIOTBASE)/Makefile.include
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Registry Core
2+
3+
This application demonstrates the most basic usage of the RIOT Registry.
4+
It implements a RGB LED schema and continuously changes its value every second.
5+
6+
## Usage
7+
8+
Simply build and flash the application for your target board:
9+
10+
```shell
11+
BOARD=YOUR_BOARD_NAME_HERE make flash term
12+
```
13+
14+
Now you should see the terminal continuously print out different LED states.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023-2026 Lasse Rosenow <lasse.rosenow@haw-hamburg.de>
3+
* SPDX-FileCopyrightText: 2023-2026 HAW Hamburg
4+
* SPDX-License-Identifier: LGPL-2.1-only
5+
*/
6+
7+
/**
8+
* @ingroup examples
9+
* @{
10+
*
11+
* @file
12+
* @brief RIOT registry core minimal example application to demonstrate
13+
* how to use the riot registry without any of its extensions.
14+
*
15+
* @author Lasse Rosenow <lasse.rosenow@haw-hamburg.de>
16+
*
17+
* @}
18+
*/
19+
20+
#include <string.h>
21+
#include <stdio.h>
22+
#include <unistd.h>
23+
24+
#include "periph_cpu.h"
25+
#include "led.h"
26+
#include "board.h"
27+
#include "registry.h"
28+
#include "registry/namespace/sys.h"
29+
#include "registry/namespace/sys/board_led.h"
30+
#include "ztimer.h"
31+
32+
registry_error_t board_led_instance_apply_cb(
33+
const registry_apply_cb_scope_t scope,
34+
const registry_group_or_parameter_id_t *group_or_parameter_id,
35+
const void *context);
36+
37+
/* This belongs into the BOARD or Driver for example */
38+
registry_sys_board_led_instance_t board_led_instance_0_data = {
39+
.enabled = 0,
40+
};
41+
42+
registry_instance_t board_led_instance = {
43+
.data = &board_led_instance_0_data,
44+
.apply_cb = &board_led_instance_apply_cb,
45+
};
46+
47+
/* This callback gets called, when new configurations shall be applied.
48+
If interacting with configurations of drivers then this callback should be
49+
implemented by the driver itself. For custom application logic, we need to
50+
define this for ourselves. */
51+
registry_error_t board_led_instance_apply_cb(
52+
const registry_apply_cb_scope_t scope,
53+
const registry_group_or_parameter_id_t *group_or_parameter_id,
54+
const void *context)
55+
{
56+
(void)scope;
57+
(void)context;
58+
59+
/* Either apply all parameters of the instance or only the given parameter.
60+
* For a single LED there is no difference as it only has one parameter. */
61+
if ((group_or_parameter_id == NULL) ||
62+
(*group_or_parameter_id == REGISTRY_SYS_BOARD_LED_ENABLED)) {
63+
/* The Driver owns the board_led data instance, so we can just get our value from there */
64+
bool led_state = board_led_instance_0_data.enabled;
65+
/* Turn the LED on or off depending on the led_state */
66+
if (led_state == true) {
67+
/* This is the apply_cb function of instance 0, so we toggle LED 0 as well */
68+
LED_ON(0);
69+
}
70+
else {
71+
LED_OFF(0);
72+
}
73+
}
74+
75+
return 0;
76+
}
77+
78+
/* This belongs into our main application */
79+
int main(void)
80+
{
81+
registry_init();
82+
83+
/* init schemas */
84+
registry_add_schema_instance(&registry_sys_board_led, &board_led_instance);
85+
86+
bool board_led_enabled = false;
87+
88+
while (true) {
89+
/* Invert the BOARD LED, to make it turn on and off on each subsequent cycle */
90+
board_led_enabled = !board_led_enabled;
91+
92+
/* Create registry_node_t for the board_led_parameter */
93+
const registry_node_t parameter_node = {
94+
.type = REGISTRY_NODE_PARAMETER,
95+
.value.parameter = {
96+
.instance = &board_led_instance,
97+
.parameter = &registry_sys_board_led_enabled,
98+
},
99+
};
100+
101+
/* Set new registry value */
102+
registry_set(&parameter_node, &board_led_enabled, sizeof(board_led_enabled));
103+
104+
/* Apply the registry value to change the LED state */
105+
/* (in this case calls the apply_cb function: "board_led_instance_apply_cb") */
106+
registry_apply(&parameter_node);
107+
108+
/* Sleep for 1 second and then do it again*/
109+
ztimer_sleep(ZTIMER_MSEC, 1000);
110+
}
111+
112+
return 0;
113+
}

0 commit comments

Comments
 (0)