LibDriver OPT300X
Loading...
Searching...
No Matches
driver_opt300x_interrupt.c
Go to the documentation of this file.
1
36
38
39static opt300x_handle_t gs_handle;
40
49{
50 if (opt300x_irq_handler(&gs_handle) != 0)
51 {
52 return 1;
53 }
54
55 return 0;
56}
57
71 float low_threshold, float high_threshold,
72 void (*callback)(uint8_t type))
73{
74 uint8_t res;
75 uint16_t limit;
76
77 /* link interface function */
85 DRIVER_OPT300X_LINK_RECEIVE_CALLBACK(&gs_handle, callback);
86
87 /* set chip type */
88 res = opt300x_set_type(&gs_handle, type);
89 if (res != 0)
90 {
91 opt300x_interface_debug_print("opt300x: set type failed.\n");
92
93 return 1;
94 }
95
96 /* set iic address */
97 res = opt300x_set_addr_pin(&gs_handle, addr_pin);
98 if (res != 0)
99 {
100 opt300x_interface_debug_print("opt300x: set addr pin failed.\n");
101
102 return 1;
103 }
104
105 /* opt300x init */
106 res = opt300x_init(&gs_handle);
107 if (res != 0)
108 {
109 opt300x_interface_debug_print("opt300x: init failed.\n");
110
111 return 1;
112 }
113
114 if (type != OPT3002)
115 {
116 /* convert low threshold lux */
117 res = opt300x_limit_convert_to_register(&gs_handle, low_threshold, &limit);
118 if (res != 0)
119 {
120 opt300x_interface_debug_print("opt300x: limit convert to register failed.\n");
121 (void)opt300x_deinit(&gs_handle);
122
123 return 1;
124 }
125
126 /* set low limit */
127 res = opt300x_set_low_limit(&gs_handle, limit);
128 if (res != 0)
129 {
130 opt300x_interface_debug_print("opt300x: set low limit failed.\n");
131 (void)opt300x_deinit(&gs_handle);
132
133 return 1;
134 }
135
136 /* convert high threshold lux */
137 res = opt300x_limit_convert_to_register(&gs_handle, high_threshold, &limit);
138 if (res != 0)
139 {
140 opt300x_interface_debug_print("opt300x: limit convert to register failed.\n");
141 (void)opt300x_deinit(&gs_handle);
142
143 return 1;
144 }
145
146 /* set high limit */
147 res = opt300x_set_high_limit(&gs_handle, limit);
148 if (res != 0)
149 {
150 opt300x_interface_debug_print("opt300x: set high limit failed.\n");
151 (void)opt300x_deinit(&gs_handle);
152
153 return 1;
154 }
155 }
156 else
157 {
158 /* convert low threshold nw_cm2 */
159 res = opt3002_limit_convert_to_register(&gs_handle, low_threshold, &limit);
160 if (res != 0)
161 {
162 opt300x_interface_debug_print("opt3002: limit convert to register failed.\n");
163 (void)opt300x_deinit(&gs_handle);
164
165 return 1;
166 }
167
168 /* set low limit */
169 res = opt300x_set_low_limit(&gs_handle, limit);
170 if (res != 0)
171 {
172 opt300x_interface_debug_print("opt3002: set low limit failed.\n");
173 (void)opt300x_deinit(&gs_handle);
174
175 return 1;
176 }
177
178 /* convert high threshold nw_cm2 */
179 res = opt3002_limit_convert_to_register(&gs_handle, high_threshold, &limit);
180 if (res != 0)
181 {
182 opt300x_interface_debug_print("opt3002: limit convert to register failed.\n");
183 (void)opt300x_deinit(&gs_handle);
184
185 return 1;
186 }
187
188 /* set high limit */
189 res = opt300x_set_high_limit(&gs_handle, limit);
190 if (res != 0)
191 {
192 opt300x_interface_debug_print("opt3002: set high limit failed.\n");
193 (void)opt300x_deinit(&gs_handle);
194
195 return 1;
196 }
197 }
198
199 /* set default interrupt latch */
201 if (res != 0)
202 {
203 opt300x_interface_debug_print("opt300x: set interrupt latch failed.\n");
204 (void)opt300x_deinit(&gs_handle);
205
206 return 1;
207 }
208
209 /* set default interrupt pin polarity */
211 if (res != 0)
212 {
213 opt300x_interface_debug_print("opt300x: set interrupt pin polarity failed.\n");
214 (void)opt300x_deinit(&gs_handle);
215
216 return 1;
217 }
218
219 /* set default range */
220 if (type == OPT3002)
221 {
223 if (res != 0)
224 {
225 opt300x_interface_debug_print("opt3002: set range failed.\n");
226 (void)opt300x_deinit(&gs_handle);
227
228 return 1;
229 }
230 }
231 else if (type == OPT3005)
232 {
234 if (res != 0)
235 {
236 opt300x_interface_debug_print("opt3005: set range failed.\n");
237 (void)opt300x_deinit(&gs_handle);
238
239 return 1;
240 }
241 }
242 else
243 {
245 if (res != 0)
246 {
247 opt300x_interface_debug_print("opt300x: set range failed.\n");
248 (void)opt300x_deinit(&gs_handle);
249
250 return 1;
251 }
252 }
253
254 /* set default conversion time */
256 if (res != 0)
257 {
258 opt300x_interface_debug_print("opt300x: set conversion time failed.\n");
259 (void)opt300x_deinit(&gs_handle);
260
261 return 1;
262 }
263
264 /* set default mask exponent */
266 if (res != 0)
267 {
268 opt300x_interface_debug_print("opt300x: set mask exponent failed.\n");
269 (void)opt300x_deinit(&gs_handle);
270
271 return 1;
272 }
273
274 /* set default fault count */
276 if (res != 0)
277 {
278 opt300x_interface_debug_print("opt300x: set fault count failed.\n");
279 (void)opt300x_deinit(&gs_handle);
280
281 return 1;
282 }
283
284 /* start continuous read */
285 res = opt300x_start_continuous_read(&gs_handle);
286 if (res != 0)
287 {
288 opt300x_interface_debug_print("opt300x: start continuous read failed.\n");
289 (void)opt300x_deinit(&gs_handle);
290
291 return 1;
292 }
293
294 return 0;
295}
296
305uint8_t opt300x_interrupt_read(float *lux)
306{
307 uint16_t raw;
308
309 /* read data */
310 if (opt300x_continuous_read(&gs_handle, (uint16_t *)&raw, lux) != 0)
311 {
312 return 1;
313 }
314
315 return 0;
316}
317
326uint8_t opt3002_interrupt_read(float *nw_cm2)
327{
328 uint16_t raw;
329
330 /* read data */
331 if (opt3002_continuous_read(&gs_handle, (uint16_t *)&raw, nw_cm2) != 0)
332 {
333 return 1;
334 }
335
336 return 0;
337}
338
347{
348 /* stop continuous read */
349 if (opt300x_stop_continuous_read(&gs_handle) != 0)
350 {
351 return 1;
352 }
353
354 /* close opt300x */
355 if (opt300x_deinit(&gs_handle) != 0)
356 {
357 return 1;
358 }
359
360 return 0;
361}
driver opt300x interrupt header file
uint8_t opt300x_set_fault_count(opt300x_handle_t *handle, opt300x_fault_count_t count)
set fault count
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
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
@ OPT3002
@ OPT3005
#define OPT300X_INTERRUPT_DEFAULT_MASK_EXPONENT
#define OPT3005_INTERRUPT_DEFAULT_RANGE
#define OPT3002_INTERRUPT_DEFAULT_RANGE
uint8_t opt300x_interrupt_irq_handler(void)
interrupt irq
#define OPT300X_INTERRUPT_DEFAULT_INTERRUPT_LATCH
opt300x interrupt example default definition
#define OPT300X_INTERRUPT_DEFAULT_RANGE
#define OPT300X_INTERRUPT_DEFAULT_INTERRUPT_POLARITY
uint8_t opt3002_interrupt_read(float *nw_cm2)
interrupt example read
uint8_t opt300x_interrupt_init(opt300x_t type, opt300x_address_t addr_pin, float low_threshold, float high_threshold, void(*callback)(uint8_t type))
interrupt example init
uint8_t opt300x_interrupt_read(float *lux)
interrupt example read
#define OPT300X_INTERRUPT_DEFAULT_FAULT_COUNT
uint8_t opt300x_interrupt_deinit(void)
interrupt example deinit
#define OPT300X_INTERRUPT_DEFAULT_CONVERSION_TIME
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