-
Notifications
You must be signed in to change notification settings - Fork 27
/
VL6180X.h
155 lines (128 loc) · 6.49 KB
/
VL6180X.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef VL6180X_h
#define VL6180X_h
#include <Arduino.h>
#include <Wire.h>
#define VL6180X_ERROR_NONE 0 // No error; Valid measurement
#define VL6180X_ERROR_SYSERR_1 1 // System error; VCSEL Continuity Test; No measurement possible
#define VL6180X_ERROR_SYSERR_2 2 // System error; VCSEL Watchdog Test; No measurement possible
#define VL6180X_ERROR_SYSERR_3 3 // System error; VCSEL Watchdog; No measurement possible
#define VL6180X_ERROR_SYSERR_4 4 // System error; PLL1 Lock; No measurement possible
#define VL6180X_ERROR_SYSERR_5 5 // System error; PLL2 Lock; No measurement possible
#define VL6180X_ERROR_ECEFAIL 6 // Early Convergence Estimate; Check fail
#define VL6180X_ERROR_NOCONVERGE 7 // Max convergence; System didn't converge before the specified time limit
#define VL6180X_ERROR_RANGEIGNORE 8 // Range ignore; No Target Ignore; Ignore threshold check failed
#define VL6180X_ERROR_SNR 11 // Max Signal To Noise Ratio; Ambient conditions too high
#define VL6180X_ERROR_RAWUFLOW 12 // Raw Range underflow; Target too close
#define VL6180X_ERROR_RAWOFLOW 13 // Raw Range overflow; Target too far
#define VL6180X_ERROR_RANGEUFLOW 14 // Range underflow; Target too close
#define VL6180X_ERROR_RANGEOFLOW 15 // Range overflow; Target too far
class VL6180X
{
public:
// register addresses
enum regAddr
{
IDENTIFICATION__MODEL_ID = 0x000,
IDENTIFICATION__MODEL_REV_MAJOR = 0x001,
IDENTIFICATION__MODEL_REV_MINOR = 0x002,
IDENTIFICATION__MODULE_REV_MAJOR = 0x003,
IDENTIFICATION__MODULE_REV_MINOR = 0x004,
IDENTIFICATION__DATE_HI = 0x006,
IDENTIFICATION__DATE_LO = 0x007,
IDENTIFICATION__TIME = 0x008, // 16-bit
SYSTEM__MODE_GPIO0 = 0x010,
SYSTEM__MODE_GPIO1 = 0x011,
SYSTEM__HISTORY_CTRL = 0x012,
SYSTEM__INTERRUPT_CONFIG_GPIO = 0x014,
SYSTEM__INTERRUPT_CLEAR = 0x015,
SYSTEM__FRESH_OUT_OF_RESET = 0x016,
SYSTEM__GROUPED_PARAMETER_HOLD = 0x017,
SYSRANGE__START = 0x018,
SYSRANGE__THRESH_HIGH = 0x019,
SYSRANGE__THRESH_LOW = 0x01A,
SYSRANGE__INTERMEASUREMENT_PERIOD = 0x01B,
SYSRANGE__MAX_CONVERGENCE_TIME = 0x01C,
SYSRANGE__CROSSTALK_COMPENSATION_RATE = 0x01E, // 16-bit
SYSRANGE__CROSSTALK_VALID_HEIGHT = 0x021,
SYSRANGE__EARLY_CONVERGENCE_ESTIMATE = 0x022, // 16-bit
SYSRANGE__PART_TO_PART_RANGE_OFFSET = 0x024,
SYSRANGE__RANGE_IGNORE_VALID_HEIGHT = 0x025,
SYSRANGE__RANGE_IGNORE_THRESHOLD = 0x026, // 16-bit
SYSRANGE__MAX_AMBIENT_LEVEL_MULT = 0x02C,
SYSRANGE__RANGE_CHECK_ENABLES = 0x02D,
SYSRANGE__VHV_RECALIBRATE = 0x02E,
SYSRANGE__VHV_REPEAT_RATE = 0x031,
SYSALS__START = 0x038,
SYSALS__THRESH_HIGH = 0x03A,
SYSALS__THRESH_LOW = 0x03C,
SYSALS__INTERMEASUREMENT_PERIOD = 0x03E,
SYSALS__ANALOGUE_GAIN = 0x03F,
SYSALS__INTEGRATION_PERIOD = 0x040,
RESULT__RANGE_STATUS = 0x04D,
RESULT__ALS_STATUS = 0x04E,
RESULT__INTERRUPT_STATUS_GPIO = 0x04F,
RESULT__ALS_VAL = 0x050, // 16-bit
RESULT__HISTORY_BUFFER_0 = 0x052, // 16-bit
RESULT__HISTORY_BUFFER_1 = 0x054, // 16-bit
RESULT__HISTORY_BUFFER_2 = 0x056, // 16-bit
RESULT__HISTORY_BUFFER_3 = 0x058, // 16-bit
RESULT__HISTORY_BUFFER_4 = 0x05A, // 16-bit
RESULT__HISTORY_BUFFER_5 = 0x05C, // 16-bit
RESULT__HISTORY_BUFFER_6 = 0x05E, // 16-bit
RESULT__HISTORY_BUFFER_7 = 0x060, // 16-bit
RESULT__RANGE_VAL = 0x062,
RESULT__RANGE_RAW = 0x064,
RESULT__RANGE_RETURN_RATE = 0x066, // 16-bit
RESULT__RANGE_REFERENCE_RATE = 0x068, // 16-bit
RESULT__RANGE_RETURN_SIGNAL_COUNT = 0x06C, // 32-bit
RESULT__RANGE_REFERENCE_SIGNAL_COUNT = 0x070, // 32-bit
RESULT__RANGE_RETURN_AMB_COUNT = 0x074, // 32-bit
RESULT__RANGE_REFERENCE_AMB_COUNT = 0x078, // 32-bit
RESULT__RANGE_RETURN_CONV_TIME = 0x07C, // 32-bit
RESULT__RANGE_REFERENCE_CONV_TIME = 0x080, // 32-bit
RANGE_SCALER = 0x096, // 16-bit - see STSW-IMG003 core/inc/vl6180x_def.h
READOUT__AVERAGING_SAMPLE_PERIOD = 0x10A,
FIRMWARE__BOOTUP = 0x119,
FIRMWARE__RESULT_SCALER = 0x120,
I2C_SLAVE__DEVICE_ADDRESS = 0x212,
INTERLEAVED_MODE__ENABLE = 0x2A3,
};
uint8_t last_status; // status of last I2C transmission
VL6180X();
void setBus(TwoWire * bus) { this->bus = bus; }
TwoWire * getBus() { return bus; }
void setAddress(uint8_t new_addr);
uint8_t getAddress() { return address; }
void init();
void configureDefault();
void writeReg(uint16_t reg, uint8_t value);
void writeReg16Bit(uint16_t reg, uint16_t value);
void writeReg32Bit(uint16_t reg, uint32_t value);
uint8_t readReg(uint16_t reg);
uint16_t readReg16Bit(uint16_t reg);
uint32_t readReg32Bit(uint16_t reg);
void setScaling(uint8_t new_scaling);
inline uint8_t getScaling() { return scaling; }
uint8_t readRangeSingle();
inline uint16_t readRangeSingleMillimeters() { return (uint16_t)scaling * readRangeSingle(); }
uint16_t readAmbientSingle();
void startRangeContinuous(uint16_t period = 100);
void startAmbientContinuous(uint16_t period = 500);
void startInterleavedContinuous(uint16_t period = 500);
void stopContinuous();
uint8_t readRangeContinuous();
inline uint16_t readRangeContinuousMillimeters() { return (uint16_t)scaling * readRangeContinuous(); }
uint16_t readAmbientContinuous();
inline void setTimeout(uint16_t timeout) { io_timeout = timeout; }
inline uint16_t getTimeout() { return io_timeout; }
bool timeoutOccurred();
uint8_t readRangeStatus();
private:
TwoWire *bus;
uint8_t address;
uint8_t scaling;
int8_t ptp_offset;
uint16_t io_timeout;
bool did_timeout;
};
#endif