|
| 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(®istry_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 = ®istry_sys_board_led_enabled, |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + /* Set new registry value */ |
| 102 | + registry_set(¶meter_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(¶meter_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