spectrometer2  2.0.0.0
spectrometer2.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef SPECTROMETER2_H
29 #define SPECTROMETER2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define SPECTROMETER2_REG_DEVICE_ID 0x00
74 #define SPECTROMETER2_REG_REVISION_ID 0x01
75 #define SPECTROMETER2_REG_INTERRUPT_CTRL 0x02
76 #define SPECTROMETER2_REG_ALS_CTRL 0x03
77 #define SPECTROMETER2_REG_ALS_PERIOD 0x04
78 #define SPECTROMETER2_REG_ALS_CH1_DATA_H 0x06
79 #define SPECTROMETER2_REG_ALS_CH1_DATA_M 0x07
80 #define SPECTROMETER2_REG_ALS_CH1_DATA_L 0x08
81 #define SPECTROMETER2_REG_ALS_CH2_DATA_H 0x0A
82 #define SPECTROMETER2_REG_ALS_CH2_DATA_M 0x0B
83 #define SPECTROMETER2_REG_ALS_CH2_DATA_L 0x0C
84 #define SPECTROMETER2_REG_ALS_CH3_DATA_H 0x0E
85 #define SPECTROMETER2_REG_ALS_CH3_DATA_M 0x0F
86 #define SPECTROMETER2_REG_ALS_CH3_DATA_L 0x10
87 #define SPECTROMETER2_REG_ALS_CH4_DATA_H 0x12
88 #define SPECTROMETER2_REG_ALS_CH4_DATA_M 0x13
89 #define SPECTROMETER2_REG_ALS_CH4_DATA_L 0x14
90 #define SPECTROMETER2_REG_ALS_CH5_DATA_H 0x16
91 #define SPECTROMETER2_REG_ALS_CH5_DATA_M 0x17
92 #define SPECTROMETER2_REG_ALS_CH5_DATA_L 0x18
93 #define SPECTROMETER2_REG_ALS_CH6_DATA_H 0x1A
94 #define SPECTROMETER2_REG_ALS_CH6_DATA_M 0x1B
95 #define SPECTROMETER2_REG_ALS_CH6_DATA_L 0x1C
96 #define SPECTROMETER2_REG_ALS_EXPOSURE_M 0x1D
97 #define SPECTROMETER2_REG_ALS_EXPOSURE_L 0x1E
98 #define SPECTROMETER2_REG_ALS_GAIN_CH1 0x25
99 #define SPECTROMETER2_REG_ALS_GAIN_CH2 0x26
100 #define SPECTROMETER2_REG_ALS_GAIN_CH3 0x27
101 #define SPECTROMETER2_REG_ALS_GAIN_CH4 0x28
102 #define SPECTROMETER2_REG_ALS_GAIN_CH5 0x29
103 #define SPECTROMETER2_REG_ALS_GAIN_CH6 0x2A
104 #define SPECTROMETER2_REG_CHANNEL6_ENABLE 0x2D
105 #define SPECTROMETER2_REG_ALS_CHANNEL_ENABLE 0x2E
106 #define SPECTROMETER2_REG_AC_MODE_CTRL 0x31
107 #define SPECTROMETER2_REG_PEDESTAL_VALUE 0x32
108 #define SPECTROMETER2_REG_SDA_DRV_CFG 0x3C
109 #define SPECTROMETER2_REG_GPIO1_DRV_CFG 0x41
110  // spectrometer2_reg
112 
127 #define SPECTROMETER2_INTR_STATUS 0x02
128 #define SPECTROMETER2_CLR_INTR_SET 0x01
129 #define SPECTROMETER2_CLR_INTR_CLEAR 0x00
130 
135 #define SPECTROMETER2_EXPOSURE_TIME_MS_MAX 1638.4
136 #define SPECTROMETER2_EXPOSURE_TIME_MS_DEFAULT 160.0
137 #define SPECTROMETER2_EXPOSURE_TIME_MS_MIN 1.6
138 #define SPECTROMETER2_EXPOSURE_TIME_MS_LSB 1.6
139 #define SPECTROMETER2_EXPOSURE_TIME_RES 0x03FF
140 
145 #define SPECTROMETER2_IM_PERIOD_MS_MAX 5227.5
146 #define SPECTROMETER2_IM_PERIOD_MS_DEFAULT 200.0
147 #define SPECTROMETER2_IM_PERIOD_MS_MIN 0.0
148 #define SPECTROMETER2_IM_PERIOD_MS_LSB 20.5
149 #define SPECTROMETER2_IM_PERIOD_RES 0xFF
150 
155 #define SPECTROMETER2_AGAIN_66p6X 0x01
156 #define SPECTROMETER2_AGAIN_50X 0x02
157 #define SPECTROMETER2_AGAIN_33X 0x03
158 #define SPECTROMETER2_AGAIN_25X 0x04
159 #define SPECTROMETER2_AGAIN_16X 0x05
160 #define SPECTROMETER2_AGAIN_10X 0x06
161 #define SPECTROMETER2_AGAIN_7p1X 0x07
162 #define SPECTROMETER2_AGAIN_5X 0x08
163 #define SPECTROMETER2_AGAIN_3p33X 0x09
164 #define SPECTROMETER2_AGAIN_2p5X 0x0A
165 #define SPECTROMETER2_AGAIN_1p67X 0x0B
166 #define SPECTROMETER2_AGAIN_1p25X 0x0C
167 #define SPECTROMETER2_AGAIN_1X 0x0D
168 #define SPECTROMETER2_AGAIN_0p83X 0x0E
169 #define SPECTROMETER2_AGAIN_0p71X 0x0F
170 #define SPECTROMETER2_AGAIN_BIT_MASK 0x0F
171 
176 #define SPECTROMETER2_CHANNEL6_DISABLE 0x00
177 #define SPECTROMETER2_CHANNEL6_ENABLE 0x01
178 
183 #define SPECTROMETER2_NO_CHANNEL_ENABLED 0x00
184 #define SPECTROMETER2_CHANNEL1_ENABLE 0x01
185 #define SPECTROMETER2_CHANNEL2_ENABLE 0x02
186 #define SPECTROMETER2_CHANNEL3_ENABLE 0x04
187 #define SPECTROMETER2_CHANNEL4_ENABLE 0x08
188 #define SPECTROMETER2_CHANNEL5_ENABLE 0x10
189 #define SPECTROMETER2_ALL_CHANNELS_ENABLE 0x1F
190 
195 #define SPECTROMETER2_ALS_MODE_SINGLE 0x00
196 #define SPECTROMETER2_ALS_MODE_CONTINUOUS 0x06
197 #define SPECTROMETER2_ALS_STOP 0x00
198 #define SPECTROMETER2_ALS_START 0x01
199 
204 #define SPECTROMETER2_UNKNOWN_COLOR 0
205 #define SPECTROMETER2_RED_COLOR 1
206 #define SPECTROMETER2_YELLOW_COLOR 2
207 #define SPECTROMETER2_GREEN_COLOR 3
208 #define SPECTROMETER2_CYAN_COLOR 4
209 #define SPECTROMETER2_BLUE_COLOR 5
210 #define SPECTROMETER2_MAGENTA_COLOR 6
211 #define SPECTROMETER2_WHITE_COLOR 7
212 #define SPECTROMETER2_BLACK_COLOR 8
213 
218 #define SPECTROMETER2_WHITE_COLOR_LIGHTNESS 95
219 #define SPECTROMETER2_BLACK_COLOR_LIGHTNESS 5
220 #define SPECTROMETER2_YELLOW_COLOR_HUE_ANGLE 30
221 #define SPECTROMETER2_GREEN_COLOR_HUE_ANGLE 90
222 #define SPECTROMETER2_CYAN_COLOR_HUE_ANGLE 150
223 #define SPECTROMETER2_BLUE_COLOR_HUE_ANGLE 210
224 #define SPECTROMETER2_MAGENTA_COLOR_HUE_ANGLE 270
225 #define SPECTROMETER2_RED_COLOR_HUE_ANGLE 330
226 
231 #define SPECTROMETER2_HSL_HUE_RESOLUTION 360
232 #define SPECTROMETER2_HSL_SATURATION_RESOLUTION 100
233 #define SPECTROMETER2_HSL_LIGHTNESS_RESOLUTION 100
234 
239 #define SPECTROMETER2_DEVICE_ID 0x70
240 
246 #define SPECTROMETER2_DEVICE_ADDRESS 0x20
247  // spectrometer2_set
249 
264 #define SPECTROMETER2_MAP_MIKROBUS( cfg, mikrobus ) \
265  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
266  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
267  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
268  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
269  // spectrometer2_map // spectrometer2
272 
277 typedef struct
278 {
279  // Input pins
280  digital_in_t an;
281  digital_in_t int_pin;
283  // Modules
284  i2c_master_t i2c;
286  // I2C slave address
287  uint8_t slave_address;
290 
295 typedef struct
296 {
297  pin_name_t scl;
298  pin_name_t sda;
300  pin_name_t an;
301  pin_name_t int_pin;
303  uint32_t i2c_speed;
304  uint8_t i2c_address;
307 
312 typedef struct
313 {
314  uint32_t red;
315  uint32_t visible;
316  uint32_t blue;
317  uint32_t green;
318  uint32_t ir;
319  uint32_t clear;
320 
322 
327 typedef struct
328 {
329  float hue;
330  float saturation;
331  float lightness;
332 
334 
339 typedef enum
340 {
343 
345 
362 
377 
391 
406 err_t spectrometer2_generic_write ( spectrometer2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
407 
422 err_t spectrometer2_generic_read ( spectrometer2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
423 
436 err_t spectrometer2_write_register ( spectrometer2_t *ctx, uint8_t reg, uint8_t data_in );
437 
450 err_t spectrometer2_read_register ( spectrometer2_t *ctx, uint8_t reg, uint8_t *data_out );
451 
463 
473 
483 
497 
509 
521 err_t spectrometer2_set_exposure_time ( spectrometer2_t *ctx, float exp_time );
522 
535 
546 
558 
559 #ifdef __cplusplus
560 }
561 #endif
562 #endif // SPECTROMETER2_H
563  // spectrometer2
565 
566 // ------------------------------------------------------------------------ END
spectrometer2_als_channels_t::green
uint32_t green
Definition: spectrometer2.h:317
spectrometer2_als_channels_t::ir
uint32_t ir
Definition: spectrometer2.h:318
spectrometer2_hsl_t
Spectrometer 2 HSL color data object.
Definition: spectrometer2.h:328
spectrometer2_als_channels_t::blue
uint32_t blue
Definition: spectrometer2.h:316
spectrometer2_cfg_t::an
pin_name_t an
Definition: spectrometer2.h:300
spectrometer2_t
Spectrometer 2 Click context object.
Definition: spectrometer2.h:278
spectrometer2_write_register
err_t spectrometer2_write_register(spectrometer2_t *ctx, uint8_t reg, uint8_t data_in)
Spectrometer 2 write register function.
spectrometer2_als_channels_t::clear
uint32_t clear
Definition: spectrometer2.h:319
spectrometer2_t::an
digital_in_t an
Definition: spectrometer2.h:280
spectrometer2_t::i2c
i2c_master_t i2c
Definition: spectrometer2.h:284
spectrometer2_get_data
err_t spectrometer2_get_data(spectrometer2_t *ctx, spectrometer2_als_channels_t *als_channels)
Spectrometer 2 get data function.
spectrometer2_rgbc_to_hsl
void spectrometer2_rgbc_to_hsl(spectrometer2_als_channels_t *rgbc, spectrometer2_hsl_t *hsl)
Spectrometer 2 rgbc to hsl function.
spectrometer2_cfg_t::sda
pin_name_t sda
Definition: spectrometer2.h:298
spectrometer2_cfg_t::scl
pin_name_t scl
Definition: spectrometer2.h:297
spectrometer2_set_exposure_time
err_t spectrometer2_set_exposure_time(spectrometer2_t *ctx, float exp_time)
Spectrometer 2 set exposure time function.
SPECTROMETER2_ERROR
@ SPECTROMETER2_ERROR
Definition: spectrometer2.h:342
spectrometer2_als_channels_t::red
uint32_t red
Definition: spectrometer2.h:314
spectrometer2_set_inter_measurement_period
err_t spectrometer2_set_inter_measurement_period(spectrometer2_t *ctx, float im_period)
Spectrometer 2 set inter measurement period function.
spectrometer2_get_an_pin
uint8_t spectrometer2_get_an_pin(spectrometer2_t *ctx)
Spectrometer 2 get an pin function.
spectrometer2_get_color
uint8_t spectrometer2_get_color(spectrometer2_hsl_t *hsl)
Spectrometer 2 get color function.
spectrometer2_read_register
err_t spectrometer2_read_register(spectrometer2_t *ctx, uint8_t reg, uint8_t *data_out)
Spectrometer 2 read register function.
spectrometer2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: spectrometer2.h:303
spectrometer2_hsl_t::hue
float hue
Definition: spectrometer2.h:329
spectrometer2_hsl_t::lightness
float lightness
Definition: spectrometer2.h:331
spectrometer2_default_cfg
err_t spectrometer2_default_cfg(spectrometer2_t *ctx)
Spectrometer 2 default configuration function.
spectrometer2_cfg_t::i2c_address
uint8_t i2c_address
Definition: spectrometer2.h:304
spectrometer2_get_int_pin
uint8_t spectrometer2_get_int_pin(spectrometer2_t *ctx)
Spectrometer 2 get int pin function.
spectrometer2_t::slave_address
uint8_t slave_address
Definition: spectrometer2.h:287
spectrometer2_als_channels_t
Spectrometer 2 ALS channels data object.
Definition: spectrometer2.h:313
spectrometer2_generic_read
err_t spectrometer2_generic_read(spectrometer2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Spectrometer 2 I2C reading function.
spectrometer2_hsl_t::saturation
float saturation
Definition: spectrometer2.h:330
spectrometer2_cfg_t::int_pin
pin_name_t int_pin
Definition: spectrometer2.h:301
spectrometer2_check_communication
err_t spectrometer2_check_communication(spectrometer2_t *ctx)
Spectrometer 2 check communication function.
spectrometer2_t::int_pin
digital_in_t int_pin
Definition: spectrometer2.h:281
spectrometer2_init
err_t spectrometer2_init(spectrometer2_t *ctx, spectrometer2_cfg_t *cfg)
Spectrometer 2 initialization function.
spectrometer2_als_channels_t::visible
uint32_t visible
Definition: spectrometer2.h:315
spectrometer2_generic_write
err_t spectrometer2_generic_write(spectrometer2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Spectrometer 2 I2C writing function.
spectrometer2_clear_interrupt
err_t spectrometer2_clear_interrupt(spectrometer2_t *ctx)
Spectrometer 2 clear interrupt function.
spectrometer2_cfg_t
Spectrometer 2 Click configuration object.
Definition: spectrometer2.h:296
spectrometer2_cfg_setup
void spectrometer2_cfg_setup(spectrometer2_cfg_t *cfg)
Spectrometer 2 configuration object setup function.
SPECTROMETER2_OK
@ SPECTROMETER2_OK
Definition: spectrometer2.h:341
spectrometer2_return_value_t
spectrometer2_return_value_t
Spectrometer 2 Click return value data.
Definition: spectrometer2.h:340