color2  2.0.0.0
color2.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef COLOR2_H
36 #define COLOR2_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define COLOR2_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
57 
63 #define COLOR2_OK 0
64 #define COLOR2_ERROR -1
65 
71 #define COLOR2_I2C_ADDR 0x44
72 #define COLOR2_DEVICE_ID 0x00
73 #define COLOR2_CONFIG_1 0x01
74 #define COLOR2_CONFIG_2 0x02
75 #define COLOR2_CONFIG_3 0x03
76 #define COLOR2_THRESHOLD_LL 0x04
77 #define COLOR2_THRESHOLD_LH 0x05
78 #define COLOR2_THRESHOLD_HL 0x06
79 #define COLOR2_THRESHOLD_HH 0x07
80 #define COLOR2_STATUS 0x08
81 #define COLOR2_GREEN_L 0x09
82 #define COLOR2_GREEN_H 0x0A
83 #define COLOR2_RED_L 0x0B
84 #define COLOR2_RED_H 0x0C
85 #define COLOR2_BLUE_L 0x0D
86 #define COLOR2_BLUE_H 0x0E
87 #define COLOR2_CFG_DEFAULT 0x00
88 #define COLOR2_CFG1_MODE_POWERDOWN 0x00
89 #define COLOR2_CFG1_MODE_G 0x01
90 #define COLOR2_CFG1_MODE_R 0x02
91 #define COLOR2_CFG1_MODE_B 0x03
92 #define COLOR2_CFG1_MODE_STANDBY 0x04
93 #define COLOR2_CFG1_MODE_RGB 0x05
94 #define COLOR2_CFG1_MODE_RG 0x06
95 #define COLOR2_CFG1_MODE_GB 0x07
96 #define COLOR2_CFG1_375LUX 0x00
97 #define COLOR2_CFG1_10KLUX 0x08
98 #define COLOR2_CFG1_16BIT 0x00
99 #define COLOR2_CFG1_12BIT 0x10
100 #define COLOR2_CFG1_ADC_SYNC_NORMAL 0x00
101 #define COLOR2_CFG1_ADC_SYNC_TO_INT 0x20
102 #define COLOR2_CFG2_IR_OFFSET_OFF 0x00
103 #define COLOR2_CFG2_IR_OFFSET_ON 0x80
104 #define COLOR2_CFG2_IR_ADJUST_LOW 0x00
105 #define COLOR2_CFG2_IR_ADJUST_MID 0x20
106 #define COLOR2_CFG2_IR_ADJUST_HIGH 0x3F
107 
113 #define COLOR2_CFG3_NO_INT 0x00
114 #define COLOR2_CFG3_G_INT 0x01
115 #define COLOR2_CFG3_R_INT 0x02
116 #define COLOR2_CFG3_B_INT 0x03
117 #define COLOR2_CFG3_INT_PRST1 0x00
118 #define COLOR2_CFG3_INT_PRST2 0x04
119 #define COLOR2_CFG3_INT_PRST4 0x08
120 #define COLOR2_CFG3_INT_PRST8 0x0C
121 #define COLOR2_CFG3_RGB_CONV_TO_INT_DISABLE 0x00
122 #define COLOR2_CFG3_RGB_CONV_TO_INT_ENABLE 0x10
123 
129 #define COLOR2_FLAG_INT 0x01
130 #define COLOR2_FLAG_CONV_DONE 0x02
131 #define COLOR2_FLAG_BROWNOUT 0x04
132 #define COLOR2_FLAG_CONV_G 0x10
133 #define COLOR2_FLAG_CONV_R 0x20
134 #define COLOR2_FLAG_CONV_B 0x30
135 
141 #define COLOR2_UNKNOWN_COLOR 0
142 #define COLOR2_RED_COLOR 1
143 #define COLOR2_YELLOW_COLOR 2
144 #define COLOR2_GREEN_COLOR 3
145 #define COLOR2_CYAN_COLOR 4
146 #define COLOR2_BLUE_COLOR 5
147 #define COLOR2_MAGENTA_COLOR 6
148 #define COLOR2_WHITE_COLOR 7
149 #define COLOR2_BLACK_COLOR 8
150 
155 #define COLOR2_WHITE_COLOR_LIGHTNESS 97
156 #define COLOR2_BLACK_COLOR_LIGHTNESS 3
157 #define COLOR2_YELLOW_COLOR_HUE_ANGLE 30
158 #define COLOR2_GREEN_COLOR_HUE_ANGLE 90
159 #define COLOR2_CYAN_COLOR_HUE_ANGLE 150
160 #define COLOR2_BLUE_COLOR_HUE_ANGLE 210
161 #define COLOR2_MAGENTA_COLOR_HUE_ANGLE 270
162 #define COLOR2_RED_COLOR_HUE_ANGLE 330
163 
168 #define COLOR2_HSL_HUE_RESOLUTION 360
169 #define COLOR2_HSL_SATURATION_RESOLUTION 100
170 #define COLOR2_HSL_LIGHTNESS_RESOLUTION 100
171  // End group macro
173 // --------------------------------------------------------------- PUBLIC TYPES
182 typedef struct
183 {
184  // Input pins
185  digital_in_t int_pin;
186 
187  // Modules
188  i2c_master_t i2c;
189 
190  // ctx variable
191  uint8_t slave_address;
192 
193 } color2_t;
194 
198 typedef struct
199 {
200  // Communication gpio pins
201  pin_name_t scl;
202  pin_name_t sda;
203 
204  // Additional gpio pins
205  pin_name_t int_pin;
206 
207  // static variable
208  uint32_t i2c_speed;
209  uint8_t i2c_address;
210 
211 } color2_cfg_t;
212 
217 typedef struct
218 {
219  uint16_t red;
220  uint16_t green;
221  uint16_t blue;
222 
223 } color2_rgb_t;
224 
229 typedef struct
230 {
231  float hue;
232  float saturation;
233  float lightness;
234 
235 } color2_hsl_t;
236  // End types group
238 
239 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
240 
246 #ifdef __cplusplus
247 extern "C"{
248 #endif
249 
259 
269 err_t color2_init ( color2_t *ctx, color2_cfg_t *cfg );
270 
281 
296 err_t color2_generic_write ( color2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
297 
312 err_t color2_generic_read ( color2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
313 
325 err_t color2_write_reg ( color2_t *ctx, uint8_t reg, uint8_t data_in );
326 
338 err_t color2_read_reg ( color2_t *ctx, uint8_t reg, uint8_t *data_out );
339 
347 uint8_t color2_get_int_pin ( color2_t *ctx );
348 
357 err_t color2_reset ( color2_t *ctx );
358 
370 err_t color2_init_advanced ( color2_t *ctx, uint8_t config1, uint8_t config2, uint8_t config3 );
371 
381 err_t color2_set_upper_threshold ( color2_t *ctx, uint16_t upper_threshold );
382 
392 err_t color2_set_lower_hreshold ( color2_t *ctx, uint16_t lower_threshold );
393 
403 err_t color2_read_upper_threshold ( color2_t *ctx, uint16_t *upper_threshold );
404 
414 err_t color2_read_lower_threshold ( color2_t *ctx, uint16_t *lower_threshold );
415 
426 err_t color2_read_rgb ( color2_t *ctx, color2_rgb_t *rgb );
427 
437 err_t color2_read_status ( color2_t *ctx, uint8_t *status );
438 
452 
463 
464 #ifdef __cplusplus
465 }
466 #endif
467 #endif // _COLOR2_H_
468  // End public_function group
471 
472 // ------------------------------------------------------------------------- END
color2_generic_write
err_t color2_generic_write(color2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Color 2 I2C writing function.
color2_cfg_t
Click configuration structure definition.
Definition: color2.h:199
color2_read_upper_threshold
err_t color2_read_upper_threshold(color2_t *ctx, uint16_t *upper_threshold)
Function read upper threshold register.
color2_cfg_t::i2c_address
uint8_t i2c_address
Definition: color2.h:209
color2_hsl_t::hue
float hue
Definition: color2.h:231
color2_set_lower_hreshold
err_t color2_set_lower_hreshold(color2_t *ctx, uint16_t lower_threshold)
Function set lower threshold register.
color2_t::i2c
i2c_master_t i2c
Definition: color2.h:188
color2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: color2.h:208
color2_generic_read
err_t color2_generic_read(color2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Color 2 I2C reading function.
color2_read_reg
err_t color2_read_reg(color2_t *ctx, uint8_t reg, uint8_t *data_out)
Generic read data function.
color2_t::int_pin
digital_in_t int_pin
Definition: color2.h:185
color2_t::slave_address
uint8_t slave_address
Definition: color2.h:191
color2_init_advanced
err_t color2_init_advanced(color2_t *ctx, uint8_t config1, uint8_t config2, uint8_t config3)
Init advanced function.
color2_rgb_t::red
uint16_t red
Definition: color2.h:219
color2_cfg_t::sda
pin_name_t sda
Definition: color2.h:202
color2_rgb_t::green
uint16_t green
Definition: color2.h:220
color2_cfg_setup
void color2_cfg_setup(color2_cfg_t *cfg)
Config Object Initialization function.
color2_rgb_to_hsl
void color2_rgb_to_hsl(color2_t *ctx, color2_rgb_t *rgb, color2_hsl_t *hsl)
Functions for read color value.
color2_rgb_t
Color 2 RGB channels data object.
Definition: color2.h:218
color2_hsl_t
Color 2 HSL color data object.
Definition: color2.h:230
color2_set_upper_threshold
err_t color2_set_upper_threshold(color2_t *ctx, uint16_t upper_threshold)
Function set upper threshold register.
color2_get_int_pin
uint8_t color2_get_int_pin(color2_t *ctx)
Get int pin function.
color2_cfg_t::scl
pin_name_t scl
Definition: color2.h:201
color2_rgb_t::blue
uint16_t blue
Definition: color2.h:221
color2_hsl_t::lightness
float lightness
Definition: color2.h:233
color2_write_reg
err_t color2_write_reg(color2_t *ctx, uint8_t reg, uint8_t data_in)
Generic write data function.
color2_get_color
uint8_t color2_get_color(color2_hsl_t *hsl)
Functions for detect colors.
color2_hsl_t::saturation
float saturation
Definition: color2.h:232
color2_cfg_t::int_pin
pin_name_t int_pin
Definition: color2.h:205
color2_reset
err_t color2_reset(color2_t *ctx)
Reset default register value function.
color2_t
Click ctx object definition.
Definition: color2.h:183
color2_read_rgb
err_t color2_read_rgb(color2_t *ctx, color2_rgb_t *rgb)
Function read rgb data.
color2_read_status
err_t color2_read_status(color2_t *ctx, uint8_t *status)
Function read status registe value.
color2_default_cfg
err_t color2_default_cfg(color2_t *ctx)
Click Default Configuration function.
color2_read_lower_threshold
err_t color2_read_lower_threshold(color2_t *ctx, uint16_t *lower_threshold)
Function read lower threshold register.
color2_init
err_t color2_init(color2_t *ctx, color2_cfg_t *cfg)
Initialization function.