e54b43089c638cba20adf69307497452dcc27e3d
[apps/native/gear-racing-car.git] / src / resource / resource_led.c
1 /*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3 *
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
7 *
8 * http://floralicense.org/license/
9 *
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.
15 */
16
17 #include <glib.h>
18 #include <stdio.h>
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <peripheral_io.h>
22 #include <time.h>
23 #include "log.h"
24 #include "resource/resource_led.h"
25 #include "config.h"
26
27 #define DEFAULT_BI_LED_RED 17
28 #define DEFAULT_BI_LED_GREEN 27
29 #define CONFIG_KEY_RPI_BI_LED_RED "bi.red"
30 #define CONFIG_KEY_RPI_BI_LED_GREEN "bi.green"
31 #define CONFIG_KEY_RPI_USE_BI_LED "bi.use"
32
33 #define DEFAULT_RGB_LED_RED 12
34 #define DEFAULT_RGB_LED_GREEN 13
35 #define DEFAULT_RGB_LED_BLUE 24
36 #define CONFIG_KEY_RPI_RGB_LED_RED "rgb.red"
37 #define CONFIG_KEY_RPI_RGB_LED_GREEN "rgb.green"
38 #define CONFIG_KEY_RPI_RGB_LED_BLUE "rgb.blue"
39 #define CONFIG_KEY_RPI_USE_RGB_LED "rgb.use"
40
41 #define CONFIG_GRP_RPI "Rpi.led"
42
43
44 typedef struct _led_s {
45         peripheral_gpio_h gpio_bi_led[2];
46         peripheral_pwm_h pwn_rgb_led[3];
47         led_color_e current_color;
48
49         int use_bi_led;
50         int use_rgb_led;
51 } led_s;
52
53 static led_s s_info = {
54         .current_color = LED_COLOR_NONE,
55         0, };
56
57 #define  CHECK_GPIO_ERROR(pin, ret) \
58         if (ret != PERIPHERAL_ERROR_NONE) { \
59                 peripheral_gpio_close(pin); \
60                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
61                 return ; \
62         }
63
64
65 static inline void _led_set(led_color_e color)
66 {
67         int red;
68         int green;
69
70         if (!s_info.use_bi_led) {
71                 _D("Bi led is turned OFF");
72                 return;
73         }
74
75         switch (color) {
76                 case LED_COLOR_RED:
77                         red = 1;
78                         green = 0;
79                         break;
80                 case LED_COLOR_GREEN:
81                         red = 0;
82                         green = 1;
83                         break;
84                 default:
85                         red = 0;
86                         green = 0;
87                         break;
88         }
89
90         _D("Colors: %d %d", red, green);
91
92         int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
93         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
94
95         peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
96         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
97 }
98
99 static gboolean _restore_color_cb(gpointer data)
100 {
101         _led_set(s_info.current_color);
102         return false;
103 }
104
105 static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
106 {
107         peripheral_gpio_h gpio;
108         int ret = PERIPHERAL_ERROR_NONE;
109         int pin = 0;
110         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
111
112         _D("gpio: %d", pin);
113
114         if (modified) {
115                 config_save();
116         }
117
118         peripheral_gpio_open(pin, &gpio);
119         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
120
121         peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
122         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
123
124         return gpio;
125
126 }
127
128 static peripheral_pwm_h _init_pwm(int default_pwm_pin, char *key)
129 {
130         peripheral_pwm_h pwm;
131         int pin;
132
133         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_pwm_pin, &pin);
134         _D("PWN: %d, DUTY: %u, period: %u", pin, (unsigned)2e6, (unsigned)2e7);
135
136         if (modified) {
137                 config_save();
138         }
139
140         int ret = peripheral_pwm_open(1, pin, &pwm);
141         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
142
143         ret = peripheral_pwm_set_period(pwm, (unsigned)2e7);
144         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
145
146         ret = peripheral_pwm_set_duty_cycle(pwm, (unsigned)2e6);
147         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
148
149         ret = peripheral_pwm_set_enabled(pwm, true);
150         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
151
152
153
154         return pwm;
155 }
156
157 static void _init_rgb_led(void)
158 {
159         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_RGB_LED, 1, &s_info.use_rgb_led);
160         if (modified) {
161                 config_save();
162         }
163
164         if (s_info.use_rgb_led) {
165                 s_info.pwn_rgb_led[LED_COLOR_RED] = _init_pwm(DEFAULT_RGB_LED_RED, CONFIG_KEY_RPI_RGB_LED_RED);
166                 s_info.pwn_rgb_led[LED_COLOR_GREEN] = _init_pwm(DEFAULT_RGB_LED_GREEN, CONFIG_KEY_RPI_RGB_LED_GREEN);
167                 s_info.pwn_rgb_led[LED_COLOR_BLUE] = _init_pwm(DEFAULT_RGB_LED_BLUE, CONFIG_KEY_RPI_RGB_LED_BLUE);
168
169
170                 peripheral_pwm_set_duty_cycle(s_info.pwn_rgb_led[LED_COLOR_RED], (unsigned)1e7);
171         }
172 }
173
174 static void _init_bi_led(void)
175 {
176         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
177         if (modified) {
178                 config_save();
179         }
180
181         if (s_info.use_bi_led) {
182                 s_info.gpio_bi_led[LED_COLOR_RED] = _init_gpio(DEFAULT_BI_LED_RED, CONFIG_KEY_RPI_BI_LED_RED);
183                 s_info.gpio_bi_led[LED_COLOR_GREEN] = _init_gpio(DEFAULT_BI_LED_GREEN, CONFIG_KEY_RPI_BI_LED_GREEN);
184         } else {
185                 _D("BI-Led is turned OFF");
186         }
187 }
188
189 void resource_led_init(void)
190 {
191         _D("Initialize Led");
192         _init_bi_led();
193         _init_rgb_led();
194 }
195
196 void resource_led_destroy(void)
197 {
198         peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
199         peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
200 }
201
202 void resource_led_set(led_color_e color)
203 {
204         _D("Set led to: %d", color);
205         if (color == s_info.current_color) {
206                 return;
207         }
208
209         s_info.current_color = color;
210         _led_set(color);
211 }
212
213 void resource_led_blink(led_color_e color, unsigned timeout)
214 {
215         if (color == s_info.current_color) {
216                 return;
217         }
218
219         _led_set(color);
220         g_timeout_add(timeout, _restore_color_cb, NULL);
221 }
222