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:
128
snippets/bluetooth/base/code1.txt
Normal file
128
snippets/bluetooth/base/code1.txt
Normal file
@@ -0,0 +1,128 @@
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* BLE */
|
||||
/* --------------------------- */
|
||||
#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/drivers/gpio.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(main);
|
||||
|
||||
static ssize_t recv(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags);
|
||||
|
||||
/* ST Custom Service */
|
||||
static struct bt_uuid_128 st_service_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x0000fe40, 0xcc7a, 0x482a, 0x984a, 0x7f2ed5b3e58f));
|
||||
|
||||
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define ADV_LEN 12
|
||||
|
||||
/* Advertising data */
|
||||
static uint8_t manuf_data[ADV_LEN] = {
|
||||
0x01 /*SKD version */,
|
||||
0x83 /* STM32WB - P2P Server 1 */,
|
||||
0x00 /* GROUP A Feature */,
|
||||
0x00 /* GROUP A Feature */,
|
||||
0x00 /* GROUP B Feature */,
|
||||
0x00 /* GROUP B Feature */,
|
||||
0x00, /* BLE MAC start -MSB */
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00, /* BLE MAC stop */
|
||||
};
|
||||
|
||||
static const struct bt_data ad[] = {
|
||||
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
||||
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
|
||||
BT_DATA(BT_DATA_MANUFACTURER_DATA, manuf_data, ADV_LEN)
|
||||
};
|
||||
|
||||
/* BLE connection */
|
||||
struct bt_conn *ble_conn;
|
||||
/* Notification state */
|
||||
volatile bool notify_enable;
|
||||
|
||||
static void mpu_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
ARG_UNUSED(attr);
|
||||
notify_enable = (value == BT_GATT_CCC_NOTIFY);
|
||||
LOG_INF("Notification %s", notify_enable ? "enabled" : "disabled");
|
||||
}
|
||||
|
||||
BT_GATT_SERVICE_DEFINE(stsensor_svc,
|
||||
BT_GATT_PRIMARY_SERVICE(&st_service_uuid),
|
||||
BT_GATT_CCC(mpu_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
|
||||
);
|
||||
|
||||
static void bt_ready(int err)
|
||||
{
|
||||
if (err) {
|
||||
LOG_ERR("Bluetooth init failed (err %d)", err);
|
||||
return;
|
||||
}
|
||||
LOG_INF("Bluetooth initialized");
|
||||
/* Start advertising */
|
||||
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), NULL, 0);
|
||||
if (err) {
|
||||
LOG_ERR("Advertising failed to start (err %d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("Configuration mode: waiting connections...");
|
||||
}
|
||||
|
||||
static void connected(struct bt_conn *connected, uint8_t err)
|
||||
{
|
||||
if (err) {
|
||||
LOG_ERR("Connection failed (err %u)", err);
|
||||
} else {
|
||||
LOG_INF("Connected");
|
||||
if (!ble_conn) {
|
||||
ble_conn = bt_conn_ref(connected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void disconnected(struct bt_conn *disconn, uint8_t reason)
|
||||
{
|
||||
if (ble_conn) {
|
||||
bt_conn_unref(ble_conn);
|
||||
ble_conn = NULL;
|
||||
}
|
||||
|
||||
LOG_INF("Disconnected (reason %u)", reason);
|
||||
}
|
||||
|
||||
BT_CONN_CB_DEFINE(conn_callbacks) = {
|
||||
.connected = connected,
|
||||
.disconnected = disconnected,
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Example:
|
||||
|
||||
err = bt_enable(bt_ready);
|
||||
if (err) {
|
||||
LOG_ERR("Bluetooth init failed (err %d)", err);
|
||||
}
|
||||
*/
|
||||
|
||||
/* --------------------------- */
|
||||
/* End BLE */
|
||||
/* --------------------------- */
|
||||
|
6
snippets/bluetooth/base/config.txt
Normal file
6
snippets/bluetooth/base/config.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_DEVICE_NAME="Zephyr"
|
||||
CONFIG_BT_GATT_CLIENT=y
|
||||
CONFIG_LOG=y
|
||||
CONFIG_LOG_BUFFER_SIZE=2048
|
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 */
|
||||
/* --------------------------- */
|
||||
|
6
snippets/bluetooth/led/code1.txt
Normal file
6
snippets/bluetooth/led/code1.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
static ssize_t recv(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
|
||||
{
|
||||
led_update();
|
||||
|
||||
return 0;
|
||||
}
|
17
snippets/bluetooth/led/code2.txt
Normal file
17
snippets/bluetooth/led/code2.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* LED */
|
||||
/* --------------------------- */
|
||||
/*
|
||||
Example:
|
||||
|
||||
err = led_init();
|
||||
if (err) {
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
/* --------------------------- */
|
||||
/* End LED */
|
||||
/* --------------------------- */
|
||||
|
52
snippets/bluetooth/led/led_svc.c
Normal file
52
snippets/bluetooth/led/led_svc.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/** @file
|
||||
* @brief Button Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2019 Marcio Montenegro <mtuxpe@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "led_svc.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(led_svc);
|
||||
|
||||
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
|
||||
static bool led_state; /* Tracking state here supports GPIO expander-based LEDs. */
|
||||
static bool led_ok;
|
||||
|
||||
void led_update(void)
|
||||
{
|
||||
if (!led_ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
led_state = !led_state;
|
||||
LOG_INF("Turn %s LED", led_state ? "on" : "off");
|
||||
gpio_pin_set(led.port, led.pin, led_state);
|
||||
}
|
||||
|
||||
int led_init(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
led_ok = gpio_is_ready_dt(&led);
|
||||
if (!led_ok) {
|
||||
LOG_ERR("Error: LED on GPIO %s pin %d is not ready",
|
||||
led.port->name, led.pin);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Error %d: failed to configure GPIO %s pin %d",
|
||||
ret, led.port->name, led.pin);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
snippets/bluetooth/led/led_svc.h
Normal file
25
snippets/bluetooth/led/led_svc.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/** @file
|
||||
* @brief LED Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2019 Marcio Montenegro <mtuxpe@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ST_BLE_SENSOR_LED_SVC_H_
|
||||
#define ST_BLE_SENSOR_LED_SVC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void led_update(void);
|
||||
int led_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user