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:
Marcio Montenegro 2019-12-16 16:52:42 -03:00 committed by Carles Cufí
commit c6df1b9395
5 changed files with 49 additions and 22 deletions

View file

@ -23,6 +23,11 @@ Building and Running
This sample can be found under :zephyr_file:`samples/bluetooth/st_ble_sensor` in the
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.
.. _ST BLE Sensor app:

View file

@ -64,18 +64,32 @@ void button_pressed(struct device *gpiob, struct gpio_callback *cb,
int button_init(void)
{
int ret;
button_dev = device_get_binding(BUT_PORT);
if (!button_dev) {
return (-EOPNOTSUPP);
}
gpio_pin_configure(button_dev, BUT_PIN,
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
GPIO_PUD_PULL_UP | GPIO_INT_DEBOUNCE |
GPIO_INT_ACTIVE_LOW);
gpio_init_callback(&gpio_cb, button_pressed, BIT(BUT_PIN));
ret = gpio_pin_configure(button_dev, BUT_PIN,
DT_ALIAS_SW0_GPIOS_FLAGS | GPIO_INPUT);
if (ret != 0) {
LOG_ERR("Error %d: failed to configure pin %d '%s'\n",
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_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;
return 0;
}

View file

@ -26,24 +26,35 @@ LOG_MODULE_REGISTER(led_svc);
struct device *led_dev;
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) {
gpio_pin_write(led_dev, LED, led_state);
gpio_pin_set(led_dev, LED, led_param);
}
}
int led_init(void)
{
int ret;
led_dev = device_get_binding(LED_PORT);
if (!led_dev) {
return (-EOPNOTSUPP);
}
/* 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;
gpio_pin_write(led_dev, LED, 0);
led_on_off(0);
led_on_off(led_state);
return 0;
}

View file

@ -12,7 +12,7 @@
extern "C" {
#endif
void led_on_off(u32_t led_state);
void led_on_off(bool led_state);
int led_init(void);
#ifdef __cplusplus

View file

@ -110,15 +110,13 @@ static ssize_t recv(struct bt_conn *conn,
u16_t len, u16_t offset, u8_t flags)
{
if (led_dev) {
if (led_state == true) {
led_on_off(0);
if (led_state) {
LOG_INF("Turn off LED");
} else {
led_on_off(1);
LOG_INF("Turn on LED");
}
led_state = !led_state;
led_on_off(led_state);
}
return 0;
@ -174,10 +172,13 @@ void main(void)
err = button_init();
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);
/* Initialize the Bluetooth Subsystem */
@ -185,8 +186,4 @@ void main(void)
if (err) {
LOG_ERR("Bluetooth init failed (err %d)", err);
}
while (1) {
k_sleep(K_SECONDS(1));
}
}