ambient19  2.0.0.0
ambient19.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef AMBIENT19_H
29 #define AMBIENT19_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define AMBIENT19_REG_SYSM_CTRL 0x00
74 #define AMBIENT19_REG_INT_CTRL 0x01
75 #define AMBIENT19_REG_INT_FLAG 0x02
76 #define AMBIENT19_REG_WAIT_TIME 0x03
77 #define AMBIENT19_REG_ALS_GAIN 0x04
78 #define AMBIENT19_REG_ALS_TIME 0x05
79 #define AMBIENT19_REG_LED_CTRL 0x06
80 #define AMBIENT19_REG_PS_GAIN 0x07
81 #define AMBIENT19_REG_PS_PULSE 0x08
82 #define AMBIENT19_REG_PS_TIME 0x09
83 #define AMBIENT19_REG_PERSISTENCE 0x0B
84 #define AMBIENT19_REG_ALS_THRES_LL 0x0C
85 #define AMBIENT19_REG_ALS_THRES_LH 0x0D
86 #define AMBIENT19_REG_ALS_THRES_HL 0x0E
87 #define AMBIENT19_REG_ALS_THRES_HH 0x0F
88 #define AMBIENT19_REG_PS_THRES_LL 0x10
89 #define AMBIENT19_REG_PS_THRES_LH 0x11
90 #define AMBIENT19_REG_PS_THRES_HL 0x12
91 #define AMBIENT19_REG_PS_THRES_HH 0x13
92 #define AMBIENT19_REG_PS_OFFSET_L 0x14
93 #define AMBIENT19_REG_PS_OFFSET_H 0x15
94 #define AMBIENT19_REG_INT_SOURCE 0x16
95 #define AMBIENT19_REG_ERROR_FLAG 0x17
96 #define AMBIENT19_REG_PS_DATA_L 0x18
97 #define AMBIENT19_REG_PS_DATA_H 0x19
98 #define AMBIENT19_REG_IR_DATA_L 0x1A
99 #define AMBIENT19_REG_IR_DATA_H 0x1B
100 #define AMBIENT19_REG_CH0_DATA_L 0x1C
101 #define AMBIENT19_REG_CH0_DATA_H 0x1D
102 #define AMBIENT19_REG_CH1_DATA_L 0x1E
103 #define AMBIENT19_REG_CH1_DATA_H 0x1F
104 #define AMBIENT19_REG_PNO_LB 0xBC
105 #define AMBIENT19_REG_PNO_HB 0xBD
106  // ambient19_reg
108 
123 #define AMBIENT19_SYSM_CTRL_SWRST 0x80
124 #define AMBIENT19_SYSM_EN_WAIT 0x40
125 #define AMBIENT19_SYSM_EN_PS 0x02
126 #define AMBIENT19_SYSM_EN_ALS 0x01
127 
132 #define AMBIENT19_INT_CTRL_PS_INT_MODE 0x80
133 #define AMBIENT19_INT_CTRL_SINT_MODE 0x40
134 #define AMBIENT19_INT_CTRL_PS_SYNC 0x20
135 #define AMBIENT19_INT_CTRL_ALS_SYNC 0x10
136 #define AMBIENT19_INT_CTRL_EN_PINT 0x02
137 #define AMBIENT19_INT_CTRL_EN_AINT 0x01
138 
143 #define AMBIENT19_INT_FLAG_INT_POR 0x80
144 #define AMBIENT19_INT_FLAG_DATA_FLAG 0x40
145 #define AMBIENT19_INT_FLAG_OBJ 0x20
146 #define AMBIENT19_INT_FLAG_INT_PS 0x02
147 #define AMBIENT19_INT_FLAG_INT_ALS 0x01
148 
153 #define AMBIENT19_WAIT_TIME_MAX 1280.0
154 #define AMBIENT19_WAIT_TIME_MIN 5.0
155 #define AMBIENT19_WAIT_TIME_STEP 5.0
156 #define AMBIENT19_WAIT_TIME_DEFAULT 100.0
157 
162 #define AMBIENT19_ALS_GAIN_1X 0x00
163 #define AMBIENT19_ALS_GAIN_4X 0x01
164 #define AMBIENT19_ALS_GAIN_8X 0x02
165 #define AMBIENT19_ALS_GAIN_32X 0x03
166 #define AMBIENT19_ALS_GAIN_96X 0x04
167 
172 #define AMBIENT19_ALS_TIME_MAX 674.888
173 #define AMBIENT19_ALS_TIME_MIN 5.513
174 #define AMBIENT19_ALS_TIME_STEP 2.625
175 #define AMBIENT19_ALS_TIME_DEFAULT 400.0
176 
181 #define AMBIENT19_LED_CTRL_IRDR_SEL_50mA 0x00
182 #define AMBIENT19_LED_CTRL_IRDR_SEL_100mA 0x40
183 #define AMBIENT19_LED_CTRL_IRDR_SEL_150mA 0x80
184 #define AMBIENT19_LED_CTRL_IRDR_SEL_200mA 0xC0
185 #define AMBIENT19_LED_CTRL_IRDR_SEL_BIT_MASK 0xC0
186 #define AMBIENT19_LED_CTRL_ITW_PS_MAX 875.213
187 #define AMBIENT19_LED_CTRL_ITW_PS_MIN 13.675
188 #define AMBIENT19_LED_CTRL_ITW_PS_STEP 13.675
189 #define AMBIENT19_LED_CTRL_ITW_PS_BIT_MASK 0x3F
190 
195 #define AMBIENT19_PS_GAIN_1X 0x00
196 #define AMBIENT19_PS_GAIN_2X 0x01
197 #define AMBIENT19_PS_GAIN_4X 0x02
198 #define AMBIENT19_PS_GAIN_8X 0x03
199 
204 #define AMBIENT19_PS_TIME_MAX_COUNT_4095 0x0F
205 
210 #define AMBIENT19_PERSISTANCE_EVERY_ALS_CONV 0x00
211 
216 #define AMBIENT19_INT_SOURCE_CH0_DATA 0x00
217 #define AMBIENT19_INT_SOURCE_CH1_DATA 0x01
218 
223 #define AMBIENT19_LUX_K1 0.52
224 #define AMBIENT19_LUX_K2 0.94
225 #define AMBIENT19_LUX_K3 1.12 // calculated using lux meter
226 
231 #define AMBIENT19_PRODUCT_NUM_LOW 0x14
232 #define AMBIENT19_PRODUCT_NUM_HIGH 0x16
233 
239 #define AMBIENT19_DEVICE_ADDRESS 0x38
240  // ambient19_set
242 
257 #define AMBIENT19_MAP_MIKROBUS( cfg, mikrobus ) \
258  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
259  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
260  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
261  // ambient19_map // ambient19
264 
269 typedef struct
270 {
271  // Input pins
272  digital_in_t int_pin;
274  // Modules
275  i2c_master_t i2c;
277  // I2C slave address
278  uint8_t slave_address;
280  uint8_t pga_als;
281  uint8_t alsconv;
283 } ambient19_t;
284 
289 typedef struct
290 {
291  pin_name_t scl;
292  pin_name_t sda;
294  pin_name_t int_pin;
296  uint32_t i2c_speed;
297  uint8_t i2c_address;
300 
305 typedef enum
306 {
308  AMBIENT19_ERROR = -1
309 
311 
328 
343 
357 
372 err_t ambient19_generic_write ( ambient19_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
373 
388 err_t ambient19_generic_read ( ambient19_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
389 
402 err_t ambient19_write_register ( ambient19_t *ctx, uint8_t reg, uint8_t data_in );
403 
416 err_t ambient19_read_register ( ambient19_t *ctx, uint8_t reg, uint8_t *data_out );
417 
429 
439 
451 
463 err_t ambient19_set_wait_time ( ambient19_t *ctx, float wait_time_ms );
464 
476 err_t ambient19_set_als_time ( ambient19_t *ctx, float als_time_ms );
477 
494 err_t ambient19_set_als_gain ( ambient19_t *ctx, uint8_t als_gain );
495 
508 err_t ambient19_read_raw_als ( ambient19_t *ctx, uint16_t *ch_0, uint16_t *ch_1 );
509 
522 err_t ambient19_read_raw_proximity ( ambient19_t *ctx, uint16_t *ps_data, uint16_t *ir_data );
523 
536 err_t ambient19_measure_light_level ( ambient19_t *ctx, uint16_t *lux );
537 
538 #ifdef __cplusplus
539 }
540 #endif
541 #endif // AMBIENT19_H
542  // ambient19
544 
545 // ------------------------------------------------------------------------ END
ambient19_cfg_t::sda
pin_name_t sda
Definition: ambient19.h:292
ambient19_cfg_t::int_pin
pin_name_t int_pin
Definition: ambient19.h:294
ambient19_read_register
err_t ambient19_read_register(ambient19_t *ctx, uint8_t reg, uint8_t *data_out)
Ambient 19 read register function.
ambient19_default_cfg
err_t ambient19_default_cfg(ambient19_t *ctx)
Ambient 19 default configuration function.
ambient19_t
Ambient 19 Click context object.
Definition: ambient19.h:270
ambient19_cfg_t
Ambient 19 Click configuration object.
Definition: ambient19.h:290
ambient19_t::slave_address
uint8_t slave_address
Definition: ambient19.h:278
ambient19_cfg_setup
void ambient19_cfg_setup(ambient19_cfg_t *cfg)
Ambient 19 configuration object setup function.
ambient19_set_als_time
err_t ambient19_set_als_time(ambient19_t *ctx, float als_time_ms)
Ambient 19 set als time function.
ambient19_read_raw_als
err_t ambient19_read_raw_als(ambient19_t *ctx, uint16_t *ch_0, uint16_t *ch_1)
Ambient 19 read raw als function.
ambient19_return_value_t
ambient19_return_value_t
Ambient 19 Click return value data.
Definition: ambient19.h:306
ambient19_init
err_t ambient19_init(ambient19_t *ctx, ambient19_cfg_t *cfg)
Ambient 19 initialization function.
ambient19_generic_write
err_t ambient19_generic_write(ambient19_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Ambient 19 I2C writing function.
ambient19_t::pga_als
uint8_t pga_als
Definition: ambient19.h:280
AMBIENT19_ERROR
@ AMBIENT19_ERROR
Definition: ambient19.h:308
ambient19_t::alsconv
uint8_t alsconv
Definition: ambient19.h:281
ambient19_set_wait_time
err_t ambient19_set_wait_time(ambient19_t *ctx, float wait_time_ms)
Ambient 19 set wait time function.
ambient19_t::int_pin
digital_in_t int_pin
Definition: ambient19.h:272
ambient19_get_int_pin
uint8_t ambient19_get_int_pin(ambient19_t *ctx)
Ambient 19 get INT pin function.
ambient19_write_register
err_t ambient19_write_register(ambient19_t *ctx, uint8_t reg, uint8_t data_in)
Ambient 19 write register function.
ambient19_read_raw_proximity
err_t ambient19_read_raw_proximity(ambient19_t *ctx, uint16_t *ps_data, uint16_t *ir_data)
Ambient 19 read raw proximity function.
ambient19_cfg_t::scl
pin_name_t scl
Definition: ambient19.h:291
AMBIENT19_OK
@ AMBIENT19_OK
Definition: ambient19.h:307
ambient19_cfg_t::i2c_address
uint8_t i2c_address
Definition: ambient19.h:297
ambient19_generic_read
err_t ambient19_generic_read(ambient19_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Ambient 19 I2C reading function.
ambient19_check_communication
err_t ambient19_check_communication(ambient19_t *ctx)
Ambient 19 check communication function.
ambient19_set_als_gain
err_t ambient19_set_als_gain(ambient19_t *ctx, uint8_t als_gain)
Ambient 19 set als gain function.
ambient19_t::i2c
i2c_master_t i2c
Definition: ambient19.h:275
ambient19_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ambient19.h:296
ambient19_clear_interrupts
err_t ambient19_clear_interrupts(ambient19_t *ctx)
Ambient 19 clear interrupts function.
ambient19_measure_light_level
err_t ambient19_measure_light_level(ambient19_t *ctx, uint16_t *lux)
Ambient 19 measure light level function.