Skip to content

Commit

Permalink
samples: cellular: nrf_cloud_rest_device_message: use CAF
Browse files Browse the repository at this point in the history
Updated the sample to use CAF instead of DK library for led
and button events. Added a device-tree overlay for LEDs to
work with CAF LEDs module. Added header files for buttons
and LEDs definition which are required by CAF.

Also, added error message for when the sample fails to send
alerts to nRF Cloud.

Signed-off-by: Tony Le <tony.le@nordicsemi.no>
  • Loading branch information
tony-le-24 committed Aug 22, 2023
1 parent cdf78d8 commit 0c7ae05
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 23 deletions.
12 changes: 12 additions & 0 deletions doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ Cellular samples (renamed from nRF9160 samples)

* Added reporting of device and connection info to the device shadow.

* :ref:`nrf_cloud_rest_device_message` sample:

* Added:

* A DTS overlay file for LEDs on the nRF9160 DK to be compatible with the :ref:`caf_leds` module.
* Header files for buttons and LEDs definition required by the :ref:`lib_caf` library.

* Updated:

* The sample to use the :ref:`lib_caf` library instead of the :ref:`dk_buttons_and_leds_readme` library.
* Displaying an error message when the sample fails to send an alert to nRF Cloud.

Trusted Firmware-M (TF-M) samples
---------------------------------

Expand Down
3 changes: 3 additions & 0 deletions samples/cellular/nrf_cloud_rest_device_message/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(nRF_Cloud_REST_Device_Message_Sample)

zephyr_compile_definitions(PROJECT_NAME=${PROJECT_NAME})
zephyr_include_directories(include/)

# NORDIC SDK APP START
target_include_directories(app PRIVATE include/)
target_sources(app PRIVATE src/main.c)
# NORDIC SDK APP END
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

