LibDriver OPT300X
Loading...
Searching...
No Matches
driver_opt300x_basic.c
Go to the documentation of this file.
1
36
38
39static opt300x_handle_t gs_handle;
40
51{
52 uint8_t res;
53
54 /* link interface function */
63
64 /* set chip type */
65 res = opt300x_set_type(&gs_handle, type);
66 if (res != 0)
67 {
68 opt300x_interface_debug_print("opt300x: set type failed.\n");
69
70 return 1;
71 }
72
73 /* set iic address */
74 res = opt300x_set_addr_pin(&gs_handle, addr_pin);
75 if (res != 0)
76 {
77 opt300x_interface_debug_print("opt300x: set addr pin failed.\n");
78
79 return 1;
80 }
81
82 /* opt300x init */
83 res = opt300x_init(&gs_handle);
84 if (res != 0)
85 {
86 opt300x_interface_debug_print("opt300x: init failed.\n");
87
88 return 1;
89 }
90
91 /* set default range */
92 if (type == OPT3002)
93 {
95 if (res != 0)
96 {
97 opt300x_interface_debug_print("opt3002: set range failed.\n");
98 (void)opt300x_deinit(&gs_handle);
99
100 return 1;
101 }
102 }
103 else if (type == OPT3005)
104 {
106 if (res != 0)
107 {
108 opt300x_interface_debug_print("opt3005: set range failed.\n");
109 (void)opt300x_deinit(&gs_handle);
110
111 return 1;
112 }
113 }
114 else
115 {
117 if (res != 0)
118 {
119 opt300x_interface_debug_print("opt300x: set range failed.\n");
120 (void)opt300x_deinit(&gs_handle);
121
122 return 1;
123 }
124 }
125
126 /* set default conversion time */
128 if (res != 0)
129 {
130 opt300x_interface_debug_print("opt300x: set conversion time failed.\n");
131 (void)opt300x_deinit(&gs_handle);
132
133 return 1;
134 }
135
136 /* set default mask exponent */
138 if (res != 0)
139 {
140 opt300x_interface_debug_print("opt300x: set mask exponent failed.\n");
141 (void)opt300x_deinit(&gs_handle);
142
143 return 1;
144 }
145
146 /* set default fault count */
148 if (res != 0)
149 {
150 opt300x_interface_debug_print("opt300x: set fault count failed.\n");
151 (void)opt300x_deinit(&gs_handle);
152
153 return 1;
154 }
155
156 /* start continuous read */
157 res = opt300x_start_continuous_read(&gs_handle);
158 if (res != 0)
159 {
160 opt300x_interface_debug_print("opt300x: start continuous read failed.\n");
161 (void)opt300x_deinit(&gs_handle);
162
163 return 1;
164 }
165
166 return 0;
167}
168
177uint8_t opt300x_basic_read(float *lux)
178{
179 uint16_t raw;
180
181 /* read data */
182 if (opt300x_continuous_read(&gs_handle, (uint16_t *)&raw, lux) != 0)
183 {
184 return 1;
185 }
186
187 return 0;
188}
189
198uint8_t opt3002_basic_read(float *nw_cm2)
199{
200 uint16_t raw;
201
202 /* read data */
203 if (opt3002_continuous_read(&gs_handle, (uint16_t *)&raw, nw_cm2) != 0)
204 {
205 return 1;
206 }
207
208 return 0;
209}
210
219{
220 /* stop continuous read */
221 if (opt300x_stop_continuous_read(&gs_handle) != 0)
222 {
223 return 1;
224 }
225
226 /* close opt300x */
227 if (opt300x_deinit(&gs_handle) != 0)
228 {
229 return 1;
230 }
231
232 return 0;
233}
driver opt300x basic 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_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
opt300x_t
opt300x type enumeration definition
uint8_t opt300x_set_conversion_time(opt300x_handle_t *handle, opt300x_conversion_time_t t)
set conversion time
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 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
uint8_t opt300x_basic_read(float *lux)
basic example read
#define OPT300X_BASIC_DEFAULT_FAULT_COUNT
#define OPT300X_BASIC_DEFAULT_CONVERSION_TIME
uint8_t opt300x_basic_init(opt300x_t type, opt300x_address_t addr_pin)
basic example init
#define OPT3002_BASIC_DEFAULT_RANGE
opt300x basic example default definition
uint8_t opt300x_basic_deinit(void)
basic example deinit
uint8_t opt3002_basic_read(float *nw_cm2)
basic example read
#define OPT300X_BASIC_DEFAULT_RANGE
#define OPT300X_BASIC_DEFAULT_MASK_EXPONENT
#define OPT3005_BASIC_DEFAULT_RANGE
void opt300x_interface_delay_ms(uint32_t ms)
interface delay ms
void opt300x_interface_receive_callback(uint8_t type)
interface receive callback
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