Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@
/samples/drivers/ht16k33/ @henrikbrixandersen
/samples/drivers/lora/ @Mani-Sadhasivam
/samples/subsys/lorawan/ @Mani-Sadhasivam
/samples/subsys/event_manager/ @nordic-krch
/samples/modules/canopennode/ @henrikbrixandersen
/samples/net/ @rlubos @tbursztyka @pfalcon
/samples/net/cloud/tagoio_http_post/ @nandojve
Expand Down
2 changes: 1 addition & 1 deletion boards/riscv/tlsr9518adk80d/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ The following example projects are supported:
- samples/hello_world
- samples/synchronization
- samples/philosophers
- samples/event_manager
- samples/application_development/event_manager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we need to have another move, this is being introduced as a subsystem, so it needs to end up in samples/subsys/event_manager. Also, I am not sure why you have it in this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it can be moved there.

- samples/basic/threads
- samples/basic/blinky
- samples/basic/blinky_pwm
Expand Down
31 changes: 31 additions & 0 deletions samples/subsys/event_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Copyright (c) 2021 Nordic Semiconductor
#
# SPDX-License-Identifier: Apache-2.0
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project("Event Manager sample")

# Include application event headers
zephyr_library_include_directories(src/events)

# Application sources
# NORDIC SDK APP START
target_sources(app PRIVATE src/main.c)

target_sources(app PRIVATE
src/events/ack_event.c
src/events/config_event.c
src/events/control_event.c
src/events/measurement_event.c
)

target_sources(app PRIVATE
src/modules/controller.c
src/modules/sensor_simulated.c
src/modules/stats.c
)
# NORDIC SDK APP END
70 changes: 70 additions & 0 deletions samples/subsys/event_manager/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.. _event_manager_sample:

Event Manager
#############

.. contents::
:local:
:depth: 2

The Event Manager sample demonstrates the functionality of the :ref:`event_manager` subsystem.
It uses an event-driven architecture, where different modules communicate through sending and processing events.


Overview
********

The sample application consists of three modules that communicate using events:

Sensor (``sensor_simulated.c``):
This module waits for a configuration event (which is sent by ``main.c``).
After receiving this event, it simulates measured data at constant intervals.
Every time the data is updated, the module sends the current values as measurement event.
When the module receives a control event from the Controller, it responds with an ACK event.

Controller (``controller.c``):
This module waits for measurement events from the sensor.
Every time a measurement event is received, the module checks one of the measurement values that are transmitted as part of the event and, if the value exceeds a static threshold, sends a control event.

Statistics (``stats.c``):
This module waits for measurement events from the sensor.
The module calculates and logs basic statistics about one of the measurement values that are transmitted as part of the event.

Building and testing
********************

1.Using development board:
Build and flash Event Manager as follows, changing nrf52840dk_nrf52840 for your board:
west build -b nrf52840dk_nrf52840 samples/subsys/event_manager
west flash
Then connect to the kit with a terminal emulator (for example, PuTTY).
#. Using qemu
If you use qemu platform changing qemu_leon3 for your board:
west build -b quemu_leon3 samples/subsys/event_manager
west build -t run
#. Observe that output similar to the following is logged on UART in case of development kit and in terminal in case of qemu::

***** Booting Zephyr OS v1.13.99-ncs1-4741-g1d6219f *****
[00:00:00.000,854] <inf> event_manager: e: config_event init_val_1=3
[00:00:00.001,068] <inf> event_manager: e: measurement_event val1=3 val2=3 val3=3
[00:00:00.509,063] <inf> event_manager: e: measurement_event val1=3 val2=6 val3=9
[00:00:01.018,005] <inf> event_manager: e: measurement_event val1=3 val2=9 val3=18
[00:00:01.526,947] <inf> event_manager: e: measurement_event val1=3 val2=12 val3=30
[00:00:02.035,888] <inf> event_manager: e: measurement_event val1=3 val2=15 val3=45
[00:00:02.035,949] <inf> event_manager: e: control_event
[00:00:02.035,980] <inf> event_manager: e: ack_event
[00:00:02.544,830] <inf> event_manager: e: measurement_event val1=-3 val2=12 val3=57
[00:00:03.053,771] <inf> event_manager: e: measurement_event val1=-3 val2=9 val3=66
[00:00:03.562,713] <inf> event_manager: e: measurement_event val1=-3 val2=6 val3=72
[00:00:04.071,655] <inf> event_manager: e: measurement_event val1=-3 val2=3 val3=75
[00:00:04.580,596] <inf> event_manager: e: measurement_event val1=-3 val2=0 val3=75
[00:00:04.580,596] <inf> stats: Average value3: 45


