ac63dfd222073b5c91685446b1a5d0539fb042ee
[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 "resource/resource_PCA9685.h"
26 #include "config.h"
27
28 #define DEFAULT_BI_LED_RED 17
29 #define DEFAULT_BI_LED_GREEN 27
30 #define CONFIG_KEY_RPI_BI_LED_RED "bi.red"
31 #define CONFIG_KEY_RPI_BI_LED_GREEN "bi.green"
32 #define CONFIG_KEY_RPI_USE_BI_LED "bi.use"
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 DEFAULT_RGB_PCA_R 4
42 #define DEFAULT_RGB_PCA_G 5
43 #define DEFAULT_RGB_PCA_B 10
44
45 #define CONFIG_GRP_RPI "Rpi.led"
46
47
48 typedef struct _led_s {
49         peripheral_gpio_h gpio_bi_led[2];
50         peripheral_pwm_h pwn_rgb_led[3];
51         led_color_e current_color;
52
53         int use_bi_led;
54         int use_rgb_led;
55 } led_s;
56
57 static led_s s_info = {
58         .current_color = LED_COLOR_NONE,
59         0, };
60
61 #define  CHECK_GPIO_ERROR(pin, ret) \
62         if (ret != PERIPHERAL_ERROR_NONE) { \
63                 peripheral_gpio_close(pin); \
64                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
65                 return ; \
66         }
67
68
69 static inline void _led_set(led_color_e color)
70 {
71         int red;
72         int green;
73
74         if (!s_info.use_bi_led) {
75                 _D("Bi led is turned OFF");
76                 return;
77         }
78
79         switch (color) {
80                 case LED_COLOR_RED:
81                         red = 1;
82                         green = 0;
83                         break;
84                 case LED_COLOR_GREEN:
85                         red = 0;
86                         green = 1;
87                         break;
88                 default:
89                         red = 0;
90                         green = 0;
91                         break;
92         }
93
94         _D("Colors: %d %d", red, green);
95
96         int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
97         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
98
99         peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
100         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
101 }
102
103 static gboolean _restore_color_cb(gpointer data)
104 {
105         _led_set(s_info.current_color);
106         return false;
107 }
108
109 static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
110 {
111         peripheral_gpio_h gpio;
112         int ret = PERIPHERAL_ERROR_NONE;
113         int pin = 0;
114         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
115
116         _D("gpio: %d", pin);
117
118         if (modified) {
119                 config_save();
120         }
121
122         peripheral_gpio_open(pin, &gpio);
123         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
124
125         peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
126         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);
127
128         return gpio;
129
130 }
131
132 //static peripheral_pwm_h _init_pwm(int default_pwm_pin, char *key)
133 //{
134 //      peripheral_pwm_h pwm;
135 //      int pin;
136 //
137 //      bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_pwm_pin, &pin);
138 //      _D("PWN: %d, DUTY: %u, period: %u", pin, (unsigned)2e6, (unsigned)2e7);
139 //
140 //      if (modified) {
141 //              config_save();
142 //      }
143 //
144 //      int ret = peripheral_pwm_open(1, pin, &pwm);
145 //      retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
146 //
147 //      ret = peripheral_pwm_set_period(pwm, (unsigned)2e7);
148 //      retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
149 //
150 //      ret = peripheral_pwm_set_duty_cycle(pwm, (unsigned)2e6);
151 //      retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
152 //
153 //      ret = peripheral_pwm_set_enabled(pwm, true);
154 //      retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
155 //
156 //
157 //
158 //      return pwm;
159 //}
160
161 //static void _init_rgb_led(void)
162 //{
163 //      bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_RGB_LED, 1, &s_info.use_rgb_led);
164 //      if (modified) {
165 //              config_save();
166 //      }
167 //
168 //      if (s_info.use_rgb_led) {
169 //              s_info.pwn_rgb_led[LED_COLOR_RED] = _init_pwm(DEFAULT_RGB_LED_RED, CONFIG_KEY_RPI_RGB_LED_RED);
170 //              s_info.pwn_rgb_led[LED_COLOR_GREEN] = _init_pwm(DEFAULT_RGB_LED_GREEN, CONFIG_KEY_RPI_RGB_LED_GREEN);
171 //              s_info.pwn_rgb_led[LED_COLOR_BLUE] = _init_pwm(DEFAULT_RGB_LED_BLUE, CONFIG_KEY_RPI_RGB_LED_BLUE);
172 //
173 //
174 //              peripheral_pwm_set_duty_cycle(s_info.pwn_rgb_led[LED_COLOR_RED], (unsigned)1e7);
175 //      }
176 //}
177
178 static void _init_bi_led(void)
179 {
180         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
181         if (modified) {
182                 config_save();
183         }
184
185         if (s_info.use_bi_led) {
186                 s_info.gpio_bi_led[LED_COLOR_RED] = _init_gpio(DEFAULT_BI_LED_RED, CONFIG_KEY_RPI_BI_LED_RED);
187                 s_info.gpio_bi_led[LED_COLOR_GREEN] = _init_gpio(DEFAULT_BI_LED_GREEN, CONFIG_KEY_RPI_BI_LED_GREEN);
188         } else {
189                 _D("BI-Led is turned OFF");
190         }
191 }
192
193 static void _init_pca(void)
194 {
195         int ret;
196 //      int ret = resource_pca9685_init(DEFAULT_RGB_PCA_R, 1000);
197 //      ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
198 ////
199 //      ret = resource_pca9685_init(DEFAULT_RGB_PCA_G, 1000);
200 //      ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
201 //
202 //      ret = resource_pca9685_init(DEFAULT_RGB_PCA_B, 1000);
203 //      ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
204 //
205         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_R, 0, 0);
206         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
207
208         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_G, 0, 255);
209         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
210
211         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_B, 0, 0);
212         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
213
214 //      peripheral_i2c_h g_i2c_h;
215 //      ret = peripheral_i2c_open(1, 0x40, &g_i2c_h);
216 //      if (ret != PERIPHERAL_ERROR_NONE) {
217 //              _E("failed to open pca9685-[bus:%d, addr:%d]", RPI3_I2C_BUS, PCA9685_ADDRESS);
218 //              return;
219 //      }
220 }
221
222 void resource_led_init(void)
223 {
224         _D("Initialize Led");
225         _init_bi_led();
226 //      _init_rgb_led();
227         _init_pca();
228 }
229
230 void resource_led_destroy(void)
231 {
232         peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
233         peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
234
235         resource_pca9685_fini(DEFAULT_RGB_PCA_R);
236         resource_pca9685_fini(DEFAULT_RGB_PCA_G);
237         resource_pca9685_fini(DEFAULT_RGB_PCA_B);
238 }
239
240 void resource_led_set(led_color_e color)
241 {
242         _D("Set led to: %d", color);
243         if (color == s_info.current_color) {
244                 return;
245         }
246
247         s_info.current_color = color;
248         _led_set(color);
249
250
251         int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_R, 0, 0);
252         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
253
254         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_G, 0, 255);
255         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
256
257         ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_B, 0, 0);
258         ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
259 }
260
261 void resource_led_blink(led_color_e color, unsigned timeout)
262 {
263         if (color == s_info.current_color) {
264                 return;
265         }
266
267         _led_set(color);
268         g_timeout_add(timeout, _restore_color_cb, NULL);
269 }
270