-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
123 lines (100 loc) · 4 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
#include "mbed.h"
#include "vl53l0x_api.h"
#include "vl53l0x_platform.h"
#include "vl53l0x_i2c_platform.h"
#define USE_I2C_2V8
Serial pc(USBTX, USBRX);
DigitalOut led(LED1);
VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint8_t NewDatReady=0;
uint32_t LoopNb;
if (Status == VL53L0X_ERROR_NONE) {
LoopNb = 0;
do {
Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
break;
}
LoopNb = LoopNb + 1;
VL53L0X_PollingDelay(Dev);
} while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
Status = VL53L0X_ERROR_TIME_OUT;
}
}
return Status;
}
VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint32_t StopCompleted=0;
uint32_t LoopNb;
if (Status == VL53L0X_ERROR_NONE) {
LoopNb = 0;
do {
Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
break;
}
LoopNb = LoopNb + 1;
VL53L0X_PollingDelay(Dev);
} while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
Status = VL53L0X_ERROR_TIME_OUT;
}
}
return Status;
}
int main()
{
int x=1, measure=0;
int ave=0, sum=0;
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
VL53L0X_Dev_t MyDevice;
VL53L0X_Dev_t *pMyDevice = &MyDevice;
VL53L0X_RangingMeasurementData_t RangingMeasurementData;
VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData;
VL53L0X_Version_t Version;
// Initialize Comms
pMyDevice->I2cDevAddr = 0x52;
pMyDevice->comms_type = 1;
pMyDevice->comms_speed_khz = 400;
VL53L0X_ERROR_CONTROL_INTERFACE;
VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
VL53L0X_DataInit(&MyDevice); // Data initialization
Status = VL53L0X_ERROR_NONE;
uint32_t refSpadCount;
uint8_t isApertureSpads;
uint8_t VhvSettings;
uint8_t PhaseCal;
VL53L0X_StaticInit(pMyDevice); // Device Initialization
VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
VL53L0X_StartMeasurement(pMyDevice);
while (1){
while(x<=50){
WaitMeasurementDataReady(pMyDevice);
VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
measure=pRangingMeasurementData->RangeMilliMeter;
//printf("In loop measurement %d\n", mea);
sum=sum+measure;
led=!led;
// Clear the interrupt
VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
VL53L0X_PollingDelay(pMyDevice);
x++;
}
ave=sum/x;
printf("Average is %d\n", ave);
x=1;
sum=0;
wait(20);
}
VL53L0X_StopMeasurement(pMyDevice);
WaitStopCompleted(pMyDevice);
VL53L0X_ClearInterruptMask(pMyDevice,VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
}