Remove the unused file and code
[apps/native/smart-ruler.git] / src / resource / resource_illuminance_sensor.c
1 /*
2  * Copyright (c) 2019 G.camp,
3  *
4  * Contact: Jin Seog Bang <seog814@gmail.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 #include "resource.h"
27
28 #define I2C_PIN_MAX 28
29 /* I2C */
30 #define LIDAR_V1_ADDRW 0x62
31 #define LIDAR_V1_ADDRR 0x63
32 #define LIDAR_CONSTANT_NUM (1.00)
33
34 static struct {
35         int opened;
36         peripheral_i2c_h sensor_h;
37 } resource_sensor_s;
38
39
40 void resource_close_initial_LIDAR_sensor(void)
41 {
42         if (!resource_sensor_s.opened)
43                 return;
44
45         _I("initial_LIDAR_Sensor is finishing...");
46         peripheral_i2c_close(resource_sensor_s.sensor_h);
47         resource_sensor_s.opened = 0;
48 }
49
50 void resource_close_two_read_set_LIDAR_sensor(void)
51 {
52         if (!resource_sensor_s.opened)
53                 return;
54
55         _I("2 byte read_set LIDAR_Sensor is finishing...");
56         peripheral_i2c_close(resource_sensor_s.sensor_h);
57         resource_sensor_s.opened = 0;
58 }
59
60 void resource_close_read_LIDAR_sensor(void)
61 {
62         if (!resource_sensor_s.opened)
63                 return;
64
65         _I("2 byte read_LIDAR_Sensor is finishing...");
66         peripheral_i2c_close(resource_sensor_s.sensor_h);
67         resource_sensor_s.opened = 0;
68 }
69
70 int resource_initial_LIDAR_sensor(int i2c_bus)
71 {
72         int ret = PERIPHERAL_ERROR_NONE;
73         static int write = 0;
74         unsigned char buf[10] = { 0, };
75         resource_write_led(5, 1); // debug led on
76         if (!resource_sensor_s.opened) {
77
78                 ret = peripheral_i2c_open(i2c_bus, LIDAR_V1_ADDRW, &resource_sensor_s.sensor_h);
79
80                 if (ret != PERIPHERAL_ERROR_NONE) {
81                         _E("i2c open error : %s", get_error_message(ret));
82
83                         return -1;
84                 }
85                 resource_sensor_s.opened = 1;
86                 write = 0;
87         }
88         
89         buf[0] = 0x00;
90         buf[1] = 0x04;
91         
92         if (!write) {
93                 ret = peripheral_i2c_write(resource_sensor_s.sensor_h, buf, 2);
94                 if (ret != PERIPHERAL_ERROR_NONE) {
95                         _E("i2c write error : %s", get_error_message(ret));
96                         return -1;
97                 }
98                 write = 0;
99         }
100         _I("iic LIDAR control reset ... step ");        
101
102         buf[0] = 0x00;
103         buf[1] = 0x03;
104         
105         if (!write) {
106                 ret = peripheral_i2c_write(resource_sensor_s.sensor_h, buf, 2);
107                 if (ret != PERIPHERAL_ERROR_NONE) {
108                         _E("i2c write error : %s", get_error_message(ret));
109                         return -1;
110                 }
111                 write = 0;
112         }
113         _I("iic LIDAR control reset ... step ");
114
115         return 0;
116 }
117
118 int resource_two_read_set_LIDAR_sensor(int i2c_bus)
119 {
120         int ret = PERIPHERAL_ERROR_NONE;
121         static int write = 0;
122         unsigned char buf[10] = { 0, }; 
123         
124         if (!resource_sensor_s.opened) {
125
126                 ret = peripheral_i2c_open(i2c_bus, LIDAR_V1_ADDRW, &resource_sensor_s.sensor_h);
127                 if (ret != PERIPHERAL_ERROR_NONE) {
128                         _E("i2c open error : %s", get_error_message(ret));
129
130                         return -1;
131                 }
132                 resource_sensor_s.opened = 1;
133                 write = 0;
134         }       
135
136         buf[0] = 0x8f;  
137
138         if (!write) {
139                 ret = peripheral_i2c_write(resource_sensor_s.sensor_h, buf, 1);
140                 if (ret != PERIPHERAL_ERROR_NONE) {
141                         _E("i2c write error : %s", get_error_message(ret));
142                         return -1;
143                 }
144                 write = 0;
145         }
146
147         _I("iic LIDAR control 2byte read set .. step ");        
148         
149         return 0;
150 }
151
152 int resource_read_LIDAR_sensor(int i2c_bus, uint32_t *out_value)
153 {
154         int ret = PERIPHERAL_ERROR_NONE;
155
156         unsigned char buf[10] = { 0, };
157
158         if (!resource_sensor_s.opened) {
159
160                 ret = peripheral_i2c_open(i2c_bus, LIDAR_V1_ADDRR, &resource_sensor_s.sensor_h);
161                 if (ret != PERIPHERAL_ERROR_NONE) {
162                         _E("i2c open error : %s", get_error_message(ret));
163                         return -1;
164                 }
165                 resource_sensor_s.opened = 1;
166         }
167
168         ret = peripheral_i2c_read(resource_sensor_s.sensor_h, buf, 2);
169
170         if (ret != PERIPHERAL_ERROR_NONE) {
171                 _E("i2c read error : %s", get_error_message(ret));
172
173                 return -1;
174         }
175
176         *out_value = (buf[0] << 8 | buf[1]) / LIDAR_CONSTANT_NUM;
177
178
179         resource_write_led(5, 0); // debug led off
180
181         return 0;
182 }