Added two color led to indicate the app's status
[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_LED_RED 17
28 #define DEFAULT_LED_GREEN 27
29 #define CONFIG_KEY_RPI_PIN_LED_RED "led.red"
30 #define CONFIG_KEY_RPI_PIN_LED_GREEN "led.green"
31 #define CONFIG_GRP_RPI "Rpi"
32
33 typedef struct _led_s {
34         peripheral_gpio_h gpio_red;
35         peripheral_gpio_h gpio_green;
36         led_color_e current_color;
37 } led_s;
38
39 static led_s s_info = { 0, };
40
41 #define  CHECK_GPIO_ERROR(pin, ret) \
42         if (ret != PERIPHERAL_ERROR_NONE) { \
43                 peripheral_gpio_close(pin); \
44                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
45                 return ; \
46         }
47
48
49 static inline void _led_set(led_color_e color)
50 {
51         int red;
52         int green;
53
54
55         switch (color) {
56                 case LED_COLOR_RED:
57                         red = 1;
58                         green = 0;
59                         break;
60                 case LED_COLOR_GREEN:
61                         red = 0;
62                         green = 1;
63                         break;
64                 default:
65                         red = 0;
66                         green = 0;
67                         break;
68         }
69
70         int ret = peripheral_gpio_write(s_info.gpio_red, red);
71         CHECK_GPIO_ERROR(s_info.gpio_red, ret);
72
73         peripheral_gpio_write(s_info.gpio_green, green);
74         CHECK_GPIO_ERROR(s_info.gpio_green, ret);
75 }
76
77 static gboolean _restore_color_cb(gpointer data)
78 {
79         _led_set(s_info.current_color);
80         return false;
81 }
82
83 static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
84 {
85         peripheral_gpio_h gpio;
86 //      int ret = PERIPHERAL_ERROR_NONE;
87         int pin = 0;
88         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin);
89
90         _D("gpio: %d", pin);
91
92         if (modified) {
93                 config_save();
94         }
95
96         peripheral_gpio_open(pin, &gpio);
97 //      ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
98
99         peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
100 //      CHECK_GPIO_ERROR(gpio, ret);
101
102         return gpio;
103
104 }
105
106 void resource_led_init(void)
107 {
108         _D("Initialize Led");
109
110         s_info.gpio_red = _init_gpio(DEFAULT_LED_RED, CONFIG_KEY_RPI_PIN_LED_RED);
111         s_info.gpio_green = _init_gpio(DEFAULT_LED_GREEN, CONFIG_KEY_RPI_PIN_LED_GREEN);
112
113
114         int ret = peripheral_gpio_write(s_info.gpio_green, 1);
115         CHECK_GPIO_ERROR(s_info.gpio_green, ret);
116 }
117
118 void resource_led_destroy(void)
119 {
120         peripheral_gpio_close(s_info.gpio_red);
121         peripheral_gpio_close(s_info.gpio_green);
122 }
123
124 void resource_led_set(led_color_e color)
125 {
126         _D("Set led to: %d", color);
127         if (color == s_info.current_color) {
128                 return;
129         }
130
131         s_info.current_color = color;
132         _led_set(color);
133 }
134
135 void resource_led_blink(led_color_e color, unsigned timeout)
136 {
137         if (color == s_info.current_color) {
138                 return;
139         }
140
141         _led_set(color);
142         g_timeout_add(timeout, _restore_color_cb, NULL);
143 }
144