LibDriver OPT300X
Loading...
Searching...
No Matches
driver_opt300x_read_test.c
Go to the documentation of this file.
1
36
38
39static opt300x_handle_t gs_handle;
40
51uint8_t opt300x_read_test(opt300x_t type, opt300x_address_t addr_pin, uint32_t times)
52{
53 uint8_t res;
54 uint32_t i;
55 opt300x_info_t info;
56
57 /* link interface function */
66
67 /* get chip information */
68 res = opt300x_info(&info);
69 if (res != 0)
70 {
71 opt300x_interface_debug_print("opt300x: get info failed.\n");
72
73 return 1;
74 }
75 else
76 {
77 /* print chip information */
78 opt300x_interface_debug_print("opt300x: chip is %s.\n", info.chip_name);
79 opt300x_interface_debug_print("opt300x: manufacturer is %s.\n", info.manufacturer_name);
80 opt300x_interface_debug_print("opt300x: interface is %s.\n", info.interface);
81 opt300x_interface_debug_print("opt300x: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
82 opt300x_interface_debug_print("opt300x: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
83 opt300x_interface_debug_print("opt300x: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
84 opt300x_interface_debug_print("opt300x: max current is %0.2fmA.\n", info.max_current_ma);
85 opt300x_interface_debug_print("opt300x: max temperature is %0.1fC.\n", info.temperature_max);
86 opt300x_interface_debug_print("opt300x: min temperature is %0.1fC.\n", info.temperature_min);
87 }
88
89 /* start read test */
90 opt300x_interface_debug_print("opt300x: start read test.\n");
91
92 /* set chip type */
93 res = opt300x_set_type(&gs_handle, type);
94 if (res != 0)
95 {
96 opt300x_interface_debug_print("opt300x: set type failed.\n");
97
98 return 1;
99 }
100
101 /* set iic address */
102 res = opt300x_set_addr_pin(&gs_handle, addr_pin);
103 if (res != 0)
104 {
105 opt300x_interface_debug_print("opt300x: set addr pin failed.\n");
106
107 return 1;
108 }
109
110 /* opt300x init */
111 res = opt300x_init(&gs_handle);
112 if (res != 0)
113 {
114 opt300x_interface_debug_print("opt300x: init failed.\n");
115
116 return 1;
117 }
118
119 /* set auto range */
120 if (type == OPT3002)
121 {
122 res = opt3002_set_range(&gs_handle, OPT3002_RANGE_AUTO);
123 if (res != 0)
124 {
125 opt300x_interface_debug_print("opt3002: set range failed.\n");
126 (void)opt300x_deinit(&gs_handle);
127
128 return 1;
129 }
130 }
131 else if (type == OPT3005)
132 {
133 res = opt3005_set_range(&gs_handle, OPT3005_RANGE_AUTO);
134 if (res != 0)
135 {
136 opt300x_interface_debug_print("opt3005: set range failed.\n");
137 (void)opt300x_deinit(&gs_handle);
138
139 return 1;
140 }
141 }
142 else
143 {
144 res = opt300x_set_range(&gs_handle, OPT300X_RANGE_AUTO);
145 if (res != 0)
146 {
147 opt300x_interface_debug_print("opt300x: set range failed.\n");
148 (void)opt300x_deinit(&gs_handle);
149
150 return 1;
151 }
152 }
153
154 /* set conversion time 800ms */
156 if (res != 0)
157 {
158 opt300x_interface_debug_print("opt300x: set conversion time failed.\n");
159 (void)opt300x_deinit(&gs_handle);
160
161 return 1;
162 }
163
164 /* disable mask exponent */
166 if (res != 0)
167 {
168 opt300x_interface_debug_print("opt300x: set mask exponent failed.\n");
169 (void)opt300x_deinit(&gs_handle);
170
171 return 1;
172 }
173
174 /* set fault count one */
176 if (res != 0)
177 {
178 opt300x_interface_debug_print("opt300x: set fault count failed.\n");
179 (void)opt300x_deinit(&gs_handle);
180
181 return 1;
182 }
183
184 /* start continuous read */
185 res = opt300x_start_continuous_read(&gs_handle);
186 if (res != 0)
187 {
188 opt300x_interface_debug_print("opt300x: start continuous read failed.\n");
189 (void)opt300x_deinit(&gs_handle);
190
191 return 1;
192 }
193
194 /* continuous read test */
195 opt300x_interface_debug_print("opt300x: continuous read test.\n");
196
197 for (i = 0; i < times; i++)
198 {
199 uint16_t raw;
200 float lux;
201 float nw_cm2;
202
203 /* delay 1000ms */
205
206 if (type == OPT3002)
207 {
208 res = opt3002_continuous_read(&gs_handle, &raw, &nw_cm2);
209 if (res != 0)
210 {
211 opt300x_interface_debug_print("opt3002: continuous read failed.\n");
212 (void)opt300x_deinit(&gs_handle);
213
214 return 1;
215 }
216 opt300x_interface_debug_print("opt300x: %0.2f nW/cm2.\n", nw_cm2);
217 }
218 else
219 {
220 res = opt300x_continuous_read(&gs_handle, &raw, &lux);
221 if (res != 0)
222 {
223 opt300x_interface_debug_print("opt3002: continuous read failed.\n");
224 (void)opt300x_deinit(&gs_handle);
225
226 return 1;
227 }
228 opt300x_interface_debug_print("opt300x: %0.2f lux.\n", lux);
229 }
230 }
231
232 /* stop continuous read */
233 res = opt300x_stop_continuous_read(&gs_handle);
234 if (res != 0)
235 {
236 opt300x_interface_debug_print("opt300x: stop continuous read failed.\n");
237 (void)opt300x_deinit(&gs_handle);
238
239 return 1;
240 }
241
242 /* single read test */
243 opt300x_interface_debug_print("opt300x: single read test.\n");
244
245 for (i = 0; i < times; i++)
246 {
247 uint16_t raw;
248 float lux;
249 float nw_cm2;
250
251 /* delay 1000ms */
253
254 if (type == OPT3002)
255 {
256 res = opt3002_single_read(&gs_handle, &raw, &nw_cm2);
257 if (res != 0)
258 {
259 opt300x_interface_debug_print("opt3002: single read failed.\n");
260 (void)opt300x_deinit(&gs_handle);
261
262 return 1;
263 }
264 opt300x_interface_debug_print("opt300x: %0.2f nW/cm2.\n", nw_cm2);
265 }
266 else
267 {
268 res = opt300x_single_read(&gs_handle, &raw, &lux);
269 if (res != 0)
270 {
271 opt300x_interface_debug_print("opt3002: single read failed.\n");
272 (void)opt300x_deinit(&gs_handle);
273
274 return 1;
275 }
276 opt300x_interface_debug_print("opt300x: %0.2f lux.\n", lux);
277 }
278 }
279
280 /* finish read test */
281 opt300x_interface_debug_print("opt300x: finish read test.\n");
282 (void)opt300x_deinit(&gs_handle);
283
284 return 0;
285}
driver opt300x read test header file
uint8_t opt300x_set_fault_count(opt300x_handle_t *handle, opt300x_fault_count_t count)
set fault count
uint8_t opt300x_info(opt300x_info_t *info)
get chip's information
uint8_t opt3002_continuous_read(opt300x_handle_t *handle, uint16_t *raw, float *nw_cm2)
read data from the chip continuously
uint8_t opt3005_set_range(opt300x_handle_t *handle, opt3005_range_t range)
set range
uint8_t opt3002_single_read(opt300x_handle_t *handle, uint16_t *raw, float *nw_cm2)
read data from the chip
uint8_t opt300x_set_type(opt300x_handle_t *handle, opt300x_t type)
set the chip type
uint8_t opt300x_set_mask_exponent(opt300x_handle_t *handle, opt300x_bool_t enable)
enable or disable mask exponent
opt300x_address_t
opt300x address enumeration definition
uint8_t opt300x_start_continuous_read(opt300x_handle_t *handle)
start the chip reading
uint8_t opt300x_continuous_read(opt300x_handle_t *handle, uint16_t *raw, float *lux)
read data from the chip continuously
struct opt300x_info_s opt300x_info_t
opt300x information structure definition
opt300x_t
opt300x type enumeration definition
uint8_t opt300x_set_conversion_time(opt300x_handle_t *handle, opt300x_conversion_time_t t)
set conversion time
uint8_t opt300x_set_range(opt300x_handle_t *handle, opt300x_range_t range)
set range
uint8_t opt300x_deinit(opt300x_handle_t *handle)
close the chip
uint8_t opt300x_stop_continuous_read(opt300x_handle_t *handle)
stop the chip reading
uint8_t opt300x_set_addr_pin(opt300x_handle_t *handle, opt300x_address_t addr_pin)
set the iic address pin
uint8_t opt300x_init(opt300x_handle_t *handle)
initialize the chip
uint8_t opt3002_set_range(opt300x_handle_t *handle, opt3002_range_t range)
set range
uint8_t opt300x_single_read(opt300x_handle_t *handle, uint16_t *raw, float *lux)
read data from the chip
struct opt300x_handle_s opt300x_handle_t
opt300x handle structure definition
@ OPT300X_BOOL_FALSE
@ OPT3005_RANGE_AUTO
@ OPT300X_RANGE_AUTO
@ OPT3002
@ OPT3005
@ OPT300X_CONVERSION_TIME_800_MS
@ OPT3002_RANGE_AUTO
@ OPT300X_FAULT_COUNT_ONE
void opt300x_interface_delay_ms(uint32_t ms)
interface delay ms
void opt300x_interface_receive_callback(uint8_t type)
interface receive callback
void opt300x_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t opt300x_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t opt300x_interface_iic_init(void)
interface iic bus init
uint8_t opt300x_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t opt300x_interface_iic_deinit(void)
interface iic bus deinit
uint8_t opt300x_read_test(opt300x_t type, opt300x_address_t addr_pin, uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]