LibDriver OPT300X
Loading...
Searching...
No Matches
driver_opt300x_interrupt_test.c
Go to the documentation of this file.
1
36
38
39static opt300x_handle_t gs_handle;
40static uint8_t gs_flag = 0;
41
50{
51 if (opt300x_irq_handler(&gs_handle) != 0)
52 {
53 return 1;
54 }
55
56 return 0;
57}
58
64static void a_receive_callback(uint8_t type)
65{
66 switch (type)
67 {
69 {
70 gs_flag = 1;
71 opt300x_interface_debug_print("opt300x: irq high limit.\n");
72
73 break;
74 }
76 {
77 gs_flag = 1;
78 opt300x_interface_debug_print("opt300x: irq low limit.\n");
79
80 break;
81 }
82 default :
83 {
84 opt300x_interface_debug_print("opt300x: unknown code.\n");
85
86 break;
87 }
88 }
89}
90
104 float low_threshold, float high_threshold, uint32_t times)
105{
106 uint8_t res;
107 uint16_t limit;
108 uint32_t t;
109 opt300x_info_t info;
110
111 /* link interface function */
119 DRIVER_OPT300X_LINK_RECEIVE_CALLBACK(&gs_handle, a_receive_callback);
120
121 /* get chip information */
122 res = opt300x_info(&info);
123 if (res != 0)
124 {
125 opt300x_interface_debug_print("opt300x: get info failed.\n");
126
127 return 1;
128 }
129 else
130 {
131 /* print chip information */
132 opt300x_interface_debug_print("opt300x: chip is %s.\n", info.chip_name);
133 opt300x_interface_debug_print("opt300x: manufacturer is %s.\n", info.manufacturer_name);
134 opt300x_interface_debug_print("opt300x: interface is %s.\n", info.interface);
135 opt300x_interface_debug_print("opt300x: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
136 opt300x_interface_debug_print("opt300x: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
137 opt300x_interface_debug_print("opt300x: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
138 opt300x_interface_debug_print("opt300x: max current is %0.2fmA.\n", info.max_current_ma);
139 opt300x_interface_debug_print("opt300x: max temperature is %0.1fC.\n", info.temperature_max);
140 opt300x_interface_debug_print("opt300x: min temperature is %0.1fC.\n", info.temperature_min);
141 }
142
143 /* set chip type */
144 res = opt300x_set_type(&gs_handle, type);
145 if (res != 0)
146 {
147 opt300x_interface_debug_print("opt300x: set type failed.\n");
148
149 return 1;
150 }
151
152 /* set iic address */
153 res = opt300x_set_addr_pin(&gs_handle, addr_pin);
154 if (res != 0)
155 {
156 opt300x_interface_debug_print("opt300x: set addr pin failed.\n");
157
158 return 1;
159 }
160
161 /* start interrupt test */
162 opt300x_interface_debug_print("opt300x: start interrupt test.\n");
163
164 /* opt300x init */
165 res = opt300x_init(&gs_handle);
166 if (res != 0)
167 {
168 opt300x_interface_debug_print("opt300x: init failed.\n");
169
170 return 1;
171 }
172
173 if (type != OPT3002)
174 {
175 /* convert low threshold lux */
176 res = opt300x_limit_convert_to_register(&gs_handle, low_threshold, &limit);
177 if (res != 0)
178 {
179 opt300x_interface_debug_print("opt300x: limit convert to register failed.\n");
180 (void)opt300x_deinit(&gs_handle);
181
182 return 1;
183 }
184
185 /* set low limit */
186 res = opt300x_set_low_limit(&gs_handle, limit);
187 if (res != 0)
188 {
189 opt300x_interface_debug_print("opt300x: set low limit failed.\n");
190 (void)opt300x_deinit(&gs_handle);
191
192 return 1;
193 }
194
195 /* convert high threshold lux */
196 res = opt300x_limit_convert_to_register(&gs_handle, high_threshold, &limit);
197 if (res != 0)
198 {
199 opt300x_interface_debug_print("opt300x: limit convert to register failed.\n");
200 (void)opt300x_deinit(&gs_handle);
201
202 return 1;
203 }
204
205 /* set high limit */
206 res = opt300x_set_high_limit(&gs_handle, limit);
207 if (res != 0)
208 {
209 opt300x_interface_debug_print("opt300x: set high limit failed.\n");
210 (void)opt300x_deinit(&gs_handle);
211
212 return 1;
213 }
214 }
215 else
216 {
217 /* convert low threshold nw_cm2 */
218 res = opt3002_limit_convert_to_register(&gs_handle, low_threshold, &limit);
219 if (res != 0)
220 {
221 opt300x_interface_debug_print("opt3002: limit convert to register failed.\n");
222 (void)opt300x_deinit(&gs_handle);
223
224 return 1;
225 }
226
227 /* set low limit */
228 res = opt300x_set_low_limit(&gs_handle, limit);
229 if (res != 0)
230 {
231 opt300x_interface_debug_print("opt3002: set low limit failed.\n");
232 (void)opt300x_deinit(&gs_handle);
233
234 return 1;
235 }
236
237 /* convert high threshold nw_cm2 */
238 res = opt3002_limit_convert_to_register(&gs_handle, high_threshold, &limit);
239 if (res != 0)
240 {
241 opt300x_interface_debug_print("opt3002: limit convert to register failed.\n");
242 (void)opt300x_deinit(&gs_handle);
243
244 return 1;
245 }
246
247 /* set high limit */
248 res = opt300x_set_high_limit(&gs_handle, limit);
249 if (res != 0)
250 {
251 opt300x_interface_debug_print("opt3002: set high limit failed.\n");
252 (void)opt300x_deinit(&gs_handle);
253
254 return 1;
255 }
256 }
257
258 /* enable interrupt latch */
260 if (res != 0)
261 {
262 opt300x_interface_debug_print("opt300x: set interrupt latch failed.\n");
263 (void)opt300x_deinit(&gs_handle);
264
265 return 1;
266 }
267
268 /* set interrupt pin polarity low */
270 if (res != 0)
271 {
272 opt300x_interface_debug_print("opt300x: set interrupt pin polarity failed.\n");
273 (void)opt300x_deinit(&gs_handle);
274
275 return 1;
276 }
277
278 /* set auto range */
279 if (type == OPT3002)
280 {
281 res = opt3002_set_range(&gs_handle, OPT3002_RANGE_AUTO);
282 if (res != 0)
283 {
284 opt300x_interface_debug_print("opt3002: set range failed.\n");
285 (void)opt300x_deinit(&gs_handle);
286
287 return 1;
288 }
289 }
290 else if (type == OPT3005)
291 {
292 res = opt3005_set_range(&gs_handle, OPT3005_RANGE_AUTO);
293 if (res != 0)
294 {
295 opt300x_interface_debug_print("opt3005: set range failed.\n");
296 (void)opt300x_deinit(&gs_handle);
297
298 return 1;
299 }
300 }
301 else
302 {
303 res = opt300x_set_range(&gs_handle, OPT300X_RANGE_AUTO);
304 if (res != 0)
305 {
306 opt300x_interface_debug_print("opt300x: set range failed.\n");
307 (void)opt300x_deinit(&gs_handle);
308
309 return 1;
310 }
311 }
312
313 /* set conversion time 800ms */
315 if (res != 0)
316 {
317 opt300x_interface_debug_print("opt300x: set conversion time failed.\n");
318 (void)opt300x_deinit(&gs_handle);
319
320 return 1;
321 }
322
323 /* disable mask exponent */
325 if (res != 0)
326 {
327 opt300x_interface_debug_print("opt300x: set mask exponent failed.\n");
328 (void)opt300x_deinit(&gs_handle);
329
330 return 1;
331 }
332
333 /* set fault count one */
335 if (res != 0)
336 {
337 opt300x_interface_debug_print("opt300x: set fault count failed.\n");
338 (void)opt300x_deinit(&gs_handle);
339
340 return 1;
341 }
342
343 /* start continuous read */
344 res = opt300x_start_continuous_read(&gs_handle);
345 if (res != 0)
346 {
347 opt300x_interface_debug_print("opt300x: start continuous read failed.\n");
348 (void)opt300x_deinit(&gs_handle);
349
350 return 1;
351 }
352
353 /* run the testing */
354 t = times;
355 gs_flag = 0;
356 while (t != 0)
357 {
358 uint16_t raw;
359 float lux;
360 float nw_cm2;
361
362 /* delay 1000ms */
364
365 if (gs_flag != 0)
366 {
367 gs_flag = 0;
368 t--;
369 }
370
371 if (type == OPT3002)
372 {
373 res = opt3002_continuous_read(&gs_handle, &raw, &nw_cm2);
374 if (res != 0)
375 {
376 opt300x_interface_debug_print("opt3002: continuous read failed.\n");
377 (void)opt300x_deinit(&gs_handle);
378
379 return 1;
380 }
381 opt300x_interface_debug_print("opt300x: %0.2f nW/cm2.\n", nw_cm2);
382 }
383 else
384 {
385 res = opt300x_continuous_read(&gs_handle, &raw, &lux);
386 if (res != 0)
387 {
388 opt300x_interface_debug_print("opt3002: continuous read failed.\n");
389 (void)opt300x_deinit(&gs_handle);
390
391 return 1;
392 }
393 opt300x_interface_debug_print("opt300x: %0.2f lux.\n", lux);
394 }
395 }
396
397 /* stop continuous read */
398 res = opt300x_stop_continuous_read(&gs_handle);
399 if (res != 0)
400 {
401 opt300x_interface_debug_print("opt300x: stop continuous read failed.\n");
402 (void)opt300x_deinit(&gs_handle);
403
404 return 1;
405 }
406
407 /* finish interrupt test */
408 opt300x_interface_debug_print("opt300x: finish interrupt test.\n");
409 (void)opt300x_deinit(&gs_handle);
410
411 return 0;
412}
driver opt300x interrupt 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 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_set_high_limit(opt300x_handle_t *handle, uint16_t limit)
set high limit
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
uint8_t opt300x_set_low_limit(opt300x_handle_t *handle, uint16_t limit)
set low limit
struct opt300x_info_s opt300x_info_t
opt300x information structure definition
uint8_t opt300x_limit_convert_to_register(opt300x_handle_t *handle, float lux, uint16_t *reg)
convert the limit threshold to the register raw data
opt300x_t
opt300x type enumeration definition
uint8_t opt300x_set_interrupt_pin_polarity(opt300x_handle_t *handle, opt300x_interrupt_polarity_t polarity)
set interrupt pin polarity
uint8_t opt3002_limit_convert_to_register(opt300x_handle_t *handle, float nw_cm2, uint16_t *reg)
convert the limit threshold to the register raw data
uint8_t opt300x_set_conversion_time(opt300x_handle_t *handle, opt300x_conversion_time_t t)
set conversion time
uint8_t opt300x_set_interrupt_latch(opt300x_handle_t *handle, opt300x_bool_t enable)
enable or disable interrupt latch
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 opt300x_irq_handler(opt300x_handle_t *handle)
irq handler
uint8_t opt3002_set_range(opt300x_handle_t *handle, opt3002_range_t range)
set range
struct opt300x_handle_s opt300x_handle_t
opt300x handle structure definition
@ OPT300X_BOOL_FALSE
@ OPT300X_BOOL_TRUE
@ OPT3005_RANGE_AUTO
@ OPT300X_RANGE_AUTO
@ OPT3002
@ OPT3005
@ OPT300X_CONVERSION_TIME_800_MS
@ OPT3002_RANGE_AUTO
@ OPT300X_INTERRUPT_HIGH_LIMIT
@ OPT300X_INTERRUPT_LOW_LIMIT
@ OPT300X_INTERRUPT_POLARITY_LOW
@ OPT300X_FAULT_COUNT_ONE
void opt300x_interface_delay_ms(uint32_t ms)
interface delay ms
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_interrupt_test_irq_handler(void)
interrupt test irq
uint8_t opt300x_interrupt_test(opt300x_t type, opt300x_address_t addr_pin, float low_threshold, float high_threshold, uint32_t times)
interrupt test
uint32_t driver_version
char manufacturer_name[32]