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.
22 #include <peripheral_io.h>
25 #include "resource/resource_led.h"
26 #include "resource/resource_PCA9685.h"
29 #define DEFAULT_BI_LED_RED 17
30 #define DEFAULT_BI_LED_GREEN 27
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"
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"
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"
47 #define DEFAULT_RGB_GPIO_R 23
48 #define DEFAULT_RGB_GPIO_G 24
49 #define DEFAULT_RGB_GPIO_B 25
51 #define DEFAULT_RGB_L2C_R 8
52 #define DEFAULT_RGB_L2C_G 9
53 #define DEFAULT_RGB_L2C_B 10
55 #define CONFIG_GRP_RPI "Rpi.led"
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;
64 bool current_rgb_gpio_color[3];
65 int current_rgb_l2c_color[3];
72 static led_s s_info = {
73 .current_color = LED_COLOR_NONE,
74 .current_rgb_gpio_color = { 0, },
75 .current_rgb_l2c_color = { 0, },
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__); \
85 #define RGB_TO_REGISTER(val) ((unsigned)((float)val / 255.0f * 4095u))
87 static inline void _led_bi_set(bi_led_color_e color)
92 if (!s_info.use_bi_led) {
93 _D("Bi led is turned OFF");
102 case LED_COLOR_GREEN:
112 int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
113 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
115 ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
116 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
118 _D("BI: [%d, %d]", red, green);
121 static inline void _rgb_gpio_set(bool red, bool green, bool blue)
123 if (!s_info.use_rgb_gpio) {
124 _D("gpio rgb led is turned OFF");
128 _D("RGB GPIO: [%d, %d, %d]", red, green, blue);
130 int ret = peripheral_gpio_write(s_info.rgb_gpio[0], red);
131 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
133 ret = peripheral_gpio_write(s_info.rgb_gpio[1], green);
134 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
136 ret = peripheral_gpio_write(s_info.rgb_gpio[2], blue);
137 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
140 static inline void _rgb_l2c_set(int red, int green, int blue)
142 if (!s_info.use_rgb_l2c) {
143 _D("l2c rgb led is turned OFF");
147 int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_R, 0, RGB_TO_REGISTER(red));
148 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
150 ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_G, 0, RGB_TO_REGISTER(green));
151 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
153 ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_B, 0, RGB_TO_REGISTER(blue));
154 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
156 _D("RGB L2C: [%d, %d, %d] -> [%d, %d, %d]", red, green, blue, RGB_TO_REGISTER(red), RGB_TO_REGISTER(green), RGB_TO_REGISTER(blue));
159 static gboolean _restore_bi_color_cb(gpointer data)
161 _led_bi_set(s_info.current_color);
165 static gboolean _restore_rgb_gpio_color_cb(gpointer data)
167 _rgb_gpio_set(s_info.current_rgb_gpio_color[0], s_info.current_rgb_gpio_color[1], s_info.current_rgb_gpio_color[2]);
171 static gboolean _restore_rgb_l2c_color_cb(gpointer data)
173 _rgb_l2c_set(s_info.current_rgb_l2c_color[0], s_info.current_rgb_l2c_color[1], s_info.current_rgb_l2c_color[2]);
177 static gboolean _restore_rgb_color_cb(gpointer data)
179 _restore_bi_color_cb(data);
180 _restore_rgb_gpio_color_cb(data);
181 _restore_rgb_l2c_color_cb(data);
186 static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
188 peripheral_gpio_h gpio;
191 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
193 _D("Initializing gpio: %d", pin);
199 int ret = peripheral_gpio_open(pin, &gpio);
200 retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
202 ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
203 retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
209 static void _init_bi_led(void)
211 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
216 if (s_info.use_bi_led) {
217 s_info.gpio_bi_led[LED_COLOR_RED] = _init_gpio(DEFAULT_BI_LED_RED, CONFIG_KEY_RPI_BI_LED_RED);
218 s_info.gpio_bi_led[LED_COLOR_GREEN] = _init_gpio(DEFAULT_BI_LED_GREEN, CONFIG_KEY_RPI_BI_LED_GREEN);
220 _D("BI-Led is turned OFF");
224 static void _init_rgb_gpio_led(void)
226 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_GPIO_RGB, 1, &s_info.use_rgb_gpio);
231 if (!s_info.use_rgb_gpio) {
232 _D("RGB GPIO is turned off");
236 s_info.rgb_gpio[0] = _init_gpio(DEFAULT_RGB_GPIO_R, CONFIG_KEY_RPI_RGB_GPIO_R);
237 s_info.rgb_gpio[1] = _init_gpio(DEFAULT_RGB_GPIO_G, CONFIG_KEY_RPI_RGB_GPIO_G);
238 s_info.rgb_gpio[2] = _init_gpio(DEFAULT_RGB_GPIO_B, CONFIG_KEY_RPI_RGB_GPIO_B);
241 void _init_l2c(int default_channel, char *config_key)
244 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, config_key, default_channel, &channel);
249 int ret = resource_pca9685_init(channel, 60);
250 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
253 static void _init_rgb_l2c_led(void)
255 bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_L2C_RGB, 1, &s_info.use_rgb_l2c);
260 if (!s_info.use_rgb_l2c) {
261 _D("RGB L2C is turned off");
265 _init_l2c(DEFAULT_RGB_L2C_R, CONFIG_KEY_RPI_RGB_L2C_R);
266 _init_l2c(DEFAULT_RGB_L2C_G, CONFIG_KEY_RPI_RGB_L2C_G);
267 _init_l2c(DEFAULT_RGB_L2C_B, CONFIG_KEY_RPI_RGB_L2C_B);
270 void resource_led_init(void)
272 _D("Initialize Led");
273 _init_rgb_gpio_led();
278 void resource_led_destroy(void)
280 peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
281 peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
283 peripheral_gpio_close(s_info.rgb_gpio[0]);
284 peripheral_gpio_close(s_info.rgb_gpio[1]);
285 peripheral_gpio_close(s_info.rgb_gpio[2]);
287 resource_led_rgb_l2c_set(0, 0, 0);
289 resource_pca9685_fini(DEFAULT_RGB_L2C_R);
290 resource_pca9685_fini(DEFAULT_RGB_L2C_G);
291 resource_pca9685_fini(DEFAULT_RGB_L2C_B);
294 void resource_led_bi_set(bi_led_color_e color)
296 _D("Set led to: %d", color);
297 if (color == s_info.current_color) {
301 s_info.current_color = color;
305 void resource_led_bi_blink(bi_led_color_e color, unsigned timeout)
307 if (color == s_info.current_color) {
312 g_timeout_add(timeout, _restore_bi_color_cb, NULL);
315 void resource_led_rgb_gpio_set(bool red, bool green, bool blue)
317 if (red == s_info.current_rgb_gpio_color[0] &&
318 green == s_info.current_rgb_gpio_color[1] &&
319 blue == s_info.current_rgb_gpio_color[2]) {
324 s_info.current_rgb_gpio_color[0] = red;
325 s_info.current_rgb_gpio_color[1] = green;
326 s_info.current_rgb_gpio_color[2] = blue;
328 _rgb_gpio_set(red, green, blue);
331 void resource_led_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout)
333 if (red == s_info.current_rgb_gpio_color[0] &&
334 green == s_info.current_rgb_gpio_color[1] &&
335 blue == s_info.current_rgb_gpio_color[2]) {
339 _rgb_gpio_set(red, green, blue);
340 g_timeout_add(timeout, _restore_rgb_gpio_color_cb, NULL);
343 void resource_led_rgb_l2c_set(int red, int green, int blue)
345 if (red == s_info.current_rgb_l2c_color[0] &&
346 green == s_info.current_rgb_l2c_color[1] &&
347 blue == s_info.current_rgb_l2c_color[2]) {
351 s_info.current_rgb_l2c_color[0] = red;
352 s_info.current_rgb_l2c_color[1] = green;
353 s_info.current_rgb_l2c_color[2] = blue;
355 _rgb_l2c_set(red, green, blue);
358 void resource_led_rgb_l2c_blink(int red, int green, int blue, unsigned timeout)
360 if (red == s_info.current_rgb_l2c_color[0] &&
361 green == s_info.current_rgb_l2c_color[1] &&
362 blue == s_info.current_rgb_l2c_color[2]) {
366 _rgb_l2c_set(red, green, blue);
367 g_timeout_add(timeout, _restore_rgb_l2c_color_cb, NULL);
370 void resource_led_set_rgb_colors(char *key,
371 int def_r_3bit, int def_g_3bit, int def_b_3bit,
372 int def_r_24bit, int def_g_24bit, int def_b_24bit,
373 bi_led_color_e bi_led)
375 int red, green, blue;
376 char final_key[PATH_MAX];
378 snprintf(final_key, PATH_MAX, "3bit.%s", key);
379 bool modified = config_get_rgb_with_default("Rpi.led", final_key, def_r_3bit, def_g_3bit, def_b_3bit, &red, &green, &blue);
380 resource_led_rgb_gpio_set(red, green, blue);
382 _D("%s: 3bit color: [%d, %d, %d]", final_key, red, green, blue);
384 snprintf(final_key, PATH_MAX, "24bit.%s", key);
385 modified |= config_get_rgb_with_default("Rpi.led", final_key, def_r_24bit, def_g_24bit, def_b_24bit, &red, &green, &blue);
386 resource_led_rgb_l2c_set(red, green, blue);
388 _D("%s: 24bit color: [%d, %d, %d]", final_key, red, green, blue);
391 snprintf(final_key, PATH_MAX, "bicolor.%s", key);
392 modified |= config_get_int_with_default("Rpi.led", final_key, bi_led, &value);
393 resource_led_bi_set(value);
398 _D("%s: bicolor: [%d]", final_key, value);
401 void resource_led_blink_rgb_colors(char *key,
402 int def_r_3bit, int def_g_3bit, int def_b_3bit,
403 int def_r_24bit, int def_g_24bit, int def_b_24bit,
404 bi_led_color_e bi_led, int timeout)
406 resource_led_set_rgb_colors(key, def_r_3bit, def_g_3bit, def_b_3bit, def_r_24bit, def_g_24bit, def_b_24bit, bi_led);
408 g_timeout_add(timeout, _restore_rgb_color_cb, NULL);