Dependencies
************

This sample uses the following Zephyr subsystems:

* :ref:`event_manager`
* :ref:`logging_api`
15 changes: 15 additions & 0 deletions samples/subsys/event_manager/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (c) 2021 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
# Enabling assert
CONFIG_ASSERT=y

# Logger configuration
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=2

# Configuration required by Event Manager
CONFIG_EVENT_MANAGER=y
CONFIG_HEAP_MEM_POOL_SIZE=2048
11 changes: 11 additions & 0 deletions samples/subsys/event_manager/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sample:
description: Sample showing Event Manager usage
name: Event Manager sample
tests:
samples.event_manager:
build_only: true
arch_exclude: xtensa
integration_platforms:
- nrf52840dk_nrf52840
- qemu_x86
tags: ci_build
15 changes: 15 additions & 0 deletions samples/subsys/event_manager/src/events/ack_event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>

#include "ack_event.h"


EVENT_TYPE_DEFINE(ack_event,
true,
NULL,
NULL);
36 changes: 36 additions & 0 deletions samples/subsys/event_manager/src/events/ack_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef _ACK_EVENT_H_
#define _ACK_EVENT_H_

/**
* @brief ACK Event
* @defgroup ack_event ACK Event
* @{
*/

#include "event_manager/event_manager.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use headers correctly: anything in the include path (non-local) must use <>.


#ifdef __cplusplus
extern "C" {
#endif

struct ack_event {
struct event_header header;
};

EVENT_TYPE_DECLARE(ack_event);

#ifdef __cplusplus
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to run on C++ code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect sample simply copied code from some previously defined event. Is there any harm coming out of it?


/**
* @}
*/

#endif /* _ACK_EVENT_H_ */
22 changes: 22 additions & 0 deletions samples/subsys/event_manager/src/events/config_event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>

#include "config_event.h"


static void log_config_event(const struct event_header *eh)
{
struct config_event *event = cast_config_event(eh);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a macro would be more useful/readable (aligning with CONTAINER_OF usage), e.g.

struct config_event *event = EVM_CAST(eh, config);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea - we could follow this up along with new_event api @zycz


EVENT_MANAGER_LOG(eh, "init_val_1=%d", event->init_value1);
}

EVENT_TYPE_DEFINE(config_event,
true,
log_config_event,
NULL);
38 changes: 38 additions & 0 deletions samples/subsys/event_manager/src/events/config_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef _CONFIG_EVENT_H_
#define _CONFIG_EVENT_H_

/**
* @brief Config Event
* @defgroup config_event Config Event
* @{
*/

#include "event_manager/event_manager.h"

#ifdef __cplusplus
extern "C" {
#endif

struct config_event {
struct event_header header;

int8_t init_value1;
};

EVENT_TYPE_DECLARE(config_event);

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* _CONFIG_EVENT_H_ */
13 changes: 13 additions & 0 deletions samples/subsys/event_manager/src/events/control_event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "control_event.h"


EVENT_TYPE_DEFINE(control_event,
true,
NULL,
NULL);
36 changes: 36 additions & 0 deletions samples/subsys/event_manager/src/events/control_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef _CONTROL_EVENT_H_
#define _CONTROL_EVENT_H_

/**
* @brief Control Event
* @defgroup control_event Control Event
* @{
*/

#include "event_manager/event_manager.h"

#ifdef __cplusplus
extern "C" {
#endif

struct control_event {
struct event_header header;
};

EVENT_TYPE_DECLARE(control_event);

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* _CONTROL_EVENT_H_ */
23 changes: 23 additions & 0 deletions samples/subsys/event_manager/src/events/measurement_event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>

#include "measurement_event.h"

static void log_measurement_event(const struct event_header *eh)
{
struct measurement_event *event = cast_measurement_event(eh);

EVENT_MANAGER_LOG(eh, "val1=%d val2=%d val3=%d", event->value1,
event->value2, event->value3);
}


EVENT_TYPE_DEFINE(measurement_event,
true,
log_measurement_event,
NULL);
40 changes: 40 additions & 0 deletions samples/subsys/event_manager/src/events/measurement_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef _MEASUREMENT_EVENT_H_
#define _MEASUREMENT_EVENT_H_

/**
* @brief Measurement Event
* @defgroup measurement_event Measurement Event
* @{
*/

#include "event_manager/event_manager.h"

#ifdef __cplusplus
extern "C" {
#endif

struct measurement_event {
struct event_header header;

int8_t value1;
int16_t value2;
int32_t value3;
};

EVENT_TYPE_DECLARE(measurement_event);

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* _MEASUREMENT_EVENT_H_ */
Loading