lightranger  2.0.0.0
lightranger.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 LIGHTRANGER_H
36 #define LIGHTRANGER_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 LIGHTRANGER_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
57  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
58 
64 #define LIGHTRANGER_RETVAL uint8_t
65 
66 #define LIGHTRANGER_OK 0x00
67 #define LIGHTRANGER_INIT_ERROR 0xFF
68 
74 #define LIGHTRANGER_IDENTIFICATION_MODEL_ID 0x0000
75 #define LIGHTRANGER_IDENTIFICATION_MODEL_REV_MAJOR 0x0001
76 #define LIGHTRANGER_IDENTIFICATION_MODEL_REV_MINOR 0x0002
77 #define LIGHTRANGER_IDENTIFICATION_MODULE_REV_MAJOR 0x0003
78 #define LIGHTRANGER_IDENTIFICATION_MODULE_REV_MINOR 0x0004
79 #define LIGHTRANGER_IDENTIFICATION_DATE_HI 0x0006
80 #define LIGHTRANGER_IDENTIFICATION_DATE_LO 0x0007
81 #define LIGHTRANGER_IDENTIFICATION_TIME_HI 0x0008
82 #define LIGHTRANGER_IDENTIFICATION_TIME_LO 0x0009
83 
89 #define LIGHTRANGER_SYSTEM_MODE_GPIO0 0x0010
90 #define LIGHTRANGER_SYSTEM_MODE_GPIO1 0x0011
91 #define LIGHTRANGER_SYSTEM_HISTORY_CTRL 0x0012
92 #define LIGHTRANGER_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0014
93 #define LIGHTRANGER_SYSTEM_INTERRUPT_CLEAR 0x0015
94 #define LIGHTRANGER_SYSTEM_FRESH_OUT_OF_RESET 0x0016
95 #define LIGHTRANGER_SYSTEM_GROUPED_PARAMETER_HOLD 0x0017
96 
102 #define LIGHTRANGER_SYSRANGE_START 0x0018
103 #define LIGHTRANGER_SYSRANGE_THRESH_HIGH 0x0019
104 #define LIGHTRANGER_SYSRANGE_THRESH_LOW 0x001A
105 #define LIGHTRANGER_SYSRANGE_INTERMEASUREMENT_PERIOD 0x001B
106 #define LIGHTRANGER_SYSRANGE_MAX_CONVERGENCE_TIME 0x001C
107 #define LIGHTRANGER_SYSRANGE_CROSSTALK_COMPENSATION_RATE 0x001E
108 #define LIGHTRANGER_SYSRANGE_CROSSTALK_VALID_HEIGHT 0x0021
109 #define LIGHTRANGER_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE 0x0022
110 #define LIGHTRANGER_SYSRANGE_PART_TO_PART_RANGE_OFFSET 0x0024
111 #define LIGHTRANGER_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT 0x0025
112 #define LIGHTRANGER_SYSRANGE_RANGE_IGNORE_THRESHOLD 0x0026
113 #define LIGHTRANGER_SYSRANGE_MAX_AMBIENT_LEVEL_MULT 0x002C
114 #define LIGHTRANGER_SYSRANGE_RANGE_CHECK_ENABLES 0x002D
115 #define LIGHTRANGER_SYSRANGE_VHV_RECALIBRATE 0x002E
116 #define LIGHTRANGER_SYSRANGE_VHV_REPEAT_RATE 0x0031
117 #define LIGHTRANGER_SYSALS_START 0x0038
118 #define LIGHTRANGER_SYSALS_THRESH_HIGH 0x003A
119 #define LIGHTRANGER_SYSALS_THRESH_LOW 0x003C
120 #define LIGHTRANGER_SYSALS_INTERMEASUREMENT_PERIOD 0x003E
121 #define LIGHTRANGER_SYSALS_ANALOGUE_GAIN 0x003F
122 #define LIGHTRANGER_SYSALS_INTEGRATION_PERIOD 0x0040
123 
129 #define LIGHTRANGER_RESULT_RANGE_STATUS 0x004D
130 #define LIGHTRANGER_RESULT_ALS_STATUS 0x004E
131 #define LIGHTRANGER_RESULT_INTERRUPT_STATUS_GPIO 0x004F
132 #define LIGHTRANGER_RESULT_ALS_VAL 0x0050
133 #define LIGHTRANGER_RESULT_HISTORY_BUFFER0 0x0052
134 #define LIGHTRANGER_RESULT_HISTORY_BUFFER1 0x0053
135 #define LIGHTRANGER_RESULT_HISTORY_BUFFER2 0x0054
136 #define LIGHTRANGER_RESULT_HISTORY_BUFFER3 0x0055
137 #define LIGHTRANGER_RESULT_HISTORY_BUFFER4 0x0056
138 #define LIGHTRANGER_RESULT_HISTORY_BUFFER5 0x0057
139 #define LIGHTRANGER_RESULT_HISTORY_BUFFER6 0x0058
140 #define LIGHTRANGER_RESULT_HISTORY_BUFFER7 0x0059
141 #define LIGHTRANGER_RESULT_HISTORY_BUFFER8 0x0060
142 #define LIGHTRANGER_RESULT_RANGE_VAL 0x0062
143 #define LIGHTRANGER_RESULT_RANGE_RAW 0x0064
144 #define LIGHTRANGER_RESULT_RANGE_RETURN_RATE 0x0066
145 #define LIGHTRANGER_RESULT_RANGE_REFERENCE_RATE 0x0068
146 #define LIGHTRANGER_RESULT_RANGE_RETURN_SIGNAL_COUNT 0x006C
147 #define LIGHTRANGER_RESULT_RANGE_REFERENCE_SIGNAL_COUNT 0x0070
148 #define LIGHTRANGER_RESULT_RANGE_RETURN_AMB_COUNT 0x0074
149 #define LIGHTRANGER_RESULT_RANGE_REFERENCE_AMB_COUNT 0x0078
150 #define LIGHTRANGER_RESULT_RANGE_RETURN_CONV_TIME 0x007C
151 #define LIGHTRANGER_RESULT_RANGE_REFERENCE_CONV_TIME 0x0080
152 
157 #define LIGHTRANGER_READOUT_AVERAGING_SAMPLE_PERIOD 0x010A
158 #define LIGHTRANGER_FIRMWARE_BOOTUP 0x0119
159 #define LIGHTRANGER_FIRMWARE_RESULT_SCALER 0x0120
160 #define LIGHTRANGER_I2C_SLAVE_DEVICE_ADDRESS 0x0212
161 #define LIGHTRANGER_INTERLEAVED_MODE_ENABLE 0x02A3
162 
168 #define LIGHTRANGER_CMD_RESET 0x1E
169 #define LIGHTRANGER_CMD_ADC_READ 0x00
170 #define LIGHTRANGER_CMD_ADC_CONV 0x40
171 #define LIGHTRANGER_CMD_ADC_D1 0x00
172 #define LIGHTRANGER_CMD_ADC_D2 0x10
173 #define LIGHTRANGER_CMD_ADC_256 0x00
174 #define LIGHTRANGER_CMD_ADC_512 0x02
175 #define LIGHTRANGER_CMD_ADC_1024 0x04
176 #define LIGHTRANGER_CMD_ADC_2048 0x06
177 #define LIGHTRANGER_CMD_ADC_4096 0x08
178 #define LIGHTRANGER_CMD_PROM_RD 0xA0
179 #define LIGHTRANGER_CMD_CALC_DIST 0x32
180 #define LIGHTRANGER_CMD_INTERRUPT_CLEAR 0x07
181 #define LIGHTRANGER_CMD_GAIN_1X 0x06
182 
188 #define LIGHTRANGER_I2C_ADDRESS 0x29
189  // End group macro
192 // --------------------------------------------------------------- PUBLIC TYPES
201 typedef struct
202 {
203 
204  digital_out_t en;
205 
206  digital_in_t int_pin;
207 
208  // Modules
209 
210  i2c_master_t i2c;
211 
212  // ctx variable
213 
214  uint8_t slave_address;
215 
216 } lightranger_t;
217 
221 typedef struct
222 {
223  // Communication gpio pins
224 
225  pin_name_t scl;
226  pin_name_t sda;
227 
228  // Additional gpio pins
229 
230  pin_name_t en;
231 
232  pin_name_t int_pin;
233 
234  // static variable
235 
236  uint32_t i2c_speed;
237  uint8_t i2c_address;
238 
240  // End types group
242 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
243 
249 #ifdef __cplusplus
250 extern "C"{
251 #endif
252 
262 
272 
281 
290 
300 
311 
322 float lightranger_get_ambiant_light ( lightranger_t* ctx, uint8_t als_gain_check );
323 
333 
344 uint16_t lightranger_read_data ( lightranger_t* ctx, uint16_t reg_addr );
345 
356 void lightranger_write_data ( lightranger_t* ctx, uint16_t reg_addr, uint16_t write_command );
357 
368 uint8_t lightranger_read_byte ( lightranger_t* ctx, uint16_t reg_addr );
369 
379 void lightranger_write_byte ( lightranger_t* ctx, uint16_t reg_addr, uint8_t write_command );
380 
381 
382 
383 #ifdef __cplusplus
384 }
385 #endif
386 #endif // _LIGHTRANGER_H_
387  // End public_function group
390 
391 // ------------------------------------------------------------------------- END
lightranger_read_data
uint16_t lightranger_read_data(lightranger_t *ctx, uint16_t reg_addr)
Generic read data function.
lightranger_read_byte
uint8_t lightranger_read_byte(lightranger_t *ctx, uint16_t reg_addr)
Generic read byte function.
lightranger_t::slave_address
uint8_t slave_address
Definition: lightranger.h:214
lightranger_get_distance
uint8_t lightranger_get_distance(lightranger_t *ctx)
Get Distance.
lightranger_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: lightranger.h:236
lightranger_write_byte
void lightranger_write_byte(lightranger_t *ctx, uint16_t reg_addr, uint8_t write_command)
Generic write byte function.
lightranger_poll_range
void lightranger_poll_range(lightranger_t *ctx)
Poll range.
lightranger_cfg_t::int_pin
pin_name_t int_pin
Definition: lightranger.h:232
LIGHTRANGER_RETVAL
#define LIGHTRANGER_RETVAL
Definition: lightranger.h:64
lightranger_cfg_t
Click configuration structure definition.
Definition: lightranger.h:222
lightranger_get_ambiant_light
float lightranger_get_ambiant_light(lightranger_t *ctx, uint8_t als_gain_check)
Get ambiant light.
lightranger_cfg_t::scl
pin_name_t scl
Definition: lightranger.h:225
lightranger_interrupts_clear
void lightranger_interrupts_clear(lightranger_t *ctx)
Clear Interrupts.
lightranger_start_single_shot_range_mode
void lightranger_start_single_shot_range_mode(lightranger_t *ctx)
Function Start Single Mode.
lightranger_t::i2c
i2c_master_t i2c
Definition: lightranger.h:210
lightranger_t::int_pin
digital_in_t int_pin
Definition: lightranger.h:206
lightranger_cfg_setup
void lightranger_cfg_setup(lightranger_cfg_t *cfg)
Config Object Initialization function.
lightranger_t::en
digital_out_t en
Definition: lightranger.h:204
lightranger_write_data
void lightranger_write_data(lightranger_t *ctx, uint16_t reg_addr, uint16_t write_command)
Generic write data function.
lightranger_cfg_t::sda
pin_name_t sda
Definition: lightranger.h:226
lightranger_init
LIGHTRANGER_RETVAL lightranger_init(lightranger_t *ctx, lightranger_cfg_t *cfg)
Initialization function.
lightranger_t
Click ctx object definition.
Definition: lightranger.h:202
lightranger_cfg_t::i2c_address
uint8_t i2c_address
Definition: lightranger.h:237
lightranger_default_cfg
void lightranger_default_cfg(lightranger_t *ctx)
Click Default Configuration function.
lightranger_cfg_t::en
pin_name_t en
Definition: lightranger.h:230