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 "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define COLOR3_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 COLOR3_OK 0
64 #define COLOR3_ERROR -1
65 
71 #define COLOR3_COMMAND_TYPE_SPECIAL ( 3 << 5 )
72 #define COLOR3_COMMAND_TYPE_AUTOINC ( 1 << 5 )
73 #define COLOR3_COMMAND_SELECT ( 1 << 7 )
74 
80 #define COLOR3_I2C_SLAVE_ADDR 0x29
81 
87 #define COLOR3_ENABLE 0x00
88 #define COLOR3_ATIME 0x01
89 #define COLOR3_PTIME 0x02
90 #define COLOR3_WTIME 0x03
91 #define COLOR3_AILTL 0x04
92 #define COLOR3_AILTH 0x05
93 #define COLOR3_AIHTL 0x06
94 #define COLOR3_AIHTH 0x07
95 #define COLOR3_PILTL 0x08
96 #define COLOR3_PILTH 0x09
97 #define COLOR3_PIHTL 0x0A
98 #define COLOR3_PIHTH 0x0B
99 #define COLOR3_PERS 0x0C
100 #define COLOR3_CONFIG 0x0D
101 #define COLOR3_PPCOUNT 0x0E
102 #define COLOR3_CONTROL 0x0F
103 #define COLOR3_ID 0x12
104 #define COLOR3_STATUS 0x13
105 #define COLOR3_CDATA 0x14
106 #define COLOR3_CDATAH 0x15
107 #define COLOR3_RDATA 0x16
108 #define COLOR3_RDATAH 0x17
109 #define COLOR3_GDATA 0x18
110 #define COLOR3_GDATAH 0x19
111 #define COLOR3_BDATA 0x1A
112 #define COLOR3_BDATAH 0x1B
113 #define COLOR3_PDATA 0x1C
114 #define COLOR3_PDATAH 0x1D
115 
121 #define COLOR3_ENABLE_REG_DEFAULT 0x00
122 #define COLOR3_ENABLE_REG_PON 0x01
123 #define COLOR3_ENABLE_REG_AEN 0x02
124 #define COLOR3_ENABLE_REG_PEN 0x04
125 #define COLOR3_ENABLE_REG_WEN 0x08
126 #define COLOR3_ENABLE_REG_AIEN 0x10
127 #define COLOR3_ENABLE_REG_PIEN 0x20
128 
134 #define COLOR3_RGBC_TIMING_1_CYC 0xFF
135 #define COLOR3_RGBC_TIMING_4_CYC 0xFC
136 #define COLOR3_RGBC_TIMING_10_CYC 0xF6
137 #define COLOR3_RGBC_TIMING_42_CYC 0xD6
138 #define COLOR3_RGBC_TIMING_64_CYC 0xAD
139 #define COLOR3_RGBC_TIMING_256_CYC 0x00
140 
146 #define COLOR3_PROXY_PULSE_1 0xFF
147 #define COLOR3_PROXY_PULSE_10 0xF6
148 #define COLOR3_PROXY_PULSE_42 0xD6
149 #define COLOR3_PROXY_PULSE_64 0xAD
150 #define COLOR3_PROXY_PULSE_256 0x00
151 
157 #define COLOR3_WAIT_TIME_REG_1_CYC 0xFF
158 #define COLOR3_WAIT_TIME_REG_42_CYC 0xD8
159 #define COLOR3_WAIT_TIME_REG_64_CYC 0xAD
160 #define COLOR3_WAIT_TIME_REG_256_CYC 0x00
161 
167 #define COLOR3_LOW_THRESHOLD_VALUE 0x0130
168 #define COLOR3_HIGH_THRESHOLD_VALUE 0x0170
169 
175 #define COLOR3_EVERY_CYC_INTER 0x00
176 #define COLOR3_1_OR_INTER 0x01
177 #define COLOR3_2_OR_INTER 0x02
178 #define COLOR3_3_OR_INTER 0x03
179 #define COLOR3_5_OR_INTER 0x04
180 #define COLOR3_10_OR_INTER 0x05
181 #define COLOR3_15_OR_INTER 0x06
182 #define COLOR3_20_OR_INTER 0x07
183 #define COLOR3_25_OR_INTER 0x08
184 #define COLOR3_30_OR_INTER 0x09
185 #define COLOR3_35_OR_INTER 0x0A
186 #define COLOR3_40_OR_INTER 0x0B
187 #define COLOR3_45_OR_INTER 0x0C
188 #define COLOR3_50_OR_INTER 0x0D
189 #define COLOR3_55_OR_INTER 0x0E
190 #define COLOR3_60_OR_INTER 0x0F
191 
197 #define COLOR3_LONG_WAIT 0x02
198 
204 #define COLOR3_A_GAIN_1 0x00
205 #define COLOR3_A_GAIN_4 0x01
206 #define COLOR3_A_GAIN_16 0x02
207 #define COLOR3_A_GAIN_60 0x03
208 
214 #define COLOR3_CLEAR_DIODE 0x10
215 #define COLOR3_IR_DIODE 0x20
216 #define COLOR3_BOTH_DIODE 0x30
217 
223 #define COLOR3_LED_STR_100 0x00
224 #define COLOR3_LED_STR_50 0x40
225 #define COLOR3_LED_STR_25 0x80
226 #define COLOR3_LED_STR_12_5 0xC0
227 
233 #define COLOR3_UNKNOWN_COLOR 0
234 #define COLOR3_RED_COLOR 1
235 #define COLOR3_YELLOW_COLOR 2
236 #define COLOR3_GREEN_COLOR 3
237 #define COLOR3_CYAN_COLOR 4
238 #define COLOR3_BLUE_COLOR 5
239 #define COLOR3_MAGENTA_COLOR 6
240 #define COLOR3_WHITE_COLOR 7
241 #define COLOR3_BLACK_COLOR 8
242 
247 #define COLOR3_WHITE_COLOR_LIGHTNESS 97
248 #define COLOR3_BLACK_COLOR_LIGHTNESS 3
249 #define COLOR3_YELLOW_COLOR_HUE_ANGLE 30
250 #define COLOR3_GREEN_COLOR_HUE_ANGLE 90
251 #define COLOR3_CYAN_COLOR_HUE_ANGLE 150
252 #define COLOR3_BLUE_COLOR_HUE_ANGLE 210
253 #define COLOR3_MAGENTA_COLOR_HUE_ANGLE 270
254 #define COLOR3_RED_COLOR_HUE_ANGLE 330
255 
260 #define COLOR3_HSL_HUE_RESOLUTION 360
261 #define COLOR3_HSL_SATURATION_RESOLUTION 100
262 #define COLOR3_HSL_LIGHTNESS_RESOLUTION 100
263  // End group macro
265 // --------------------------------------------------------------- PUBLIC TYPES
274 typedef struct
275 {
276  // Input pins
277  digital_in_t int_pin;
278 
279  // Modules
280  i2c_master_t i2c;
281 
282  // ctx variable
283  uint8_t slave_address;
284 
285 } color3_t;
286 
290 typedef struct
291 {
292  // Communication gpio pins
293  pin_name_t scl;
294  pin_name_t sda;
295 
296  // Additional gpio pins
297  pin_name_t int_pin;
298 
299  // static variable
300  uint32_t i2c_speed;
301  uint8_t i2c_address;
302 
303 } color3_cfg_t;
304 
309 typedef struct
310 {
311  uint16_t red;
312  uint16_t green;
313  uint16_t blue;
314  uint16_t clear;
315 
317 
322 typedef struct
323 {
324  float hue;
325  float saturation;
326  float lightness;
327 
328 } color3_hsl_t;
329  // End types group
331 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
332 
338 #ifdef __cplusplus
339 extern "C"{
340 #endif
341 
351 
361 err_t color3_init ( color3_t *ctx, color3_cfg_t *cfg );
362 
374 err_t color3_generic_write ( color3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
375 
387 err_t color3_generic_read ( color3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
388 
397 void color3_normal_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
398 
408 void color3_special_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
409 
418 uint8_t color3_read_byte ( color3_t *ctx, uint8_t rd_addr );
419 
429 void color3_normal_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
430 
440 void color3_special_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
441 
450 uint16_t color3_read_data ( color3_t *ctx, uint8_t rd_addr );
451 
460 void color3_write_enable_reg ( color3_t *ctx, uint8_t wr_data );
461 
470 
479 void color3_write_rgbc_timing_reg ( color3_t *ctx , uint8_t wr_data );
480 
492 
502 
514 
523 void color3_write_wait_time_reg ( color3_t *ctx, uint8_t wr_data );
524 
533 
542 void color3_set_inter_trsh_lo( color3_t *ctx, uint16_t wr_data );
543 
552 void color3_set_inter_trsh_hi ( color3_t *ctx, uint16_t wr_data );
553 
562 void color3_set_prox_inter_trsh_lo ( color3_t *ctx, uint16_t wr_data );
563 
572 void color3_set_prox_inter_trsh_hi ( color3_t *ctx, uint16_t wr_data );
573 
582 void color3_set_int_pers ( color3_t *ctx, uint8_t int_pers );
583 
592 
601 void color3_set_prox_int_pers ( color3_t *ctx, uint8_t int_pers );
602 
611 
620 void color3_set_long_wait ( color3_t *ctx, uint8_t lon_wait );
621 
630 
639 void color3_write_pulse_cnt ( color3_t *ctx, uint8_t pulse_cnt );
640 
649 
658 void color3_write_rgbc_gain ( color3_t *ctx, uint8_t gain );
659 
668 
677 void color3_set_diode_select ( color3_t *ctx, uint8_t dio_sel );
678 
687 
696 void color3_set_led_str ( color3_t *ctx, uint8_t led_str );
697 
705 uint8_t color3_read_led_str ( color3_t *ctx );
706 
714 uint8_t color3_read_dev_id ( color3_t *ctx );
715 
724 
733 
741 uint16_t color3_read_red_data ( color3_t *ctx );
742 
751 
759 uint16_t color3_read_blue_data ( color3_t *ctx );
760 
769 
778 
787 
798 void color3_get_rgb_data ( color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data );
799 
808 float color3_get_color_ratio ( color3_t *ctx, uint16_t color_data );
809 
823 
834 
848 
849 #ifdef __cplusplus
850 }
851 #endif
852 #endif // _COLOR3_H_
853  // End public_function group
856 
857 // ------------------------------------------------------------------------- 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:291
color3_t::i2c
i2c_master_t i2c
Definition: color3.h:280
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:293
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:325
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:294
color3_channels_t
Color 3 RGBC channels data object.
Definition: color3.h:310
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:297
color3_channels_t::blue
uint16_t blue
Definition: color3.h:313
color3_channels_t::red
uint16_t red
Definition: color3.h:311
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:301
color3_t::slave_address
uint8_t slave_address
Definition: color3.h:283
color3_hsl_t
Color 3 HSL color data object.
Definition: color3.h:323
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:277
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:324
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:275
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:300
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:312
color3_hsl_t::lightness
float lightness
Definition: color3.h:326
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:314
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.