1 /* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
2 *
3 * Copyright (c) 2018 Vikrant More
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/kernel.h>
10
11 #include "app_gpio.h"
12 #include "publisher.h"
13
14 K_WORK_DEFINE(button_work, publish);
15
button_pressed(const struct device * dev,struct gpio_callback * cb,uint32_t pins)16 static void button_pressed(const struct device *dev,
17 struct gpio_callback *cb, uint32_t pins)
18 {
19 k_work_submit(&button_work);
20 }
21
22 #define LED0_NODE DT_ALIAS(led0)
23 #define LED1_NODE DT_ALIAS(led1)
24 #define LED2_NODE DT_ALIAS(led2)
25 #define LED3_NODE DT_ALIAS(led3)
26
27 #define SW0_NODE DT_ALIAS(sw0)
28 #define SW1_NODE DT_ALIAS(sw1)
29 #define SW2_NODE DT_ALIAS(sw2)
30 #define SW3_NODE DT_ALIAS(sw3)
31
32 #ifdef ONE_LED_ONE_BUTTON_BOARD
33 #define NUM_LED_NUM_BUTTON 1
34 #else
35 #define NUM_LED_NUM_BUTTON 4
36 #endif
37
38 const struct gpio_dt_spec led_device[NUM_LED_NUM_BUTTON] = {
39 GPIO_DT_SPEC_GET(LED0_NODE, gpios),
40 #ifndef ONE_LED_ONE_BUTTON_BOARD
41 GPIO_DT_SPEC_GET(LED1_NODE, gpios),
42 GPIO_DT_SPEC_GET(LED2_NODE, gpios),
43 GPIO_DT_SPEC_GET(LED3_NODE, gpios),
44 #endif
45 };
46
47 const struct gpio_dt_spec button_device[NUM_LED_NUM_BUTTON] = {
48 GPIO_DT_SPEC_GET(SW0_NODE, gpios),
49 #ifndef ONE_LED_ONE_BUTTON_BOARD
50 GPIO_DT_SPEC_GET(SW1_NODE, gpios),
51 GPIO_DT_SPEC_GET(SW2_NODE, gpios),
52 GPIO_DT_SPEC_GET(SW3_NODE, gpios),
53 #endif
54 };
55
app_gpio_init(void)56 void app_gpio_init(void)
57 {
58 static struct gpio_callback button_cb[4];
59 int i;
60
61 /* LEDs configuration & setting */
62
63 for (i = 0; i < ARRAY_SIZE(led_device); i++) {
64 if (!gpio_is_ready_dt(&led_device[i])) {
65 return;
66 }
67 gpio_pin_configure_dt(&led_device[i], GPIO_OUTPUT_INACTIVE);
68 }
69
70 /* Buttons configuration & setting */
71
72 k_work_init(&button_work, publish);
73
74 for (i = 0; i < ARRAY_SIZE(button_device); i++) {
75 if (!gpio_is_ready_dt(&button_device[i])) {
76 return;
77 }
78 gpio_pin_configure_dt(&button_device[i], GPIO_INPUT);
79 gpio_pin_interrupt_configure_dt(&button_device[i],
80 GPIO_INT_EDGE_TO_ACTIVE);
81 gpio_init_callback(&button_cb[i], button_pressed,
82 BIT(button_device[i].pin));
83 gpio_add_callback(button_device[i].port, &button_cb[i]);
84 }
85 }
86