summaryrefslogtreecommitdiff
path: root/src/resource/resource_led.c
blob: 9c40a5dc168fcfacd0415bb8ae2bdfa46d8c449c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Flora License, Version 1.1 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <glib.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <peripheral_io.h>
#include <time.h>
#include "log.h"
#include "resource/resource_led.h"
#include "resource/resource_PCA9685.h"
#include "config.h"

#define DEFAULT_BI_LED_RED 17
#define DEFAULT_BI_LED_GREEN 27

#define CONFIG_KEY_RPI_BI_LED_RED "bi.red"
#define CONFIG_KEY_RPI_BI_LED_GREEN "bi.green"
#define CONFIG_KEY_RPI_USE_BI_LED "bi.use"

#define CONFIG_KEY_RPI_RGB_GPIO_R "rgb.gpio.red"
#define CONFIG_KEY_RPI_RGB_GPIO_G "rgb.gpio.green"
#define CONFIG_KEY_RPI_RGB_GPIO_B "rgb.gpio.blue"
#define CONFIG_KEY_RPI_USE_GPIO_RGB "rgb.gpio.use"

#define CONFIG_KEY_RPI_RGB_L2C_R "rgb.l2c.red"
#define CONFIG_KEY_RPI_RGB_L2C_G "rgb.l2c.green"
#define CONFIG_KEY_RPI_RGB_L2C_B "rgb.l2c.blue"
#define CONFIG_KEY_RPI_USE_L2C_RGB "rgb.l2c.use"


#define DEFAULT_RGB_GPIO_R 23
#define DEFAULT_RGB_GPIO_G 24
#define DEFAULT_RGB_GPIO_B 25

#define DEFAULT_RGB_L2C_R 8
#define DEFAULT_RGB_L2C_G 9
#define DEFAULT_RGB_L2C_B 10

#define CONFIG_GRP_RPI "Rpi.led"


typedef struct _led_s {
	peripheral_gpio_h gpio_bi_led[2];
	peripheral_gpio_h rgb_gpio[3];
	bi_led_color_e current_color;


	bool current_rgb_gpio_color[3];
	int current_rgb_l2c_color[3];

	int use_bi_led;
	int use_rgb_gpio;
	int use_rgb_l2c;
} led_s;

static led_s s_info = {
	.current_color = LED_COLOR_NONE,
	.current_rgb_gpio_color = { 0, },
	.current_rgb_l2c_color = { 0, },
	0, };

#define  CHECK_GPIO_ERROR(pin, ret) \
	if (ret != PERIPHERAL_ERROR_NONE) { \
		peripheral_gpio_close(pin); \
		_E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
		return ; \
	}

#define RGB_TO_REGISTER(val) ((unsigned)((float)val / 255.0f * 4095u))

static inline void _led_bi_set(bi_led_color_e color)
{
	int red;
	int green;

	if (!s_info.use_bi_led) {
		_D("Bi led is turned OFF");
		return;
	}

	switch (color) {
		case LED_COLOR_RED:
			red = 1;
			green = 0;
			break;
		case LED_COLOR_GREEN:
			red = 0;
			green = 1;
			break;
		default:
			red = 0;
			green = 0;
			break;
	}

	int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	_D("BI: [%d, %d]", red, green);
}

static inline void _rgb_gpio_set(bool red, bool green, bool blue)
{
	if (!s_info.use_rgb_gpio) {
		_D("gpio rgb led is turned OFF");
		return;
	}

	_D("RGB GPIO: [%d, %d, %d]", red, green, blue);

	int ret = peripheral_gpio_write(s_info.rgb_gpio[0], red);
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	ret = peripheral_gpio_write(s_info.rgb_gpio[1], green);
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	ret = peripheral_gpio_write(s_info.rgb_gpio[2], blue);
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
}

static inline void _rgb_l2c_set(int red, int green, int blue)
{
	if (!s_info.use_rgb_l2c) {
		_D("l2c rgb led is turned OFF");
		return;
	}

	int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_R, 0, RGB_TO_REGISTER(red));
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_G, 0, RGB_TO_REGISTER(green));
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_B, 0, RGB_TO_REGISTER(blue));
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);

	_D("RGB L2C: [%d, %d, %d] -> [%d, %d, %d]", red, green, blue, RGB_TO_REGISTER(red), RGB_TO_REGISTER(green), RGB_TO_REGISTER(blue));
}

static gboolean _restore_bi_color_cb(gpointer data)
{
	_led_bi_set(s_info.current_color);
	return false;
}

static gboolean _restore_rgb_gpio_color_cb(gpointer data)
{
	_rgb_gpio_set(s_info.current_rgb_gpio_color[0], s_info.current_rgb_gpio_color[1], s_info.current_rgb_gpio_color[2]);
	return false;
}

static gboolean _restore_rgb_l2c_color_cb(gpointer data)
{
	_rgb_l2c_set(s_info.current_rgb_l2c_color[0], s_info.current_rgb_l2c_color[1], s_info.current_rgb_l2c_color[2]);
	return false;
}

static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
{
	peripheral_gpio_h gpio;

	int pin = 0;
	bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);

	_D("Initializing gpio: %d", pin);

	if (modified) {
		config_save();
	}

	int ret = peripheral_gpio_open(pin, &gpio);
	retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);

	ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
	retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, 0);

	return gpio;

}

