color3  2.0.0.0
color3.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 COLOR3_H
36 #define COLOR3_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define COLOR3_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
67 
73 #define COLOR3_OK 0
74 #define COLOR3_ERROR -1
75 
81 #define COLOR3_COMMAND_TYPE_SPECIAL ( 3 << 5 )
82 #define COLOR3_COMMAND_TYPE_AUTOINC ( 1 << 5 )
83 #define COLOR3_COMMAND_SELECT ( 1 << 7 )
84 
90 #define COLOR3_I2C_SLAVE_ADDR 0x29
91 
97 #define COLOR3_ENABLE 0x00
98 #define COLOR3_ATIME 0x01
99 #define COLOR3_PTIME 0x02
100 #define COLOR3_WTIME 0x03
101 #define COLOR3_AILTL 0x04
102 #define COLOR3_AILTH 0x05
103 #define COLOR3_AIHTL 0x06
104 #define COLOR3_AIHTH 0x07
105 #define COLOR3_PILTL 0x08
106 #define COLOR3_PILTH 0x09
107 #define COLOR3_PIHTL 0x0A
108 #define COLOR3_PIHTH 0x0B
109 #define COLOR3_PERS 0x0C
110 #define COLOR3_CONFIG 0x0D
111 #define COLOR3_PPCOUNT 0x0E
112 #define COLOR3_CONTROL 0x0F
113 #define COLOR3_ID 0x12
114 #define COLOR3_STATUS 0x13
115 #define COLOR3_CDATA 0x14
116 #define COLOR3_CDATAH 0x15
117 #define COLOR3_RDATA 0x16
118 #define COLOR3_RDATAH 0x17
119 #define COLOR3_GDATA 0x18
120 #define COLOR3_GDATAH 0x19
121 #define COLOR3_BDATA 0x1A
122 #define COLOR3_BDATAH 0x1B
123 #define COLOR3_PDATA 0x1C
124 #define COLOR3_PDATAH 0x1D
125 
131 #define COLOR3_ENABLE_REG_DEFAULT 0x00
132 #define COLOR3_ENABLE_REG_PON 0x01
133 #define COLOR3_ENABLE_REG_AEN 0x02
134 #define COLOR3_ENABLE_REG_PEN 0x04
135 #define COLOR3_ENABLE_REG_WEN 0x08
136 #define COLOR3_ENABLE_REG_AIEN 0x10
137 #define COLOR3_ENABLE_REG_PIEN 0x20
138 
144 #define COLOR3_RGBC_TIMING_1_CYC 0xFF
145 #define COLOR3_RGBC_TIMING_4_CYC 0xFC
146 #define COLOR3_RGBC_TIMING_10_CYC 0xF6
147 #define COLOR3_RGBC_TIMING_42_CYC 0xD6
148 #define COLOR3_RGBC_TIMING_64_CYC 0xAD
149 #define COLOR3_RGBC_TIMING_256_CYC 0x00
150 
156 #define COLOR3_PROXY_PULSE_1 0xFF
157 #define COLOR3_PROXY_PULSE_10 0xF6
158 #define COLOR3_PROXY_PULSE_42 0xD6
159 #define COLOR3_PROXY_PULSE_64 0xAD
160 #define COLOR3_PROXY_PULSE_256 0x00
161 
167 #define COLOR3_WAIT_TIME_REG_1_CYC 0xFF
168 #define COLOR3_WAIT_TIME_REG_42_CYC 0xD8
169 #define COLOR3_WAIT_TIME_REG_64_CYC 0xAD
170 #define COLOR3_WAIT_TIME_REG_256_CYC 0x00
171 
177 #define COLOR3_LOW_THRESHOLD_VALUE 0x0130
178 #define COLOR3_HIGH_THRESHOLD_VALUE 0x0170
179 
185 #define COLOR3_EVERY_CYC_INTER 0x00
186 #define COLOR3_1_OR_INTER 0x01
187 #define COLOR3_2_OR_INTER 0x02
188 #define COLOR3_3_OR_INTER 0x03
189 #define COLOR3_5_OR_INTER 0x04
190 #define COLOR3_10_OR_INTER 0x05
191 #define COLOR3_15_OR_INTER 0x06
192 #define COLOR3_20_OR_INTER 0x07
193 #define COLOR3_25_OR_INTER 0x08
194 #define COLOR3_30_OR_INTER 0x09
195 #define COLOR3_35_OR_INTER 0x0A
196 #define COLOR3_40_OR_INTER 0x0B
197 #define COLOR3_45_OR_INTER 0x0C
198 #define COLOR3_50_OR_INTER 0x0D
199 #define COLOR3_55_OR_INTER 0x0E
200 #define COLOR3_60_OR_INTER 0x0F
201 
207 #define COLOR3_LONG_WAIT 0x02
208 
214 #define COLOR3_A_GAIN_1 0x00
215 #define COLOR3_A_GAIN_4 0x01
216 #define COLOR3_A_GAIN_16 0x02
217 #define COLOR3_A_GAIN_60 0x03
218 
224 #define COLOR3_CLEAR_DIODE 0x10
225 #define COLOR3_IR_DIODE 0x20
226 #define COLOR3_BOTH_DIODE 0x30
227 
233 #define COLOR3_LED_STR_100 0x00
234 #define COLOR3_LED_STR_50 0x40
235 #define COLOR3_LED_STR_25 0x80
236 #define COLOR3_LED_STR_12_5 0xC0
237 
243 #define COLOR3_UNKNOWN_COLOR 0
244 #define COLOR3_RED_COLOR 1
245 #define COLOR3_YELLOW_COLOR 2
246 #define COLOR3_GREEN_COLOR 3
247 #define COLOR3_CYAN_COLOR 4
248 #define COLOR3_BLUE_COLOR 5
249 #define COLOR3_MAGENTA_COLOR 6
250 #define COLOR3_WHITE_COLOR 7
251 #define COLOR3_BLACK_COLOR 8
252 
257 #define COLOR3_WHITE_COLOR_LIGHTNESS 97
258 #define COLOR3_BLACK_COLOR_LIGHTNESS 3
259 #define COLOR3_YELLOW_COLOR_HUE_ANGLE 30
260 #define COLOR3_GREEN_COLOR_HUE_ANGLE 90
261 #define COLOR3_CYAN_COLOR_HUE_ANGLE 150
262 #define COLOR3_BLUE_COLOR_HUE_ANGLE 210
263 #define COLOR3_MAGENTA_COLOR_HUE_ANGLE 270
264 #define COLOR3_RED_COLOR_HUE_ANGLE 330
265 
270 #define COLOR3_HSL_HUE_RESOLUTION 360
271 #define COLOR3_HSL_SATURATION_RESOLUTION 100
272 #define COLOR3_HSL_LIGHTNESS_RESOLUTION 100
273  // End group macro
275 // --------------------------------------------------------------- PUBLIC TYPES
284 typedef struct
285 {
286  // Input pins
287  digital_in_t int_pin;
288 
289  // Modules
290  i2c_master_t i2c;
291 
292  // ctx variable
293  uint8_t slave_address;
294 
295 } color3_t;
296 
300 typedef struct
301 {
302  // Communication gpio pins
303  pin_name_t scl;
304  pin_name_t sda;
305 
306  // Additional gpio pins
307  pin_name_t int_pin;
308 
309  // static variable
310  uint32_t i2c_speed;
311  uint8_t i2c_address;
312 
313 } color3_cfg_t;
314 
319 typedef struct
320 {
321  uint16_t red;
322  uint16_t green;
323  uint16_t blue;
324  uint16_t clear;
325 
327 
332 typedef struct
333 {
334  float hue;
335  float saturation;
336  float lightness;
337 
338 } color3_hsl_t;
339  // End types group
341 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
342 
348 #ifdef __cplusplus
349 extern "C"{
350 #endif
351 
361 
371 err_t color3_init ( color3_t *ctx, color3_cfg_t *cfg );
372 
384 err_t color3_generic_write ( color3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
385 
397 err_t color3_generic_read ( color3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
398 
407 void color3_normal_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
408 
418 void color3_special_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
419 
428 uint8_t color3_read_byte ( color3_t *ctx, uint8_t rd_addr );
429 
439 void color3_normal_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
440 
450 void color3_special_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
451 
460 uint16_t color3_read_data ( color3_t *ctx, uint8_t rd_addr );
461 
470 void color3_write_enable_reg ( color3_t *ctx, uint8_t wr_data );
471 
480 
489 void color3_write_rgbc_timing_reg ( color3_t *ctx , uint8_t wr_data );
490 
502 
512 
524 
533 void color3_write_wait_time_reg ( color3_t *ctx, uint8_t wr_data );
534 
543 
552 void color3_set_inter_trsh_lo( color3_t *ctx, uint16_t wr_data );
553 
562 void color3_set_inter_trsh_hi ( color3_t *ctx, uint16_t wr_data );
563 
572 void color3_set_prox_inter_trsh_lo ( color3_t *ctx, uint16_t wr_data );
573 
582 void color3_set_prox_inter_trsh_hi ( color3_t *ctx, uint16_t wr_data );
583 
592 void color3_set_int_pers ( color3_t *ctx, uint8_t int_pers );
593 
602 
611 void color3_set_prox_int_pers ( color3_t *ctx, uint8_t int_pers );
612 
621 
630 void color3_set_long_wait ( color3_t *ctx, uint8_t lon_wait );
631 
640 
649 void color3_write_pulse_cnt ( color3_t *ctx, uint8_t pulse_cnt );
650 
659 
668 void color3_write_rgbc_gain ( color3_t *ctx, uint8_t gain );
669 
678 
687 void color3_set_diode_select ( color3_t *ctx, uint8_t dio_sel );
688 
697 
706 void color3_set_led_str ( color3_t *ctx, uint8_t led_str );
707 
715 uint8_t color3_read_led_str ( color3_t *ctx );
716 
724 uint8_t color3_read_dev_id ( color3_t *ctx );
725 
734 
743 
751 uint16_t color3_read_red_data ( color3_t *ctx );
752 
761 
769 uint16_t color3_read_blue_data ( color3_t *ctx );
770 
779 
788 
797 
808 void color3_get_rgb_data ( color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data );
809 
818 float color3_get_color_ratio ( color3_t *ctx, uint16_t color_data );
819 
833 
844 
858 
859 #ifdef __cplusplus
860 }
861 #endif
862 #endif // _COLOR3_H_
863  // End public_function group
866 
867 // ------------------------------------------------------------------------- END
color3_write_wait_time_reg
void color3_write_wait_time_reg(color3_t *ctx, uint8_t wr_data)
Write Wait Time Register function.
color3_get_rgbc_data
err_t color3_get_rgbc_data(color3_t *ctx, color3_channels_t *channels)
Color 3 get rgbc data function.
color3_read_pulse_cnt
uint8_t color3_read_pulse_cnt(color3_t *ctx)
Read Proximity Pulse Count function.
color3_get_color
uint8_t color3_get_color(color3_hsl_t *hsl)
Color 3 get color function.
color3_set_diode_select
void color3_set_diode_select(color3_t *ctx, uint8_t dio_sel)
Set Diode Selection function.
color3_cfg_t
Click configuration structure definition.
Definition: color3.h:301
color3_t::i2c
i2c_master_t i2c
Definition: color3.h:290
color3_get_diode_select
uint8_t color3_get_diode_select(color3_t *ctx)
Get Diode Selection function.
color3_cfg_t::scl
pin_name_t scl
Definition: color3.h:303
color3_normal_wr_data
void color3_normal_wr_data(color3_t *ctx, uint8_t wr_addr, uint16_t wr_data)
Normal write data function.
color3_hsl_t::saturation
float saturation
Definition: color3.h:335
color3_set_prox_inter_trsh_lo
void color3_set_prox_inter_trsh_lo(color3_t *ctx, uint16_t wr_data)
Proximity interrupt threshold low value function.
color3_read_dev_id
uint8_t color3_read_dev_id(color3_t *ctx)
Read device ID function.
color3_rgbc_to_hsl
void color3_rgbc_to_hsl(color3_t *ctx, color3_channels_t *rgbc, color3_hsl_t *hsl)
Color 3 rgbc to hsl function.
color3_cfg_t::sda
pin_name_t sda
Definition: color3.h:304
color3_channels_t
Color 3 RGBC channels data object.
Definition: color3.h:320
color3_read_rgbc_timing_reg
uint8_t color3_read_rgbc_timing_reg(color3_t *ctx)
Read RGBC Timing Register function.
color3_read_prox_data
uint16_t color3_read_prox_data(color3_t *ctx)
ead proximity data function.
color3_cfg_t::int_pin
pin_name_t int_pin
Definition: color3.h:307
color3_channels_t::blue
uint16_t blue
Definition: color3.h:323
color3_channels_t::red
uint16_t red
Definition: color3.h:321
color3_read_red_data
uint16_t color3_read_red_data(color3_t *ctx)
Read red data function.
color3_read_enable_reg
uint8_t color3_read_enable_reg(color3_t *ctx)
Read Enable Register function.
color3_cfg_t::i2c_address
uint8_t i2c_address
Definition: color3.h:311
color3_t::slave_address
uint8_t slave_address
Definition: color3.h:293
color3_hsl_t
Color 3 HSL color data object.
Definition: color3.h:333
color3_write_rgbc_gain
void color3_write_rgbc_gain(color3_t *ctx, uint8_t gain)
Write RGBC Gain Value function.
color3_t::int_pin
digital_in_t int_pin
Definition: color3.h:287
color3_write_pulse_cnt
void color3_write_pulse_cnt(color3_t *ctx, uint8_t pulse_cnt)
Write Proximity Pulse Count function.
color3_init
err_t color3_init(color3_t *ctx, color3_cfg_t *cfg)
Initialization function.
color3_get_inter_state
uint8_t color3_get_inter_state(color3_t *ctx)
Get Interrupt state function.
color3_set_inter_trsh_hi
void color3_set_inter_trsh_hi(color3_t *ctx, uint16_t wr_data)
RGBC interrupt threshold high value function.
color3_hsl_t::hue
float hue
Definition: color3.h:334
color3_special_wr_data
void color3_special_wr_data(color3_t *ctx, uint8_t wr_addr, uint16_t wr_data)
Special write function.
color3_special_wr_byte
void color3_special_wr_byte(color3_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Special write byte function.
color3_read_led_str
uint8_t color3_read_led_str(color3_t *ctx)
Read LED Strength function.
color3_read_prox_timing_reg
uint8_t color3_read_prox_timing_reg(color3_t *ctx)
Read Proximity Timing Register function.
color3_generic_read
err_t color3_generic_read(color3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Generic read function.
color3_set_prox_inter_trsh_hi
void color3_set_prox_inter_trsh_hi(color3_t *ctx, uint16_t wr_data)
Proximity interrupt threshold high value function.
color3_t
Click ctx object definition.
Definition: color3.h:285
color3_read_byte
uint8_t color3_read_byte(color3_t *ctx, uint8_t rd_addr)
Read byte function.
color3_cfg_setup
void color3_cfg_setup(color3_cfg_t *cfg)
Config Object Initialization function.
color3_read_wait_time_reg
uint8_t color3_read_wait_time_reg(color3_t *ctx)
Read Wait Time Register function.
color3_read_rgbc_gain
uint8_t color3_read_rgbc_gain(color3_t *ctx)
Read RGBC Gain Value function.
color3_read_blue_data
uint16_t color3_read_blue_data(color3_t *ctx)
Read blue data function.
color3_read_dev_stat
uint8_t color3_read_dev_stat(color3_t *ctx)
Read device status function.
color3_generic_write
err_t color3_generic_write(color3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Generic write function.
color3_set_int_pers
void color3_set_int_pers(color3_t *ctx, uint8_t int_pers)
Set Interrupt persistence persistence function.
color3_set_inter_trsh_lo
void color3_set_inter_trsh_lo(color3_t *ctx, uint16_t wr_data)
RGBC interrupt threshold low value function.
color3_read_data
uint16_t color3_read_data(color3_t *ctx, uint8_t rd_addr)
Read data function.
color3_write_rgbc_timing_reg
void color3_write_rgbc_timing_reg(color3_t *ctx, uint8_t wr_data)
Write RGBC Timing Register function.
color3_set_default_settings
void color3_set_default_settings(color3_t *ctx)
Default settings function.
color3_write_prox_timing_reg
void color3_write_prox_timing_reg(color3_t *ctx)
Write Proximity Timing Register function.
color3_read_int_pers
uint8_t color3_read_int_pers(color3_t *ctx)
Read Interrupt persistence function.
color3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: color3.h:310
color3_read_prox_int_pers
uint8_t color3_read_prox_int_pers(color3_t *ctx)
Read Proximity interrupt persistence function.
color3_write_enable_reg
void color3_write_enable_reg(color3_t *ctx, uint8_t wr_data)
Write Enable Register function.
color3_set_prox_int_pers
void color3_set_prox_int_pers(color3_t *ctx, uint8_t int_pers)
Set Proximity interrupt persistence function.
color3_channels_t::green
uint16_t green
Definition: color3.h:322
color3_hsl_t::lightness
float lightness
Definition: color3.h:336
color3_read_clear_data
uint16_t color3_read_clear_data(color3_t *ctx)
Read clear data function.
color3_read_long_wait
uint8_t color3_read_long_wait(color3_t *ctx)
Read long wait function.
color3_channels_t::clear
uint16_t clear
Definition: color3.h:324
color3_normal_wr_byte
void color3_normal_wr_byte(color3_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Generic read function.
color3_set_led_str
void color3_set_led_str(color3_t *ctx, uint8_t led_str)
Set LED Strength function.
color3_get_rgb_data
void color3_get_rgb_data(color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data)
Get color data function.
color3_set_long_wait
void color3_set_long_wait(color3_t *ctx, uint8_t lon_wait)
Set long wait function.
color3_get_color_ratio
float color3_get_color_ratio(color3_t *ctx, uint16_t color_data)
Get color ratio function.
color3_read_green_data
uint16_t color3_read_green_data(color3_t *ctx)
Read green data function.