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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 
69 #define SPECTROMETER2_REG_DEVICE_ID 0x00
70 #define SPECTROMETER2_REG_REVISION_ID 0x01
71 #define SPECTROMETER2_REG_INTERRUPT_CTRL 0x02
72 #define SPECTROMETER2_REG_ALS_CTRL 0x03
73 #define SPECTROMETER2_REG_ALS_PERIOD 0x04
74 #define SPECTROMETER2_REG_ALS_CH1_DATA_H 0x06
75 #define SPECTROMETER2_REG_ALS_CH1_DATA_M 0x07
76 #define SPECTROMETER2_REG_ALS_CH1_DATA_L 0x08
77 #define SPECTROMETER2_REG_ALS_CH2_DATA_H 0x0A
78 #define SPECTROMETER2_REG_ALS_CH2_DATA_M 0x0B
79 #define SPECTROMETER2_REG_ALS_CH2_DATA_L 0x0C
80 #define SPECTROMETER2_REG_ALS_CH3_DATA_H 0x0E
81 #define SPECTROMETER2_REG_ALS_CH3_DATA_M 0x0F
82 #define SPECTROMETER2_REG_ALS_CH3_DATA_L 0x10
83 #define SPECTROMETER2_REG_ALS_CH4_DATA_H 0x12
84 #define SPECTROMETER2_REG_ALS_CH4_DATA_M 0x13
85 #define SPECTROMETER2_REG_ALS_CH4_DATA_L 0x14
86 #define SPECTROMETER2_REG_ALS_CH5_DATA_H 0x16
87 #define SPECTROMETER2_REG_ALS_CH5_DATA_M 0x17
88 #define SPECTROMETER2_REG_ALS_CH5_DATA_L 0x18
89 #define SPECTROMETER2_REG_ALS_CH6_DATA_H 0x1A
90 #define SPECTROMETER2_REG_ALS_CH6_DATA_M 0x1B
91 #define SPECTROMETER2_REG_ALS_CH6_DATA_L 0x1C
92 #define SPECTROMETER2_REG_ALS_EXPOSURE_M 0x1D
93 #define SPECTROMETER2_REG_ALS_EXPOSURE_L 0x1E
94 #define SPECTROMETER2_REG_ALS_GAIN_CH1 0x25
95 #define SPECTROMETER2_REG_ALS_GAIN_CH2 0x26
96 #define SPECTROMETER2_REG_ALS_GAIN_CH3 0x27
97 #define SPECTROMETER2_REG_ALS_GAIN_CH4 0x28
98 #define SPECTROMETER2_REG_ALS_GAIN_CH5 0x29
99 #define SPECTROMETER2_REG_ALS_GAIN_CH6 0x2A
100 #define SPECTROMETER2_REG_CHANNEL6_ENABLE 0x2D
101 #define SPECTROMETER2_REG_ALS_CHANNEL_ENABLE 0x2E
102 #define SPECTROMETER2_REG_AC_MODE_CTRL 0x31
103 #define SPECTROMETER2_REG_PEDESTAL_VALUE 0x32
104 #define SPECTROMETER2_REG_SDA_DRV_CFG 0x3C
105 #define SPECTROMETER2_REG_GPIO1_DRV_CFG 0x41
106  // spectrometer2_reg
108 
123 #define SPECTROMETER2_INTR_STATUS 0x02
124 #define SPECTROMETER2_CLR_INTR_SET 0x01
125 #define SPECTROMETER2_CLR_INTR_CLEAR 0x00
126 
131 #define SPECTROMETER2_EXPOSURE_TIME_MS_MAX 1638.4
132 #define SPECTROMETER2_EXPOSURE_TIME_MS_DEFAULT 160.0
133 #define SPECTROMETER2_EXPOSURE_TIME_MS_MIN 1.6
134 #define SPECTROMETER2_EXPOSURE_TIME_MS_LSB 1.6
135 #define SPECTROMETER2_EXPOSURE_TIME_RES 0x03FF
136 
141 #define SPECTROMETER2_IM_PERIOD_MS_MAX 5227.5
142 #define SPECTROMETER2_IM_PERIOD_MS_DEFAULT 200.0
143 #define SPECTROMETER2_IM_PERIOD_MS_MIN 0.0
144 #define SPECTROMETER2_IM_PERIOD_MS_LSB 20.5
145 #define SPECTROMETER2_IM_PERIOD_RES 0xFF
146 
151 #define SPECTROMETER2_AGAIN_66p6X 0x01
152 #define SPECTROMETER2_AGAIN_50X 0x02
153 #define SPECTROMETER2_AGAIN_33X 0x03
154 #define SPECTROMETER2_AGAIN_25X 0x04
155 #define SPECTROMETER2_AGAIN_16X 0x05
156 #define SPECTROMETER2_AGAIN_10X 0x06
157 #define SPECTROMETER2_AGAIN_7p1X 0x07
158 #define SPECTROMETER2_AGAIN_5X 0x08
159 #define SPECTROMETER2_AGAIN_3p33X 0x09
160 #define SPECTROMETER2_AGAIN_2p5X 0x0A
161 #define SPECTROMETER2_AGAIN_1p67X 0x0B
162 #define SPECTROMETER2_AGAIN_1p25X 0x0C
163 #define SPECTROMETER2_AGAIN_1X 0x0D
164 #define SPECTROMETER2_AGAIN_0p83X 0x0E
165 #define SPECTROMETER2_AGAIN_0p71X 0x0F
166 #define SPECTROMETER2_AGAIN_BIT_MASK 0x0F
167 
172 #define SPECTROMETER2_CHANNEL6_DISABLE 0x00
173 #define SPECTROMETER2_CHANNEL6_ENABLE 0x01
174 
179 #define SPECTROMETER2_NO_CHANNEL_ENABLED 0x00
180 #define SPECTROMETER2_CHANNEL1_ENABLE 0x01
181 #define SPECTROMETER2_CHANNEL2_ENABLE 0x02
182 #define SPECTROMETER2_CHANNEL3_ENABLE 0x04
183 #define SPECTROMETER2_CHANNEL4_ENABLE 0x08
184 #define SPECTROMETER2_CHANNEL5_ENABLE 0x10
185 #define SPECTROMETER2_ALL_CHANNELS_ENABLE 0x1F
186 
191 #define SPECTROMETER2_ALS_MODE_SINGLE 0x00
192 #define SPECTROMETER2_ALS_MODE_CONTINUOUS 0x06
193 #define SPECTROMETER2_ALS_STOP 0x00
194 #define SPECTROMETER2_ALS_START 0x01
195 
200 #define SPECTROMETER2_UNKNOWN_COLOR 0
201 #define SPECTROMETER2_RED_COLOR 1
202 #define SPECTROMETER2_YELLOW_COLOR 2
203 #define SPECTROMETER2_GREEN_COLOR 3
204 #define SPECTROMETER2_CYAN_COLOR 4
205 #define SPECTROMETER2_BLUE_COLOR 5
206 #define SPECTROMETER2_MAGENTA_COLOR 6
207 #define SPECTROMETER2_WHITE_COLOR 7
208 #define SPECTROMETER2_BLACK_COLOR 8
209 
214 #define SPECTROMETER2_WHITE_COLOR_LIGHTNESS 95
215 #define SPECTROMETER2_BLACK_COLOR_LIGHTNESS 5
216 #define SPECTROMETER2_YELLOW_COLOR_HUE_ANGLE 30
217 #define SPECTROMETER2_GREEN_COLOR_HUE_ANGLE 90
218 #define SPECTROMETER2_CYAN_COLOR_HUE_ANGLE 150
219 #define SPECTROMETER2_BLUE_COLOR_HUE_ANGLE 210
220 #define SPECTROMETER2_MAGENTA_COLOR_HUE_ANGLE 270
221 #define SPECTROMETER2_RED_COLOR_HUE_ANGLE 330
222 
227 #define SPECTROMETER2_HSL_HUE_RESOLUTION 360
228 #define SPECTROMETER2_HSL_SATURATION_RESOLUTION 100
229 #define SPECTROMETER2_HSL_LIGHTNESS_RESOLUTION 100
230 
235 #define SPECTROMETER2_DEVICE_ID 0x70
236 
242 #define SPECTROMETER2_DEVICE_ADDRESS 0x20
243  // spectrometer2_set
245 
260 #define SPECTROMETER2_MAP_MIKROBUS( cfg, mikrobus ) \
261  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
262  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
263  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
264  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
265  // spectrometer2_map // spectrometer2
268 
273 typedef struct
274 {
275  // Input pins
276  digital_in_t an;
277  digital_in_t int_pin;
279  // Modules
280  i2c_master_t i2c;
282  // I2C slave address
283  uint8_t slave_address;
286 
291 typedef struct
292 {
293  pin_name_t scl;
294  pin_name_t sda;
296  pin_name_t an;
297  pin_name_t int_pin;
299  uint32_t i2c_speed;
300  uint8_t i2c_address;
303 
308 typedef struct
309 {
310  uint32_t red;
311  uint32_t visible;
312  uint32_t blue;
313  uint32_t green;
314  uint32_t ir;
315  uint32_t clear;
316 
318 
323 typedef struct
324 {
325  float hue;
326  float saturation;
327  float lightness;
328 
330 
335 typedef enum
336 {
339 
341 
358 
373 
387 
402 err_t spectrometer2_generic_write ( spectrometer2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
403 
418 err_t spectrometer2_generic_read ( spectrometer2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
419 
432 err_t spectrometer2_write_register ( spectrometer2_t *ctx, uint8_t reg, uint8_t data_in );
433 
446 err_t spectrometer2_read_register ( spectrometer2_t *ctx, uint8_t reg, uint8_t *data_out );
447 
459 
469 
479 
493 
505 
517 err_t spectrometer2_set_exposure_time ( spectrometer2_t *ctx, float exp_time );
518 
531 
542 
554 
555 #ifdef __cplusplus
556 }
557 #endif
558 #endif // SPECTROMETER2_H
559  // spectrometer2
561 
562 // ------------------------------------------------------------------------ END
spectrometer2_als_channels_t::green
uint32_t green
Definition: spectrometer2.h:313
spectrometer2_als_channels_t::ir
uint32_t ir
Definition: spectrometer2.h:314
spectrometer2_hsl_t
Spectrometer 2 HSL color data object.
Definition: spectrometer2.h:324
spectrometer2_als_channels_t::blue
uint32_t blue
Definition: spectrometer2.h:312
spectrometer2_cfg_t::an
pin_name_t an
Definition: spectrometer2.h:296
spectrometer2_t
Spectrometer 2 Click context object.
Definition: spectrometer2.h:274
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:315
spectrometer2_t::an
digital_in_t an
Definition: spectrometer2.h:276
spectrometer2_t::i2c
i2c_master_t i2c
Definition: spectrometer2.h:280
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:294
spectrometer2_cfg_t::scl
pin_name_t scl
Definition: spectrometer2.h:293
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:338
spectrometer2_als_channels_t::red
uint32_t red
Definition: spectrometer2.h:310
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:299
spectrometer2_hsl_t::hue
float hue
Definition: spectrometer2.h:325
spectrometer2_hsl_t::lightness
float lightness
Definition: spectrometer2.h:327
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:300
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:283
spectrometer2_als_channels_t
Spectrometer 2 ALS channels data object.
Definition: spectrometer2.h:309
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:326
spectrometer2_cfg_t::int_pin
pin_name_t int_pin
Definition: spectrometer2.h:297
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:277
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:311
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:292
spectrometer2_cfg_setup
void spectrometer2_cfg_setup(spectrometer2_cfg_t *cfg)
Spectrometer 2 configuration object setup function.
SPECTROMETER2_OK
@ SPECTROMETER2_OK
Definition: spectrometer2.h:337
spectrometer2_return_value_t
spectrometer2_return_value_t
Spectrometer 2 Click return value data.
Definition: spectrometer2.h:336