Skip to content

Commit 1cb6c96

Browse files
committed
samples: add low power snippet to Light Switch
Add low_power snippet that disables serial/console/logging, enables sleepy behavior, disables LEDs, and optimizes power consumption. Usage: west build samples/light_switch -b <board> --snippet=low_power Signed-off-by: Eduardo Montoya <[email protected]>
1 parent 088fefe commit 1cb6c96

File tree

5 files changed

+88
-14
lines changed

5 files changed

+88
-14
lines changed

samples/light_switch/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ config LIGHT_SWITCH_TX_POWER
2525
default 0
2626
help
2727
TX power level in dBm.
28+
29+
config LIGHT_SWITCH_LOW_POWER
30+
bool "Low power mode"
31+
default n
32+
help
33+
Enable low power mode for the Light Switch sample.
34+
This enables sleepy behavior and disables LEDs for power savings.

samples/light_switch/README.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ For more information about configuration files in the |NCS|, see `Build and conf
145145
146146
.. include:: /includes/sample_fem_support.txt
147147

148+
Snippets
149+
========
150+
151+
.. |snippet| replace:: :makevar:`light_switch_SNIPPET`
152+
153+
The following snippets are available:
154+
155+
* ``low_power`` - Enables low power consumption mode for the Light Switch sample.
156+
This snippet disables serial communication, console output, enables sleepy end device behavior,
157+
and disables LED indications to minimize power consumption.
158+
159+
To build with the low power snippet, use the following command:
160+
161+
.. parsed-literal::
162+
:class: highlight
163+
164+
west build samples/light_switch -b *board_target* -- --snippet=low_power
165+
148166
Configurable transmission power
149167
===============================
150168

@@ -319,7 +337,7 @@ After programming the sample to your development kits, complete the following st
319337

320338
This LED indicates that the light switch found a light bulb to control.
321339

322-
.. group-tab:: nRF52840 and nRF5340 DKs DK
340+
.. group-tab:: nRF52840 and nRF5340 DKs
323341

324342
1. Turn on the development kit that runs the Network coordinator sample.
325343

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Low power configuration for Zigbee Light Switch
8+
CONFIG_LIGHT_SWITCH_LOW_POWER=y
9+
10+
# Disable serial and console for power savings
11+
CONFIG_SERIAL=n
12+
CONFIG_CONSOLE=n
13+
14+
# Disable logging for additional power savings
15+
CONFIG_LOG=n
16+
17+
# Set TX power to 0 dBm
18+
CONFIG_LIGHT_SWITCH_TX_POWER=0
19+
20+
# Enable device power management for low power consumption
21+
CONFIG_PM_DEVICE=y
22+
23+
# Ensure RAM power down is enabled for low power consumption
24+
CONFIG_RAM_POWER_DOWN_LIBRARY=y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
name: low_power
8+
append:
9+
EXTRA_CONF_FILE: low_power.conf

samples/light_switch/src/main.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@
103103

104104
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
105105

106+
/* Helper functions for LED control in low power mode */
107+
#if CONFIG_LIGHT_SWITCH_LOW_POWER
108+
static inline void led_set(uint32_t led, uint32_t val) { ARG_UNUSED(led); ARG_UNUSED(val); }
109+
static inline void led_set_on(uint32_t led) { ARG_UNUSED(led); }
110+
static inline void led_set_off(uint32_t led) { ARG_UNUSED(led); }
111+
#else
112+
static inline void led_set(uint32_t led, uint32_t val) { dk_set_led(led, val); }
113+
static inline void led_set_on(uint32_t led) { dk_set_led_on(led); }
114+
static inline void led_set_off(uint32_t led) { dk_set_led_off(led); }
115+
#endif
116+
106117
struct bulb_context {
107118
zb_uint8_t endpoint;
108119
zb_uint16_t short_addr;
@@ -319,10 +330,12 @@ static void configure_gpio(void)
319330
LOG_ERR("Cannot init buttons (err: %d)", err);
320331
}
321332

333+
#if !CONFIG_LIGHT_SWITCH_LOW_POWER
322334
err = dk_leds_init();
323335
if (err) {
324336
LOG_ERR("Cannot init LEDs (err: %d)", err);
325337
}
338+
#endif
326339
}
327340

328341
static void alarm_timers_init(void)
@@ -350,7 +363,7 @@ static void toggle_identify_led(zb_bufid_t bufid)
350363
{
351364
static int blink_status;
352365

353-
dk_set_led(IDENTIFY_LED, (++blink_status) % 2);
366+
led_set(IDENTIFY_LED, (++blink_status) % 2);
354367
ZB_SCHEDULE_APP_ALARM(toggle_identify_led, bufid, ZB_MILLISECONDS_TO_BEACON_INTERVAL(100));
355368
}
356369

@@ -372,9 +385,9 @@ static void identify_cb(zb_bufid_t bufid)
372385

373386
/* Update network status/idenitfication LED. */
374387
if (ZB_JOINED()) {
375-
dk_set_led_on(ZIGBEE_NETWORK_STATE_LED);
388+
led_set_on(ZIGBEE_NETWORK_STATE_LED);
376389
} else {
377-
dk_set_led_off(ZIGBEE_NETWORK_STATE_LED);
390+
led_set_off(ZIGBEE_NETWORK_STATE_LED);
378391
}
379392
}
380393
}
@@ -458,7 +471,7 @@ static void find_light_bulb_cb(zb_bufid_t bufid)
458471
bulb_ctx.endpoint);
459472

460473
k_timer_stop(&bulb_ctx.find_alarm);
461-
dk_set_led_on(BULB_FOUND_LED);
474+
led_set_on(BULB_FOUND_LED);
462475
} else {
463476
LOG_INF("Bulb not found, try again");
464477
}
@@ -567,7 +580,7 @@ static void ota_evt_handler(const struct zigbee_fota_evt *evt)
567580
{
568581
switch (evt->id) {
569582
case ZIGBEE_FOTA_EVT_PROGRESS:
570-
dk_set_led(OTA_ACTIVITY_LED, evt->dl.progress % 2);
583+
led_set(OTA_ACTIVITY_LED, evt->dl.progress % 2);
571584
break;
572585

573586
case ZIGBEE_FOTA_EVT_FINISHED:
@@ -706,13 +719,13 @@ static void decrease_cmd(struct k_work *item)
706719
static void on_nus_connect(struct k_work *item)
707720
{
708721
ARG_UNUSED(item);
709-
dk_set_led_on(NUS_STATUS_LED);
722+
led_set_on(NUS_STATUS_LED);
710723
}
711724

712725
static void on_nus_disconnect(struct k_work *item)
713726
{
714727
ARG_UNUSED(item);
715-
dk_set_led_off(NUS_STATUS_LED);
728+
led_set_off(NUS_STATUS_LED);
716729
}
717730

718731
static struct nus_entry commands[] = {
@@ -825,15 +838,18 @@ int main(void)
825838
/* Set default bulb short_addr. */
826839
bulb_ctx.short_addr = 0xFFFF;
827840

828-
/* If "sleepy button" is defined, check its state during Zigbee
829-
* initialization and enable sleepy behavior at device if defined button
830-
* is pressed.
841+
/* Check if sleepy button is pressed during Zigbee initialization
842+
* and enable sleepy behavior. In low power mode, always enable sleepy behavior.
831843
*/
832-
#if defined BUTTON_SLEEPY
833-
if (dk_get_buttons() & BUTTON_SLEEPY) {
844+
#if CONFIG_LIGHT_SWITCH_LOW_POWER
845+
bool enable_sleepy = true;
846+
#else
847+
bool enable_sleepy = (dk_get_buttons() & BUTTON_SLEEPY) ? true : false;
848+
#endif
849+
850+
if (enable_sleepy) {
834851
zigbee_configure_sleepy_behavior(true);
835852
}
836-
#endif
837853

838854
/* Power off unused sections of RAM to lower device power consumption. */
839855
if (IS_ENABLED(CONFIG_RAM_POWER_DOWN_LIBRARY)) {

0 commit comments

Comments
 (0)