LibDriver MAX44009
Loading...
Searching...
No Matches
driver_max44009_interrupt_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
40static max44009_handle_t gs_handle;
41
53uint8_t max44009_interrupt_test(max44009_address_t addr, float low_threshold_lux, float high_threshold_lux, uint32_t times)
54{
55 uint8_t res;
56 uint8_t reg;
57 uint16_t raw;
58 float lux;
59 uint32_t i;
60 max44009_bool_t enable;
61 max44009_info_t info;
62
63 /* link interface function */
71
72 /* get information */
73 res = max44009_info(&info);
74 if (res != 0)
75 {
76 max44009_interface_debug_print("max44009: get info failed.\n");
77
78 return 1;
79 }
80 else
81 {
82 /* print chip info */
83 max44009_interface_debug_print("max44009: chip is %s.\n", info.chip_name);
84 max44009_interface_debug_print("max44009: manufacturer is %s.\n", info.manufacturer_name);
85 max44009_interface_debug_print("max44009: interface is %s.\n", info.interface);
86 max44009_interface_debug_print("max44009: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
87 max44009_interface_debug_print("max44009: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
88 max44009_interface_debug_print("max44009: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
89 max44009_interface_debug_print("max44009: max current is %0.3fmA.\n", info.max_current_ma);
90 max44009_interface_debug_print("max44009: max temperature is %0.1fC.\n", info.temperature_max);
91 max44009_interface_debug_print("max44009: min temperature is %0.1fC.\n", info.temperature_min);
92 }
93
94 /* start interrupt test */
95 max44009_interface_debug_print("max44009: start interrupt test.\n");
96
97 /* set addr pin */
98 res = max44009_set_addr_pin(&gs_handle, addr);
99 if (res != 0)
100 {
101 max44009_interface_debug_print("max44009: set addr pin test.\n");
102
103 return 1;
104 }
105
106 /* init */
107 res = max44009_init(&gs_handle);
108 if (res != 0)
109 {
110 max44009_interface_debug_print("max44009: init failed.\n");
111
112 return 1;
113 }
114
115 /* enable auto range */
117 if (res != 0)
118 {
119 max44009_interface_debug_print("max44009: set auto range failed.\n");
120 (void)max44009_deinit(&gs_handle);
121
122 return 1;
123 }
124
125 /* enable continuous mode */
127 if (res != 0)
128 {
129 max44009_interface_debug_print("max44009: set continuous mode failed.\n");
130 (void)max44009_deinit(&gs_handle);
131
132 return 1;
133 }
134
135 /* threshold convert to register */
136 res = max44009_threshold_convert_to_register(&gs_handle, low_threshold_lux, &reg);
137 if (res != 0)
138 {
139 max44009_interface_debug_print("max44009: threshold convert to register failed.\n");
140 (void)max44009_deinit(&gs_handle);
141
142 return 1;
143 }
144
145 /* set interrupt low threshold */
146 res = max44009_set_interrupt_low_threshold(&gs_handle, reg);
147 if (res != 0)
148 {
149 max44009_interface_debug_print("max44009: set interrupt low threshold failed.\n");
150 (void)max44009_deinit(&gs_handle);
151
152 return 1;
153 }
154
155 /* threshold convert to register */
156 res = max44009_threshold_convert_to_register(&gs_handle, high_threshold_lux, &reg);
157 if (res != 0)
158 {
159 max44009_interface_debug_print("max44009: threshold convert to register failed.\n");
160 (void)max44009_deinit(&gs_handle);
161
162 return 1;
163 }
164
165 /* set interrupt high threshold */
166 res = max44009_set_interrupt_high_threshold(&gs_handle, reg);
167 if (res != 0)
168 {
169 max44009_interface_debug_print("max44009: set interrupt high threshold failed.\n");
170 (void)max44009_deinit(&gs_handle);
171
172 return 1;
173 }
174
175 /* timer convert to register */
176 res = max44009_timer_convert_to_register(&gs_handle, 500.0f, &reg);
177 if (res != 0)
178 {
179 max44009_interface_debug_print("max44009: timer convert to register failed.\n");
180 (void)max44009_deinit(&gs_handle);
181
182 return 1;
183 }
184
185 /* set threshold timer */
186 res = max44009_set_threshold_timer(&gs_handle, reg);
187 if (res != 0)
188 {
189 max44009_interface_debug_print("max44009: set threshold timer failed.\n");
190 (void)max44009_deinit(&gs_handle);
191
192 return 1;
193 }
194
195 /* enable interrupt */
197 if (res != 0)
198 {
199 max44009_interface_debug_print("max44009: set interrupt failed.\n");
200 (void)max44009_deinit(&gs_handle);
201
202 return 1;
203 }
204
205 for (i = 0; i < times; i++)
206 {
207 res = max44009_read(&gs_handle, &raw, &lux);
208 if (res != 0)
209 {
210 max44009_interface_debug_print("max44009: read failed.\n");
211 (void)max44009_deinit(&gs_handle);
212
213 return 1;
214 }
215
216 /* get interrupt status */
217 res = max44009_get_interrupt_status(&gs_handle, &enable);
218 if (res != 0)
219 {
220 max44009_interface_debug_print("max44009: get interrupt status failed.\n");
221 (void)max44009_deinit(&gs_handle);
222
223 return 1;
224 }
225
226 /* output */
227 max44009_interface_debug_print("max44009: data is %0.2flux, interrupt status is %s.\n", lux, (enable == MAX44009_BOOL_TRUE) ? "enable" : "disable");
228
229 if (lux < low_threshold_lux)
230 {
231 /* output */
232 max44009_interface_debug_print("max44009: low threshold %0.2flux and please check int pin.\n", lux);
233
234 if (enable == MAX44009_BOOL_TRUE)
235 {
236 break;
237 }
238 }
239 if (lux > high_threshold_lux)
240 {
241 /* output */
242 max44009_interface_debug_print("max44009: high threshold %0.2flux and please check int pin.\n", lux);
243
244 if (enable == MAX44009_BOOL_TRUE)
245 {
246 break;
247 }
248 }
249
250 /* delay 1000ms */
252 }
253
254 /* finish interrupt test */
255 max44009_interface_debug_print("max44009: finish interrupt test.\n");
256 (void)max44009_deinit(&gs_handle);
257
258 return 0;
259}
driver max44009 interrupt test header file
uint8_t max44009_read(max44009_handle_t *handle, uint16_t *raw, float *lux)
read data
uint8_t max44009_get_interrupt_status(max44009_handle_t *handle, max44009_bool_t *enable)
get interrupt status
uint8_t max44009_set_interrupt_high_threshold(max44009_handle_t *handle, uint8_t threshold)
set interrupt high threshold
struct max44009_handle_s max44009_handle_t
max44009 handle structure definition
uint8_t max44009_threshold_convert_to_register(max44009_handle_t *handle, float lux, uint8_t *reg)
convert the threshold to the register raw data
uint8_t max44009_set_addr_pin(max44009_handle_t *handle, max44009_address_t addr_pin)
set the address pin
uint8_t max44009_set_interrupt_low_threshold(max44009_handle_t *handle, uint8_t threshold)
set interrupt low threshold
uint8_t max44009_info(max44009_info_t *info)
get chip's information
struct max44009_info_s max44009_info_t
max44009 information structure definition
uint8_t max44009_set_continuous_mode(max44009_handle_t *handle, max44009_bool_t enable)
enable or disable continuous mode
max44009_address_t
max44009 address enumeration definition
uint8_t max44009_set_interrupt(max44009_handle_t *handle, max44009_bool_t enable)
enable or disable interrupt
uint8_t max44009_deinit(max44009_handle_t *handle)
close the chip
uint8_t max44009_init(max44009_handle_t *handle)
initialize the chip
uint8_t max44009_set_auto_range(max44009_handle_t *handle, max44009_bool_t enable)
enable or disable auto range
uint8_t max44009_set_threshold_timer(max44009_handle_t *handle, uint8_t t)
set threshold timer
max44009_bool_t
max44009 bool enumeration definition
uint8_t max44009_timer_convert_to_register(max44009_handle_t *handle, float ms, uint8_t *reg)
convert the timer to the register raw data
@ MAX44009_BOOL_TRUE
void max44009_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t max44009_interface_iic_init(void)
interface iic bus init
uint8_t max44009_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t max44009_interface_iic_deinit(void)
interface iic bus deinit
uint8_t max44009_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
void max44009_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t max44009_interrupt_test(max44009_address_t addr, float low_threshold_lux, float high_threshold_lux, uint32_t times)
interrupt test
char manufacturer_name[32]