/ {
/* Define 4 monochromatic LEDs connected directly
* to the GPIO to be used with CAF LEDs module
*/
led0 {
compatible = "gpio-leds";
status = "okay";

led0_0: led_0 {
gpios = <&gpio0 2 0>;
};
};

led1 {
compatible = "gpio-leds";
status = "okay";

led1_0: led_0 {
gpios = <&gpio0 3 0>;
};
};

led2 {
compatible = "gpio-leds";
status = "okay";

led2_0: led_0 {
gpios = <&gpio0 4 0>;
};
};

led3 {
compatible = "gpio-leds";
status = "okay";

led3_0: led_0 {
gpios = <&gpio0 5 0>;
};
};

/* Disable the original leds node from device-tree
* since it is incompatible with the CAF LEDs module
*/
leds {
status = "disabled";
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <caf/gpio_pins.h>

/* This file is included by the common application framework (CAF) library */

/* This configuration file is included only once from buttons module and holds
* information about pins forming the keyboard matrix.
*/

/* This structure enforces the header file to be included only once in the build.
* Violating this requirement triggers a multiple definition error at link time.
*/
const struct {} buttons_def_include_once;

static const struct gpio_pin col[] = {};

static const struct gpio_pin row[] = {
{ .port = 0, .pin = DT_GPIO_PIN(DT_NODELABEL(button0), gpios) },
{ .port = 0, .pin = DT_GPIO_PIN(DT_NODELABEL(button1), gpios) },
};
28 changes: 28 additions & 0 deletions samples/cellular/nrf_cloud_rest_device_message/include/leds_def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <caf/led_effect.h>

/* This configuration file is included only once from the main module and holds
* information about LED on and off effects.
*/

/* This structure enforces the header file to be included only once in the build.
* Violating this requirement triggers a multiple definition error at link time.
*/
const struct {} leds_def_include_once;

enum led_id {
LED_ID_0,
LED_ID_1,
LED_ID_2,
LED_ID_3,

LED_ID_COUNT
};

static const struct led_effect led_effect_on = LED_EFFECT_LED_ON(LED_COLOR(255, 255, 255));
static const struct led_effect led_effect_off = LED_EFFECT_LED_OFF();
17 changes: 15 additions & 2 deletions samples/cellular/nrf_cloud_rest_device_message/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_NATIVE=n

# Button/LED support
CONFIG_DK_LIBRARY=y
# Common Application Framework (CAF)
CONFIG_CAF=y
CONFIG_APP_EVENT_MANAGER=y
CONFIG_APP_EVENT_MANAGER_LOG_LEVEL_ERR=y

# CAF button module with GPIOs
CONFIG_GPIO=y
CONFIG_CAF_BUTTONS=y
CONFIG_CAF_BUTTONS_POLARITY_INVERSED=y

# CAF LEDs module with GPIOs
CONFIG_LED=y
CONFIG_CAF_LEDS=y
CONFIG_LED_GPIO=y
CONFIG_CAF_LEDS_GPIO=y

# Modem/LTE Link
CONFIG_NRF_MODEM_LIB=y
Expand Down
83 changes: 62 additions & 21 deletions samples/cellular/nrf_cloud_rest_device_message/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
#include <net/nrf_cloud_log.h>
#include <net/nrf_cloud_alert.h>
#include <zephyr/logging/log.h>
#include <dk_buttons_and_leds.h>
#include <date_time.h>
#include <zephyr/random/rand32.h>

#include <app_event_manager.h>
#include <caf/events/button_event.h>
#include <caf/events/led_event.h>
#define MODULE main
#include <caf/events/module_state_event.h>

#include "leds_def.h"

LOG_MODULE_REGISTER(nrf_cloud_rest_device_message,
CONFIG_NRF_CLOUD_REST_DEVICE_MESSAGE_SAMPLE_LOG_LEVEL);

Expand Down Expand Up @@ -58,9 +65,30 @@ static struct nrf_cloud_rest_context rest_ctx = {
/* Flag to indicate if the user requested JITP to be performed */
static bool jitp_requested;

/* Register a listener for application events, specifically a button event */
static bool app_event_handler(const struct app_event_header *aeh);
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, button_event);

static int send_led_event(enum led_id led_idx, const int state)
{
if (led_idx >= LED_ID_COUNT) {
LOG_ERR("Invalid LED ID: %d", led_idx);
return -EINVAL;
}

struct led_event *event = new_led_event();

event->led_id = led_idx;
event->led_effect = state != 0 ? &led_effect_on : &led_effect_off;
APP_EVENT_SUBMIT(event);

return 0;
}

static int set_led(const int led, const int state)
{
int err = dk_set_led(led, state);
int err = send_led_event(led, state);

if (err) {
LOG_ERR("Failed to set LED %d, error: %d", led, err);
Expand All @@ -69,16 +97,10 @@ static int set_led(const int led, const int state)
return 0;
}

static int init_leds(void)
static int set_leds_off(void)
{
int err = dk_leds_init();
int err = set_led(LTE_LED_NUM, 0);

if (err) {
LOG_ERR("LED init failed, error: %d", err);
return err;
}

err = set_led(LTE_LED_NUM, 0);
if (err) {
return err;
}
Expand All @@ -88,15 +110,25 @@ static int init_leds(void)
return err;
}

return err;
return 0;
}

static void button_handler(uint32_t button_states, uint32_t has_changed)
static bool app_event_handler(const struct app_event_header *aeh)
{
if (has_changed & button_states & BIT(BTN_NUM - 1)) {
LOG_DBG("Button %d pressed", BTN_NUM);
struct button_event *event = is_button_event(aeh) ? cast_button_event(aeh) : NULL;

if (!event) {
return false;
} else if (!event->pressed) {
return true;
}

LOG_DBG("Button %d pressed", BTN_NUM);
if (event->key_id == (uint16_t)(BTN_NUM - 1)) {
k_sem_give(&button_press_sem);
}

return true;
}

static int send_message(const char *const msg)
Expand Down Expand Up @@ -303,10 +335,12 @@ static int init(void)
{
int err;

/* Init the LEDs */
err = init_leds();
/* Application event manager is used for button press and LED
* events from the common application framework (CAF) library
*/
err = app_event_manager_init();
if (err) {
LOG_ERR("LED initialization failed");
LOG_ERR("Application Event Manager could not be initialized, error: %d", err);
return err;
}

Expand All @@ -317,10 +351,13 @@ static int init(void)
return -EFAULT;
}

/* Init the button */
err = dk_buttons_init(button_handler);
/* Inform the app event manager that this module is ready to receive events */
module_set_state(MODULE_STATE_READY);

/* Set the LEDs off after all modules are ready */
err = set_leds_off();
if (err) {
LOG_ERR("Failed to initialize button: error: %d", err);
LOG_ERR("Failed to set LEDs off");
return err;
}

Expand Down Expand Up @@ -397,9 +434,13 @@ int main(void)
}

rest_ctx.keep_alive = true;
(void)nrf_cloud_rest_alert_send(&rest_ctx, device_id,
err = nrf_cloud_rest_alert_send(&rest_ctx, device_id,
ALERT_TYPE_DEVICE_NOW_ONLINE, 0, NULL);

if (err) {
LOG_ERR("Error sending alert to cloud: %d", err);
}

err = nrf_cloud_rest_log_send(&rest_ctx, device_id, LOG_LEVEL_INF,
SAMPLE_SIGNON_FMT,
CONFIG_REST_DEVICE_MESSAGE_SAMPLE_VERSION);
Expand Down

0 comments on commit 0c7ae05

Please sign in to comment.