Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 0e7c381

Browse files
committed
Add child class to VL53L1 which accepts GpioInterface.
1 parent 125e65e commit 0e7c381

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "VL53L1GpioInterface.hpp"
2+
#include "GpioInterface.hpp"
3+
#include <Arduino.h>
4+
5+
VL53L1GpioInterface::~VL53L1GpioInterface() {
6+
end();
7+
}
8+
9+
VL53L1GpioInterface::VL53L1GpioInterface(TwoWire *const i2cDevice,
10+
GpioInterface &xshutdownPin) : VL53L1(i2cDevice, -1), xshutdown(xshutdownPin) {
11+
}
12+
13+
void VL53L1GpioInterface::VL53L1_SetDeviceAddressValue(
14+
std::uint8_t const newValue) {
15+
VL53L1_SetDeviceAddress(newValue << 1);
16+
}
17+
18+
std::uint8_t VL53L1GpioInterface::VL53L1_GetDeviceAddressValue() const {
19+
return MyDevice.I2cDevAddr >> 1;
20+
}
21+
22+
VL53L1_Error VL53L1GpioInterface::initSensorWithAddressValue(
23+
std::uint8_t const addressValue) {
24+
return InitSensor(addressValue << 1);
25+
}
26+
27+
void VL53L1GpioInterface::VL53L1_Off() {
28+
digitalWrite(xshutdown, LOW);
29+
delay(10);
30+
}
31+
32+
int VL53L1GpioInterface::end() {
33+
pinMode(xshutdown, INPUT);
34+
return 0;
35+
}
36+
37+
void VL53L1GpioInterface::VL53L1_On() {
38+
digitalWrite(xshutdown, HIGH);
39+
delay(10);
40+
}
41+
42+
int VL53L1GpioInterface::begin() {
43+
pinMode(xshutdown, OUTPUT);
44+
return 0;
45+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#ifndef VL53L1GPIOINTERFACE_HPP_
2+
#define VL53L1GPIOINTERFACE_HPP_
3+
4+
#include <vl53l1_class.h>
5+
#include <cstdint>
6+
7+
class TwoWire;
8+
class GpioInterface;
9+
10+
class VL53L1GpioInterface : public VL53L1 {
11+
public:
12+
/** Calls end(). */
13+
virtual ~VL53L1GpioInterface();
14+
15+
/**
16+
* Sets default values for members.
17+
* @param i2cDevice Is the I2C interface to be used.
18+
* It must be initialized and started.
19+
* @param xshutdownPin Is the digital output connected to XSHUT.
20+
*/
21+
VL53L1GpioInterface(TwoWire * const i2cDevice, GpioInterface& xshutdownPin);
22+
23+
/** The 7 bits of the default address value right aligned. */
24+
static constexpr std::uint8_t defaultAddressValue = VL53L1_DEFAULT_DEVICE_ADDRESS >> 1;
25+
26+
/**
27+
* Initializes the sensor and sets it's address.
28+
* @param addressValue 7 bits of the address right aligned.
29+
* @return VL53L1_ERROR_NONE on success
30+
*/
31+
virtual VL53L1_Error initSensorWithAddressValue(const std::uint8_t addressValue = defaultAddressValue);
32+
33+
/**
34+
* Sets the address of the sensor.
35+
* @param newValue is the 7 bits of the address value right aligned.
36+
*/
37+
virtual void VL53L1_SetDeviceAddressValue(const std::uint8_t newValue);
38+
39+
/**
40+
* Gets the address which has been written to the sensor.
41+
*
42+
* @return The 7 bits of the address value right-aligned.
43+
*/
44+
virtual std::uint8_t VL53L1_GetDeviceAddressValue() const;
45+
46+
/**
47+
* Sets the mode of the xshutdownPin to OUTPUT.
48+
* @return 0
49+
*/
50+
virtual int begin() override;
51+
52+
/**
53+
* Sets the mode of XShutdown pin to INPUT.
54+
* @return 0
55+
*/
56+
virtual int end() override;
57+
58+
/**
59+
* Switches on the sensor.
60+
*
61+
* The digital output connected to XSHUT is set to high.
62+
*/
63+
virtual void VL53L1_On() override;
64+
65+
/**
66+
* Switches off the sensor.
67+
*
68+
* The digital output connected to XSHUT is set to low.
69+
*/
70+
virtual void VL53L1_Off() override;
71+
72+
protected:
73+
GpioInterface& xshutdown;
74+
};
75+
76+
#endif /* VL53L1GPIOINTERFACE_HPP_ */

0 commit comments

Comments
 (0)