LibDriver TSL2561
Loading...
Searching...
No Matches
driver_tsl2561_read_test.c
Go to the documentation of this file.
1
37
39
40static tsl2561_handle_t gs_handle;
41
51uint8_t tsl2561_read_test(tsl2561_address_t addr_pin, uint32_t times)
52{
53 uint8_t res;
54 uint32_t t;
55 uint16_t channel_0_raw;
56 uint16_t channel_1_raw;
57 uint32_t lux;
58 tsl2561_info_t info;
59
60 /* link interface function */
68
69 /* get chip information */
70 res = tsl2561_info(&info);
71 if (res != 0)
72 {
73 tsl2561_interface_debug_print("tsl2561: get info failed.\n");
74
75 return 1;
76 }
77 else
78 {
79 /* print chip information */
80 tsl2561_interface_debug_print("tsl2561: chip is %s.\n", info.chip_name);
81 tsl2561_interface_debug_print("tsl2561: manufacturer is %s.\n", info.manufacturer_name);
82 tsl2561_interface_debug_print("tsl2561: interface is %s.\n", info.interface);
83 tsl2561_interface_debug_print("tsl2561: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
84 tsl2561_interface_debug_print("tsl2561: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
85 tsl2561_interface_debug_print("tsl2561: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
86 tsl2561_interface_debug_print("tsl2561: max current is %0.2fmA.\n", info.max_current_ma);
87 tsl2561_interface_debug_print("tsl2561: max temperature is %0.1fC.\n", info.temperature_max);
88 tsl2561_interface_debug_print("tsl2561: min temperature is %0.1fC.\n", info.temperature_min);
89 }
90
91 /* set iic address */
92 res = tsl2561_set_addr_pin(&gs_handle, addr_pin);
93 if (res != 0)
94 {
95 tsl2561_interface_debug_print("tsl2561: set addr pin failed.\n");
96
97 return 1;
98 }
99
100 /* tsl2561 init */
101 res = tsl2561_init(&gs_handle);
102 if (res != 0)
103 {
104 tsl2561_interface_debug_print("tsl2561: init failed.\n");
105
106 return 1;
107 }
108
109 /* wake up chip */
110 res = tsl2561_wake_up(&gs_handle);
111 if (res != 0)
112 {
113 tsl2561_interface_debug_print("tsl2561: wake up failed.\n");
114 (void)tsl2561_deinit(&gs_handle);
115
116 return 1;
117 }
118
119 /* set interrupt_mode out of 15 times */
121 if (res != 0)
122 {
123 tsl2561_interface_debug_print("tsl2561: set interrupt mode failed.\n");
124 (void)tsl2561_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* disable interrupt */
130 res = tsl2561_set_interrupt(&gs_handle, TSL2561_BOOL_FALSE);
131 if (res != 0)
132 {
133 tsl2561_interface_debug_print("tsl2561: disable interrupt failed.\n");
134 (void)tsl2561_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* set interrupt high threshold 0xFFFF */
140 res = tsl2561_set_interrupt_high_threshold(&gs_handle, 0xFFFFU);
141 if (res != 0)
142 {
143 tsl2561_interface_debug_print("tsl2561: set interrupt high threshold failed.\n");
144 (void)tsl2561_deinit(&gs_handle);
145
146 return 1;
147 }
148
149 /* set interrupt low threshold 0x0000 */
150 res = tsl2561_set_interrupt_low_threshold(&gs_handle, 0x0000U);
151 if (res != 0)
152 {
153 tsl2561_interface_debug_print("tsl2561: set interrupt low threshold failed.\n");
154 (void)tsl2561_deinit(&gs_handle);
155
156 return 1;
157 }
158
159 /* start read test */
160 tsl2561_interface_debug_print("tsl2561: start read test.\n");
161 tsl2561_interface_debug_print("tsl2561: set gain 1.\n");
162 res = tsl2561_set_gain(&gs_handle, TSL2561_GAIN_1);
163 if (res != 0)
164 {
165 tsl2561_interface_debug_print("tsl2561: set gain failed.\n");
166 (void)tsl2561_deinit(&gs_handle);
167
168 return 1;
169 }
170
171 /* set integration time 13 ms */
172 tsl2561_interface_debug_print("tsl2561: set integration time 13 ms.\n");
174 if (res != 0)
175 {
176 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
177 (void)tsl2561_deinit(&gs_handle);
178
179 return 1;
180 }
181 t = times;
182 while (t != 0)
183 {
185
186 /* read data */
187 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
188 if (res != 0)
189 {
190 tsl2561_interface_debug_print("tsl2561: read failed.\n");
191 (void)tsl2561_deinit(&gs_handle);
192
193 return 1;
194 }
195 tsl2561_interface_debug_print("tsl2561: read lux %d.\n", lux);
196 t--;
197 }
198
199 /* set integration time 101 ms */
200 tsl2561_interface_debug_print("tsl2561: set integration time 101 ms.\n");
202 if (res != 0)
203 {
204 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
205 (void)tsl2561_deinit(&gs_handle);
206
207 return 1;
208 }
209 t = times;
210 while (t != 0)
211 {
213
214 /* read data */
215 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
216 if (res != 0)
217 {
218 tsl2561_interface_debug_print("tsl2561: read failed.\n");
219 (void)tsl2561_deinit(&gs_handle);
220
221 return 1;
222 }
223 tsl2561_interface_debug_print("tsl2561: read lux %d.\n", lux);
224 t--;
225 }
226
227 /* set integration time 402 ms */
228 tsl2561_interface_debug_print("tsl2561: set integration time 402 ms.\n");
230 if (res != 0)
231 {
232 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
233 (void)tsl2561_deinit(&gs_handle);
234
235 return 1;
236 }
237 t = times;
238 while (t != 0)
239 {
241
242 /* read data */
243 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
244 if (res != 0)
245 {
246 tsl2561_interface_debug_print("tsl2561: read failed.\n");
247 (void)tsl2561_deinit(&gs_handle);
248
249 return 1;
250 }
251 tsl2561_interface_debug_print("tsl2561: read lux %d.\n", lux);
252 t--;
253 }
254
255 /* set gain 16 */
256 tsl2561_interface_debug_print("tsl2561: set gain 16.\n");
257 res = tsl2561_set_gain(&gs_handle, TSL2561_GAIN_16);
258 if (res != 0)
259 {
260 tsl2561_interface_debug_print("tsl2561: set gain failed.\n");
261 (void)tsl2561_deinit(&gs_handle);
262
263 return 1;
264 }
265
266 /* set integration time 13 ms */
267 tsl2561_interface_debug_print("tsl2561: set integration time 13 ms.\n");
269 if (res != 0)
270 {
271 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
272 (void)tsl2561_deinit(&gs_handle);
273
274 return 1;
275 }
276 t = times;
277 while (t != 0)
278 {
280
281 /* read data */
282 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
283 if (res != 0)
284 {
285 tsl2561_interface_debug_print("tsl2561: read failed.\n");
286 (void)tsl2561_deinit(&gs_handle);
287
288 return 1;
289 }
290 tsl2561_interface_debug_print("tsl2561: read lux %d.\n", lux);
291 t--;
292 }
293
294 /* set integration time 101 ms */
295 tsl2561_interface_debug_print("tsl2561: set integration time 101 ms.\n");
297 if (res != 0)
298 {
299 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
300 (void)tsl2561_deinit(&gs_handle);
301
302 return 1;
303 }
304 t= times;
305 while (t != 0)
306 {
308
309 /* read data */
310 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
311 if (res != 0)
312 {
313 tsl2561_interface_debug_print("tsl2561: read failed.\n");
314 (void)tsl2561_deinit(&gs_handle);
315
316 return 1;
317 }
318 tsl2561_interface_debug_print("tsl2561: read lux %d.\n", lux);
319 t--;
320 }
321
322 /* set integration time 402 ms */
323 tsl2561_interface_debug_print("tsl2561: set integration time 402 ms.\n");
325 if (res != 0)
326 {
327 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
328 (void)tsl2561_deinit(&gs_handle);
329
330 return 1;
331 }
332 t = times;
333 while (t != 0)
334 {
336
337 /* read data */
338 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
339 if (res != 0)
340 {
341 tsl2561_interface_debug_print("tsl2561: read failed.\n");
342 (void)tsl2561_deinit(&gs_handle);
343
344 return 1;
345 }
346 tsl2561_interface_debug_print("tsl2561: read lux %d.\n", lux);
347 t--;
348 }
349
350 /* finish read test */
351 tsl2561_interface_debug_print("tsl2561: finish read test.\n");
352 (void)tsl2561_deinit(&gs_handle);
353
354 return 0;
355}
driver tsl2561 read test header file
uint8_t tsl2561_read(tsl2561_handle_t *handle, uint16_t *channel_0_raw, uint16_t *channel_1_raw, uint32_t *lux)
read data from the chip
uint8_t tsl2561_deinit(tsl2561_handle_t *handle)
close the chip
uint8_t tsl2561_set_addr_pin(tsl2561_handle_t *handle, tsl2561_address_t addr_pin)
set the iic address pin
uint8_t tsl2561_init(tsl2561_handle_t *handle)
initialize the chip
uint8_t tsl2561_info(tsl2561_info_t *info)
get chip's information
tsl2561_address_t
tsl2561 address enumeration definition
uint8_t tsl2561_set_gain(tsl2561_handle_t *handle, tsl2561_gain_t gain)
set the adc gain
struct tsl2561_handle_s tsl2561_handle_t
tsl2561 handle structure definition
uint8_t tsl2561_set_integration_time(tsl2561_handle_t *handle, tsl2561_integration_time_t t)
set the integration time
struct tsl2561_info_s tsl2561_info_t
tsl2561 information structure definition
uint8_t tsl2561_wake_up(tsl2561_handle_t *handle)
wake up the chip
@ TSL2561_BOOL_FALSE
@ TSL2561_GAIN_16
@ TSL2561_GAIN_1
@ TSL2561_INTEGRATION_TIME_402MS
@ TSL2561_INTEGRATION_TIME_101MS
@ TSL2561_INTEGRATION_TIME_13MS
uint8_t tsl2561_interface_iic_init(void)
interface iic bus init
uint8_t tsl2561_interface_iic_deinit(void)
interface iic bus deinit
void tsl2561_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t tsl2561_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t tsl2561_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
void tsl2561_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t tsl2561_set_interrupt_mode(tsl2561_handle_t *handle, tsl2561_interrupt_mode_t mode)
set the interrupt mode
uint8_t tsl2561_set_interrupt_low_threshold(tsl2561_handle_t *handle, uint16_t ch0_raw)
set the interrupt low threshold
uint8_t tsl2561_set_interrupt_high_threshold(tsl2561_handle_t *handle, uint16_t ch0_raw)
set the interrupt high threshold
uint8_t tsl2561_set_interrupt(tsl2561_handle_t *handle, tsl2561_bool_t enable)
enable or disable the chip interrupt
@ TSL2561_INTERRUPT_MODE_15_INTEGRATION_TIME_OUT_OF_RANGE
uint8_t tsl2561_read_test(tsl2561_address_t addr_pin, uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]