static void _init_bi_led(void)
{
	bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
	if (modified) {
		config_save();
	}

	if (s_info.use_bi_led) {
		s_info.gpio_bi_led[LED_COLOR_RED] = _init_gpio(DEFAULT_BI_LED_RED, CONFIG_KEY_RPI_BI_LED_RED);
		s_info.gpio_bi_led[LED_COLOR_GREEN] = _init_gpio(DEFAULT_BI_LED_GREEN, CONFIG_KEY_RPI_BI_LED_GREEN);
	} else {
		_D("BI-Led is turned OFF");
	}
}

static void _init_rgb_gpio_led(void)
{
	bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_GPIO_RGB, 1, &s_info.use_rgb_gpio);
	if (modified) {
		config_save();
	}

	if (!s_info.use_rgb_gpio) {
		_D("RGB GPIO is turned off");
		return;
	}

	s_info.rgb_gpio[0] = _init_gpio(DEFAULT_RGB_GPIO_R, CONFIG_KEY_RPI_RGB_GPIO_R);
	s_info.rgb_gpio[1] = _init_gpio(DEFAULT_RGB_GPIO_G, CONFIG_KEY_RPI_RGB_GPIO_G);
	s_info.rgb_gpio[2] = _init_gpio(DEFAULT_RGB_GPIO_B, CONFIG_KEY_RPI_RGB_GPIO_B);
}

void _init_l2c(int default_channel, char *config_key)
{
	int channel;
	bool modified = config_get_int_with_default(CONFIG_GRP_RPI, config_key, default_channel, &channel);
	if (modified) {
		config_save();
	}

	int ret = resource_pca9685_init(channel, 60);
	ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
}

static void _init_rgb_l2c_led(void)
{
	bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_L2C_RGB, 1, &s_info.use_rgb_l2c);
	if (modified) {
		config_save();
	}

	if (!s_info.use_rgb_l2c) {
		_D("RGB L2C is turned off");
		return;
	}

	_init_l2c(DEFAULT_RGB_L2C_R, CONFIG_KEY_RPI_RGB_L2C_R);
	_init_l2c(DEFAULT_RGB_L2C_G, CONFIG_KEY_RPI_RGB_L2C_G);
	_init_l2c(DEFAULT_RGB_L2C_B, CONFIG_KEY_RPI_RGB_L2C_B);
}

void resource_led_init(void)
{
	_D("Initialize Led");
	_init_rgb_gpio_led();
	_init_bi_led();
	_init_rgb_l2c_led();
}

void resource_led_destroy(void)
{
	peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
	peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);

	peripheral_gpio_close(s_info.rgb_gpio[0]);
	peripheral_gpio_close(s_info.rgb_gpio[1]);
	peripheral_gpio_close(s_info.rgb_gpio[2]);

	resource_rgb_l2c_set(0, 0, 0);

	resource_pca9685_fini(DEFAULT_RGB_L2C_R);
	resource_pca9685_fini(DEFAULT_RGB_L2C_G);
	resource_pca9685_fini(DEFAULT_RGB_L2C_B);
}

void resource_bi_led_set(bi_led_color_e color)
{
	_D("Set led to: %d", color);
	if (color == s_info.current_color) {
		return;
	}

	s_info.current_color = color;
	_led_bi_set(color);
}

void resource_bi_led_blink(bi_led_color_e color, unsigned timeout)
{
	if (color == s_info.current_color) {
		return;
	}

	_led_bi_set(color);
	g_timeout_add(timeout, _restore_bi_color_cb, NULL);
}

void resource_rgb_gpio_set(bool red, bool green, bool blue)
{
	if (red == s_info.current_rgb_gpio_color[0] &&
			green == s_info.current_rgb_gpio_color[1] &&
			blue == s_info.current_rgb_gpio_color[2]) {
		return;
	}


	s_info.current_rgb_gpio_color[0] = red;
	s_info.current_rgb_gpio_color[1] = green;
	s_info.current_rgb_gpio_color[2] = blue;

	_rgb_gpio_set(red, green, blue);
}

void resource_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout)
{
	if (red == s_info.current_rgb_gpio_color[0] &&
			green == s_info.current_rgb_gpio_color[1] &&
			blue == s_info.current_rgb_gpio_color[2]) {
		return;
	}

	_rgb_gpio_set(red, green, blue);
	g_timeout_add(timeout, _restore_rgb_gpio_color_cb, NULL);
}

void resource_rgb_l2c_set(int red, int green, int blue)
{
	if (red == s_info.current_rgb_l2c_color[0] &&
			green == s_info.current_rgb_l2c_color[1] &&
			blue == s_info.current_rgb_l2c_color[2]) {
		return;
	}

	s_info.current_rgb_l2c_color[0] = red;
	s_info.current_rgb_l2c_color[1] = green;
	s_info.current_rgb_l2c_color[2] = blue;

	_rgb_l2c_set(red, green, blue);
}

void resource_rgb_l2c_blink(int red, int green, int blue, unsigned timeout)
{
	if (red == s_info.current_rgb_l2c_color[0] &&
			green == s_info.current_rgb_l2c_color[1] &&
			blue == s_info.current_rgb_l2c_color[2]) {
		return;
	}

	_rgb_l2c_set(red, green, blue);
	g_timeout_add(timeout, _restore_rgb_l2c_color_cb, NULL);
}