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 <vich@nordicsemi.no>
This commit is contained in:
parent
5acb79f4e4
commit
bdf6230236
1 changed files with 85 additions and 7 deletions
|
@ -7,13 +7,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/hci.h>
|
||||
|
@ -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 <zephyr/drivers/gpio.h>
|
||||
#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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue