-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
153 lines (123 loc) · 5.16 KB
/
main.cpp
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
153
//--------------------------------------------------------------------------------
// ESP32 & VL53L3CX test app by Motti Bazar.
// App utilizes the interrupt method.
// Code parts below that were developed by me can be used for non-commercial use
// but I am asking to be mentioned as the developer.
//
// 5-May-2021
//
// My ESP32 setup:
// SDA - pin 21
// SCL - pin 22
// INT - pin 32
// XSHUT - pin 33
//--------------------------------------------------------------------------------
#include <Arduino.h>
#include <Wire.h>
#include <vl53lx_class.h>
#define XSHUT_PIN 33
#define INT_PIN 32
#define SERIAL_BAUD_RATE 115200
#define DEVICE_I2C_ADDR 0x12
VL53LX vl53lx(&Wire, XSHUT_PIN);
// Holds latest measured distances (up to 4)
#define MAX_OBJECTS 4
uint16_t dist[MAX_OBJECTS];
//----------------------------------------
// intaISR - Interrupt handler
//----------------------------------------
portMUX_TYPE syncINTA = portMUX_INITIALIZER_UNLOCKED; // For handling ISR entry/exit
bool intaINT = false; // True = Interrupt occured
unsigned long intervalTimer; // Timer used for checking for lost interrupts
void IRAM_ATTR intaISR() {
portENTER_CRITICAL(&syncINTA);
intaINT = true; // Signal that MCP INTA occured
portEXIT_CRITICAL(&syncINTA);
}
void setup()
{
int i;
VL53LX_Error rc;
VL53LX_Version_t ver;
uint8_t ProductRevisionMajor, ProductRevisionMinor, byteData;
VL53LX_DeviceInfo_t devInfo;
uint64_t Uid;
Serial.begin(SERIAL_BAUD_RATE, SERIAL_8N1);
while (!Serial);
delay(5000);
Serial.println("Starting...");
// Initialize the array
for (i=0; i<MAX_OBJECTS; i++)
dist[i] = 0;
// Initialize the I2C bus
Wire.begin();
Wire.setClock(400000);
// Configure the VL53LX
vl53lx.begin();
vl53lx.VL53LX_Off();
vl53lx.InitSensor(DEVICE_I2C_ADDR);
// Print chip info
rc = vl53lx.VL53LX_GetVersion(&ver);
Serial.printf("GetVersion: rc=%d, Build=%d, Major=%d, Minor=%d, Revision=%d\n", rc, ver.build, ver.major, ver.minor, ver.revision);
rc = vl53lx.VL53LX_GetProductRevision(&ProductRevisionMajor, &ProductRevisionMinor);
Serial.printf("GetProductRevision: rc=%d, Major=%d, Minor=%d\n", rc, ProductRevisionMajor, ProductRevisionMinor);
rc = vl53lx.VL53LX_GetDeviceInfo(&devInfo);
Serial.printf("GetDeviceInfo: rc=%d, ModuleType(0xAA?)=0x%X, ProdRevMajor=%d, ProdRevMinor=%d\n", rc, devInfo.ProductType, devInfo.ProductRevisionMajor, devInfo.ProductRevisionMinor);
rc = vl53lx.VL53LX_GetUID(&Uid);
Serial.printf("GetUID: rc=%d, UID=0x%llX\n", rc, Uid);
// To enable using the below I2CRead function, you need to move its definition in vl53lx_class.h from the Protected section to the Public section
rc = vl53lx.VL53LX_I2CRead(DEVICE_I2C_ADDR, 0x010F, &byteData, 1);
Serial.printf("I2CRead 0x010F: rc=%d, Model_ID=0x%02X\n", rc, byteData);
rc = vl53lx.VL53LX_I2CRead(DEVICE_I2C_ADDR, 0x0110, &byteData, 1);
Serial.printf("I2CRead 0x0110: rc=%d, Module_Type=0x%02X\n", rc, byteData);
pinMode(INT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INT_PIN), intaISR, FALLING);
intervalTimer = millis(); // Set timer
vl53lx.VL53LX_ClearInterruptAndStartMeasurement();
Serial.println("Ready...");
}
void loop()
{
VL53LX_MultiRangingData_t MultiRangingData;
VL53LX_MultiRangingData_t *rangingData = &MultiRangingData;
uint8_t NewDataReady = 0;
int objectsFound = 0, j, status;
bool dataChanged = false;
if (intaINT) {
status = vl53lx.VL53LX_GetMeasurementDataReady(&NewDataReady);
if (status == 0 && NewDataReady != 0) {
status = vl53lx.VL53LX_GetMultiRangingData(rangingData);
objectsFound = rangingData->NumberOfObjectsFound;
if (objectsFound > 0) {
for(j = 0; j < objectsFound; j++) {
// Check only if status is OK
if (rangingData->RangeData[j].RangeStatus == 0) {
// Only consider changes of over 10 mm
if (abs(rangingData->RangeData[j].RangeMilliMeter - dist[j]) > 10) {
// Movement happened
dist[j] = rangingData->RangeData[j].RangeMilliMeter;
dataChanged = true;
}
}
}
// Clear rest of values
for (j=objectsFound; j < MAX_OBJECTS; j++)
dist[j] = 0;
if (dataChanged) {
dataChanged = false;
Serial.printf("%d %d %d %d\n", dist[0], dist[1], dist[2], dist[3]);
}
}
}
intaINT = false;
vl53lx.VL53LX_ClearInterruptAndStartMeasurement();
intervalTimer = millis();
} else {
// Did we lose an interrupt?
if ((millis() - intervalTimer) > 2000) {
Serial.println("Lost interrupt?");
vl53lx.VL53LX_ClearInterruptAndStartMeasurement();
intervalTimer = millis();
}
}
}