2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <peripheral_io.h>
24 #include "resource/resource_led.h"
27 #define DEFAULT_LED_RED 17
28 #define DEFAULT_LED_GREEN 27
29 #define CONFIG_KEY_RPI_PIN_LED_RED "bi.red"
30 #define CONFIG_KEY_RPI_PIN_LED_GREEN "bi.green"
31 #define CONFIG_KEY_RPI_USE_BI_LED "bi.use"
32 #define CONFIG_GRP_RPI "Rpi.led"
35 typedef struct _led_s {
36 peripheral_gpio_h gpio_bi_led[2];
37 led_color_e current_color;
42 static led_s s_info = {
43 .current_color = LED_COLOR_NONE,
46 #define CHECK_GPIO_ERROR(pin, ret) \
47 if (ret != PERIPHERAL_ERROR_NONE) { \
48 peripheral_gpio_close(pin); \
49 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
54 static inline void _led_set(led_color_e color)
59 if (!s_info.use_bi_led) {
60 _D("Bi led is turned OFF");
79 _D("Colors: %d %d", red, green);
81 int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
82 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
84 peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
85 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
88 static gboolean _restore_color_cb(gpointer data)
90 _led_set(s_info.current_color);
94 static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
96 peripheral_gpio_h gpio;
97 int ret = PERIPHERAL_ERROR_NONE;
99 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
107 peripheral_gpio_open(pin, &gpio);
108 retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
110 peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
111 retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
117 static void _init_bi_led(void)
119 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
121 if (s_info.use_bi_led) {
122 s_info.gpio_bi_led[LED_COLOR_RED] = _init_gpio(DEFAULT_LED_RED, CONFIG_KEY_RPI_PIN_LED_RED);
123 s_info.gpio_bi_led[LED_COLOR_GREEN] = _init_gpio(DEFAULT_LED_GREEN, CONFIG_KEY_RPI_PIN_LED_GREEN);
125 _D("BI-Led is turned OFF");
133 void resource_led_init(void)
135 _D("Initialize Led");
139 void resource_led_destroy(void)
141 peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
142 peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
145 void resource_led_set(led_color_e color)
147 _D("Set led to: %d", color);
148 if (color == s_info.current_color) {
152 s_info.current_color = color;
156 void resource_led_blink(led_color_e color, unsigned timeout)
158 if (color == s_info.current_color) {
163 g_timeout_add(timeout, _restore_color_cb, NULL);