d0605e663ffebf2d78b6f3eb277c7c7f4cb0df8f
[apps/native/st-things-light.git] / src / resource / resource_infrared_motion_sensor.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
19 #include "log.h"
20
21 static peripheral_gpio_h g_sensor_h = NULL;
22 static int g_pin_num = -1;
23
24 void resource_close_infrared_motion_sensor(void)
25 {
26         if (!g_sensor_h) return;
27
28         _I("Infrared Motion Sensor is finishing...");
29
30         peripheral_gpio_close(g_sensor_h);
31
32         g_sensor_h = NULL;
33         g_pin_num = -1;
34 }
35
36 int resource_read_infrared_motion_sensor(int pin_num, uint32_t *out_value)
37 {
38         int ret = PERIPHERAL_ERROR_NONE;
39
40         if (!g_sensor_h) {
41                 peripheral_gpio_h temp = NULL;
42
43                 ret = peripheral_gpio_open(pin_num, &temp);
44                 retv_if(ret, -1);
45
46                 ret = peripheral_gpio_set_direction(temp, PERIPHERAL_GPIO_DIRECTION_IN);
47                 if (ret) {
48                         peripheral_gpio_close(temp);
49                         _E("peripheral_gpio_set_direction failed.");
50                         return -1;
51                 }
52
53                 g_sensor_h = temp;
54                 g_pin_num = pin_num;
55         }
56
57         if (g_pin_num != pin_num) {
58                 _E("Invalid pin number.");
59                 return -1;
60         }
61
62         ret = peripheral_gpio_read(g_sensor_h, out_value);
63         retv_if(ret < 0, -1);
64
65         return 0;
66 }