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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 
69 #define AMBIENT19_REG_SYSM_CTRL 0x00
70 #define AMBIENT19_REG_INT_CTRL 0x01
71 #define AMBIENT19_REG_INT_FLAG 0x02
72 #define AMBIENT19_REG_WAIT_TIME 0x03
73 #define AMBIENT19_REG_ALS_GAIN 0x04
74 #define AMBIENT19_REG_ALS_TIME 0x05
75 #define AMBIENT19_REG_LED_CTRL 0x06
76 #define AMBIENT19_REG_PS_GAIN 0x07
77 #define AMBIENT19_REG_PS_PULSE 0x08
78 #define AMBIENT19_REG_PS_TIME 0x09
79 #define AMBIENT19_REG_PERSISTENCE 0x0B
80 #define AMBIENT19_REG_ALS_THRES_LL 0x0C
81 #define AMBIENT19_REG_ALS_THRES_LH 0x0D
82 #define AMBIENT19_REG_ALS_THRES_HL 0x0E
83 #define AMBIENT19_REG_ALS_THRES_HH 0x0F
84 #define AMBIENT19_REG_PS_THRES_LL 0x10
85 #define AMBIENT19_REG_PS_THRES_LH 0x11
86 #define AMBIENT19_REG_PS_THRES_HL 0x12
87 #define AMBIENT19_REG_PS_THRES_HH 0x13
88 #define AMBIENT19_REG_PS_OFFSET_L 0x14
89 #define AMBIENT19_REG_PS_OFFSET_H 0x15
90 #define AMBIENT19_REG_INT_SOURCE 0x16
91 #define AMBIENT19_REG_ERROR_FLAG 0x17
92 #define AMBIENT19_REG_PS_DATA_L 0x18
93 #define AMBIENT19_REG_PS_DATA_H 0x19
94 #define AMBIENT19_REG_IR_DATA_L 0x1A
95 #define AMBIENT19_REG_IR_DATA_H 0x1B
96 #define AMBIENT19_REG_CH0_DATA_L 0x1C
97 #define AMBIENT19_REG_CH0_DATA_H 0x1D
98 #define AMBIENT19_REG_CH1_DATA_L 0x1E
99 #define AMBIENT19_REG_CH1_DATA_H 0x1F
100 #define AMBIENT19_REG_PNO_LB 0xBC
101 #define AMBIENT19_REG_PNO_HB 0xBD
102  // ambient19_reg
104 
119 #define AMBIENT19_SYSM_CTRL_SWRST 0x80
120 #define AMBIENT19_SYSM_EN_WAIT 0x40
121 #define AMBIENT19_SYSM_EN_PS 0x02
122 #define AMBIENT19_SYSM_EN_ALS 0x01
123 
128 #define AMBIENT19_INT_CTRL_PS_INT_MODE 0x80
129 #define AMBIENT19_INT_CTRL_SINT_MODE 0x40
130 #define AMBIENT19_INT_CTRL_PS_SYNC 0x20
131 #define AMBIENT19_INT_CTRL_ALS_SYNC 0x10
132 #define AMBIENT19_INT_CTRL_EN_PINT 0x02
133 #define AMBIENT19_INT_CTRL_EN_AINT 0x01
134 
139 #define AMBIENT19_INT_FLAG_INT_POR 0x80
140 #define AMBIENT19_INT_FLAG_DATA_FLAG 0x40
141 #define AMBIENT19_INT_FLAG_OBJ 0x20
142 #define AMBIENT19_INT_FLAG_INT_PS 0x02
143 #define AMBIENT19_INT_FLAG_INT_ALS 0x01
144 
149 #define AMBIENT19_WAIT_TIME_MAX 1280.0
150 #define AMBIENT19_WAIT_TIME_MIN 5.0
151 #define AMBIENT19_WAIT_TIME_STEP 5.0
152 #define AMBIENT19_WAIT_TIME_DEFAULT 100.0
153 
158 #define AMBIENT19_ALS_GAIN_1X 0x00
159 #define AMBIENT19_ALS_GAIN_4X 0x01
160 #define AMBIENT19_ALS_GAIN_8X 0x02
161 #define AMBIENT19_ALS_GAIN_32X 0x03
162 #define AMBIENT19_ALS_GAIN_96X 0x04
163 
168 #define AMBIENT19_ALS_TIME_MAX 674.888
169 #define AMBIENT19_ALS_TIME_MIN 5.513
170 #define AMBIENT19_ALS_TIME_STEP 2.625
171 #define AMBIENT19_ALS_TIME_DEFAULT 400.0
172 
177 #define AMBIENT19_LED_CTRL_IRDR_SEL_50mA 0x00
178 #define AMBIENT19_LED_CTRL_IRDR_SEL_100mA 0x40
179 #define AMBIENT19_LED_CTRL_IRDR_SEL_150mA 0x80
180 #define AMBIENT19_LED_CTRL_IRDR_SEL_200mA 0xC0
181 #define AMBIENT19_LED_CTRL_IRDR_SEL_BIT_MASK 0xC0
182 #define AMBIENT19_LED_CTRL_ITW_PS_MAX 875.213
183 #define AMBIENT19_LED_CTRL_ITW_PS_MIN 13.675
184 #define AMBIENT19_LED_CTRL_ITW_PS_STEP 13.675
185 #define AMBIENT19_LED_CTRL_ITW_PS_BIT_MASK 0x3F
186 
191 #define AMBIENT19_PS_GAIN_1X 0x00
192 #define AMBIENT19_PS_GAIN_2X 0x01
193 #define AMBIENT19_PS_GAIN_4X 0x02
194 #define AMBIENT19_PS_GAIN_8X 0x03
195 
200 #define AMBIENT19_PS_TIME_MAX_COUNT_4095 0x0F
201 
206 #define AMBIENT19_PERSISTANCE_EVERY_ALS_CONV 0x00
207 
212 #define AMBIENT19_INT_SOURCE_CH0_DATA 0x00
213 #define AMBIENT19_INT_SOURCE_CH1_DATA 0x01
214 
219 #define AMBIENT19_LUX_K1 0.52
220 #define AMBIENT19_LUX_K2 0.94
221 #define AMBIENT19_LUX_K3 1.12 // calculated using lux meter
222 
227 #define AMBIENT19_PRODUCT_NUM_LOW 0x14
228 #define AMBIENT19_PRODUCT_NUM_HIGH 0x16
229 
235 #define AMBIENT19_DEVICE_ADDRESS 0x38
236  // ambient19_set
238 
253 #define AMBIENT19_MAP_MIKROBUS( cfg, mikrobus ) \
254  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
255  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
256  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
257  // ambient19_map // ambient19
260 
265 typedef struct
266 {
267  // Input pins
268  digital_in_t int_pin;
270  // Modules
271  i2c_master_t i2c;
273  // I2C slave address
274  uint8_t slave_address;
276  uint8_t pga_als;
277  uint8_t alsconv;
279 } ambient19_t;
280 
285 typedef struct
286 {
287  pin_name_t scl;
288  pin_name_t sda;
290  pin_name_t int_pin;
292  uint32_t i2c_speed;
293  uint8_t i2c_address;
296 
301 typedef enum
302 {
304  AMBIENT19_ERROR = -1
305 
307 
324 
339 
353 
368 err_t ambient19_generic_write ( ambient19_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
369 
384 err_t ambient19_generic_read ( ambient19_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
385 
398 err_t ambient19_write_register ( ambient19_t *ctx, uint8_t reg, uint8_t data_in );
399 
412 err_t ambient19_read_register ( ambient19_t *ctx, uint8_t reg, uint8_t *data_out );
413 
425 
435 
447 
459 err_t ambient19_set_wait_time ( ambient19_t *ctx, float wait_time_ms );
460 
472 err_t ambient19_set_als_time ( ambient19_t *ctx, float als_time_ms );
473 
490 err_t ambient19_set_als_gain ( ambient19_t *ctx, uint8_t als_gain );
491 
504 err_t ambient19_read_raw_als ( ambient19_t *ctx, uint16_t *ch_0, uint16_t *ch_1 );
505 
518 err_t ambient19_read_raw_proximity ( ambient19_t *ctx, uint16_t *ps_data, uint16_t *ir_data );
519 
532 err_t ambient19_measure_light_level ( ambient19_t *ctx, uint16_t *lux );
533 
534 #ifdef __cplusplus
535 }
536 #endif
537 #endif // AMBIENT19_H
538  // ambient19
540 
541 // ------------------------------------------------------------------------ END
ambient19_cfg_t::sda
pin_name_t sda
Definition: ambient19.h:288
ambient19_cfg_t::int_pin
pin_name_t int_pin
Definition: ambient19.h:290
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:266
ambient19_cfg_t
Ambient 19 Click configuration object.
Definition: ambient19.h:286
ambient19_t::slave_address
uint8_t slave_address
Definition: ambient19.h:274
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:302
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:276
AMBIENT19_ERROR
@ AMBIENT19_ERROR
Definition: ambient19.h:304
ambient19_t::alsconv
uint8_t alsconv
Definition: ambient19.h:277
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:268
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:287
AMBIENT19_OK
@ AMBIENT19_OK
Definition: ambient19.h:303
ambient19_cfg_t::i2c_address
uint8_t i2c_address
Definition: ambient19.h:293
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:271
ambient19_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ambient19.h:292
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.