2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <peripheral_io.h>
18 #include "resource/resource_led.h"
22 LED_HANDLE_ERROR_NONE = 0, /**< Successful */
23 LED_HANDLE_ERROR_NOT_OPEN,
24 LED_HANDLE_ERROR_INVALID_PIN
27 static peripheral_gpio_h g_sensor_h = NULL;
28 static int g_pin_num = -1;
30 int _resource_validate_led(int pin_num)
32 int ret = LED_HANDLE_ERROR_NONE;
36 ret = LED_HANDLE_ERROR_NOT_OPEN;
37 } else if (g_pin_num != pin_num) {
38 ret = LED_HANDLE_ERROR_INVALID_PIN;
44 int resource_open_led(int pin_num)
46 int ret = PERIPHERAL_ERROR_NONE;
47 peripheral_gpio_h temp = NULL;
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.");
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.");
69 void resource_close_led(void)
71 if (!g_sensor_h) return;
73 _I("LED is finishing...");
75 peripheral_gpio_close(g_sensor_h);
81 int resource_write_led(int pin_num, int write_value)
83 int ret = PERIPHERAL_ERROR_NONE;
84 int ret_valid = LED_HANDLE_ERROR_NONE;
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.");
97 ret = peripheral_gpio_write(g_sensor_h, write_value);