Added new bluetooth samples
All checks were successful
Build & Publish / build (push) Successful in 8m17s
All checks were successful
Build & Publish / build (push) Successful in 8m17s
This commit is contained in:
54
snippets/bluetooth/button/button_svc.c
Normal file
54
snippets/bluetooth/button/button_svc.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/** @file
|
||||
* @brief Button Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2019 Marcio Montenegro <mtuxpe@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "button_svc.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/hci.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/uuid.h>
|
||||
#include <zephyr/bluetooth/gatt.h>
|
||||
|
||||
LOG_MODULE_REGISTER(button_svc);
|
||||
|
||||
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
|
||||
static struct gpio_callback gpio_cb;
|
||||
|
||||
int button_init(gpio_callback_handler_t handler)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&button)) {
|
||||
LOG_ERR("Error: button GPIO device %s is not ready",
|
||||
button.port->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("Error %d: can't configure button on GPIO %s pin %d",
|
||||
ret, button.port->name, button.pin);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
gpio_init_callback(&gpio_cb, handler, BIT(button.pin));
|
||||
gpio_add_callback(button.port, &gpio_cb);
|
||||
ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("Error %d: can't configure button interrupt on "
|
||||
"GPIO %s pin %d", ret, button.port->name, button.pin);
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
26
snippets/bluetooth/button/button_svc.h
Normal file
26
snippets/bluetooth/button/button_svc.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/** @file
|
||||
* @brief Button Service
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2019 Marcio Montenegro <mtuxpe@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ST_BLE_SENSOR_BUTTON_SVC_H_
|
||||
#define ST_BLE_SENSOR_BUTTON_SVC_H_
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int button_init(gpio_callback_handler_t handler);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
21
snippets/bluetooth/button/code1.txt
Normal file
21
snippets/bluetooth/button/code1.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
static void button_callback(const struct device *gpiob, struct gpio_callback *cb, uint32_t pins)
|
||||
{
|
||||
int err;
|
||||
|
||||
LOG_INF("Button pressed");
|
||||
if (ble_conn) {
|
||||
if (notify_enable) {
|
||||
err = bt_gatt_notify(NULL, &stsensor_svc.attrs[4], &but_val, sizeof(but_val));
|
||||
if (err) {
|
||||
LOG_ERR("Notify error: %d", err);
|
||||
} else {
|
||||
LOG_INF("Send notify ok");
|
||||
but_val = (but_val == 0) ? 0x100 : 0;
|
||||
}
|
||||
} else {
|
||||
LOG_INF("Notify not enabled");
|
||||
}
|
||||
} else {
|
||||
LOG_INF("BLE not connected");
|
||||
}
|
||||
}
|
17
snippets/bluetooth/button/code2.txt
Normal file
17
snippets/bluetooth/button/code2.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Button */
|
||||
/* --------------------------- */
|
||||
/*
|
||||
Example:
|
||||
|
||||
err = button_init(button_callback);
|
||||
if (err) {
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
/* --------------------------- */
|
||||
/* Button LED */
|
||||
/* --------------------------- */
|
||||
|
Reference in New Issue
Block a user