Init master branch with timer code
[apps/native/st-things-light.git] / src / resource / resource_led.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 <peripheral_io.h>
18 #include "resource/resource_led.h"
19 #include "log.h"
20
21 typedef enum {
22         LED_HANDLE_ERROR_NONE = 0,                /**< Successful */
23         LED_HANDLE_ERROR_NOT_OPEN,
24         LED_HANDLE_ERROR_INVALID_PIN
25 } led_handle_error_e;
26
27 static peripheral_gpio_h g_sensor_h = NULL;
28 static int g_pin_num = -1;
29
30 int _resource_validate_led(int pin_num)
31 {
32         int ret = LED_HANDLE_ERROR_NONE;
33
34         if (!g_sensor_h)
35         {
36                 ret = LED_HANDLE_ERROR_NOT_OPEN;
37         } else if (g_pin_num != pin_num) {
38                 ret = LED_HANDLE_ERROR_INVALID_PIN;
39         }
40
41         return ret;
42 }
43
44 int resource_open_led(int pin_num)
45 {
46         int ret = PERIPHERAL_ERROR_NONE;
47         peripheral_gpio_h temp = NULL;
48
49         ret = peripheral_gpio_open(pin_num, &temp);
50         if (ret != PERIPHERAL_ERROR_NONE) {
51                 peripheral_gpio_close(temp);
52                 _E("peripheral_gpio_open failed.");
53                 return -1;
54         }
55
56         ret = peripheral_gpio_set_direction(temp, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
57         if (ret != PERIPHERAL_ERROR_NONE) {
58                 peripheral_gpio_close(temp);
59                 _E("peripheral_gpio_set_direction failed.");
60                 return -1;
61         }
62
63         g_sensor_h = temp;
64         g_pin_num = pin_num;
65
66         return 0;
67 }
68
69 void resource_close_led(void)
70 {
71         if (!g_sensor_h) return;
72
73         _I("LED is finishing...");
74
75         peripheral_gpio_close(g_sensor_h);
76
77         g_sensor_h = NULL;
78         g_pin_num = -1;
79 }
80
81 int resource_write_led(int pin_num, int write_value)
82 {
83         int ret = PERIPHERAL_ERROR_NONE;
84         int ret_valid = LED_HANDLE_ERROR_NONE;
85
86         ret_valid = _resource_validate_led(pin_num);
87         if (ret_valid != LED_HANDLE_ERROR_NONE) {
88                 if (ret_valid == LED_HANDLE_ERROR_NOT_OPEN) {
89                         ret = resource_open_led(pin_num);
90                         retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
91                 } else if (ret_valid == LED_HANDLE_ERROR_INVALID_PIN) {
92                         _E("Invalid pin number.");
93                         return -1;
94                 }
95         }
96
97         ret = peripheral_gpio_write(g_sensor_h, write_value);
98         retv_if(ret < 0, -1);
99
100         return 0;
101 }