LibDriver TSL2561
Loading...
Searching...
No Matches
driver_tsl2561_interrupt_test.c
Go to the documentation of this file.
1
37
39
40static tsl2561_handle_t gs_handle;
41
55 uint16_t ch0_low_threshold, uint16_t ch0_high_threshold, uint32_t times)
56{
57 uint8_t res;
58 uint16_t channel_0_raw;
59 uint16_t channel_1_raw;
60 uint32_t lux;
61 tsl2561_info_t info;
62
63 /* link interface function */
71
72 /* get chip information */
73 res = tsl2561_info(&info);
74 if (res != 0)
75 {
76 tsl2561_interface_debug_print("tsl2561: get info failed.\n");
77
78 return 1;
79 }
80 else
81 {
82 /* print chip information */
83 tsl2561_interface_debug_print("tsl2561: chip is %s.\n", info.chip_name);
84 tsl2561_interface_debug_print("tsl2561: manufacturer is %s.\n", info.manufacturer_name);
85 tsl2561_interface_debug_print("tsl2561: interface is %s.\n", info.interface);
86 tsl2561_interface_debug_print("tsl2561: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
87 tsl2561_interface_debug_print("tsl2561: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
88 tsl2561_interface_debug_print("tsl2561: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
89 tsl2561_interface_debug_print("tsl2561: max current is %0.2fmA.\n", info.max_current_ma);
90 tsl2561_interface_debug_print("tsl2561: max temperature is %0.1fC.\n", info.temperature_max);
91 tsl2561_interface_debug_print("tsl2561: min temperature is %0.1fC.\n", info.temperature_min);
92 }
93
94 /* set iic address */
95 res = tsl2561_set_addr_pin(&gs_handle, addr_pin);
96 if (res != 0)
97 {
98 tsl2561_interface_debug_print("tsl2561: set addr pin failed.\n");
99
100 return 1;
101 }
102
103 /* start interrupt test */
104 tsl2561_interface_debug_print("tsl2561: start interrupt test.\n");
105
106 /* tsl2561 init */
107 res = tsl2561_init(&gs_handle);
108 if (res != 0)
109 {
110 tsl2561_interface_debug_print("tsl2561: init failed.\n");
111
112 return 1;
113 }
114
115 /* wake up chip */
116 res = tsl2561_wake_up(&gs_handle);
117 if (res != 0)
118 {
119 tsl2561_interface_debug_print("tsl2561: wake up failed.\n");
120 (void)tsl2561_deinit(&gs_handle);
121
122 return 1;
123 }
124
125 /* set gain 16 */
126 res = tsl2561_set_gain(&gs_handle, TSL2561_GAIN_16);
127 if (res != 0)
128 {
129 tsl2561_interface_debug_print("tsl2561: set gain failed.\n");
130 (void)tsl2561_deinit(&gs_handle);
131
132 return 1;
133 }
134
135 /* set integration time 101 */
137 if (res != 0)
138 {
139 tsl2561_interface_debug_print("tsl2561: set integration time failed.\n");
140 (void)tsl2561_deinit(&gs_handle);
141
142 return 1;
143 }
144
145 /* set interrupt mode */
146 res = tsl2561_set_interrupt_mode(&gs_handle, mode);
147 if (res != 0)
148 {
149 tsl2561_interface_debug_print("tsl2561: set interrupt mode failed.\n");
150 (void)tsl2561_deinit(&gs_handle);
151
152 return 1;
153 }
154
155 /* enable interrupt */
156 res = tsl2561_set_interrupt(&gs_handle, TSL2561_BOOL_TRUE);
157 if (res != 0)
158 {
159 tsl2561_interface_debug_print("tsl2561: enable interrupt failed.\n");
160 (void)tsl2561_deinit(&gs_handle);
161
162 return 1;
163 }
164
165 /* set high_threshold */
166 res = tsl2561_set_interrupt_high_threshold(&gs_handle, ch0_high_threshold);
167 if (res != 0)
168 {
169 tsl2561_interface_debug_print("tsl2561: set interrupt high threshold failed.\n");
170 (void)tsl2561_deinit(&gs_handle);
171
172 return 1;
173 }
174
175 /* set low threshold */
176 res = tsl2561_set_interrupt_low_threshold(&gs_handle, ch0_low_threshold);
177 if (res != 0)
178 {
179 tsl2561_interface_debug_print("tsl2561: set interrupt low threshold failed.\n");
180 (void)tsl2561_deinit(&gs_handle);
181
182 return 1;
183 }
184 while (times != 0)
185 {
187
188 /* read data */
189 res = tsl2561_read(&gs_handle, (uint16_t *)&channel_0_raw, (uint16_t *)&channel_1_raw, (uint32_t *)&lux);
190 if (res != 0)
191 {
192 tsl2561_interface_debug_print("tsl2561: read failed.\n");
193 (void)tsl2561_deinit(&gs_handle);
194
195 return 1;
196 }
197 tsl2561_interface_debug_print("tsl2561: read 0x%04X and check interrupt.\n", channel_0_raw);
198 times--;
199 }
200
201 /* finish interrupt test */
202 tsl2561_interface_debug_print("tsl2561: finish interrupt test.\n");
203 (void)tsl2561_deinit(&gs_handle);
204
205 return 0;
206}
driver tsl2561 interrupt 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_TRUE
@ TSL2561_GAIN_16
@ TSL2561_INTEGRATION_TIME_101MS
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
tsl2561_interrupt_mode_t
tsl2561 interrupt mode enumeration definition
uint8_t tsl2561_set_interrupt(tsl2561_handle_t *handle, tsl2561_bool_t enable)
enable or disable the chip interrupt
uint8_t tsl2561_interrupt_test(tsl2561_address_t addr_pin, tsl2561_interrupt_mode_t mode, uint16_t ch0_low_threshold, uint16_t ch0_high_threshold, uint32_t times)
interrupt test
uint32_t driver_version
char manufacturer_name[32]