1 /** @file
2  *  @brief Button Service sample
3  */
4 
5 /*
6  * Copyright (c) 2019 Marcio Montenegro <mtuxpe@gmail.com>
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #include "button_svc.h"
12 
13 #include <zephyr/drivers/gpio.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/logging/log.h>
16 
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/hci.h>
19 #include <zephyr/bluetooth/conn.h>
20 #include <zephyr/bluetooth/uuid.h>
21 #include <zephyr/bluetooth/gatt.h>
22 
23 LOG_MODULE_REGISTER(button_svc);
24 
25 static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
26 static struct gpio_callback gpio_cb;
27 
button_init(gpio_callback_handler_t handler)28 int button_init(gpio_callback_handler_t handler)
29 {
30 	int ret;
31 
32 	if (!gpio_is_ready_dt(&button)) {
33 		LOG_ERR("Error: button GPIO device %s is not ready",
34 			button.port->name);
35 		return -ENODEV;
36 	}
37 
38 	ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
39 	if (ret != 0) {
40 		LOG_ERR("Error %d: can't configure button on GPIO %s pin %d",
41 			ret, button.port->name, button.pin);
42 		return ret;
43 
44 	}
45 
46 	gpio_init_callback(&gpio_cb, handler, BIT(button.pin));
47 	gpio_add_callback(button.port, &gpio_cb);
48 	ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
49 	if (ret != 0) {
50 		LOG_ERR("Error %d: can't configure button interrupt on "
51 			"GPIO %s pin %d", ret, button.port->name, button.pin);
52 		return ret;
53 	}
54 	return 0;
55 }
56