Change peripheral io APIs
[apps/native/position-finder-server.git] / src / resource / resource_illuminance_sensor.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Flora License, Version 1.1 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://floralicense.org/license/
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <peripheral_io.h>
25 #include <sys/time.h>
26
27 #include "log.h"
28 #include "resource_internal.h"
29
30 #define I2C_PIN_MAX 28
31 /* I2C */
32 #define GY30_ADDR 0x23 /* Address of GY30 light sensor */
33 #define GY30_CONT_HIGH_RES_MODE 0x10 /* Start measurement at 11x resolution. Measurement time is approx 120mx */
34 #define GY30_CONSTANT_NUM (1.2)
35
36 static struct {
37         int opened;
38         peripheral_i2c_h sensor_h;
39 } resource_sensor_s;
40
41 void resource_close_illuminance_sensor(void)
42 {
43         if (!resource_sensor_s.opened) return;
44
45         _I("Illuminance Sensor is finishing...");
46         peripheral_i2c_close(resource_sensor_s.sensor_h);
47         resource_sensor_s.opened = 0;
48 }
49
50 int resource_read_illuminance_sensor(int i2c_bus, int *out_value)
51 {
52         int ret = PERIPHERAL_ERROR_NONE;
53         unsigned char buf[10] = { 0, };
54
55         if (!resource_sensor_s.opened) {
56                 ret = peripheral_i2c_open(i2c_bus, GY30_ADDR, &resource_sensor_s.sensor_h);
57                 retv_if(!resource_sensor_s.sensor_h, -1);
58                 resource_sensor_s.opened = 1;
59         }
60
61         buf[0] = GY30_CONT_HIGH_RES_MODE;
62         ret = peripheral_i2c_write(resource_sensor_s.sensor_h, buf, 1);
63         retv_if(ret < 0, -1);
64
65         ret = peripheral_i2c_read(resource_sensor_s.sensor_h, buf, 2);
66         retv_if(ret < 0, -1);
67
68         *out_value = (buf[0] << 8 | buf[1]) / GY30_CONSTANT_NUM; // Just Sum High 8bit and Low 8bit
69
70         _I("Illuminance Sensor Value : %d", *out_value);
71
72         return 0;
73 }