LibDriver VEML7700
Loading...
Searching...
No Matches
driver_veml7700.c
Go to the documentation of this file.
1
36
37#include "driver_veml7700.h"
38
42#define CHIP_NAME "VISHAY VEML7700"
43#define MANUFACTURER_NAME "VISHAY"
44#define SUPPLY_VOLTAGE_MIN 2.5f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.045f
47#define TEMPERATURE_MIN -25.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define VEML7700_ADDRESS 0x20
55
59#define VEML7700_REG_ALS_CONF_0 0x00
60#define VEML7700_REG_ALS_WH 0x01
61#define VEML7700_REG_ALS_WL 0x02
62#define VEML7700_REG_POWER_SAVING 0x03
63#define VEML7700_REG_ALS 0x04
64#define VEML7700_REG_WHITE 0x05
65#define VEML7700_REG_ALS_INT 0x06
66#define VEML7700_REG_ID 0x07
67
71static const double gsc_resolution[6][4] =
72{
73 {0.0042, 0.0084, 0.0336, 0.0672},
74 {0.0084, 0.0168, 0.0672, 0.1344},
75 {0.0168, 0.0336, 0.1344, 0.2688},
76 {0.0336, 0.0672, 0.2688, 0.5376},
77 {0.0672, 0.1344, 0.5376, 1.0752},
78 {0.1344, 0.2688, 1.0752, 2.1504},
79};
80
91static uint8_t a_veml7700_iic_read(veml7700_handle_t *handle, uint8_t reg, uint16_t *data)
92{
93 uint8_t res;
94 uint8_t buf[2];
95
96 res = handle->iic_read(VEML7700_ADDRESS, reg, buf, 2); /* read data */
97 if (res != 0) /* check the result */
98 {
99 return 1; /* return error */
100 }
101 *data = (((uint16_t)buf[1]) << 8) | buf[0]; /* set data */
102
103 return 0; /* success return 0 */
104}
105
116static uint8_t a_veml7700_iic_write(veml7700_handle_t *handle, uint8_t reg, uint16_t data)
117{
118 uint8_t res;
119 uint8_t buf[2];
120
121 buf[0] = (data >> 0) & 0xFF;
122 buf[1] = (data >> 8) & 0xFF;
123 res = handle->iic_write(VEML7700_ADDRESS, reg, buf, 2); /* write data */
124 if (res != 0) /* check the result */
125 {
126 return 1; /* return error */
127 }
128
129 return 0; /* success return 0 */
130}
131
142static uint8_t a_calculate(veml7700_handle_t *handle, uint16_t raw, double *lux)
143{
144 uint8_t res;
145 uint8_t ind;
146 uint8_t gain;
147 uint8_t integration;
148 uint16_t prev;
149 double resolution;
150
151 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
152 if (res != 0) /* check error */
153 {
154 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
155
156 return 1; /* return error */
157 }
158
159 ind = (prev >> 11) & 0x3; /* get param */
160 if (ind == 0) /* gain 1 */
161 {
162 gain = 1; /* set index 1 */
163 }
164 else if (ind == 1) /* gain 2 */
165 {
166 gain = 0; /* set index 0 */
167 }
168 else if (ind == 2) /* gain 1/8 */
169 {
170 gain = 3; /* set index 3 */
171 }
172 else /* gain 1/4 */
173 {
174 gain = 2; /* set index 2 */
175 }
176
177 ind = (prev >> 6) & 0xF; /* get param */
178 if (ind == 0x0C) /* 25 ms */
179 {
180 integration = 5; /* set index 5 */
181 }
182 else if (ind == 0x08) /* 50 ms */
183 {
184 integration = 4; /* set index 4 */
185 }
186 else if (ind == 0x00) /* 100 ms*/
187 {
188 integration = 3; /* set index 3 */
189 }
190 else if (ind == 0x01) /* 200 ms */
191 {
192 integration = 2; /* set index 2 */
193 }
194 else if (ind == 0x02) /* 400 ms */
195 {
196 integration = 1; /* set index 1 */
197 }
198 else if (ind == 0x03) /* 800 ms */
199 {
200 integration = 0; /* set index 0 */
201 }
202 else
203 {
204 return 1; /* return error */
205 }
206 resolution = gsc_resolution[integration][gain]; /* set resolution */
207 resolution *= (double)(raw); /* convert */
208 *lux = (((6.0135e-13 * resolution - 9.3924e-9) *
209 resolution + 8.1488e-5) *
210 resolution + 1.0023) * resolution; /* convert */
211
212 return 0; /* success return 0 */
213}
214
225static uint8_t a_run_auto_range(veml7700_handle_t *handle, uint16_t raw, double lux)
226{
227 uint8_t res;
228 uint16_t prev;
229 uint8_t gain;
230 uint8_t integration;
231
232 if (raw < VEML7700_AUTO_RANGE_LOW_THRESHOLD) /* check low threshold */
233 {
234 if (lux < 275.0) /* less than 275.0 */
235 {
236 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
237 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_800MS; /* set integration time 800ms */
238 }
239 else if (lux < 550.0) /* less than 550.0 */
240 {
241 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
242 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_400MS; /* set integration time 400ms */
243 }
244 else if (lux < 1101.0) /* less than 1101.0 */
245 {
246 gain = (uint8_t)VEML7700_ALS_GAIN_2;
247 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_200MS; /* set integration time 200ms */
248 }
249 else if (lux < 2202.0) /* less than 2202.0 */
250 {
251 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
252 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_100MS; /* set integration time 100ms */
253 }
254 else if (lux < 4404.0) /* less than 4404.0 */
255 {
256 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
257 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_50MS; /* set integration time 50ms */
258 }
259 else if (lux < 8808.0) /* less than 8808.0 */
260 {
261 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
262 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
263 }
264 else if (lux < 17616.0) /* less than 17616.0 */
265 {
266 gain = (uint8_t)VEML7700_ALS_GAIN_1; /* set gain 1 */
267 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
268 }
269 else if (lux < 35232.0) /* less than 35232.0 */
270 {
271 gain = (uint8_t)VEML7700_ALS_GAIN_1_DIV_4; /* set gain 1/4 */
272 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_50MS; /* set integration time 50ms */
273 }
274 else if (lux < 70463.0) /* less than 70463.0 */
275 {
276 gain = (uint8_t)VEML7700_ALS_GAIN_1_DIV_4; /* set gain 1/4 */
277 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
278 }
279 else /* 140926.0 */
280 {
281 gain = (uint8_t)VEML7700_ALS_GAIN_1_DIV_8; /* set gain 1/8 */
282 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
283 }
284
285 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
286 if (res != 0) /* check error */
287 {
288 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
289
290 return 1; /* return error */
291 }
292 prev &= ~(3 << 11); /* clear settings */
293 prev |= gain << 11; /* set gain */
294 prev &= ~(0xF << 6); /* clear settings */
295 prev |= integration << 6; /* set integration time */
296 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
297 if (res != 0) /* check error */
298 {
299 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
300
301 return 1; /* return error */
302 }
303
304 return 0; /* success return 0 */
305 }
306 else if (raw > VEML7700_AUTO_RANGE_HIGH_THRESHOLD) /* check high threshold */
307 {
308 if (lux > 70463.0) /* over 70463.0 */
309 {
310 gain = (uint8_t)VEML7700_ALS_GAIN_1_DIV_8; /* set gain 1/8 */
311 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
312 }
313 else if (lux > 35232.0) /* over 35232.0 */
314 {
315 gain = (uint8_t)VEML7700_ALS_GAIN_1_DIV_4; /* set gain 1/4 */
316 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
317 }
318 else if (lux > 17616.0) /* over 17616.0 */
319 {
320 gain = (uint8_t)VEML7700_ALS_GAIN_1_DIV_4; /* set gain 1/4 */
321 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_50MS; /* set integration time 50ms */
322 }
323 else if (lux > 8808.0) /* over 8808.0 */
324 {
325 gain = (uint8_t)VEML7700_ALS_GAIN_1; /* set gain 1 */
326 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
327 }
328 else if (lux > 4404.0) /* over 4404.0 */
329 {
330 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
331 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_25MS; /* set integration time 25ms */
332 }
333 else if (lux > 2202.0) /* over 2202.0 */
334 {
335 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
336 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_50MS; /* set integration time 50ms */
337 }
338 else if (lux > 1101.0) /* over 1101.0 */
339 {
340 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
341 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_100MS; /* set integration time 100ms */
342 }
343 else if (lux > 550.0) /* over 550.0 */
344 {
345 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
346 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_200MS; /* set integration time 200ms */
347 }
348 else if (lux > 275.0) /* over 275.0 */
349 {
350 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
351 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_400MS; /* set integration time 400ms */
352 }
353 else /* 275.0 */
354 {
355 gain = (uint8_t)VEML7700_ALS_GAIN_2; /* set gain 2 */
356 integration = (uint8_t)VEML7700_ALS_INTEGRATION_TIME_800MS; /* set integration time 800ms */
357 }
358
359 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
360 if (res != 0) /* check error */
361 {
362 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
363
364 return 1; /* return error */
365 }
366 prev &= ~(3 << 11); /* clear settings */
367 prev |= gain << 11; /* set gain */
368 prev &= ~(0xF << 6); /* clear settings */
369 prev |= integration << 6; /* set integration time */
370 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
371 if (res != 0) /* check error */
372 {
373 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
374
375 return 1; /* return error */
376 }
377
378 return 0; /* success return 0 */
379 }
380 else /* do nothing */
381 {
382 return 0; /* success return 0 */
383 }
384}
385
398{
399 uint8_t res;
400 uint16_t prev;
401
402 if (handle == NULL) /* check handle */
403 {
404 return 2; /* return error */
405 }
406 if (handle->inited != 1) /* check handle initialization */
407 {
408 return 3; /* return error */
409 }
410
411 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
412 if (res != 0) /* check error */
413 {
414 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
415
416 return 1; /* return error */
417 }
418 prev &= ~(3 << 11); /* clear settings */
419 prev |= gain << 11; /* set gain */
420 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
421 if (res != 0) /* check error */
422 {
423 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
424
425 return 1; /* return error */
426 }
427
428 return 0; /* success return 0 */
429}
430
443{
444 uint8_t res;
445 uint16_t prev;
446
447 if (handle == NULL) /* check handle */
448 {
449 return 2; /* return error */
450 }
451 if (handle->inited != 1) /* check handle initialization */
452 {
453 return 3; /* return error */
454 }
455
456 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
457 if (res != 0) /* check error */
458 {
459 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
460
461 return 1; /* return error */
462 }
463 *gain = (veml7700_als_gain_t)((prev >> 11) & 0x03); /* set gain */
464
465 return 0; /* success return 0 */
466}
467
480{
481 uint8_t res;
482 uint16_t prev;
483
484 if (handle == NULL) /* check handle */
485 {
486 return 2; /* return error */
487 }
488 if (handle->inited != 1) /* check handle initialization */
489 {
490 return 3; /* return error */
491 }
492
493 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
494 if (res != 0) /* check error */
495 {
496 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
497
498 return 1; /* return error */
499 }
500 prev &= ~(0xF << 6); /* clear settings */
501 prev |= t << 6; /* set time */
502 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
503 if (res != 0) /* check error */
504 {
505 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
506
507 return 1; /* return error */
508 }
509
510 return 0; /* success return 0 */
511}
512
525{
526 uint8_t res;
527 uint16_t prev;
528
529 if (handle == NULL) /* check handle */
530 {
531 return 2; /* return error */
532 }
533 if (handle->inited != 1) /* check handle initialization */
534 {
535 return 3; /* return error */
536 }
537
538 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
539 if (res != 0) /* check error */
540 {
541 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
542
543 return 1; /* return error */
544 }
545 *t = (veml7700_als_integration_time_t)((prev >> 6) & 0xF); /* set time */
546
547 return 0; /* success return 0 */
548}
549
562{
563 uint8_t res;
564 uint16_t prev;
565
566 if (handle == NULL) /* check handle */
567 {
568 return 2; /* return error */
569 }
570 if (handle->inited != 1) /* check handle initialization */
571 {
572 return 3; /* return error */
573 }
574
575 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
576 if (res != 0) /* check error */
577 {
578 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
579
580 return 1; /* return error */
581 }
582 prev &= ~(3 << 4); /* clear settings */
583 prev |= persistence << 4; /* set als_persistence */
584 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
585 if (res != 0) /* check error */
586 {
587 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
588
589 return 1; /* return error */
590 }
591
592 return 0; /* success return 0 */
593}
594
607{
608 uint8_t res;
609 uint16_t prev;
610
611 if (handle == NULL) /* check handle */
612 {
613 return 2; /* return error */
614 }
615 if (handle->inited != 1) /* check handle initialization */
616 {
617 return 3; /* return error */
618 }
619
620 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
621 if (res != 0) /* check error */
622 {
623 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
624
625 return 1; /* return error */
626 }
627 *persistence = (veml7700_als_persistence_t)((prev >> 4) & 0x3); /* set als_persistence */
628
629 return 0; /* success return 0 */
630}
631
644{
645 uint8_t res;
646 uint16_t prev;
647
648 if (handle == NULL) /* check handle */
649 {
650 return 2; /* return error */
651 }
652 if (handle->inited != 1) /* check handle initialization */
653 {
654 return 3; /* return error */
655 }
656
657 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
658 if (res != 0) /* check error */
659 {
660 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
661
662 return 1; /* return error */
663 }
664 prev &= ~(1 << 1); /* clear settings */
665 prev |= enable << 1; /* set bool */
666 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
667 if (res != 0) /* check error */
668 {
669 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
670
671 return 1; /* return error */
672 }
673
674 return 0; /* success return 0 */
675}
676
689{
690 uint8_t res;
691 uint16_t prev;
692
693 if (handle == NULL) /* check handle */
694 {
695 return 2; /* return error */
696 }
697 if (handle->inited != 1) /* check handle initialization */
698 {
699 return 3; /* return error */
700 }
701
702 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
703 if (res != 0) /* check error */
704 {
705 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
706
707 return 1; /* return error */
708 }
709 *enable = (veml7700_bool_t)((prev >> 1) & 0x01); /* set bool */
710
711 return 0; /* success return 0 */
712}
713
726{
727 uint8_t res;
728 uint16_t prev;
729
730 if (handle == NULL) /* check handle */
731 {
732 return 2; /* return error */
733 }
734 if (handle->inited != 1) /* check handle initialization */
735 {
736 return 3; /* return error */
737 }
738
739 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
740 if (res != 0) /* check error */
741 {
742 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
743
744 return 1; /* return error */
745 }
746 prev &= ~(1 << 0); /* clear settings */
747 prev |= mode << 0; /* set mode */
748 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
749 if (res != 0) /* check error */
750 {
751 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
752
753 return 1; /* return error */
754 }
755
756 return 0; /* success return 0 */
757}
758
771{
772 uint8_t res;
773 uint16_t prev;
774
775 if (handle == NULL) /* check handle */
776 {
777 return 2; /* return error */
778 }
779 if (handle->inited != 1) /* check handle initialization */
780 {
781 return 3; /* return error */
782 }
783
784 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
785 if (res != 0) /* check error */
786 {
787 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
788
789 return 1; /* return error */
790 }
791 *mode = (veml7700_als_mode_t)((prev >> 0) & 0x01); /* set mode */
792
793 return 0; /* success return 0 */
794}
795
807uint8_t veml7700_set_als_high_threshold(veml7700_handle_t *handle, uint16_t threshold)
808{
809 uint8_t res;
810
811 if (handle == NULL) /* check handle */
812 {
813 return 2; /* return error */
814 }
815 if (handle->inited != 1) /* check handle initialization */
816 {
817 return 3; /* return error */
818 }
819
820 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_WH, threshold); /* write conf */
821 if (res != 0) /* check error */
822 {
823 handle->debug_print("veml7700: write als high threshold failed.\n"); /* write als high threshold failed */
824
825 return 1; /* return error */
826 }
827
828 return 0; /* success return 0 */
829}
830
842uint8_t veml7700_get_als_high_threshold(veml7700_handle_t *handle, uint16_t *threshold)
843{
844 uint8_t res;
845
846 if (handle == NULL) /* check handle */
847 {
848 return 2; /* return error */
849 }
850 if (handle->inited != 1) /* check handle initialization */
851 {
852 return 3; /* return error */
853 }
854
855 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_WH, threshold); /* read conf */
856 if (res != 0) /* check error */
857 {
858 handle->debug_print("veml7700: read als high threshold failed.\n"); /* read als high threshold failed */
859
860 return 1; /* return error */
861 }
862
863 return 0; /* success return 0 */
864}
865
877uint8_t veml7700_set_als_low_threshold(veml7700_handle_t *handle, uint16_t threshold)
878{
879 uint8_t res;
880
881 if (handle == NULL) /* check handle */
882 {
883 return 2; /* return error */
884 }
885 if (handle->inited != 1) /* check handle initialization */
886 {
887 return 3; /* return error */
888 }
889
890 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_WL, threshold); /* write conf */
891 if (res != 0) /* check error */
892 {
893 handle->debug_print("veml7700: write als low threshold failed.\n"); /* write als low threshold failed */
894
895 return 1; /* return error */
896 }
897
898 return 0; /* success return 0 */
899}
900
912uint8_t veml7700_get_als_low_threshold(veml7700_handle_t *handle, uint16_t *threshold)
913{
914 uint8_t res;
915
916 if (handle == NULL) /* check handle */
917 {
918 return 2; /* return error */
919 }
920 if (handle->inited != 1) /* check handle initialization */
921 {
922 return 3; /* return error */
923 }
924
925 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_WL, threshold); /* read conf */
926 if (res != 0) /* check error */
927 {
928 handle->debug_print("veml7700: read als low threshold failed.\n"); /* read als low threshold failed */
929
930 return 1; /* return error */
931 }
932
933 return 0; /* success return 0 */
934}
935
948{
949 uint8_t res;
950 uint16_t prev;
951
952 if (handle == NULL) /* check handle */
953 {
954 return 2; /* return error */
955 }
956 if (handle->inited != 1) /* check handle initialization */
957 {
958 return 3; /* return error */
959 }
960
961 res = a_veml7700_iic_read(handle, VEML7700_REG_POWER_SAVING, &prev); /* read conf */
962 if (res != 0) /* check error */
963 {
964 handle->debug_print("veml7700: read power saving mode failed.\n"); /* read power saving mode failed */
965
966 return 1; /* return error */
967 }
968 prev &= ~(3 << 1); /* clear settings */
969 prev |= mode << 1; /* set mode */
970 res = a_veml7700_iic_write(handle, VEML7700_REG_POWER_SAVING, prev); /* write conf */
971 if (res != 0) /* check error */
972 {
973 handle->debug_print("veml7700: write power saving mode failed.\n"); /* write power saving mode failed */
974
975 return 1; /* return error */
976 }
977
978 return 0; /* success return 0 */
979}
980
993{
994 uint8_t res;
995 uint16_t prev;
996
997 if (handle == NULL) /* check handle */
998 {
999 return 2; /* return error */
1000 }
1001 if (handle->inited != 1) /* check handle initialization */
1002 {
1003 return 3; /* return error */
1004 }
1005
1006 res = a_veml7700_iic_read(handle, VEML7700_REG_POWER_SAVING, &prev); /* read conf */
1007 if (res != 0) /* check error */
1008 {
1009 handle->debug_print("veml7700: read power saving mode failed.\n"); /* read power saving mode failed */
1010
1011 return 1; /* return error */
1012 }
1013 *mode = (veml7700_power_saving_mode_t)((prev >> 1) & 0x3); /* set mode */
1014
1015 return 0; /* success return 0 */
1016}
1017
1030{
1031 uint8_t res;
1032 uint16_t prev;
1033
1034 if (handle == NULL) /* check handle */
1035 {
1036 return 2; /* return error */
1037 }
1038 if (handle->inited != 1) /* check handle initialization */
1039 {
1040 return 3; /* return error */
1041 }
1042
1043 res = a_veml7700_iic_read(handle, VEML7700_REG_POWER_SAVING, &prev); /* read conf */
1044 if (res != 0) /* check error */
1045 {
1046 handle->debug_print("veml7700: read power saving mode failed.\n"); /* read power saving mode failed */
1047
1048 return 1; /* return error */
1049 }
1050 prev &= ~(1 << 0); /* clear settings */
1051 prev |= enable << 0; /* set bool */
1052 res = a_veml7700_iic_write(handle, VEML7700_REG_POWER_SAVING, prev); /* write conf */
1053 if (res != 0) /* check error */
1054 {
1055 handle->debug_print("veml7700: write power saving mode failed.\n"); /* write power saving mode failed */
1056
1057 return 1; /* return error */
1058 }
1059
1060 return 0; /* success return 0 */
1061}
1062
1075{
1076 uint8_t res;
1077 uint16_t prev;
1078
1079 if (handle == NULL) /* check handle */
1080 {
1081 return 2; /* return error */
1082 }
1083 if (handle->inited != 1) /* check handle initialization */
1084 {
1085 return 3; /* return error */
1086 }
1087
1088 res = a_veml7700_iic_read(handle, VEML7700_REG_POWER_SAVING, &prev); /* read conf */
1089 if (res != 0) /* check error */
1090 {
1091 handle->debug_print("veml7700: read power saving mode failed.\n"); /* read power saving mode failed */
1092
1093 return 1; /* return error */
1094 }
1095 *enable = (veml7700_bool_t)((prev >> 0) & 0x01); /* set bool */
1096
1097 return 0; /* success return 0 */
1098}
1099
1113{
1114 uint8_t res;
1115 uint16_t prev;
1116
1117 if (handle == NULL) /* check handle */
1118 {
1119 return 2; /* return error */
1120 }
1121 if (handle->inited != 1) /* check handle initialization */
1122 {
1123 return 3; /* return error */
1124 }
1125
1126 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_INT, &prev); /* read conf */
1127 if (res != 0) /* check error */
1128 {
1129 handle->debug_print("veml7700: read als int failed.\n"); /* read als int failed */
1130
1131 return 1; /* return error */
1132 }
1133 *low_threshold = (veml7700_bool_t)((prev >> 15) & 0x01); /* set bool */
1134 *high_threshold = (veml7700_bool_t)((prev >> 14) & 0x01); /* set bool */
1135
1136 return 0; /* success return 0 */
1137}
1138
1151{
1152 uint8_t res;
1153 uint8_t id;
1154 uint16_t prev;
1155
1156 if (handle == NULL) /* check handle */
1157 {
1158 return 2; /* return error */
1159 }
1160 if (handle->debug_print == NULL) /* check debug_print */
1161 {
1162 return 3; /* return error */
1163 }
1164 if (handle->iic_init == NULL) /* check iic_init */
1165 {
1166 handle->debug_print("veml7700: iic_init is null.\n"); /* iic_init is null */
1167
1168 return 3; /* return error */
1169 }
1170 if (handle->iic_deinit == NULL) /* check iic_deinit */
1171 {
1172 handle->debug_print("veml7700: iic_deinit is null.\n"); /* iic_deinit is null */
1173
1174 return 3; /* return error */
1175 }
1176 if (handle->iic_read == NULL) /* check iic_read */
1177 {
1178 handle->debug_print("veml7700: iic_read is null.\n"); /* iic_read is null */
1179
1180 return 3; /* return error */
1181 }
1182 if (handle->iic_write == NULL) /* check iic_write */
1183 {
1184 handle->debug_print("veml7700: iic_write is null.\n"); /* iic_write is null */
1185
1186 return 3; /* return error */
1187 }
1188 if (handle->delay_ms == NULL) /* check delay_ms */
1189 {
1190 handle->debug_print("veml7700: delay_ms is null.\n"); /* delay_ms is null */
1191
1192 return 3; /* return error */
1193 }
1194
1195 if (handle->iic_init() != 0) /* iic init */
1196 {
1197 handle->debug_print("veml7700: iic init failed.\n"); /* iic init failed */
1198
1199 return 1; /* return error */
1200 }
1201
1202 res = a_veml7700_iic_read(handle, VEML7700_REG_ID, &prev); /* read conf */
1203 if (res != 0) /* check error */
1204 {
1205 handle->debug_print("veml7700: read id failed.\n"); /* read id failed */
1206 (void)handle->iic_deinit(); /* iid deinit */
1207
1208 return 4; /* return error */
1209 }
1210 id = prev & 0xFF; /* set id */
1211 if (id != 0x81) /* check id */
1212 {
1213 handle->debug_print("veml7700: id is invalid.\n"); /* id is invalid */
1214 (void)handle->iic_deinit(); /* iid deinit */
1215
1216 return 4; /* return error */
1217 }
1218 handle->inited = 1; /* flag finish initialization */
1219
1220 return 0; /* success return 0 */
1221}
1222
1235{
1236 uint8_t res;
1237 uint16_t prev;
1238
1239 if (handle == NULL) /* check handle */
1240 {
1241 return 2; /* return error */
1242 }
1243 if (handle->inited != 1) /* check handle initialization */
1244 {
1245 return 3; /* return error */
1246 }
1247
1248 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS_CONF_0, &prev); /* read conf */
1249 if (res != 0) /* check error */
1250 {
1251 handle->debug_print("veml7700: read als conf0 failed.\n"); /* read als conf0 failed */
1252
1253 return 4; /* return error */
1254 }
1255 prev |= 1 << 0; /* set als shut down */
1256 res = a_veml7700_iic_write(handle, VEML7700_REG_ALS_CONF_0, prev); /* write conf */
1257 if (res != 0) /* check error */
1258 {
1259 handle->debug_print("veml7700: write als conf0 failed.\n"); /* write als conf0 failed */
1260
1261 return 4; /* return error */
1262 }
1263 res = handle->iic_deinit(); /* iic deinit */
1264 if (res != 0) /* check error */
1265 {
1266 handle->debug_print("veml7700: iic deinit failed.\n"); /* iic deinit failed */
1267
1268 return 1; /* return error */
1269 }
1270 handle->inited = 0; /* flag closed */
1271
1272 return 0; /* success return 0 */
1273}
1274
1286{
1287 if (handle == NULL) /* check handle */
1288 {
1289 return 2; /* return error */
1290 }
1291 if (handle->inited != 1) /* check handle initialization */
1292 {
1293 return 3; /* return error */
1294 }
1295 handle->auto_mode = (uint8_t)enable; /* set auto mode */
1296
1297 return 0; /* success return 0 */
1298}
1299
1311{
1312 if (handle == NULL) /* check handle */
1313 {
1314 return 2; /* return error */
1315 }
1316 if (handle->inited != 1) /* check handle initialization */
1317 {
1318 return 3; /* return error */
1319 }
1320 *enable = (veml7700_bool_t)(handle->auto_mode); /* set auto mode */
1321
1322 return 0; /* success return 0 */
1323}
1324
1336uint8_t veml7700_read_white(veml7700_handle_t *handle, uint16_t *raw)
1337{
1338 uint8_t res;
1339
1340 if (handle == NULL) /* check handle */
1341 {
1342 return 2; /* return error */
1343 }
1344 if (handle->inited != 1) /* check handle initialization */
1345 {
1346 return 3; /* return error */
1347 }
1348
1349 res = a_veml7700_iic_read(handle, VEML7700_REG_WHITE, raw); /* read conf */
1350 if (res != 0) /* check error */
1351 {
1352 handle->debug_print("veml7700: read white failed.\n"); /* read white failed */
1353
1354 return 1; /* return error */
1355 }
1356
1357 return 0; /* success return 0 */
1358}
1359
1374uint8_t veml7700_read_als(veml7700_handle_t *handle, uint16_t *raw, double *lux)
1375{
1376 uint8_t res;
1377
1378 if (handle == NULL) /* check handle */
1379 {
1380 return 2; /* return error */
1381 }
1382 if (handle->inited != 1) /* check handle initialization */
1383 {
1384 return 3; /* return error */
1385 }
1386
1387 res = a_veml7700_iic_read(handle, VEML7700_REG_ALS, raw); /* read conf */
1388 if (res != 0) /* check error */
1389 {
1390 handle->debug_print("veml7700: read als failed.\n"); /* read als failed */
1391
1392 return 1; /* return error */
1393 }
1394 res = a_calculate(handle, *raw, lux); /* calculate */
1395 if (res != 0) /* check error */
1396 {
1397 handle->debug_print("veml7700: calculate failed.\n"); /* calculate failed */
1398
1399 return 4; /* return error */
1400 }
1401 if (handle->auto_mode != 0) /* check auto mode */
1402 {
1403 res = a_run_auto_range(handle, *raw, *lux); /* run auto range */
1404 if (res != 0) /* check error */
1405 {
1406 handle->debug_print("veml7700: run auto range failed.\n"); /* run auto range */
1407
1408 return 5; /* return error */
1409 }
1410 }
1411
1412 return 0; /* success return 0 */
1413}
1414
1427uint8_t veml7700_set_reg(veml7700_handle_t *handle, uint8_t reg, uint16_t data)
1428{
1429 if (handle == NULL) /* check handle */
1430 {
1431 return 2; /* return error */
1432 }
1433 if (handle->inited != 1) /* check handle initialization */
1434 {
1435 return 3; /* return error */
1436 }
1437
1438 return a_veml7700_iic_write(handle, reg, data); /* write command */
1439}
1440
1453uint8_t veml7700_get_reg(veml7700_handle_t *handle, uint8_t reg, uint16_t *data)
1454{
1455 if (handle == NULL) /* check handle */
1456 {
1457 return 2; /* return error */
1458 }
1459 if (handle->inited != 1) /* check handle initialization */
1460 {
1461 return 3; /* return error */
1462 }
1463
1464 return a_veml7700_iic_read(handle, reg, data); /* read command */
1465}
1466
1476{
1477 if (info == NULL) /* check handle */
1478 {
1479 return 2; /* return error */
1480 }
1481
1482 memset(info, 0, sizeof(veml7700_info_t)); /* initialize veml7700 info structure */
1483 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1484 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1485 strncpy(info->interface, "IIC", 8); /* copy interface name */
1486 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1487 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1488 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1489 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1490 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1491 info->driver_version = DRIVER_VERSION; /* set driver version */
1492
1493 return 0; /* success return 0 */
1494}
#define VEML7700_REG_ALS_WL
#define MAX_CURRENT
#define VEML7700_REG_WHITE
#define VEML7700_REG_ID
#define VEML7700_REG_ALS_CONF_0
chip register definition
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define VEML7700_REG_ALS_INT
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define VEML7700_REG_ALS_WH
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define VEML7700_ADDRESS
chip address definition
#define VEML7700_REG_ALS
#define VEML7700_REG_POWER_SAVING
driver veml7700 header file
uint8_t veml7700_get_interrupt_status(veml7700_handle_t *handle, veml7700_bool_t *low_threshold, veml7700_bool_t *high_threshold)
get interrupt status
uint8_t veml7700_set_als_integration_time(veml7700_handle_t *handle, veml7700_als_integration_time_t t)
set als integration time
veml7700_bool_t
veml7700 bool enumeration definition
uint8_t veml7700_deinit(veml7700_handle_t *handle)
close the chip
#define VEML7700_AUTO_RANGE_HIGH_THRESHOLD
veml7700 auto range high threshold definition
uint8_t veml7700_set_als_interrupt(veml7700_handle_t *handle, veml7700_bool_t enable)
enable or disable als interrupt
uint8_t veml7700_get_als_persistence(veml7700_handle_t *handle, veml7700_als_persistence_t *persistence)
get als persistence
uint8_t veml7700_read_white(veml7700_handle_t *handle, uint16_t *raw)
read white channel
uint8_t veml7700_get_power_saving_mode(veml7700_handle_t *handle, veml7700_power_saving_mode_t *mode)
get power saving mode
uint8_t veml7700_info(veml7700_info_t *info)
get chip's information
uint8_t veml7700_get_als_low_threshold(veml7700_handle_t *handle, uint16_t *threshold)
get als low threshold
struct veml7700_info_s veml7700_info_t
veml7700 information structure definition
uint8_t veml7700_init(veml7700_handle_t *handle)
initialize the chip
uint8_t veml7700_read_als(veml7700_handle_t *handle, uint16_t *raw, double *lux)
read als channel
uint8_t veml7700_set_als_mode(veml7700_handle_t *handle, veml7700_als_mode_t mode)
set als mode
uint8_t veml7700_set_als_gain(veml7700_handle_t *handle, veml7700_als_gain_t gain)
set als gain
veml7700_power_saving_mode_t
veml7700 power saving mode enumeration definition
uint8_t veml7700_get_als_gain(veml7700_handle_t *handle, veml7700_als_gain_t *gain)
get als gain
#define VEML7700_AUTO_RANGE_LOW_THRESHOLD
veml7700 auto range low threshold definition
uint8_t veml7700_get_als_high_threshold(veml7700_handle_t *handle, uint16_t *threshold)
get als high threshold
struct veml7700_handle_s veml7700_handle_t
veml7700 handle structure definition
uint8_t veml7700_get_als_interrupt(veml7700_handle_t *handle, veml7700_bool_t *enable)
get als interrupt status
veml7700_als_integration_time_t
veml7700 als integration time enumeration definition
uint8_t veml7700_set_als_persistence(veml7700_handle_t *handle, veml7700_als_persistence_t persistence)
set als persistence
veml7700_als_gain_t
veml7700 als gain enumeration definition
uint8_t veml7700_set_als_low_threshold(veml7700_handle_t *handle, uint16_t threshold)
set als low threshold
uint8_t veml7700_get_als_integration_time(veml7700_handle_t *handle, veml7700_als_integration_time_t *t)
get als integration time
uint8_t veml7700_set_als_high_threshold(veml7700_handle_t *handle, uint16_t threshold)
set als high threshold
uint8_t veml7700_get_auto_range(veml7700_handle_t *handle, veml7700_bool_t *enable)
get auto range
uint8_t veml7700_get_power_saving(veml7700_handle_t *handle, veml7700_bool_t *enable)
get power saving status
uint8_t veml7700_set_auto_range(veml7700_handle_t *handle, veml7700_bool_t enable)
set auto range
veml7700_als_persistence_t
veml7700 als persistence enumeration definition
veml7700_als_mode_t
veml7700 als mode enumeration definition
uint8_t veml7700_set_power_saving(veml7700_handle_t *handle, veml7700_bool_t enable)
enable or disable power saving
uint8_t veml7700_set_power_saving_mode(veml7700_handle_t *handle, veml7700_power_saving_mode_t mode)
set power saving mode
uint8_t veml7700_get_als_mode(veml7700_handle_t *handle, veml7700_als_mode_t *mode)
get als mode
@ VEML7700_ALS_INTEGRATION_TIME_400MS
@ VEML7700_ALS_INTEGRATION_TIME_50MS
@ VEML7700_ALS_INTEGRATION_TIME_25MS
@ VEML7700_ALS_INTEGRATION_TIME_200MS
@ VEML7700_ALS_INTEGRATION_TIME_800MS
@ VEML7700_ALS_INTEGRATION_TIME_100MS
@ VEML7700_ALS_GAIN_1
@ VEML7700_ALS_GAIN_1_DIV_4
@ VEML7700_ALS_GAIN_2
@ VEML7700_ALS_GAIN_1_DIV_8
uint8_t veml7700_set_reg(veml7700_handle_t *handle, uint8_t reg, uint16_t data)
set the chip register
uint8_t veml7700_get_reg(veml7700_handle_t *handle, uint8_t reg, uint16_t *data)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
char manufacturer_name[32]