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