LibDriver OPT300X
Loading...
Searching...
No Matches
driver_opt300x.c
Go to the documentation of this file.
1
36
37#include "driver_opt300x.h"
38#include <math.h>
39
43#define CHIP_NAME "Texas Instruments OPT300X"
44#define MANUFACTURER_NAME "Texas Instruments"
45#define SUPPLY_VOLTAGE_MIN 1.6f
46#define SUPPLY_VOLTAGE_MAX 3.6f
47#define MAX_CURRENT 0.01f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 1000
51
55#define OPT300X_REG_RESULT 0x00
56#define OPT300X_REG_CONFIGURATION 0x01
57#define OPT300X_REG_LOW_LIMIT 0x02
58#define OPT300X_REG_HIGH_LIMIT 0x03
59#define OPT300X_REG_MANUFACTURER_ID 0x7E
60#define OPT300X_REG_DEVICE_ID 0x7F
61
72static uint8_t a_opt300x_iic_read(opt300x_handle_t *handle, uint8_t reg, uint16_t *data)
73{
74 uint8_t buf[2];
75
76 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
77 if (handle->iic_read(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* read data */
78 {
79 return 1; /* return error */
80 }
81 *data = (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set data */
82
83 return 0; /* success return 0 */
84}
85
96static uint8_t a_opt300x_iic_write(opt300x_handle_t *handle, uint8_t reg, uint16_t data)
97{
98 uint8_t buf[2];
99
100 buf[0] = (data >> 8) & 0xFF; /* set MSB */
101 buf[1] = data & 0xFF; /* set LSB */
102 if (handle->iic_write(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* write data */
103 {
104 return 1; /* return error */
105 }
106
107 return 0; /* success return 0 */
108}
109
120{
121 if (handle == NULL) /* check handle */
122 {
123 return 2; /* return error */
124 }
125
126 handle->type = (uint8_t)type; /* set chip type */
127
128 return 0; /* success return 0 */
129}
130
141{
142 if (handle == NULL) /* check handle */
143 {
144 return 2; /* return error */
145 }
146
147 *type = (opt300x_t)handle->type; /* get iic address */
148
149 return 0; /* success return 0 */
150}
151
162{
163 if (handle == NULL) /* check handle */
164 {
165 return 2; /* return error */
166 }
167
168 handle->iic_addr = (uint8_t)addr_pin; /* set iic address */
169
170 return 0; /* success return 0 */
171}
172
183{
184 if (handle == NULL) /* check handle */
185 {
186 return 2; /* return error */
187 }
188
189 *addr_pin = (opt300x_address_t)handle->iic_addr; /* get iic address */
190
191 return 0; /* success return 0 */
192}
193
206{
207 uint8_t res;
208 uint16_t id;
209
210 if (handle == NULL) /* check handle */
211 {
212 return 2; /* return error */
213 }
214 if (handle->debug_print == NULL) /* check debug_print */
215 {
216 return 3; /* return error */
217 }
218 if (handle->iic_init == NULL) /* check iic_init */
219 {
220 handle->debug_print("opt300x: iic_init is null.\n"); /* iic_init is null */
221
222 return 3; /* return error */
223 }
224 if (handle->iic_deinit == NULL) /* check iic_deinit */
225 {
226 handle->debug_print("opt300x: iic_deinit is null.\n"); /* iic_deinit is null */
227
228 return 3; /* return error */
229 }
230 if (handle->iic_read == NULL) /* check iic_read */
231 {
232 handle->debug_print("opt300x: iic_read is null.\n"); /* iic_read is null */
233
234 return 3; /* return error */
235 }
236 if (handle->iic_write == NULL) /* check iic_write */
237 {
238 handle->debug_print("opt300x: iic_write is null.\n"); /* iic_write is null */
239
240 return 3; /* return error */
241 }
242 if (handle->delay_ms == NULL) /* check delay_ms */
243 {
244 handle->debug_print("opt300x: delay_ms is null.\n"); /* delay_ms is null */
245
246 return 3; /* return error */
247 }
248 if (handle->receive_callback == NULL) /* check receive_callback */
249 {
250 handle->debug_print("opt300x: receive_callback is null.\n"); /* receive_callback is null */
251
252 return 3; /* return error */
253 }
254
255 if (handle->iic_init() != 0) /* iic init */
256 {
257 handle->debug_print("opt300x: iic init failed.\n"); /* iic init failed */
258
259 return 1; /* return error */
260 }
261 res = a_opt300x_iic_read(handle, OPT300X_REG_MANUFACTURER_ID, &id); /* read id */
262 if (res != 0) /* check the result */
263 {
264 handle->debug_print("opt300x: read id failed.\n"); /* read id failed */
265 (void)handle->iic_deinit(); /* iic deinit */
266
267 return 4; /* return error */
268 }
269 if (id != 0x5449) /* check id */
270 {
271 handle->debug_print("opt300x: manufacturer id is invalid.\n"); /* manufacturer id is invalid */
272 (void)handle->iic_deinit(); /* iic deinit */
273
274 return 4; /* return error */
275 }
276 res = a_opt300x_iic_read(handle, OPT300X_REG_DEVICE_ID, &id); /* read id */
277 if (res != 0) /* check the result */
278 {
279 handle->debug_print("opt300x: read id failed.\n"); /* read id failed */
280 (void)handle->iic_deinit(); /* iic deinit */
281
282 return 4; /* return error */
283 }
284 if (id != 0x3001) /* check id */
285 {
286 handle->debug_print("opt300x: device id is invalid.\n"); /* device id is invalid */
287 (void)handle->iic_deinit(); /* iic deinit */
288
289 return 4; /* return error */
290 }
291 handle->inited = 1; /* flag finish initialization */
292
293 return 0; /* success return 0 */
294}
295
308{
309 uint8_t res;
310 uint16_t prev;
311
312 if (handle == NULL) /* check handle */
313 {
314 return 2; /* return error */
315 }
316 if (handle->inited != 1) /* check handle initialization */
317 {
318 return 3; /* return error */
319 }
320
321 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
322 if (res != 0) /* check the result */
323 {
324 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
325
326 return 4; /* return error */
327 }
328 prev &= ~(3 << 9); /* clear settings */
329 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
330 if (res != 0) /* check the result */
331 {
332 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
333
334 return 4; /* return error */
335 }
336 if (handle->iic_deinit() != 0) /* iic deinit */
337 {
338 handle->debug_print("opt300x: iic deinit failed.\n"); /* iic deinit failed */
339
340 return 1; /* return error */
341 }
342 handle->inited = 0; /* flag close */
343
344 return 0; /* success return 0 */
345}
346
358{
359 uint8_t res;
360 uint16_t prev;
361
362 if (handle == NULL) /* check handle */
363 {
364 return 2; /* return error */
365 }
366 if (handle->inited != 1) /* check handle initialization */
367 {
368 return 3; /* return error */
369 }
370
371 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
372 if (res != 0) /* check the result */
373 {
374 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
375
376 return 1; /* return error */
377 }
378 prev &= ~(3 << 9); /* clear settings */
379 prev |= 2 << 9; /* continuous conversions */
380 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
381 if (res != 0) /* check the result */
382 {
383 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
384
385 return 1; /* return error */
386 }
387
388 return 0; /* success return 0 */
389}
390
402{
403 uint8_t res;
404 uint16_t prev;
405
406 if (handle == NULL) /* check handle */
407 {
408 return 2; /* return error */
409 }
410 if (handle->inited != 1) /* check handle initialization */
411 {
412 return 3; /* return error */
413 }
414
415 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
416 if (res != 0) /* check the result */
417 {
418 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
419
420 return 1; /* return error */
421 }
422 prev &= ~(3 << 9); /* clear settings */
423 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
424 if (res != 0) /* check the result */
425 {
426 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
427
428 return 1; /* return error */
429 }
430
431 return 0; /* success return 0 */
432}
433
448uint8_t opt300x_continuous_read(opt300x_handle_t *handle, uint16_t *raw, float *lux)
449{
450 uint8_t res;
451 uint8_t exponent;
452 uint16_t fractional;
453 uint16_t prev;
454
455 if (handle == NULL) /* check handle */
456 {
457 return 2; /* return error */
458 }
459 if (handle->inited != 1) /* check handle initialization */
460 {
461 return 3; /* return error */
462 }
463 if (handle->type == (uint8_t)OPT3002) /* check type */
464 {
465 handle->debug_print("opt300x: opt3002 can't use this function.\n"); /* opt3002 can't use this function */
466
467 return 5; /* return error */
468 }
469
470 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
471 if (res != 0) /* check the result */
472 {
473 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
474
475 return 1; /* return error */
476 }
477 if ((prev & (1 << 8)) != 0) /* check ovf bit */
478 {
479 handle->debug_print("opt300x: data is overflow.\n"); /* data is overflow */
480
481 return 4; /* return error */
482 }
483
484 res = a_opt300x_iic_read(handle, OPT300X_REG_RESULT, raw); /* read result */
485 if (res != 0) /* check the result */
486 {
487 handle->debug_print("opt300x: read result failed.\n"); /* read result failed */
488
489 return 1; /* return error */
490 }
491 exponent = ((*raw) >> 12) & 0xF; /* set exponent */
492 fractional = (*raw) & 0xFFF; /* set fractional */
493 if (handle->type == (uint8_t)OPT3005) /* opt3005 */
494 {
495 *lux = 0.02f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
496 }
497 else /* the others */
498 {
499 *lux = 0.01f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
500 }
501
502 return 0; /* success return 0 */
503}
504
519uint8_t opt3002_continuous_read(opt300x_handle_t *handle, uint16_t *raw, float *nw_cm2)
520{
521 uint8_t res;
522 uint8_t exponent;
523 uint16_t fractional;
524 uint16_t prev;
525
526 if (handle == NULL) /* check handle */
527 {
528 return 2; /* return error */
529 }
530 if (handle->inited != 1) /* check handle initialization */
531 {
532 return 3; /* return error */
533 }
534 if (handle->type != (uint8_t)OPT3002) /* check type */
535 {
536 handle->debug_print("opt300x: only opt3002 can use this function.\n"); /* only opt3002 can use this function */
537
538 return 5; /* return error */
539 }
540
541 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
542 if (res != 0) /* check the result */
543 {
544 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
545
546 return 1; /* return error */
547 }
548 if ((prev & (1 << 8)) != 0) /* check ovf bit */
549 {
550 handle->debug_print("opt300x: data is overflow.\n"); /* data is overflow */
551
552 return 4; /* return error */
553 }
554
555 res = a_opt300x_iic_read(handle, OPT300X_REG_RESULT, raw); /* read result */
556 if (res != 0) /* check the result */
557 {
558 handle->debug_print("opt300x: read result failed.\n"); /* read result failed */
559
560 return 1; /* return error */
561 }
562 exponent = ((*raw) >> 12) & 0xF; /* set exponent */
563 fractional = (*raw) & 0xFFF; /* set fractional */
564 *nw_cm2 = 1.2f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate nw/cm2 */
565
566 return 0; /* success return 0 */
567}
568
584uint8_t opt300x_single_read(opt300x_handle_t *handle, uint16_t *raw, float *lux)
585{
586 uint8_t res;
587 uint8_t exponent;
588 uint16_t fractional;
589 uint16_t prev;
590 uint32_t timeout = 500;
591
592 if (handle == NULL) /* check handle */
593 {
594 return 2; /* return error */
595 }
596 if (handle->inited != 1) /* check handle initialization */
597 {
598 return 3; /* return error */
599 }
600 if (handle->type == (uint8_t)OPT3002) /* check type */
601 {
602 handle->debug_print("opt300x: opt3002 can't use this function.\n"); /* opt3002 can't use this function */
603
604 return 6; /* return error */
605 }
606
607 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
608 if (res != 0) /* check the result */
609 {
610 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
611
612 return 1; /* return error */
613 }
614 prev &= ~(3 << 9); /* clear settings */
615 prev |= (1 << 9); /* clear settings */
616 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
617 if (res != 0) /* check the result */
618 {
619 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
620
621 return 1; /* return error */
622 }
623
624 while (timeout != 0) /* 5s */
625 {
626 handle->delay_ms(10); /* delay 10ms */
627 timeout--; /* timeout-- */
628 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
629 if (res != 0) /* check the result */
630 {
631 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
632
633 return 1; /* return error */
634 }
635 if ((prev & (1 << 7)) != 0) /* check ready bit */
636 {
637 break; /* break */
638 }
639 if ((prev & (1 << 8)) != 0) /* check ovf bit */
640 {
641 handle->debug_print("opt300x: data is overflow.\n"); /* data is overflow */
642
643 return 4; /* return error */
644 }
645 }
646 if (timeout == 0) /* check timeout */
647 {
648 handle->debug_print("opt300x: read timeout.\n"); /* read timeout */
649
650 return 5; /* return error */
651 }
652
653 res = a_opt300x_iic_read(handle, OPT300X_REG_RESULT, raw); /* read result */
654 if (res != 0) /* check the result */
655 {
656 handle->debug_print("opt300x: read result failed.\n"); /* read result failed */
657
658 return 1; /* return error */
659 }
660 exponent = ((*raw) >> 12) & 0xF; /* set exponent */
661 fractional = (*raw) & 0xFFF; /* set fractional */
662 if (handle->type == (uint8_t)OPT3005) /* opt3005 */
663 {
664 *lux = 0.02f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
665 }
666 else /* the others */
667 {
668 *lux = 0.01f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
669 }
670
671 return 0; /* success return 0 */
672}
673
689uint8_t opt3002_single_read(opt300x_handle_t *handle, uint16_t *raw, float *nw_cm2)
690{
691 uint8_t res;
692 uint8_t exponent;
693 uint16_t fractional;
694 uint16_t prev;
695 uint32_t timeout = 500;
696
697 if (handle == NULL) /* check handle */
698 {
699 return 2; /* return error */
700 }
701 if (handle->inited != 1) /* check handle initialization */
702 {
703 return 3; /* return error */
704 }
705 if (handle->type != (uint8_t)OPT3002) /* check type */
706 {
707 handle->debug_print("opt300x: only opt3002 can use this function.\n"); /* only opt3002 can use this function */
708
709 return 6; /* return error */
710 }
711
712 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
713 if (res != 0) /* check the result */
714 {
715 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
716
717 return 1; /* return error */
718 }
719 prev &= ~(3 << 9); /* clear settings */
720 prev |= (1 << 9); /* clear settings */
721 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
722 if (res != 0) /* check the result */
723 {
724 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
725
726 return 1; /* return error */
727 }
728
729 while (timeout != 0) /* 5s */
730 {
731 handle->delay_ms(10); /* delay 10ms */
732 timeout--; /* timeout-- */
733 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
734 if (res != 0) /* check the result */
735 {
736 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
737
738 return 1; /* return error */
739 }
740 if ((prev & (1 << 7)) != 0) /* check ready bit */
741 {
742 break; /* break */
743 }
744 if ((prev & (1 << 8)) != 0) /* check ovf bit */
745 {
746 handle->debug_print("opt300x: data is overflow.\n"); /* data is overflow */
747
748 return 4; /* return error */
749 }
750 }
751 if (timeout == 0) /* check timeout */
752 {
753 handle->debug_print("opt300x: read timeout.\n"); /* read timeout */
754
755 return 5; /* return error */
756 }
757
758 res = a_opt300x_iic_read(handle, OPT300X_REG_RESULT, raw); /* read result */
759 if (res != 0) /* check the result */
760 {
761 handle->debug_print("opt300x: read result failed.\n"); /* read result failed */
762
763 return 1; /* return error */
764 }
765 exponent = ((*raw) >> 12) & 0xF; /* set exponent */
766 fractional = (*raw) & 0xFFF; /* set fractional */
767 *nw_cm2 = 1.2f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate nw/cm2 */
768
769 return 0; /* success return 0 */
770}
771
783uint8_t opt300x_set_low_limit(opt300x_handle_t *handle, uint16_t limit)
784{
785 uint8_t res;
786
787 if (handle == NULL) /* check handle */
788 {
789 return 2; /* return error */
790 }
791 if (handle->inited != 1) /* check handle initialization */
792 {
793 return 3; /* return error */
794 }
795
796 res = a_opt300x_iic_write(handle, OPT300X_REG_LOW_LIMIT, limit); /* write low limit */
797 if (res != 0) /* check the result */
798 {
799 handle->debug_print("opt300x: write low limit failed.\n"); /* write low limit failed */
800
801 return 1; /* return error */
802 }
803
804 return 0; /* success return 0 */
805}
806
818uint8_t opt300x_get_low_limit(opt300x_handle_t *handle, uint16_t *limit)
819{
820 uint8_t res;
821
822 if (handle == NULL) /* check handle */
823 {
824 return 2; /* return error */
825 }
826 if (handle->inited != 1) /* check handle initialization */
827 {
828 return 3; /* return error */
829 }
830
831 res = a_opt300x_iic_read(handle, OPT300X_REG_LOW_LIMIT, limit); /* read low limit */
832 if (res != 0) /* check the result */
833 {
834 handle->debug_print("opt300x: read low limit failed.\n"); /* read low limit failed */
835
836 return 1; /* return error */
837 }
838
839 return 0; /* success return 0 */
840}
841
853uint8_t opt300x_set_high_limit(opt300x_handle_t *handle, uint16_t limit)
854{
855 uint8_t res;
856
857 if (handle == NULL) /* check handle */
858 {
859 return 2; /* return error */
860 }
861 if (handle->inited != 1) /* check handle initialization */
862 {
863 return 3; /* return error */
864 }
865
866 res = a_opt300x_iic_write(handle, OPT300X_REG_HIGH_LIMIT, limit); /* write high limit */
867 if (res != 0) /* check the result */
868 {
869 handle->debug_print("opt300x: write high limit failed.\n"); /* write high limit failed */
870
871 return 1; /* return error */
872 }
873
874 return 0; /* success return 0 */
875}
876
888uint8_t opt300x_get_high_limit(opt300x_handle_t *handle, uint16_t *limit)
889{
890 uint8_t res;
891
892 if (handle == NULL) /* check handle */
893 {
894 return 2; /* return error */
895 }
896 if (handle->inited != 1) /* check handle initialization */
897 {
898 return 3; /* return error */
899 }
900
901 res = a_opt300x_iic_read(handle, OPT300X_REG_HIGH_LIMIT, limit); /* read high limit */
902 if (res != 0) /* check the result */
903 {
904 handle->debug_print("opt300x: read high limit failed.\n"); /* read high limit failed */
905
906 return 1; /* return error */
907 }
908
909 return 0; /* success return 0 */
910}
911
924uint8_t opt300x_limit_convert_to_register(opt300x_handle_t *handle, float lux, uint16_t *reg)
925{
926 uint16_t remain;
927 int xp;
928 float f;
929 float fraction;
930
931 if (handle == NULL) /* check handle */
932 {
933 return 2; /* return error */
934 }
935 if (handle->inited != 1) /* check handle initialization */
936 {
937 return 3; /* return error */
938 }
939 if (handle->type == (uint8_t)OPT3002) /* check type */
940 {
941 handle->debug_print("opt300x: opt3002 can't use this function.\n"); /* opt3002 can't use this function */
942
943 return 4; /* return error */
944 }
945
946 if (handle->type == (uint8_t)OPT3005) /* opt3005 */
947 {
948 f = lux / 0.02f; /* convert */
949 }
950 else
951 {
952 f = lux / 0.01f; /* convert */
953 }
954 fraction = frexpf(f, &xp); /* run frexp */
955 while ((xp > 0xB) || (fraction < 4095.0f)) /* adjust params */
956 {
957 if (xp == 0) /* reach the min */
958 {
959 break; /* break */
960 }
961 if (fraction * 2.0f > 4095.0f) /* reach the max */
962 {
963 break; /* break */
964 }
965 xp--; /* xp-- */
966 fraction *= 2.0f; /* fraction * 2 */
967 }
968 remain = (uint16_t)fraction; /* get the integer part */
969 *reg = (((uint16_t)(xp & 0xF)) << 12) | (remain & 0xFFF); /* convert real data to register data */
970
971 return 0; /* success return 0 */
972}
973
986uint8_t opt300x_limit_convert_to_data(opt300x_handle_t *handle, uint16_t reg, float *lux)
987{
988 uint8_t exponent;
989 uint16_t fractional;
990
991 if (handle == NULL) /* check handle */
992 {
993 return 2; /* return error */
994 }
995 if (handle->inited != 1) /* check handle initialization */
996 {
997 return 3; /* return error */
998 }
999 if (handle->type == (uint8_t)OPT3002) /* check type */
1000 {
1001 handle->debug_print("opt300x: opt3002 can't use this function.\n"); /* opt3002 can't use this function */
1002
1003 return 4; /* return error */
1004 }
1005
1006 exponent = (reg >> 12) & 0xF; /* set exponent */
1007 fractional = reg & 0xFFF; /* set fractional */
1008 if (handle->type == (uint8_t)OPT3005) /* opt3005 */
1009 {
1010 *lux = 0.02f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
1011 }
1012 else /* the others */
1013 {
1014 *lux = 0.01f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
1015 }
1016
1017 return 0; /* success return 0 */
1018}
1019
1032uint8_t opt3002_limit_convert_to_register(opt300x_handle_t *handle, float nw_cm2, uint16_t *reg)
1033{
1034 uint16_t remain;
1035 int xp;
1036 float f;
1037 float fraction;
1038
1039 if (handle == NULL) /* check handle */
1040 {
1041 return 2; /* return error */
1042 }
1043 if (handle->inited != 1) /* check handle initialization */
1044 {
1045 return 3; /* return error */
1046 }
1047 if (handle->type != (uint8_t)OPT3002) /* check type */
1048 {
1049 handle->debug_print("opt300x: only opt3002 can use this function.\n"); /* only opt3002 can use this function */
1050
1051 return 4; /* return error */
1052 }
1053
1054 f = nw_cm2 / 1.2f; /* convert */
1055 fraction = frexpf(f, &xp); /* run frexp */
1056 while ((xp > 0xB) || (fraction < 4095.0f)) /* adjust params */
1057 {
1058 if (xp == 0) /* reach the min */
1059 {
1060 break; /* break */
1061 }
1062 if (fraction * 2.0f > 4095.0f) /* reach the max */
1063 {
1064 break; /* break */
1065 }
1066 xp--; /* xp-- */
1067 fraction *= 2.0f; /* fraction * 2 */
1068 }
1069 remain = (uint16_t)fraction; /* get the integer part */
1070 *reg = (((uint16_t)(xp & 0xF)) << 12) | (remain & 0xFFF); /* convert real data to register data */
1071
1072 return 0; /* success return 0 */
1073}
1074
1087uint8_t opt3002_limit_convert_to_data(opt300x_handle_t *handle, uint16_t reg, float *nw_cm2)
1088{
1089 uint8_t exponent;
1090 uint16_t fractional;
1091
1092 if (handle == NULL) /* check handle */
1093 {
1094 return 2; /* return error */
1095 }
1096 if (handle->inited != 1) /* check handle initialization */
1097 {
1098 return 3; /* return error */
1099 }
1100 if (handle->type != (uint8_t)OPT3002) /* check type */
1101 {
1102 handle->debug_print("opt300x: only opt3002 can use this function.\n"); /* only opt3002 can use this function */
1103
1104 return 4; /* return error */
1105 }
1106
1107 exponent = (reg >> 12) & 0xF; /* set exponent */
1108 fractional = reg & 0xFFF; /* set fractional */
1109 *nw_cm2 = 1.2f * powf(2.0f, (float)exponent) * ((float)fractional); /* calculate lux */
1110
1111 return 0; /* success return 0 */
1112}
1113
1127{
1128 uint8_t res;
1129 uint16_t prev;
1130
1131 if (handle == NULL) /* check handle */
1132 {
1133 return 2; /* return error */
1134 }
1135 if (handle->inited != 1) /* check handle initialization */
1136 {
1137 return 3; /* return error */
1138 }
1139 if (handle->type == (uint8_t)OPT3002) /* check type */
1140 {
1141 handle->debug_print("opt300x: opt3002 can't use this function.\n"); /* opt3002 can't use this function */
1142
1143 return 4; /* return error */
1144 }
1145 if (handle->type == (uint8_t)OPT3005) /* check type */
1146 {
1147 handle->debug_print("opt300x: opt3005 can't use this function.\n"); /* opt3005 can't use this function */
1148
1149 return 4; /* return error */
1150 }
1151
1152 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1153 if (res != 0) /* check the result */
1154 {
1155 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1156
1157 return 1; /* return error */
1158 }
1159 prev &= ~(0xF << 12); /* clear settings */
1160 prev |= range << 12; /* set range */
1161 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1162 if (res != 0) /* check the result */
1163 {
1164 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1165
1166 return 1; /* return error */
1167 }
1168
1169 return 0; /* success return 0 */
1170}
1171
1185{
1186 uint8_t res;
1187 uint16_t prev;
1188
1189 if (handle == NULL) /* check handle */
1190 {
1191 return 2; /* return error */
1192 }
1193 if (handle->inited != 1) /* check handle initialization */
1194 {
1195 return 3; /* return error */
1196 }
1197 if (handle->type == (uint8_t)OPT3002) /* check type */
1198 {
1199 handle->debug_print("opt300x: opt3002 can't use this function.\n"); /* opt3002 can't use this function */
1200
1201 return 4; /* return error */
1202 }
1203 if (handle->type == (uint8_t)OPT3005) /* check type */
1204 {
1205 handle->debug_print("opt300x: opt3005 can't use this function.\n"); /* opt3005 can't use this function */
1206
1207 return 4; /* return error */
1208 }
1209
1210 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1211 if (res != 0) /* check the result */
1212 {
1213 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1214
1215 return 1; /* return error */
1216 }
1217 *range = (opt300x_range_t)((prev >> 12) & 0xF); /* set range */
1218
1219 return 0; /* success return 0 */
1220}
1221
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 if (handle->type != (uint8_t)OPT3002) /* check type */
1248 {
1249 handle->debug_print("opt300x: only opt3002 can use this function.\n"); /* only opt3002 can use this function */
1250
1251 return 4; /* return error */
1252 }
1253
1254 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1255 if (res != 0) /* check the result */
1256 {
1257 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1258
1259 return 1; /* return error */
1260 }
1261 prev &= ~(0xF << 12); /* clear settings */
1262 prev |= range << 12; /* set range */
1263 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1264 if (res != 0) /* check the result */
1265 {
1266 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1267
1268 return 1; /* return error */
1269 }
1270
1271 return 0; /* success return 0 */
1272}
1273
1287{
1288 uint8_t res;
1289 uint16_t prev;
1290
1291 if (handle == NULL) /* check handle */
1292 {
1293 return 2; /* return error */
1294 }
1295 if (handle->inited != 1) /* check handle initialization */
1296 {
1297 return 3; /* return error */
1298 }
1299 if (handle->type != (uint8_t)OPT3002) /* check type */
1300 {
1301 handle->debug_print("opt300x: only opt3002 can use this function.\n"); /* only opt3002 can use this function */
1302
1303 return 4; /* return error */
1304 }
1305
1306 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1307 if (res != 0) /* check the result */
1308 {
1309 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1310
1311 return 1; /* return error */
1312 }
1313 *range = (opt3002_range_t)((prev >> 12) & 0xF); /* set range */
1314
1315 return 0; /* success return 0 */
1316}
1317
1331{
1332 uint8_t res;
1333 uint16_t prev;
1334
1335 if (handle == NULL) /* check handle */
1336 {
1337 return 2; /* return error */
1338 }
1339 if (handle->inited != 1) /* check handle initialization */
1340 {
1341 return 3; /* return error */
1342 }
1343 if (handle->type != (uint8_t)OPT3005) /* check type */
1344 {
1345 handle->debug_print("opt300x: only opt3005 can use this function.\n"); /* only opt3005 can use this function */
1346
1347 return 4; /* return error */
1348 }
1349
1350 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1351 if (res != 0) /* check the result */
1352 {
1353 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1354
1355 return 1; /* return error */
1356 }
1357 prev &= ~(0xF << 12); /* clear settings */
1358 prev |= range << 12; /* set range */
1359 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1360 if (res != 0) /* check the result */
1361 {
1362 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1363
1364 return 1; /* return error */
1365 }
1366
1367 return 0; /* success return 0 */
1368}
1369
1383{
1384 uint8_t res;
1385 uint16_t prev;
1386
1387 if (handle == NULL) /* check handle */
1388 {
1389 return 2; /* return error */
1390 }
1391 if (handle->inited != 1) /* check handle initialization */
1392 {
1393 return 3; /* return error */
1394 }
1395 if (handle->type != (uint8_t)OPT3005) /* check type */
1396 {
1397 handle->debug_print("opt300x: only opt3005 can use this function.\n"); /* only opt3005 can use this function */
1398
1399 return 4; /* return error */
1400 }
1401
1402 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1403 if (res != 0) /* check the result */
1404 {
1405 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1406
1407 return 1; /* return error */
1408 }
1409 *range = (opt3005_range_t)((prev >> 12) & 0xF); /* set range */
1410
1411 return 0; /* success return 0 */
1412}
1413
1426{
1427 uint8_t res;
1428 uint16_t prev;
1429
1430 if (handle == NULL) /* check handle */
1431 {
1432 return 2; /* return error */
1433 }
1434 if (handle->inited != 1) /* check handle initialization */
1435 {
1436 return 3; /* return error */
1437 }
1438
1439 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1440 if (res != 0) /* check the result */
1441 {
1442 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1443
1444 return 1; /* return error */
1445 }
1446 prev &= ~(1 << 11); /* clear settings */
1447 prev |= t << 11; /* set time */
1448 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1449 if (res != 0) /* check the result */
1450 {
1451 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1452
1453 return 1; /* return error */
1454 }
1455
1456 return 0; /* success return 0 */
1457}
1458
1471{
1472 uint8_t res;
1473 uint16_t prev;
1474
1475 if (handle == NULL) /* check handle */
1476 {
1477 return 2; /* return error */
1478 }
1479 if (handle->inited != 1) /* check handle initialization */
1480 {
1481 return 3; /* return error */
1482 }
1483
1484 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1485 if (res != 0) /* check the result */
1486 {
1487 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1488
1489 return 1; /* return error */
1490 }
1491 *t = (opt300x_conversion_time_t)((prev >> 11) & 0x01); /* set time */
1492
1493 return 0; /* success return 0 */
1494}
1495
1508{
1509 uint8_t res;
1510 uint16_t prev;
1511
1512 if (handle == NULL) /* check handle */
1513 {
1514 return 2; /* return error */
1515 }
1516 if (handle->inited != 1) /* check handle initialization */
1517 {
1518 return 3; /* return error */
1519 }
1520
1521 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1522 if (res != 0) /* check the result */
1523 {
1524 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1525
1526 return 1; /* return error */
1527 }
1528 prev &= ~(1 << 4); /* clear settings */
1529 prev |= enable << 4; /* set bool */
1530 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1531 if (res != 0) /* check the result */
1532 {
1533 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1534
1535 return 1; /* return error */
1536 }
1537
1538 return 0; /* success return 0 */
1539}
1540
1553{
1554 uint8_t res;
1555 uint16_t prev;
1556
1557 if (handle == NULL) /* check handle */
1558 {
1559 return 2; /* return error */
1560 }
1561 if (handle->inited != 1) /* check handle initialization */
1562 {
1563 return 3; /* return error */
1564 }
1565
1566 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1567 if (res != 0) /* check the result */
1568 {
1569 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1570
1571 return 1; /* return error */
1572 }
1573 *enable = (opt300x_bool_t)((prev >> 4) & 0x01); /* set bool */
1574
1575 return 0; /* success return 0 */
1576}
1577
1590{
1591 uint8_t res;
1592 uint16_t prev;
1593
1594 if (handle == NULL) /* check handle */
1595 {
1596 return 2; /* return error */
1597 }
1598 if (handle->inited != 1) /* check handle initialization */
1599 {
1600 return 3; /* return error */
1601 }
1602
1603 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1604 if (res != 0) /* check the result */
1605 {
1606 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1607
1608 return 1; /* return error */
1609 }
1610 prev &= ~(1 << 3); /* clear settings */
1611 prev |= polarity << 3; /* set polarity */
1612 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1613 if (res != 0) /* check the result */
1614 {
1615 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1616
1617 return 1; /* return error */
1618 }
1619
1620 return 0; /* success return 0 */
1621}
1622
1635{
1636 uint8_t res;
1637 uint16_t prev;
1638
1639 if (handle == NULL) /* check handle */
1640 {
1641 return 2; /* return error */
1642 }
1643 if (handle->inited != 1) /* check handle initialization */
1644 {
1645 return 3; /* return error */
1646 }
1647
1648 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1649 if (res != 0) /* check the result */
1650 {
1651 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1652
1653 return 1; /* return error */
1654 }
1655 *polarity = (opt300x_interrupt_polarity_t)((prev >> 3) & 0x01); /* set polarity */
1656
1657 return 0; /* success return 0 */
1658}
1659
1672{
1673 uint8_t res;
1674 uint16_t prev;
1675
1676 if (handle == NULL) /* check handle */
1677 {
1678 return 2; /* return error */
1679 }
1680 if (handle->inited != 1) /* check handle initialization */
1681 {
1682 return 3; /* return error */
1683 }
1684
1685 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1686 if (res != 0) /* check the result */
1687 {
1688 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1689
1690 return 1; /* return error */
1691 }
1692 prev &= ~(1 << 2); /* clear settings */
1693 prev |= enable << 2; /* set bool */
1694 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1695 if (res != 0) /* check the result */
1696 {
1697 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1698
1699 return 1; /* return error */
1700 }
1701
1702 return 0; /* success return 0 */
1703}
1704
1717{
1718 uint8_t res;
1719 uint16_t prev;
1720
1721 if (handle == NULL) /* check handle */
1722 {
1723 return 2; /* return error */
1724 }
1725 if (handle->inited != 1) /* check handle initialization */
1726 {
1727 return 3; /* return error */
1728 }
1729
1730 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1731 if (res != 0) /* check the result */
1732 {
1733 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1734
1735 return 1; /* return error */
1736 }
1737 *enable = (opt300x_bool_t)((prev >> 2) & 0x01); /* set bool */
1738
1739 return 0; /* success return 0 */
1740}
1741
1754{
1755 uint8_t res;
1756 uint16_t prev;
1757
1758 if (handle == NULL) /* check handle */
1759 {
1760 return 2; /* return error */
1761 }
1762 if (handle->inited != 1) /* check handle initialization */
1763 {
1764 return 3; /* return error */
1765 }
1766
1767 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1768 if (res != 0) /* check the result */
1769 {
1770 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1771
1772 return 1; /* return error */
1773 }
1774 prev &= ~(3 << 0); /* clear settings */
1775 prev |= count << 0; /* set count */
1776 res = a_opt300x_iic_write(handle, OPT300X_REG_CONFIGURATION, prev); /* write configuration */
1777 if (res != 0) /* check the result */
1778 {
1779 handle->debug_print("opt300x: write configuration failed.\n"); /* write configuration failed */
1780
1781 return 1; /* return error */
1782 }
1783
1784 return 0; /* success return 0 */
1785}
1786
1799{
1800 uint8_t res;
1801 uint16_t prev;
1802
1803 if (handle == NULL) /* check handle */
1804 {
1805 return 2; /* return error */
1806 }
1807 if (handle->inited != 1) /* check handle initialization */
1808 {
1809 return 3; /* return error */
1810 }
1811
1812 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1813 if (res != 0) /* check the result */
1814 {
1815 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1816
1817 return 1; /* return error */
1818 }
1819 *count = (opt300x_fault_count_t)((prev >> 0) & 0x03); /* set count */
1820
1821 return 0; /* success return 0 */
1822}
1823
1835{
1836 uint8_t res;
1837 uint16_t prev;
1838
1839 if (handle == NULL) /* check handle */
1840 {
1841 return 2; /* return error */
1842 }
1843 if (handle->inited != 1) /* check handle initialization */
1844 {
1845 return 3; /* return error */
1846 }
1847
1848 res = a_opt300x_iic_read(handle, OPT300X_REG_CONFIGURATION, &prev); /* read configuration */
1849 if (res != 0) /* check the result */
1850 {
1851 handle->debug_print("opt300x: read configuration failed.\n"); /* read configuration failed */
1852
1853 return 1; /* return error */
1854 }
1855
1856 if ((prev & (1 << 6)) != 0) /* check flag */
1857 {
1858 if (handle->receive_callback != NULL) /* if not null */
1859 {
1860 handle->receive_callback(OPT300X_INTERRUPT_HIGH_LIMIT); /* run the callback */
1861 }
1862 }
1863 if ((prev & (1 << 5)) != 0) /* check flag */
1864 {
1865 if (handle->receive_callback != NULL) /* if not null */
1866 {
1867 handle->receive_callback(OPT300X_INTERRUPT_LOW_LIMIT); /* run the callback */
1868 }
1869 }
1870
1871 return 0; /* success return 0 */
1872}
1873
1886uint8_t opt300x_set_reg(opt300x_handle_t *handle, uint8_t reg, uint16_t data)
1887{
1888 if (handle == NULL) /* check handle */
1889 {
1890 return 2; /* return error */
1891 }
1892 if (handle->inited != 1) /* check handle initialization */
1893 {
1894 return 3; /* return error */
1895 }
1896
1897 if (a_opt300x_iic_write(handle, reg, data) != 0) /* write data */
1898 {
1899 handle->debug_print("opt300x: write failed.\n"); /* write failed */
1900
1901 return 1; /* return error */
1902 }
1903
1904 return 0; /* success return 0 */
1905}
1906
1919uint8_t opt300x_get_reg(opt300x_handle_t *handle, uint8_t reg, uint16_t *data)
1920{
1921 if (handle == NULL) /* check handle */
1922 {
1923 return 2; /* return error */
1924 }
1925 if (handle->inited != 1) /* check handle initialization */
1926 {
1927 return 3; /* return error */
1928 }
1929
1930 if (a_opt300x_iic_read(handle, reg, data) != 0) /* read data */
1931 {
1932 handle->debug_print("opt300x: read failed.\n"); /* read failed */
1933
1934 return 1; /* return error */
1935 }
1936
1937 return 0; /* success return 0 */
1938}
1939
1949{
1950 if (info == NULL) /* check handle */
1951 {
1952 return 2; /* return error */
1953 }
1954
1955 memset(info, 0, sizeof(opt300x_info_t)); /* initialize opt300x info structure */
1956 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1957 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1958 strncpy(info->interface, "IIC", 8); /* copy interface name */
1959 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1960 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1961 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1962 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1963 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1964 info->driver_version = DRIVER_VERSION; /* set driver version */
1965
1966 return 0; /* success return 0 */
1967}
#define OPT300X_REG_HIGH_LIMIT
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define OPT300X_REG_RESULT
chip register definition
#define OPT300X_REG_DEVICE_ID
#define OPT300X_REG_MANUFACTURER_ID
#define CHIP_NAME
chip information definition
#define OPT300X_REG_LOW_LIMIT
#define OPT300X_REG_CONFIGURATION
#define DRIVER_VERSION
driver opt300x header file
uint8_t opt300x_get_range(opt300x_handle_t *handle, opt300x_range_t *range)
get range
uint8_t opt300x_set_fault_count(opt300x_handle_t *handle, opt300x_fault_count_t count)
set fault count
uint8_t opt300x_info(opt300x_info_t *info)
get chip's information
uint8_t opt300x_get_low_limit(opt300x_handle_t *handle, uint16_t *limit)
get low limit
uint8_t opt300x_get_high_limit(opt300x_handle_t *handle, uint16_t *limit)
get high limit
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 opt3002_single_read(opt300x_handle_t *handle, uint16_t *raw, float *nw_cm2)
read data from the chip
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
opt300x_bool_t
opt300x bool enumeration definition
uint8_t opt300x_get_interrupt_pin_polarity(opt300x_handle_t *handle, opt300x_interrupt_polarity_t *polarity)
get interrupt pin polarity
opt3005_range_t
opt3005 range enumeration definition
uint8_t opt300x_get_conversion_time(opt300x_handle_t *handle, opt300x_conversion_time_t *t)
get conversion time
uint8_t opt300x_start_continuous_read(opt300x_handle_t *handle)
start the chip reading
opt300x_range_t
opt300x range enumeration definition
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
struct opt300x_info_s opt300x_info_t
opt300x information structure definition
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
uint8_t opt3005_get_range(opt300x_handle_t *handle, opt3005_range_t *range)
get range
opt300x_t
opt300x type enumeration definition
uint8_t opt3002_get_range(opt300x_handle_t *handle, opt3002_range_t *range)
get range
uint8_t opt300x_set_interrupt_pin_polarity(opt300x_handle_t *handle, opt300x_interrupt_polarity_t polarity)
set interrupt pin polarity
uint8_t opt300x_get_type(opt300x_handle_t *handle, opt300x_t *type)
get the iic chip type
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_get_addr_pin(opt300x_handle_t *handle, opt300x_address_t *addr_pin)
get the iic address pin
uint8_t opt300x_get_interrupt_latch(opt300x_handle_t *handle, opt300x_bool_t *enable)
get interrupt latch status
uint8_t opt3002_limit_convert_to_data(opt300x_handle_t *handle, uint16_t reg, float *nw_cm2)
convert the register raw data to the limit threshold
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
opt300x_conversion_time_t
opt300x conversion time enumeration definition
uint8_t opt300x_get_fault_count(opt300x_handle_t *handle, opt300x_fault_count_t *count)
get fault count
uint8_t opt300x_stop_continuous_read(opt300x_handle_t *handle)
stop the chip reading
uint8_t opt300x_limit_convert_to_data(opt300x_handle_t *handle, uint16_t reg, float *lux)
convert the register raw data to the limit threshold
opt3002_range_t
opt3002 range enumeration definition
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
opt300x_interrupt_polarity_t
opt300x interrupt polarity enumeration definition
uint8_t opt3002_set_range(opt300x_handle_t *handle, opt3002_range_t range)
set range
uint8_t opt300x_single_read(opt300x_handle_t *handle, uint16_t *raw, float *lux)
read data from the chip
opt300x_fault_count_t
opt300x fault count enumeration definition
struct opt300x_handle_s opt300x_handle_t
opt300x handle structure definition
uint8_t opt300x_get_mask_exponent(opt300x_handle_t *handle, opt300x_bool_t *enable)
get mask exponent status
@ OPT3002
@ OPT3005
@ OPT300X_INTERRUPT_HIGH_LIMIT
@ OPT300X_INTERRUPT_LOW_LIMIT
uint8_t opt300x_get_reg(opt300x_handle_t *handle, uint8_t reg, uint16_t *data)
get the chip register
uint8_t opt300x_set_reg(opt300x_handle_t *handle, uint8_t reg, uint16_t data)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
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)
uint32_t driver_version
char manufacturer_name[32]