Samples: Bluetooth: st_ble_demo: Update to new GPIO API
Convert to the new API pin and interrupt API. Signed-off-by: Marcio Montenegro <mtuxpe@gmail.com>
This commit is contained in:
parent
e1c10f6454
commit
c6df1b9395
5 changed files with 49 additions and 22 deletions
|
@ -23,6 +23,11 @@ Building and Running
|
||||||
This sample can be found under :zephyr_file:`samples/bluetooth/st_ble_sensor` in the
|
This sample can be found under :zephyr_file:`samples/bluetooth/st_ble_sensor` in the
|
||||||
Zephyr tree.
|
Zephyr tree.
|
||||||
|
|
||||||
|
Open ST BLE Sensor app and click on "CONNECT TO A DEVICE" button to scan BLE devices.
|
||||||
|
To connect click on the device shown in the Device List.
|
||||||
|
After connected, tap LED image on Android to test LED service.
|
||||||
|
Push SW0 button on embedded device to test button service.
|
||||||
|
|
||||||
See :ref:`bluetooth samples section <bluetooth-samples>` for details.
|
See :ref:`bluetooth samples section <bluetooth-samples>` for details.
|
||||||
|
|
||||||
.. _ST BLE Sensor app:
|
.. _ST BLE Sensor app:
|
||||||
|
|
|
@ -64,18 +64,32 @@ void button_pressed(struct device *gpiob, struct gpio_callback *cb,
|
||||||
|
|
||||||
int button_init(void)
|
int button_init(void)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
button_dev = device_get_binding(BUT_PORT);
|
button_dev = device_get_binding(BUT_PORT);
|
||||||
if (!button_dev) {
|
if (!button_dev) {
|
||||||
return (-EOPNOTSUPP);
|
return (-EOPNOTSUPP);
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_pin_configure(button_dev, BUT_PIN,
|
ret = gpio_pin_configure(button_dev, BUT_PIN,
|
||||||
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
|
DT_ALIAS_SW0_GPIOS_FLAGS | GPIO_INPUT);
|
||||||
GPIO_PUD_PULL_UP | GPIO_INT_DEBOUNCE |
|
if (ret != 0) {
|
||||||
GPIO_INT_ACTIVE_LOW);
|
LOG_ERR("Error %d: failed to configure pin %d '%s'\n",
|
||||||
gpio_init_callback(&gpio_cb, button_pressed, BIT(BUT_PIN));
|
ret, BUT_PIN, DT_ALIAS_SW0_LABEL);
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
gpio_init_callback(&gpio_cb, button_pressed,
|
||||||
|
BIT(BUT_PIN));
|
||||||
gpio_add_callback(button_dev, &gpio_cb);
|
gpio_add_callback(button_dev, &gpio_cb);
|
||||||
gpio_pin_enable_callback(button_dev, BUT_PIN);
|
ret = gpio_pin_interrupt_configure(button_dev, BUT_PIN,
|
||||||
|
GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
|
if (ret != 0) {
|
||||||
|
LOG_ERR("Error %d: failed to configure interrupt on pin "
|
||||||
|
"%d '%s'\n", ret, BUT_PIN, DT_ALIAS_SW0_LABEL);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
but_val = 0;
|
but_val = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,24 +26,35 @@ LOG_MODULE_REGISTER(led_svc);
|
||||||
struct device *led_dev;
|
struct device *led_dev;
|
||||||
bool led_state;
|
bool led_state;
|
||||||
|
|
||||||
void led_on_off(u32_t led_state)
|
void led_on_off(bool led_state)
|
||||||
{
|
{
|
||||||
|
int led_param = (led_state == true) ? 1 : 0;
|
||||||
if (led_dev) {
|
if (led_dev) {
|
||||||
gpio_pin_write(led_dev, LED, led_state);
|
gpio_pin_set(led_dev, LED, led_param);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int led_init(void)
|
int led_init(void)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
led_dev = device_get_binding(LED_PORT);
|
led_dev = device_get_binding(LED_PORT);
|
||||||
if (!led_dev) {
|
if (!led_dev) {
|
||||||
return (-EOPNOTSUPP);
|
return (-EOPNOTSUPP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set LED pin as output */
|
/* Set LED pin as output */
|
||||||
gpio_pin_configure(led_dev, LED, GPIO_DIR_OUT);
|
|
||||||
|
ret = gpio_pin_configure(led_dev, LED,
|
||||||
|
GPIO_OUTPUT_ACTIVE
|
||||||
|
| DT_ALIAS_LED0_GPIOS_FLAGS);
|
||||||
|
if (ret < 0) {
|
||||||
|
LOG_ERR("Error %d: failed to configure pin %d '%s'\n",
|
||||||
|
ret, LED, DT_ALIAS_LED0_LABEL);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
led_state = false;
|
led_state = false;
|
||||||
gpio_pin_write(led_dev, LED, 0);
|
led_on_off(led_state);
|
||||||
led_on_off(0);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void led_on_off(u32_t led_state);
|
void led_on_off(bool led_state);
|
||||||
int led_init(void);
|
int led_init(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -110,15 +110,13 @@ static ssize_t recv(struct bt_conn *conn,
|
||||||
u16_t len, u16_t offset, u8_t flags)
|
u16_t len, u16_t offset, u8_t flags)
|
||||||
{
|
{
|
||||||
if (led_dev) {
|
if (led_dev) {
|
||||||
if (led_state == true) {
|
if (led_state) {
|
||||||
led_on_off(0);
|
|
||||||
LOG_INF("Turn off LED");
|
LOG_INF("Turn off LED");
|
||||||
} else {
|
} else {
|
||||||
led_on_off(1);
|
|
||||||
LOG_INF("Turn on LED");
|
LOG_INF("Turn on LED");
|
||||||
}
|
}
|
||||||
|
|
||||||
led_state = !led_state;
|
led_state = !led_state;
|
||||||
|
led_on_off(led_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -174,10 +172,13 @@ void main(void)
|
||||||
|
|
||||||
err = button_init();
|
err = button_init();
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_ERR("Button init error: (err %d)", err);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
led_init();
|
err = led_init();
|
||||||
|
if (err) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
bt_conn_cb_register(&conn_callbacks);
|
bt_conn_cb_register(&conn_callbacks);
|
||||||
|
|
||||||
/* Initialize the Bluetooth Subsystem */
|
/* Initialize the Bluetooth Subsystem */
|
||||||
|
@ -185,8 +186,4 @@ void main(void)
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_ERR("Bluetooth init failed (err %d)", err);
|
LOG_ERR("Bluetooth init failed (err %d)", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
|
||||||
k_sleep(K_SECONDS(1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue