samples: bluetooth: periodic_sync: add option to control led0

Add a new option to control led0 behavior. If enabled, led0 will be used
for blinking. This option also allows to select GPIO when needed.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2024-12-17 09:56:24 +01:00 committed by Benjamin Cabé
parent ecdb4ffaf1
commit 75154a6443
2 changed files with 13 additions and 8 deletions

View file

@ -12,4 +12,10 @@ config PER_ADV_NAME
help help
Name of target advertising for Periodic Advertising Synchronization. Name of target advertising for Periodic Advertising Synchronization.
config PER_BLINK_LED0
bool "Blink led0"
depends on $(dt_alias_enabled,led0)
select GPIO
default y
source "Kconfig.zephyr" source "Kconfig.zephyr"

View file

@ -27,8 +27,7 @@ static K_SEM_DEFINE(sem_per_sync_lost, 0, 1);
/* The devicetree node identifier for the "led0" alias. */ /* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0) #define LED0_NODE DT_ALIAS(led0)
#if DT_NODE_HAS_STATUS_OKAY(LED0_NODE) #ifdef CONFIG_PER_BLINK_LED0
#define HAS_LED 1
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
#define BLINK_ONOFF K_MSEC(500) #define BLINK_ONOFF K_MSEC(500)
@ -186,7 +185,7 @@ int main(void)
printk("Starting Periodic Advertising Synchronization Demo\n"); printk("Starting Periodic Advertising Synchronization Demo\n");
#if defined(HAS_LED) #ifdef CONFIG_PER_BLINK_LED0
printk("Checking LED device..."); printk("Checking LED device...");
if (!gpio_is_ready_dt(&led)) { if (!gpio_is_ready_dt(&led)) {
printk("failed.\n"); printk("failed.\n");
@ -203,7 +202,7 @@ int main(void)
printk("done.\n"); printk("done.\n");
k_work_init_delayable(&blink_work, blink_timeout); k_work_init_delayable(&blink_work, blink_timeout);
#endif /* HAS_LED */ #endif /* CONFIG_PER_BLINK_LED0 */
/* Initialize the Bluetooth Subsystem */ /* Initialize the Bluetooth Subsystem */
err = bt_enable(NULL); err = bt_enable(NULL);
@ -229,14 +228,14 @@ int main(void)
printk("success.\n"); printk("success.\n");
do { do {
#if defined(HAS_LED) #ifdef CONFIG_PER_BLINK_LED0
struct k_work_sync work_sync; struct k_work_sync work_sync;
printk("Start blinking LED...\n"); printk("Start blinking LED...\n");
led_is_on = false; led_is_on = false;
gpio_pin_set(led.port, led.pin, (int)led_is_on); gpio_pin_set(led.port, led.pin, (int)led_is_on);
k_work_schedule(&blink_work, BLINK_ONOFF); k_work_schedule(&blink_work, BLINK_ONOFF);
#endif /* HAS_LED */ #endif /* CONFIG_PER_BLINK_LED0 */
printk("Waiting for periodic advertising...\n"); printk("Waiting for periodic advertising...\n");
per_adv_found = false; per_adv_found = false;
@ -275,14 +274,14 @@ int main(void)
} }
printk("Periodic sync established.\n"); printk("Periodic sync established.\n");
#if defined(HAS_LED) #ifdef CONFIG_PER_BLINK_LED0
printk("Stop blinking LED.\n"); printk("Stop blinking LED.\n");
k_work_cancel_delayable_sync(&blink_work, &work_sync); k_work_cancel_delayable_sync(&blink_work, &work_sync);
/* Keep LED on */ /* Keep LED on */
led_is_on = true; led_is_on = true;
gpio_pin_set(led.port, led.pin, (int)led_is_on); gpio_pin_set(led.port, led.pin, (int)led_is_on);
#endif /* HAS_LED */ #endif /* CONFIG_PER_BLINK_LED0 */
printk("Waiting for periodic sync lost...\n"); printk("Waiting for periodic sync lost...\n");
err = k_sem_take(&sem_per_sync_lost, K_FOREVER); err = k_sem_take(&sem_per_sync_lost, K_FOREVER);