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
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54#include "drv_i2c_master.h"
55
56
57// -------------------------------------------------------------- PUBLIC MACROS
67#define COLOR3_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
77#define COLOR3_OK 0
78#define COLOR3_ERROR -1
85#define COLOR3_COMMAND_TYPE_SPECIAL ( 3 << 5 )
86#define COLOR3_COMMAND_TYPE_AUTOINC ( 1 << 5 )
87#define COLOR3_COMMAND_SELECT ( 1 << 7 )
94#define COLOR3_I2C_SLAVE_ADDR 0x29
101#define COLOR3_ENABLE 0x00
102#define COLOR3_ATIME 0x01
103#define COLOR3_PTIME 0x02
104#define COLOR3_WTIME 0x03
105#define COLOR3_AILTL 0x04
106#define COLOR3_AILTH 0x05
107#define COLOR3_AIHTL 0x06
108#define COLOR3_AIHTH 0x07
109#define COLOR3_PILTL 0x08
110#define COLOR3_PILTH 0x09
111#define COLOR3_PIHTL 0x0A
112#define COLOR3_PIHTH 0x0B
113#define COLOR3_PERS 0x0C
114#define COLOR3_CONFIG 0x0D
115#define COLOR3_PPCOUNT 0x0E
116#define COLOR3_CONTROL 0x0F
117#define COLOR3_ID 0x12
118#define COLOR3_STATUS 0x13
119#define COLOR3_CDATA 0x14
120#define COLOR3_CDATAH 0x15
121#define COLOR3_RDATA 0x16
122#define COLOR3_RDATAH 0x17
123#define COLOR3_GDATA 0x18
124#define COLOR3_GDATAH 0x19
125#define COLOR3_BDATA 0x1A
126#define COLOR3_BDATAH 0x1B
127#define COLOR3_PDATA 0x1C
128#define COLOR3_PDATAH 0x1D
135#define COLOR3_ENABLE_REG_DEFAULT 0x00
136#define COLOR3_ENABLE_REG_PON 0x01
137#define COLOR3_ENABLE_REG_AEN 0x02
138#define COLOR3_ENABLE_REG_PEN 0x04
139#define COLOR3_ENABLE_REG_WEN 0x08
140#define COLOR3_ENABLE_REG_AIEN 0x10
141#define COLOR3_ENABLE_REG_PIEN 0x20
148#define COLOR3_RGBC_TIMING_1_CYC 0xFF
149#define COLOR3_RGBC_TIMING_4_CYC 0xFC
150#define COLOR3_RGBC_TIMING_10_CYC 0xF6
151#define COLOR3_RGBC_TIMING_42_CYC 0xD6
152#define COLOR3_RGBC_TIMING_64_CYC 0xAD
153#define COLOR3_RGBC_TIMING_256_CYC 0x00
160#define COLOR3_PROXY_PULSE_1 0xFF
161#define COLOR3_PROXY_PULSE_10 0xF6
162#define COLOR3_PROXY_PULSE_42 0xD6
163#define COLOR3_PROXY_PULSE_64 0xAD
164#define COLOR3_PROXY_PULSE_256 0x00
171#define COLOR3_WAIT_TIME_REG_1_CYC 0xFF
172#define COLOR3_WAIT_TIME_REG_42_CYC 0xD8
173#define COLOR3_WAIT_TIME_REG_64_CYC 0xAD
174#define COLOR3_WAIT_TIME_REG_256_CYC 0x00
181#define COLOR3_LOW_THRESHOLD_VALUE 0x0130
182#define COLOR3_HIGH_THRESHOLD_VALUE 0x0170
189#define COLOR3_EVERY_CYC_INTER 0x00
190#define COLOR3_1_OR_INTER 0x01
191#define COLOR3_2_OR_INTER 0x02
192#define COLOR3_3_OR_INTER 0x03
193#define COLOR3_5_OR_INTER 0x04
194#define COLOR3_10_OR_INTER 0x05
195#define COLOR3_15_OR_INTER 0x06
196#define COLOR3_20_OR_INTER 0x07
197#define COLOR3_25_OR_INTER 0x08
198#define COLOR3_30_OR_INTER 0x09
199#define COLOR3_35_OR_INTER 0x0A
200#define COLOR3_40_OR_INTER 0x0B
201#define COLOR3_45_OR_INTER 0x0C
202#define COLOR3_50_OR_INTER 0x0D
203#define COLOR3_55_OR_INTER 0x0E
204#define COLOR3_60_OR_INTER 0x0F
211#define COLOR3_LONG_WAIT 0x02
218#define COLOR3_A_GAIN_1 0x00
219#define COLOR3_A_GAIN_4 0x01
220#define COLOR3_A_GAIN_16 0x02
221#define COLOR3_A_GAIN_60 0x03
228#define COLOR3_CLEAR_DIODE 0x10
229#define COLOR3_IR_DIODE 0x20
230#define COLOR3_BOTH_DIODE 0x30
237#define COLOR3_LED_STR_100 0x00
238#define COLOR3_LED_STR_50 0x40
239#define COLOR3_LED_STR_25 0x80
240#define COLOR3_LED_STR_12_5 0xC0
247#define COLOR3_UNKNOWN_COLOR 0
248#define COLOR3_RED_COLOR 1
249#define COLOR3_YELLOW_COLOR 2
250#define COLOR3_GREEN_COLOR 3
251#define COLOR3_CYAN_COLOR 4
252#define COLOR3_BLUE_COLOR 5
253#define COLOR3_MAGENTA_COLOR 6
254#define COLOR3_WHITE_COLOR 7
255#define COLOR3_BLACK_COLOR 8
256
261#define COLOR3_WHITE_COLOR_LIGHTNESS 97
262#define COLOR3_BLACK_COLOR_LIGHTNESS 3
263#define COLOR3_YELLOW_COLOR_HUE_ANGLE 30
264#define COLOR3_GREEN_COLOR_HUE_ANGLE 90
265#define COLOR3_CYAN_COLOR_HUE_ANGLE 150
266#define COLOR3_BLUE_COLOR_HUE_ANGLE 210
267#define COLOR3_MAGENTA_COLOR_HUE_ANGLE 270
268#define COLOR3_RED_COLOR_HUE_ANGLE 330
269
274#define COLOR3_HSL_HUE_RESOLUTION 360
275#define COLOR3_HSL_SATURATION_RESOLUTION 100
276#define COLOR3_HSL_LIGHTNESS_RESOLUTION 100
277
278 // End group macro
279// --------------------------------------------------------------- PUBLIC TYPES
288typedef struct
289{
290 // Input pins
291 digital_in_t int_pin;
292
293 // Modules
294 i2c_master_t i2c;
295
296 // ctx variable
298
299} color3_t;
300
304typedef struct
305{
306 // Communication gpio pins
307 pin_name_t scl;
308 pin_name_t sda;
309
310 // Additional gpio pins
311 pin_name_t int_pin;
312
313 // static variable
314 uint32_t i2c_speed;
315 uint8_t i2c_address;
316
318
323typedef struct
324{
325 uint16_t red;
326 uint16_t green;
327 uint16_t blue;
328 uint16_t clear;
329
331
336typedef struct
337{
338 float hue;
341
343
344 // End types group
345// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
346
352#ifdef __cplusplus
353extern "C"{
354#endif
355
365
375err_t color3_init ( color3_t *ctx, color3_cfg_t *cfg );
376
388err_t color3_generic_write ( color3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
389
401err_t color3_generic_read ( color3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
402
411void color3_normal_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
412
422void color3_special_wr_byte ( color3_t *ctx, uint8_t wr_addr, uint8_t wr_data );
423
432uint8_t color3_read_byte ( color3_t *ctx, uint8_t rd_addr );
433
443void color3_normal_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
444
454void color3_special_wr_data ( color3_t *ctx, uint8_t wr_addr, uint16_t wr_data );
455
464uint16_t color3_read_data ( color3_t *ctx, uint8_t rd_addr );
465
474void color3_write_enable_reg ( color3_t *ctx, uint8_t wr_data );
475
484
493void color3_write_rgbc_timing_reg ( color3_t *ctx , uint8_t wr_data );
494
506
516
528
537void color3_write_wait_time_reg ( color3_t *ctx, uint8_t wr_data );
538
547
556void color3_set_inter_trsh_lo( color3_t *ctx, uint16_t wr_data );
557
566void color3_set_inter_trsh_hi ( color3_t *ctx, uint16_t wr_data );
567
576void color3_set_prox_inter_trsh_lo ( color3_t *ctx, uint16_t wr_data );
577
586void color3_set_prox_inter_trsh_hi ( color3_t *ctx, uint16_t wr_data );
587
596void color3_set_int_pers ( color3_t *ctx, uint8_t int_pers );
597
606
615void color3_set_prox_int_pers ( color3_t *ctx, uint8_t int_pers );
616
625
634void color3_set_long_wait ( color3_t *ctx, uint8_t lon_wait );
635
644
653void color3_write_pulse_cnt ( color3_t *ctx, uint8_t pulse_cnt );
654
663
672void color3_write_rgbc_gain ( color3_t *ctx, uint8_t gain );
673
682
691void color3_set_diode_select ( color3_t *ctx, uint8_t dio_sel );
692
701
710void color3_set_led_str ( color3_t *ctx, uint8_t led_str );
711
720
729
738
747
756
765
774
783
792
801
812void color3_get_rgb_data ( color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data );
813
822float color3_get_color_ratio ( color3_t *ctx, uint16_t color_data );
823
837
848
862
863#ifdef __cplusplus
864}
865#endif
866#endif // _COLOR3_H_
867
868 // End public_function group
870
871// ------------------------------------------------------------------------- END
void color3_set_long_wait(color3_t *ctx, uint8_t lon_wait)
Set long wait function.
void color3_normal_wr_byte(color3_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Generic read function.
uint16_t color3_read_blue_data(color3_t *ctx)
Read blue data function.
uint8_t color3_get_diode_select(color3_t *ctx)
Get Diode Selection function.
uint16_t color3_read_prox_data(color3_t *ctx)
ead proximity data function.
void color3_set_inter_trsh_lo(color3_t *ctx, uint16_t wr_data)
RGBC interrupt threshold low value function.
uint8_t color3_get_inter_state(color3_t *ctx)
Get Interrupt state function.
uint16_t color3_read_data(color3_t *ctx, uint8_t rd_addr)
Read data function.
void color3_special_wr_byte(color3_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Special write byte function.
uint16_t color3_read_red_data(color3_t *ctx)
Read red data function.
void color3_rgbc_to_hsl(color3_t *ctx, color3_channels_t *rgbc, color3_hsl_t *hsl)
Color 3 rgbc to hsl function.
void color3_get_rgb_data(color3_t *ctx, uint16_t *red_data, uint16_t *green_data, uint16_t *blue_data)
Get color data function.
void color3_set_led_str(color3_t *ctx, uint8_t led_str)
Set LED Strength function.
void color3_set_int_pers(color3_t *ctx, uint8_t int_pers)
Set Interrupt persistence persistence function.
void color3_set_prox_inter_trsh_hi(color3_t *ctx, uint16_t wr_data)
Proximity interrupt threshold high value function.
uint8_t color3_read_dev_id(color3_t *ctx)
Read device ID function.
err_t color3_generic_write(color3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Generic write function.
void color3_cfg_setup(color3_cfg_t *cfg)
Config Object Initialization function.
float color3_get_color_ratio(color3_t *ctx, uint16_t color_data)
Get color ratio function.
void color3_set_prox_int_pers(color3_t *ctx, uint8_t int_pers)
Set Proximity interrupt persistence function.
void color3_write_rgbc_timing_reg(color3_t *ctx, uint8_t wr_data)
Write RGBC Timing Register function.
uint8_t color3_read_rgbc_gain(color3_t *ctx)
Read RGBC Gain Value function.
void color3_write_rgbc_gain(color3_t *ctx, uint8_t gain)
Write RGBC Gain Value function.
void color3_normal_wr_data(color3_t *ctx, uint8_t wr_addr, uint16_t wr_data)
Normal write data function.
void color3_set_diode_select(color3_t *ctx, uint8_t dio_sel)
Set Diode Selection function.
void color3_write_prox_timing_reg(color3_t *ctx)
Write Proximity Timing Register function.
void color3_write_wait_time_reg(color3_t *ctx, uint8_t wr_data)
Write Wait Time Register function.
void color3_set_default_settings(color3_t *ctx)
Default settings function.
void color3_set_inter_trsh_hi(color3_t *ctx, uint16_t wr_data)
RGBC interrupt threshold high value function.
uint16_t color3_read_green_data(color3_t *ctx)
Read green data function.
uint8_t color3_read_dev_stat(color3_t *ctx)
Read device status function.
uint8_t color3_get_color(color3_hsl_t *hsl)
Color 3 get color function.
uint8_t color3_read_prox_int_pers(color3_t *ctx)
Read Proximity interrupt persistence function.
uint8_t color3_read_enable_reg(color3_t *ctx)
Read Enable Register function.
uint8_t color3_read_pulse_cnt(color3_t *ctx)
Read Proximity Pulse Count function.
err_t color3_get_rgbc_data(color3_t *ctx, color3_channels_t *channels)
Color 3 get rgbc data function.
void color3_set_prox_inter_trsh_lo(color3_t *ctx, uint16_t wr_data)
Proximity interrupt threshold low value function.
void color3_write_enable_reg(color3_t *ctx, uint8_t wr_data)
Write Enable Register function.
uint8_t color3_read_rgbc_timing_reg(color3_t *ctx)
Read RGBC Timing Register function.
uint8_t color3_read_led_str(color3_t *ctx)
Read LED Strength function.
void color3_write_pulse_cnt(color3_t *ctx, uint8_t pulse_cnt)
Write Proximity Pulse Count function.
err_t color3_generic_read(color3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Generic read function.
uint8_t color3_read_prox_timing_reg(color3_t *ctx)
Read Proximity Timing Register function.
uint8_t color3_read_int_pers(color3_t *ctx)
Read Interrupt persistence function.
uint8_t color3_read_wait_time_reg(color3_t *ctx)
Read Wait Time Register function.
void color3_special_wr_data(color3_t *ctx, uint8_t wr_addr, uint16_t wr_data)
Special write function.
err_t color3_init(color3_t *ctx, color3_cfg_t *cfg)
Initialization function.
uint8_t color3_read_byte(color3_t *ctx, uint8_t rd_addr)
Read byte function.
uint16_t color3_read_clear_data(color3_t *ctx)
Read clear data function.
uint8_t color3_read_long_wait(color3_t *ctx)
Read long wait function.
Click configuration structure definition.
Definition color3.h:305
uint32_t i2c_speed
Definition color3.h:314
pin_name_t scl
Definition color3.h:307
pin_name_t int_pin
Definition color3.h:311
pin_name_t sda
Definition color3.h:308
uint8_t i2c_address
Definition color3.h:315
Color 3 RGBC channels data object.
Definition color3.h:324
uint16_t clear
Definition color3.h:328
uint16_t blue
Definition color3.h:327
uint16_t green
Definition color3.h:326
uint16_t red
Definition color3.h:325
Color 3 HSL color data object.
Definition color3.h:337
float hue
Definition color3.h:338
float saturation
Definition color3.h:339
float lightness
Definition color3.h:340
Click ctx object definition.
Definition color3.h:289
digital_in_t int_pin
Definition color3.h:291
i2c_master_t i2c
Definition color3.h:294
uint8_t slave_address
Definition color3.h:297