LibDriver BH1750FVI
Loading...
Searching...
No Matches
driver_bh1750fvi_read_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
40static bh1750fvi_handle_t gs_handle;
41
51uint8_t bh1750fvi_read_test(bh1750fvi_address_t addr, uint32_t times)
52{
53 uint8_t res;
54 uint8_t t;
55 uint16_t raw;
56 float lux;
57 uint32_t i;
59
60 /* link interface function */
68
69 /* get information */
70 res = bh1750fvi_info(&info);
71 if (res != 0)
72 {
73 bh1750fvi_interface_debug_print("bh1750fvi: get info failed.\n");
74
75 return 1;
76 }
77 else
78 {
79 /* print chip info */
80 bh1750fvi_interface_debug_print("bh1750fvi: chip is %s.\n", info.chip_name);
81 bh1750fvi_interface_debug_print("bh1750fvi: manufacturer is %s.\n", info.manufacturer_name);
82 bh1750fvi_interface_debug_print("bh1750fvi: interface is %s.\n", info.interface);
83 bh1750fvi_interface_debug_print("bh1750fvi: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
84 bh1750fvi_interface_debug_print("bh1750fvi: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
85 bh1750fvi_interface_debug_print("bh1750fvi: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
86 bh1750fvi_interface_debug_print("bh1750fvi: max current is %0.2fmA.\n", info.max_current_ma);
87 bh1750fvi_interface_debug_print("bh1750fvi: max temperature is %0.1fC.\n", info.temperature_max);
88 bh1750fvi_interface_debug_print("bh1750fvi: min temperature is %0.1fC.\n", info.temperature_min);
89 }
90
91 /* start read test */
92 bh1750fvi_interface_debug_print("bh1750fvi: start read test.\n");
93
94 /* set addr pin test */
95 res = bh1750fvi_set_addr_pin(&gs_handle, addr);
96 if (res != 0)
97 {
98 bh1750fvi_interface_debug_print("bh1750fvi: set addr pin test.\n");
99
100 return 1;
101 }
102
103 /* init */
104 res = bh1750fvi_init(&gs_handle);
105 if (res != 0)
106 {
107 bh1750fvi_interface_debug_print("bh1750fvi: init failed.\n");
108
109 return 1;
110 }
111
112 /* power on */
113 res = bh1750fvi_power_on(&gs_handle);
114 if (res != 0)
115 {
116 bh1750fvi_interface_debug_print("bh1750fvi: power on failed.\n");
117 (void)bh1750fvi_deinit(&gs_handle);
118
119 return 1;
120 }
121
122 /* high resolution mode test */
123 bh1750fvi_interface_debug_print("bh1750fvi: high resolution mode test.\n");
124
125 /* high resolution mode */
127 if (res != 0)
128 {
129 bh1750fvi_interface_debug_print("bh1750fvi: set mode failed.\n");
130 (void)bh1750fvi_deinit(&gs_handle);
131
132 return 1;
133 }
134
135 for (i = 0; i < times; i++)
136 {
137 /* single read */
138 res = bh1750fvi_single_read(&gs_handle, &raw, &lux);
139 if (res != 0)
140 {
141 bh1750fvi_interface_debug_print("bh1750fvi: single read failed.\n");
142 (void)bh1750fvi_deinit(&gs_handle);
143
144 return 1;
145 }
146
147 /* output */
148 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
149
150 /* delay 1000 ms */
152 }
153
154 /* high resolution mode2 test */
155 bh1750fvi_interface_debug_print("bh1750fvi: high resolution mode2 test.\n");
156
157 /* high resolution mode2 */
159 if (res != 0)
160 {
161 bh1750fvi_interface_debug_print("bh1750fvi: set mode failed.\n");
162 (void)bh1750fvi_deinit(&gs_handle);
163
164 return 1;
165 }
166
167 for (i = 0; i < times; i++)
168 {
169 /* single read */
170 res = bh1750fvi_single_read(&gs_handle, &raw, &lux);
171 if (res != 0)
172 {
173 bh1750fvi_interface_debug_print("bh1750fvi: single read failed.\n");
174 (void)bh1750fvi_deinit(&gs_handle);
175
176 return 1;
177 }
178
179 /* output */
180 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
181
182 /* delay 1000 ms */
184 }
185
186 /* low resolution mode test */
187 bh1750fvi_interface_debug_print("bh1750fvi: low resolution mode test.\n");
188
189 /* low resolution mode */
191 if (res != 0)
192 {
193 bh1750fvi_interface_debug_print("bh1750fvi: set mode failed.\n");
194 (void)bh1750fvi_deinit(&gs_handle);
195
196 return 1;
197 }
198
199 for (i = 0; i < times; i++)
200 {
201 /* single read */
202 res = bh1750fvi_single_read(&gs_handle, &raw, &lux);
203 if (res != 0)
204 {
205 bh1750fvi_interface_debug_print("bh1750fvi: single read failed.\n");
206 (void)bh1750fvi_deinit(&gs_handle);
207
208 return 1;
209 }
210
211 /* output */
212 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
213
214 /* delay 1000 ms */
216 }
217
218 /* continuous read test */
219 bh1750fvi_interface_debug_print("bh1750fvi: continuous read test.\n");
220
221 /* start continuous read */
222 res = bh1750fvi_start_continuous_read(&gs_handle);
223 if (res != 0)
224 {
225 bh1750fvi_interface_debug_print("bh1750fvi: start continuous read failed.\n");
226 (void)bh1750fvi_deinit(&gs_handle);
227
228 return 1;
229 }
230
231 for (i = 0; i < times; i++)
232 {
233 /* continuous read */
234 res = bh1750fvi_continuous_read(&gs_handle, &raw, &lux);
235 if (res != 0)
236 {
237 bh1750fvi_interface_debug_print("bh1750fvi: continuous read failed.\n");
238 (void)bh1750fvi_deinit(&gs_handle);
239
240 return 1;
241 }
242
243 /* output */
244 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
245
246 /* delay 1000 ms */
248 }
249
250 /* stop continuous read */
251 res = bh1750fvi_stop_continuous_read(&gs_handle);
252 if (res != 0)
253 {
254 bh1750fvi_interface_debug_print("bh1750fvi: stop continuous read failed.\n");
255 (void)bh1750fvi_deinit(&gs_handle);
256
257 return 1;
258 }
259
260 /* measurement time test */
261 bh1750fvi_interface_debug_print("bh1750fvi: measurement time test.\n");
262
263 t = rand() % 50 + 50;
264 res = bh1750fvi_set_measurement_time(&gs_handle, t);
265 if (res != 0)
266 {
267 bh1750fvi_interface_debug_print("bh1750fvi: set measurement time failed.\n");
268 (void)bh1750fvi_deinit(&gs_handle);
269
270 return 1;
271 }
272
273 /* set measurement time */
274 bh1750fvi_interface_debug_print("bh1750fvi: set measurement time %d.\n", t);
275
276 for (i = 0; i < times; i++)
277 {
278 /* single read */
279 res = bh1750fvi_single_read(&gs_handle, &raw, &lux);
280 if (res != 0)
281 {
282 bh1750fvi_interface_debug_print("bh1750fvi: single read failed.\n");
283 (void)bh1750fvi_deinit(&gs_handle);
284
285 return 1;
286 }
287
288 /* output */
289 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
290
291 /* delay 1000 ms */
293 }
294
295 t = rand() % 50 + 100;
296 res = bh1750fvi_set_measurement_time(&gs_handle, t);
297 if (res != 0)
298 {
299 bh1750fvi_interface_debug_print("bh1750fvi: set measurement time failed.\n");
300 (void)bh1750fvi_deinit(&gs_handle);
301
302 return 1;
303 }
304
305 /* set measurement time */
306 bh1750fvi_interface_debug_print("bh1750fvi: set measurement time %d.\n", t);
307
308 for (i = 0; i < times; i++)
309 {
310 /* single read */
311 res = bh1750fvi_single_read(&gs_handle, &raw, &lux);
312 if (res != 0)
313 {
314 bh1750fvi_interface_debug_print("bh1750fvi: single read failed.\n");
315 (void)bh1750fvi_deinit(&gs_handle);
316
317 return 1;
318 }
319
320 /* output */
321 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
322
323 /* delay 1000 ms */
325 }
326
327 t = rand() % 50 + 200;
328 res = bh1750fvi_set_measurement_time(&gs_handle, t);
329 if (res != 0)
330 {
331 bh1750fvi_interface_debug_print("bh1750fvi: set measurement time failed.\n");
332 (void)bh1750fvi_deinit(&gs_handle);
333
334 return 1;
335 }
336
337 /* set measurement time */
338 bh1750fvi_interface_debug_print("bh1750fvi: set measurement time %d.\n", t);
339
340 for (i = 0; i < times; i++)
341 {
342 /* single read */
343 res = bh1750fvi_single_read(&gs_handle, &raw, &lux);
344 if (res != 0)
345 {
346 bh1750fvi_interface_debug_print("bh1750fvi: single read failed.\n");
347 (void)bh1750fvi_deinit(&gs_handle);
348
349 return 1;
350 }
351
352 /* output */
353 bh1750fvi_interface_debug_print("bh1750fvi: %0.2flux.\n", lux);
354
355 /* delay 1000 ms */
357 }
358
359 /* power down */
360 res = bh1750fvi_power_down(&gs_handle);
361 if (res != 0)
362 {
363 bh1750fvi_interface_debug_print("bh1750fvi: power down failed.\n");
364 (void)bh1750fvi_deinit(&gs_handle);
365
366 return 1;
367 }
368
369 /* finish read test */
370 bh1750fvi_interface_debug_print("bh1750fvi: finish read test.\n");
371 (void)bh1750fvi_deinit(&gs_handle);
372
373 return 0;
374}
driver bh1750fvi read test header file
uint8_t bh1750fvi_stop_continuous_read(bh1750fvi_handle_t *handle)
stop chip reading
uint8_t bh1750fvi_single_read(bh1750fvi_handle_t *handle, uint16_t *raw, float *lux)
read data from the chip once
uint8_t bh1750fvi_deinit(bh1750fvi_handle_t *handle)
close the chip
uint8_t bh1750fvi_start_continuous_read(bh1750fvi_handle_t *handle)
start chip reading
struct bh1750fvi_info_s bh1750fvi_info_t
bh1750fvi information structure definition
struct bh1750fvi_handle_s bh1750fvi_handle_t
bh1750fvi handle structure definition
uint8_t bh1750fvi_set_measurement_time(bh1750fvi_handle_t *handle, uint8_t t)
set the measurement time
uint8_t bh1750fvi_continuous_read(bh1750fvi_handle_t *handle, uint16_t *raw, float *lux)
read data from the chip continuously
uint8_t bh1750fvi_set_addr_pin(bh1750fvi_handle_t *handle, bh1750fvi_address_t addr_pin)
set the address pin
uint8_t bh1750fvi_info(bh1750fvi_info_t *info)
get chip's information
uint8_t bh1750fvi_power_down(bh1750fvi_handle_t *handle)
power down
uint8_t bh1750fvi_power_on(bh1750fvi_handle_t *handle)
power on
uint8_t bh1750fvi_init(bh1750fvi_handle_t *handle)
initialize the chip
bh1750fvi_address_t
bh1750fvi address enumeration definition
uint8_t bh1750fvi_set_mode(bh1750fvi_handle_t *handle, bh1750fvi_mode_t mode)
set the mode
@ BH1750FVI_MODE_HIGH_RESOLUTION_MODE
@ BH1750FVI_MODE_LOW_RESOLUTION_MODE
@ BH1750FVI_MODE_HIGH_RESOLUTION_MODE2
void bh1750fvi_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t bh1750fvi_interface_iic_deinit(void)
interface iic bus deinit
uint8_t bh1750fvi_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus write command
uint8_t bh1750fvi_interface_iic_init(void)
interface iic bus init
uint8_t bh1750fvi_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic bus read command
void bh1750fvi_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t bh1750fvi_read_test(bh1750fvi_address_t addr, uint32_t times)
read test