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 "led_svc.h"
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/logging/log.h>
16 
17 LOG_MODULE_REGISTER(led_svc);
18 
19 static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
20 static bool led_state; /* Tracking state here supports GPIO expander-based LEDs. */
21 static bool led_ok;
22 
led_update(void)23 void led_update(void)
24 {
25 	if (!led_ok) {
26 		return;
27 	}
28 
29 	led_state = !led_state;
30 	LOG_INF("Turn %s LED", led_state ? "on" : "off");
31 	gpio_pin_set(led.port, led.pin, led_state);
32 }
33 
led_init(void)34 int led_init(void)
35 {
36 	int ret;
37 
38 	led_ok = gpio_is_ready_dt(&led);
39 	if (!led_ok) {
40 		LOG_ERR("Error: LED on GPIO %s pin %d is not ready",
41 			led.port->name, led.pin);
42 		return -ENODEV;
43 	}
44 
45 	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE);
46 	if (ret < 0) {
47 		LOG_ERR("Error %d: failed to configure GPIO %s pin %d",
48 			ret, led.port->name, led.pin);
49 	}
50 
51 	return ret;
52 }
53