Fix in led colors when a new lap event is detected
[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 <stdlib.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <peripheral_io.h>
23 #include <time.h>
24 #include "log.h"
25 #include "resource/resource_led.h"
26 #include "resource/resource_PCA9685.h"
27 #include "config.h"
28
29 #define DEFAULT_BI_LED_RED 17
30 #define DEFAULT_BI_LED_GREEN 27
31
32 #define CONFIG_KEY_RPI_BI_LED_RED "bi.red"
33 #define CONFIG_KEY_RPI_BI_LED_GREEN "bi.green"
34 #define CONFIG_KEY_RPI_USE_BI_LED "bi.use"
35
36 #define CONFIG_KEY_RPI_RGB_GPIO_R "rgb.gpio.red"
37 #define CONFIG_KEY_RPI_RGB_GPIO_G "rgb.gpio.green"
38 #define CONFIG_KEY_RPI_RGB_GPIO_B "rgb.gpio.blue"
39 #define CONFIG_KEY_RPI_USE_GPIO_RGB "rgb.gpio.use"
40
41 #define CONFIG_KEY_RPI_RGB_L2C_R "rgb.l2c.red"
42 #define CONFIG_KEY_RPI_RGB_L2C_G "rgb.l2c.green"
43 #define CONFIG_KEY_RPI_RGB_L2C_B "rgb.l2c.blue"
44 #define CONFIG_KEY_RPI_USE_L2C_RGB "rgb.l2c.use"
45
46
47 #define DEFAULT_RGB_GPIO_R 23
48 #define DEFAULT_RGB_GPIO_G 24
49 #define DEFAULT_RGB_GPIO_B 25
50
51 #define DEFAULT_RGB_L2C_R 8
52 #define DEFAULT_RGB_L2C_G 9
53 #define DEFAULT_RGB_L2C_B 10
54
55 #define CONFIG_GRP_RPI "Rpi.led"
56
57
58 typedef struct _led_s {
59         peripheral_gpio_h gpio_bi_led[2];
60         peripheral_gpio_h rgb_gpio[3];
61         bi_led_color_e current_color;
62
63
64         bool current_rgb_gpio_color[3];
65         int current_rgb_l2c_color[3];
66
67         int use_bi_led;
68         int use_rgb_gpio;
69         int use_rgb_l2c;
70 } led_s;
71
72 static led_s s_info = {
73         .current_color = LED_COLOR_NONE,
74         .current_rgb_gpio_color = { 0, },
75         .current_rgb_l2c_color = { 0, },
76         0, };
77
78 #define  CHECK_GPIO_ERROR(pin, ret) \
79         if (ret != PERIPHERAL_ERROR_NONE) { \
80                 peripheral_gpio_close(pin); \
81                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
82                 return ; \
83         }
84
85 #define RGB_TO_REGISTER(val) ((unsigned)((float)val / 255.0f * 4095u))
86
87 static inline void _led_bi_set(bi_led_color_e color, bool store_current_color)
88 {
89         int red;
90         int green;
91
92         if (!s_info.use_bi_led) {
93                 _D("Bi led is turned OFF");
94                 return;
95         }
96
97 //      if (color == s_info.current_color) {
98 //              _D("No change in bi led");
99 //              return;
100 //      }
101
102         if (store_current_color) {
103                 s_info.current_color = color;
104         }
105
106         switch (color) {
107                 case LED_COLOR_RED:
108                         red = 1;
109                         green = 0;
110                         break;
111                 case LED_COLOR_GREEN:
112                         red = 0;
113                         green = 1;
114                         break;
115                 default:
116                         red = 0;
117                         green = 0;
118                         break;
119         }
120
121         int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
122         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
123
124         ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
125         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
126
127         _D("BI: [%d, %d]", red, green);
128 }
129
130 static inline void _rgb_gpio_set(bool red, bool green, bool blue, bool store_current_color)
131 {
132         if (!s_info.use_rgb_gpio) {
133                 _D("gpio rgb led is turned OFF");
134                 return;
135         }
136
137 //      if (red == s_info.current_rgb_gpio_color[0] &&
138 //                      green == s_info.current_rgb_gpio_color[1] &&
139 //                      blue == s_info.current_rgb_gpio_color[2]) {
140 //              _D("No change in gpio led");
141 //              return;
142 //      }
143
144         if (store_current_color) {
145                 s_info.current_rgb_gpio_color[0] = red;
146                 s_info.current_rgb_gpio_color[1] = green;
147                 s_info.current_rgb_gpio_color[2] = blue;
148         }
149         _D("RGB GPIO: [%d, %d, %d]", red, green, blue);
150
151         int ret = peripheral_gpio_write(s_info.rgb_gpio[0], red);
152         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
153
154         ret = peripheral_gpio_write(s_info.rgb_gpio[1], green);
155         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
156
157         ret = peripheral_gpio_write(s_info.rgb_gpio[2], blue);
158         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
159 }
160
161 static inline void _rgb_l2c_set(int red, int green, int blue, bool store_current_color)
162 {
163         FUNCTION_START;
164         if (!s_info.use_rgb_l2c) {
165                 _D("l2c rgb led is turned OFF");
166                 return;
167         }
168
169 //      if (red == s_info.current_rgb_l2c_color[0] &&
170 //                      green == s_info.current_rgb_l2c_color[1] &&
171 //                      blue == s_info.current_rgb_l2c_color[2]) {
172 //              _D("No change in l2c led");
173 //              return;
174 //      }
175
176         if (store_current_color) {
177                 s_info.current_rgb_l2c_color[0] = red;
178                 s_info.current_rgb_l2c_color[1] = green;
179                 s_info.current_rgb_l2c_color[2] = blue;
180         }
181
182         int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_R, 0, RGB_TO_REGISTER(red));
183         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
184
185         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_G, 0, RGB_TO_REGISTER(green));
186         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
187
188         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_B, 0, RGB_TO_REGISTER(blue));
189         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
190
191         _D("RGB L2C: [%d, %d, %d] -> [%d, %d, %d]", red, green, blue, RGB_TO_REGISTER(red), RGB_TO_REGISTER(green), RGB_TO_REGISTER(blue));
192         FUNCTION_END;
193 }
194
195 static gboolean _restore_bi_color_cb(gpointer data)
196 {
197         _led_bi_set(s_info.current_color, false);
198         return false;
199 }
200
201 static gboolean _restore_rgb_gpio_color_cb(gpointer data)
202 {
203         _rgb_gpio_set(s_info.current_rgb_gpio_color[0], s_info.current_rgb_gpio_color[1], s_info.current_rgb_gpio_color[2], false);
204         return false;
205 }
206
207 static gboolean _restore_rgb_l2c_color_cb(gpointer data)
208 {
209         FUNCTION_START;
210         _rgb_l2c_set(s_info.current_rgb_l2c_color[0], s_info.current_rgb_l2c_color[1], s_info.current_rgb_l2c_color[2], false);
211         FUNCTION_END;
212         return false;
213 }
214
215 static gboolean _restore_rgb_color_cb(gpointer data)
216 {
217         FUNCTION_START;
218
219         _restore_bi_color_cb(data);
220         _restore_rgb_gpio_color_cb(data);
221         _restore_rgb_l2c_color_cb(data);
222
223         FUNCTION_END;
224
225         return false;
226 }
227
228 static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
229 {
230         peripheral_gpio_h gpio;
231
232         int pin = 0;
233         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
234
235         _D("Initializing gpio: %d", pin);
236
237         if (modified) {
238                 config_save();
239         }
240
241         int ret = peripheral_gpio_open(pin, &gpio);
242         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
243
244         ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
245         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
246
247         return gpio;
248
249 }
250
251 static void _init_bi_led(void)
252 {
253         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
254         if (modified) {
255                 config_save();
256         }
257
258         _D("Use bi_led: %d", s_info.use_bi_led);
259
260
261         if (s_info.use_bi_led) {
262                 s_info.gpio_bi_led[LED_COLOR_RED] = _init_gpio(DEFAULT_BI_LED_RED, CONFIG_KEY_RPI_BI_LED_RED);
263                 s_info.gpio_bi_led[LED_COLOR_GREEN] = _init_gpio(DEFAULT_BI_LED_GREEN, CONFIG_KEY_RPI_BI_LED_GREEN);
264         } else {
265                 _D("BI-Led is turned OFF");
266         }
267 }
268
269 static void _init_rgb_gpio_led(void)
270 {
271         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_GPIO_RGB, 1, &s_info.use_rgb_gpio);
272         if (modified) {
273                 config_save();
274         }
275
276         _D("Use rgb_gpio_led: %d", s_info.use_rgb_gpio);
277
278         if (!s_info.use_rgb_gpio) {
279                 _D("RGB GPIO is turned off");
280                 return;
281         }
282
283         s_info.rgb_gpio[0] = _init_gpio(DEFAULT_RGB_GPIO_R, CONFIG_KEY_RPI_RGB_GPIO_R);
284         s_info.rgb_gpio[1] = _init_gpio(DEFAULT_RGB_GPIO_G, CONFIG_KEY_RPI_RGB_GPIO_G);
285         s_info.rgb_gpio[2] = _init_gpio(DEFAULT_RGB_GPIO_B, CONFIG_KEY_RPI_RGB_GPIO_B);
286 }
287
288 void _init_l2c(int default_channel, char *config_key)
289 {
290         int channel;
291         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, config_key, default_channel, &channel);
292         if (modified) {
293                 config_save();
294         }
295
296         int ret = resource_pca9685_init(channel, 60);
297         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
298 }
299
300 static void _init_rgb_l2c_led(void)
301 {
302         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_L2C_RGB, 1, &s_info.use_rgb_l2c);
303         if (modified) {
304                 config_save();
305         }
306
307         _D("Use rgb_l2c_led: %d", s_info.use_rgb_l2c);
308
309         if (!s_info.use_rgb_l2c) {
310                 _D("RGB L2C is turned off");
311                 return;
312         }
313
314         _init_l2c(DEFAULT_RGB_L2C_R, CONFIG_KEY_RPI_RGB_L2C_R);
315         _init_l2c(DEFAULT_RGB_L2C_G, CONFIG_KEY_RPI_RGB_L2C_G);
316         _init_l2c(DEFAULT_RGB_L2C_B, CONFIG_KEY_RPI_RGB_L2C_B);
317 }
318
319 void resource_led_init(void)
320 {
321         FUNCTION_START;
322         _D("Initialize Led");
323         _init_rgb_gpio_led();
324         _init_bi_led();
325         _init_rgb_l2c_led();
326         FUNCTION_END;
327 }
328
329 void resource_led_destroy(void)
330 {
331         peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
332         peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
333
334         peripheral_gpio_close(s_info.rgb_gpio[0]);
335         peripheral_gpio_close(s_info.rgb_gpio[1]);
336         peripheral_gpio_close(s_info.rgb_gpio[2]);
337
338         _rgb_l2c_set(0, 0, 0, true);
339
340         resource_pca9685_fini(DEFAULT_RGB_L2C_R);
341         resource_pca9685_fini(DEFAULT_RGB_L2C_G);
342         resource_pca9685_fini(DEFAULT_RGB_L2C_B);
343 }
344
345 static void _set_rgb(char *key, bool store_current,
346                 int def_r_3bit, int def_g_3bit, int def_b_3bit,
347                 int def_r_24bit, int def_g_24bit, int def_b_24bit,
348                 bi_led_color_e bi_led)
349 {
350         int red, green, blue;
351         char final_key[PATH_MAX];
352
353         snprintf(final_key, PATH_MAX, "3bit.%s", key);
354         bool modified = config_get_rgb_with_default("Rpi.led", final_key, def_r_3bit, def_g_3bit, def_b_3bit, &red, &green, &blue);
355         _rgb_gpio_set(red, green, blue, store_current);
356
357         snprintf(final_key, PATH_MAX, "24bit.%s", key);
358         modified |= config_get_rgb_with_default("Rpi.led", final_key, def_r_24bit, def_g_24bit, def_b_24bit, &red, &green, &blue);
359         _rgb_l2c_set(red, green, blue, store_current);
360
361         int value;
362         snprintf(final_key, PATH_MAX, "bicolor.%s", key);
363         modified |= config_get_int_with_default("Rpi.led", final_key, bi_led, &value);
364         _led_bi_set(value, store_current);
365
366         if (modified) {
367                 config_save();
368         }
369 }
370
371 void resource_led_set_rgb_colors(char *key,
372                 int def_r_3bit, int def_g_3bit, int def_b_3bit,
373                 int def_r_24bit, int def_g_24bit, int def_b_24bit,
374                 bi_led_color_e bi_led)
375 {
376         FUNCTION_START;
377
378         _set_rgb(key, true, def_r_3bit, def_g_3bit, def_b_3bit, def_r_24bit, def_g_24bit, def_b_24bit, bi_led);
379
380         FUNCTION_END;
381 }
382
383 void resource_led_blink_rgb_colors(char *key,
384                 int def_r_3bit, int def_g_3bit, int def_b_3bit,
385                 int def_r_24bit, int def_g_24bit, int def_b_24bit,
386                 bi_led_color_e bi_led, int timeout)
387 {
388         FUNCTION_START;
389
390         _set_rgb(key, false, def_r_3bit, def_g_3bit, def_b_3bit, def_r_24bit, def_g_24bit, def_b_24bit, bi_led);
391         g_timeout_add(timeout, _restore_rgb_color_cb, NULL);
392
393
394         FUNCTION_END;
395 }