Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nrf_cloud_rest_device_message sample to use CAF instead of DK library #12026

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to add support for the 9161 to all our samples. I've created a ticket for it: https://nordicsemi.atlassian.net/browse/IRIS-6848.

When that happens, there will need to be an overlay for that as well here, I assume.

*
* 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) },
};
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)) {
tony-le-24 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading