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_RETVAL uint8_t
64 
65 #define COLOR3_OK 0x00
66 #define COLOR3_INIT_ERROR 0xFF
67 
73 #define COLOR_3_COMMAND_TYPE_SPECIAL (3 << 5)
74 #define COLOR_3_COMMAND_TYPE_AUTOINC (0b01 << 5)
75 #define COLOR_3_COMMAND_SELECT (1 << 7)
76 
82 #define COLOR_3_I2C_SLAVE_ADDR 0x29
83 
89 #define COLOR_3_ENABLE 0x00
90 #define COLOR_3_ATIME 0x01
91 #define COLOR_3_PTIME 0x02
92 #define COLOR_3_WTIME 0x03
93 #define COLOR_3_AILTL 0x04
94 #define COLOR_3_AILTH 0x05
95 #define COLOR_3_AIHTL 0x06
96 #define COLOR_3_AIHTH 0x07
97 #define COLOR_3_PILTL 0x08
98 #define COLOR_3_PILTH 0x09
99 #define COLOR_3_PIHTL 0x0A
100 #define COLOR_3_PIHTH 0x0B
101 #define COLOR_3_PERS 0x0C
102 #define COLOR_3_CONFIG 0x0D
103 #define COLOR_3_PPCOUNT 0x0E
104 #define COLOR_3_CONTROL 0x0F
105 #define COLOR_3_ID 0x12
106 #define COLOR_3_STATUS 0x13
107 #define COLOR_3_CDATA 0x14
108 #define COLOR_3_CDATAH 0x15
109 #define COLOR_3_RDATA 0x16
110 #define COLOR_3_RDATAH 0x17
111 #define COLOR_3_GDATA 0x18
112 #define COLOR_3_GDATAH 0x19
113 #define COLOR_3_BDATA 0x1A
114 #define COLOR_3_BDATAH 0x1B
115 #define COLOR_3_PDATA 0x1C
116 #define COLOR_3_PDATAH 0x1D
117 
123 #define COLOR_3_ENABLE_REG_DEFAULT 0x00
124 #define COLOR_3_ENABLE_REG_PON 0x01
125 #define COLOR_3_ENABLE_REG_AEN 0x02
126 #define COLOR_3_ENABLE_REG_PEN 0x04
127 #define COLOR_3_ENABLE_REG_WEN 0x08
128 #define COLOR_3_ENABLE_REG_AIEN 0x10
129 #define COLOR_3_ENABLE_REG_PIEN 0x20
130 
136 #define COLOR_3_RGBC_TIMING_1_CYC 0xFF
137 #define COLOR_3_RGBC_TIMING_4_CYC 0xFC
138 #define COLOR_3_RGBC_TIMING_10_CYC 0xF6
139 #define COLOR_3_RGBC_TIMING_42_CYC 0xD6
140 #define COLOR_3_RGBC_TIMING_64_CYC 0xAD
141 #define COLOR_3_RGBC_TIMING_256_CYC 0x00
142 
148 #define COLOR_3_PROXY_PULSE_1 0xFF
149 #define COLOR_3_PROXY_PULSE_10 0xF6
150 #define COLOR_3_PROXY_PULSE_42 0xD6
151 #define COLOR_3_PROXY_PULSE_64 0xAD
152 #define COLOR_3_PROXY_PULSE_256 0x00
153 
159 #define COLOR_3_WAIT_TIME_REG_1_CYC 0xFF
160 #define COLOR_3_WAIT_TIME_REG_40_CYC 0xD8
161 #define COLOR_3_WAIT_TIME_REG_85_CYC 0xAB
162 #define COLOR_3_WAIT_TIME_REG_256_CYC 0x00
163 
169 #define COLOR_3_LOW_THRESHOLD_VALUE 0x0130
170 #define COLOR_3_HIGH_THRESHOLD_VALUE 0x0170
171 
177 #define COLOR_3_EVERY_CYC_INTER 0x00
178 #define COLOR_3_1_OR_INTER 0x01
179 #define COLOR_3_2_OR_INTER 0x02
180 #define COLOR_3_3_OR_INTER 0x03
181 #define COLOR_3_5_OR_INTER 0x04
182 #define COLOR_3_10_OR_INTER 0x05
183 #define COLOR_3_15_OR_INTER 0x06
184 #define COLOR_3_20_OR_INTER 0x07
185 #define COLOR_3_25_OR_INTER 0x08
186 #define COLOR_3_30_OR_INTER 0x09
187 #define COLOR_3_35_OR_INTER 0x0A
188 #define COLOR_3_40_OR_INTER 0x0B
189 #define COLOR_3_45_OR_INTER 0x0C
190 #define COLOR_3_50_OR_INTER 0x0D
191 #define COLOR_3_55_OR_INTER 0x0E
192 #define COLOR_3_60_OR_INTER 0x0F
193 
199 #define COLOR_3_LONG_WAIT 0x02
200 
206 #define COLOR_3_A_GAIN_1 0x00
207 #define COLOR_3_A_GAIN_4 0x01
208 #define COLOR_3_A_GAIN_16 0x02
209 #define COLOR_3_A_GAIN_60 0x03
210 
216 #define COLOR_3_CLEAR_DIODE 0x10
217 #define COLOR_3_IR_DIODE 0x20
218 #define COLOR_3_BOTH_DIODE 0x30
219 
225 #define COLOR_3_LED_STR_100 0x00
226 #define COLOR_3_LED_STR_50 0x40
227 #define COLOR_3_LED_STR_25 0x80
228 #define COLOR_3_LED_STR_12_5 0xC0
229  // End group macro
235 // --------------------------------------------------------------- PUBLIC TYPES
244 typedef struct
245 {
246  // Output pins
247 
248  // Input pins
249 
250  digital_in_t int_pin;
251 
252  // Modules
253 
254  i2c_master_t i2c;
255 
256  // ctx variable
257 
258  uint8_t slave_address;
259 
260  uint16_t *clear_data;
261  uint16_t *red_data;
262  uint16_t *green_data;
263  uint16_t *blue_data;
264 } color3_t;
265 
266 typedef struct
267 {
268  uint16_t *clear_data;
269  uint16_t *red_data;
270  uint16_t *green_data;
271  uint16_t *blue_data;
272 
274 
278 typedef struct
279 {
280  // Communication gpio pins
281 
282  pin_name_t scl;
283  pin_name_t sda;
284 
285  // Additional gpio pins
286 
287  pin_name_t int_pin;
288 
289  // static variable
290 
291  uint32_t i2c_speed;
292  uint8_t i2c_address;
293 
295 
296 } color3_cfg_t;
297  // End types group
299 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
300 
306 #ifdef __cplusplus
307 extern "C"{
308 #endif
309 
318 void color3_cfg_setup ( color3_cfg_t *cfg );
319 
328 
339 void color3_generic_write ( color3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
340 
352 void color3_generic_read ( color3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
353 
364 void color3_normal_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
365 
376  void color3_special_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
377 
387  uint8_t color3_read_byte ( color3_t *ctx, uint8_t rd_addr );
388 
399  void color3_normal_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
400 
411  void color3_special_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
412 
422  uint16_t color3_read_data ( color3_t *ctx, uint8_t rd_addr );
423 
433  void color3_write_enable_register ( color3_t *ctx, uint8_t wr_data );
434 
443  uint8_t color3_read_enable_register ( color3_t *ctx );
444 
454  void color3_write_rgbc_timing_register ( color3_t *ctx , uint8_t wr_data );
455 
467  uint8_t color3_read_rgbc_timing_register ( color3_t *ctx );
468 
479 
492 
502  void color3_write_wait_time_register ( color3_t *ctx, uint8_t wr_data );
503 
512  uint8_t color3_read_wait_time_register ( color3_t *ctx );
513 
523  void color3_set_inter_trsh_lo_val( color3_t *ctx, uint16_t wr_data );
524 
534  void color3_set_inter_trsh_hi_val ( color3_t *ctx, uint16_t wr_data );
535 
545  void color3_set_proxy_inter_trsh_lo_val ( color3_t *ctx, uint16_t wr_data );
546 
556  void color3_set_proxy_inter_trsh_hi_val ( color3_t *ctx, uint16_t wr_data );
557 
567  void color3_set_interrupt_persistence ( color3_t *ctx, uint8_t int_pers );
568 
578 
588  void color3_setproxy_interrupt_persistence ( color3_t *ctx, uint8_t int_pers );
589 
599 
609  void color3_setLongWait ( color3_t *ctx, uint8_t lon_wait );
610 
619  uint8_t color3_read_long_wait ( color3_t *ctx );
620 
630  void color3_write_pulse_cnt ( color3_t *ctx, uint8_t pulse_cnt );
631 
640  uint8_t color3_read_pulse_cnt ( color3_t *ctx );
641 
651  void color3_write_rgbc_gain ( color3_t *ctx, uint8_t gain_val );
652 
661  uint8_t color3_read_rgbc_gain ( color3_t *ctx );
662 
672  void color3_set_diode_select ( color3_t *ctx, uint8_t dio_sel );
673 
682  uint8_t color3_get_diode_select ( color3_t *ctx );
683 
693  void color3_set_led_str ( color3_t *ctx, uint8_t led_str );
694 
703  uint8_t color3_read_led_str ( color3_t *ctx );
704 
713  uint8_t color3_read_dev_id ( color3_t *ctx );
714 
723  uint8_t color3_read_dev_stat ( color3_t *ctx );
724 
733  uint16_t color3_read_clear_data ( color3_t *ctx );
734 
743  uint16_t color3_read_red_data ( color3_t *ctx );
744 
753  uint16_t color3_read_green_data ( color3_t *ctx );
754 
763  uint16_t color3_read_blue_data ( color3_t *ctx );
764 
773  uint16_t color3_read_proxy_data( color3_t *ctx );
774 
783  uint8_t color3_get_inter_state ( color3_t *ctx );
784 
794 
806  void color_3_get_rgb_data ( color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data );
807 
817  float color3_get_color_ratio ( color3_t *ctx, uint16_t color_data );
818 
827  float color3_get_color_value ( color3_t *ctx );
828 
841  void color_3_get_crgb_data ( color3_t *ctx, color3_const_t *data_const );
842 
854 static float color3_rgb_to_hsl( color3_t *ctx, float red, float green, float blue );
855 
856 #ifdef __cplusplus
857 }
858 #endif
859 #endif // _COLOR3_H_
860  // End public_function group
863 
864 // ------------------------------------------------------------------------- END
uint8_t color3_read_proxy_timing_register(color3_t *ctx)
Read Proximity Timing Register function.
uint16_t * blue_data
Definition: color3.h:271
Click configuration structure definition.
Definition: color3.h:278
float color3_get_color_value(color3_t *ctx)
Get color value function.
uint8_t color3_read_rgbc_timing_register(color3_t *ctx)
Read RGBC Timing Register function.
uint32_t i2c_speed
Definition: color3.h:291
void color3_write_enable_register(color3_t *ctx, uint8_t wr_data)
Write Enable Register function.
uint8_t color3_read_byte(color3_t *ctx, uint8_t rd_addr)
Read byte function.
void color3_normal_wr_data(color3_t *ctx, uint8_t wr_addr, uint16_t wr_data)
Normal write data function.
uint8_t color3_get_diode_select(color3_t *ctx)
Get Diode Selection function.
void color_3_get_crgb_data(color3_t *ctx, color3_const_t *data_const)
Get CRBG data function.
#define COLOR3_RETVAL
Definition: color3.h:63
i2c_master_t i2c
Definition: color3.h:254
uint16_t * green_data
Definition: color3.h:262
uint16_t * red_data
Definition: color3.h:269
uint8_t color3_read_long_wait(color3_t *ctx)
Read long wait function.
uint16_t * red_data
Definition: color3.h:261
pin_name_t sda
Definition: color3.h:283
void color3_cfg_setup(color3_cfg_t *cfg)
Config Object Initialization function.
void color3_write_pulse_cnt(color3_t *ctx, uint8_t pulse_cnt)
Write Proximity Pulse Count function.
uint8_t slave_address
Definition: color3.h:258
digital_in_t int_pin
Definition: color3.h:250
uint16_t color3_read_red_data(color3_t *ctx)
Read red data function.
uint8_t color3_read_rgbc_gain(color3_t *ctx)
Read RGBC Gain Value function.
uint16_t color3_read_clear_data(color3_t *ctx)
Read clear data function.
Click ctx object definition.
Definition: color3.h:244
Definition: color3.h:266
void color3_set_default_settings(color3_t *ctx)
Default settings function.
void color3_set_proxy_inter_trsh_lo_val(color3_t *ctx, uint16_t wr_data)
Proximity interrupt threshold low value function.
uint8_t color3_read_dev_stat(color3_t *ctx)
Read device status function.
void color3_set_interrupt_persistence(color3_t *ctx, uint8_t int_pers)
Set Interrupt persistence persistence function.
uint16_t * clear_data
Definition: color3.h:260
uint16_t * green_data
Definition: color3.h:270
void color3_setproxy_interrupt_persistence(color3_t *ctx, uint8_t int_pers)
Set Proximity interrupt persistence function.
uint16_t color3_read_proxy_data(color3_t *ctx)
ead proximity data function.
uint8_t color3_read_proxy_interrupt_persistence(color3_t *ctx)
Read Proximity interrupt persistence function.
void color3_generic_write(color3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void color3_set_inter_trsh_lo_val(color3_t *ctx, uint16_t wr_data)
RGBC interrupt threshold low value function.
pin_name_t int_pin
Definition: color3.h:287
uint16_t color3_read_blue_data(color3_t *ctx)
Read blue data function.
void color_3_get_rgb_data(color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data)
Get color data function.
pin_name_t scl
Definition: color3.h:282
void color3_write_rgbc_timing_register(color3_t *ctx, uint8_t wr_data)
Write RGBC Timing Register function.
COLOR3_RETVAL color3_init(color3_t *ctx, color3_cfg_t *cfg)
Initialization function.
uint16_t * clear_data
Definition: color3.h:268
void color3_special_wr_data(color3_t *ctx, uint8_t wr_addr, uint16_t wr_data)
Special write function.
void color3_generic_read(color3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
uint16_t color3_read_green_data(color3_t *ctx)
Read green data function.
color3_const_t dev_const
Definition: color3.h:294
void color3_special_wr_byte(color3_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Special write byte function.
void color3_write_proxy_timing_register(color3_t *ctx)
Write Proximity Timing Register function.
float color3_get_color_ratio(color3_t *ctx, uint16_t color_data)
Get color ratio function.
uint8_t color3_read_interrupt_persistence(color3_t *ctx)
Read Interrupt persistence function.
void color3_normal_wr_byte(color3_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Generic read function.
uint8_t color3_read_enable_register(color3_t *ctx)
Read Enable Register function.
uint16_t color3_read_data(color3_t *ctx, uint8_t rd_addr)
Read data function.
uint8_t color3_read_pulse_cnt(color3_t *ctx)
Read Proximity Pulse Count function.
void color3_write_wait_time_register(color3_t *ctx, uint8_t wr_data)
Write Wait Time Register function.
uint8_t color3_read_dev_id(color3_t *ctx)
Read device ID function.
uint8_t color3_read_led_str(color3_t *ctx)
Read LED Strength function.
void color3_set_proxy_inter_trsh_hi_val(color3_t *ctx, uint16_t wr_data)
Proximity interrupt threshold high value function.
uint16_t * blue_data
Definition: color3.h:263
void color3_setLongWait(color3_t *ctx, uint8_t lon_wait)
Set long wait function.
uint8_t color3_read_wait_time_register(color3_t *ctx)
Read Wait Time Register function.
void color3_set_diode_select(color3_t *ctx, uint8_t dio_sel)
Set Diode Selection function.
uint8_t i2c_address
Definition: color3.h:292
void color3_set_inter_trsh_hi_val(color3_t *ctx, uint16_t wr_data)
RGBC interrupt threshold high value function.
void color3_set_led_str(color3_t *ctx, uint8_t led_str)
Set LED Strength function.
uint8_t color3_get_inter_state(color3_t *ctx)
Get Interrupt state function.
void color3_write_rgbc_gain(color3_t *ctx, uint8_t gain_val)
Write RGBC Gain Value function.