From bdf62302363eff157211db57b3a9688a00ad3ca4 Mon Sep 17 00:00:00 2001 From: Vinayak Kariappa Chettimada Date: Fri, 15 Mar 2024 12:24:33 +0100 Subject: [PATCH] samples: Bluetooth: Add LED blinking to peripheral_hr Add LED blinking state to peripheral_hr indicating advertising state, and LED On state to indicate connected to a central device, respectively. Signed-off-by: Vinayak Kariappa Chettimada --- samples/bluetooth/peripheral_hr/src/main.c | 92 ++++++++++++++++++++-- 1 file changed, 85 insertions(+), 7 deletions(-) diff --git a/samples/bluetooth/peripheral_hr/src/main.c b/samples/bluetooth/peripheral_hr/src/main.c index 938de5f01e1..03279c0840f 100644 --- a/samples/bluetooth/peripheral_hr/src/main.c +++ b/samples/bluetooth/peripheral_hr/src/main.c @@ -7,13 +7,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include -#include -#include -#include -#include -#include +#include +#include #include #include @@ -124,6 +119,73 @@ static void hrs_notify(void) } } +#if defined(CONFIG_GPIO) +/* The devicetree node identifier for the "led0" alias. */ +#define LED0_NODE DT_ALIAS(led0) + +#if DT_NODE_HAS_STATUS(LED0_NODE, okay) +#include +#define HAS_LED 1 +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); +#define BLINK_ONOFF K_MSEC(500) + +static struct k_work_delayable blink_work; +static bool led_is_on; + +static void blink_timeout(struct k_work *work) +{ + led_is_on = !led_is_on; + gpio_pin_set(led.port, led.pin, (int)led_is_on); + + k_work_schedule(&blink_work, BLINK_ONOFF); +} + +static int blink_setup(void) +{ + int err; + + printk("Checking LED device..."); + if (!gpio_is_ready_dt(&led)) { + printk("failed.\n"); + return -EIO; + } + printk("done.\n"); + + printk("Configuring GPIO pin..."); + err = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); + if (err) { + printk("failed.\n"); + return -EIO; + } + printk("done.\n"); + + k_work_init_delayable(&blink_work, blink_timeout); + + return 0; +} + +static void blink_start(void) +{ + printk("Start blinking LED...\n"); + led_is_on = false; + gpio_pin_set(led.port, led.pin, (int)led_is_on); + k_work_schedule(&blink_work, BLINK_ONOFF); +} + +static void blink_stop(void) +{ + struct k_work_sync work_sync; + + printk("Stop blinking LED.\n"); + k_work_cancel_delayable_sync(&blink_work, &work_sync); + + /* Keep LED on */ + led_is_on = true; + gpio_pin_set(led.port, led.pin, (int)led_is_on); +} +#endif /* LED0_NODE */ +#endif /* CONFIG_GPIO */ + int main(void) { int err; @@ -193,6 +255,15 @@ int main(void) printk("Advertising successfully started\n"); +#if defined(HAS_LED) + err = blink_setup(); + if (err) { + return 0; + } + + blink_start(); +#endif /* HAS_LED */ + /* Implement notification. */ while (1) { k_sleep(K_SECONDS(1)); @@ -206,6 +277,9 @@ int main(void) if (atomic_test_and_clear_bit(state, STATE_CONNECTED)) { /* Connected callback executed */ +#if defined(HAS_LED) + blink_stop(); +#endif /* HAS_LED */ } else if (atomic_test_and_clear_bit(state, STATE_DISCONNECTED)) { #if defined(CONFIG_BT_EXT_ADV) printk("Starting Extended Advertising (connectable and non-scannable)\n"); @@ -215,6 +289,10 @@ int main(void) return 0; } #endif /* CONFIG_BT_EXT_ADV */ + +#if defined(HAS_LED) + blink_start(); +#endif /* HAS_LED */ } }