kopia lustrzana https://github.com/pimoroni/pimoroni-pico
VL53L5CX: Class wrapper
rodzic
6028bc16b6
commit
637f6d18ca
|
@ -0,0 +1,61 @@
|
||||||
|
#include "vl53l5cx.hpp"
|
||||||
|
|
||||||
|
namespace pimoroni {
|
||||||
|
bool VL53L5CX::init() {
|
||||||
|
uint8_t status = vl53l5cx_init(configuration);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::start_ranging() {
|
||||||
|
uint8_t status = vl53l5cx_start_ranging(configuration);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::stop_ranging() {
|
||||||
|
uint8_t status = vl53l5cx_stop_ranging(configuration);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_i2c_address(uint8_t i2c_address) {
|
||||||
|
/* Must be a 7-bit i2c address */
|
||||||
|
uint8_t status = vl53l5cx_set_i2c_address(configuration, i2c_address << 1);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_ranging_mode(RangingMode ranging_mode) {
|
||||||
|
uint8_t status = vl53l5cx_set_ranging_mode(configuration, (uint8_t)ranging_mode);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_ranging_frequency_hz(uint8_t ranging_frequency_hz) {
|
||||||
|
uint8_t status = vl53l5cx_set_ranging_frequency_hz(configuration, ranging_frequency_hz);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_resolution(Resolution resolution) {
|
||||||
|
/* One of VL53L5CX_RESOLUTION_4X4 or VL53L5CX_RESOLUTION_8X8 */
|
||||||
|
uint8_t status = vl53l5cx_set_resolution(configuration, (uint8_t)resolution);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_integration_time_ms(uint32_t integration_time_ms) {
|
||||||
|
/* Integration time between 2ms and 1000ms */
|
||||||
|
uint8_t status = vl53l5cx_set_integration_time_ms(configuration, integration_time_ms);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_sharpener_percent(uint8_t sharpener_percent) {
|
||||||
|
/* Sharpener intensity from 0 to 99 */
|
||||||
|
uint8_t status = vl53l5cx_set_sharpener_percent(configuration, sharpener_percent);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_target_order(TargetOrder target_order) {
|
||||||
|
uint8_t status = vl53l5cx_set_target_order(configuration, (uint8_t)target_order);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::set_power_mode(PowerMode power_mode) {
|
||||||
|
uint8_t status = vl53l5cx_set_power_mode(configuration, (uint8_t)power_mode);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::data_ready() {
|
||||||
|
uint8_t is_ready;
|
||||||
|
uint8_t status = vl53l5cx_check_data_ready(configuration, &is_ready);
|
||||||
|
return status == VL53L5CX_STATUS_OK && is_ready;
|
||||||
|
}
|
||||||
|
bool VL53L5CX::get_data(ResultsData *results) {
|
||||||
|
uint8_t status = vl53l5cx_get_ranging_data(configuration, results);
|
||||||
|
return status == VL53L5CX_STATUS_OK;
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,3 +3,56 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "drivers/vl53l5cx/src/VL53L5CX_ULD_API/inc/vl53l5cx_api.h"
|
#include "drivers/vl53l5cx/src/VL53L5CX_ULD_API/inc/vl53l5cx_api.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "common/pimoroni_i2c.hpp"
|
||||||
|
#include "src/VL53L5CX_ULD_API/inc/vl53l5cx_api.h"
|
||||||
|
|
||||||
|
namespace pimoroni {
|
||||||
|
class VL53L5CX {
|
||||||
|
public:
|
||||||
|
typedef VL53L5CX_ResultsData ResultsData;
|
||||||
|
enum TargetOrder : uint8_t {
|
||||||
|
TARGET_ORDER_CLOSEST = VL53L5CX_TARGET_ORDER_CLOSEST,
|
||||||
|
TARGET_ORDER_STRONGEST = VL53L5CX_TARGET_ORDER_STRONGEST
|
||||||
|
};
|
||||||
|
enum Resolution : uint8_t {
|
||||||
|
RESOLUTION_4X4 = VL53L5CX_RESOLUTION_4X4,
|
||||||
|
RESOLUTION_8X8 = VL53L5CX_RESOLUTION_8X8
|
||||||
|
};
|
||||||
|
enum RangingMode : uint8_t {
|
||||||
|
RANGING_MODE_CONTINUOUS = VL53L5CX_RANGING_MODE_CONTINUOUS,
|
||||||
|
RANGING_MODE_AUTONOMOUS = VL53L5CX_RANGING_MODE_AUTONOMOUS
|
||||||
|
};
|
||||||
|
enum PowerMode : uint8_t {
|
||||||
|
POWER_MODE_SLEEP = VL53L5CX_POWER_MODE_SLEEP,
|
||||||
|
POWER_MODE_WAKEUP = VL53L5CX_POWER_MODE_WAKEUP
|
||||||
|
};
|
||||||
|
|
||||||
|
// 7-bit version of the default address (0x52)
|
||||||
|
static const uint8_t DEFAULT_ADDRESS = VL53L5CX_DEFAULT_I2C_ADDRESS >> 1;
|
||||||
|
|
||||||
|
VL53L5CX(I2C *i2c, uint8_t i2c_addr=DEFAULT_ADDRESS) {
|
||||||
|
configuration = new VL53L5CX_Configuration{
|
||||||
|
.platform = VL53L5CX_Platform{
|
||||||
|
.address = i2c_addr,
|
||||||
|
.i2c = i2c->get_i2c()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
bool init();
|
||||||
|
bool start_ranging();
|
||||||
|
bool stop_ranging();
|
||||||
|
bool set_i2c_address(uint8_t i2c_address);
|
||||||
|
bool set_ranging_mode(RangingMode ranging_mode);
|
||||||
|
bool set_ranging_frequency_hz(uint8_t ranging_frequency_hz);
|
||||||
|
bool set_resolution(Resolution resolution);
|
||||||
|
bool set_integration_time_ms(uint32_t integration_time_ms);
|
||||||
|
bool set_sharpener_percent(uint8_t sharpener_percent);
|
||||||
|
bool set_target_order(TargetOrder target_order);
|
||||||
|
bool set_power_mode(PowerMode power_mode);
|
||||||
|
bool data_ready();
|
||||||
|
bool get_data(ResultsData *results);
|
||||||
|
private:
|
||||||
|
VL53L5CX_Configuration *configuration;
|
||||||
|
};
|
||||||
|
}
|
|
@ -5,35 +5,26 @@
|
||||||
|
|
||||||
#include "common/pimoroni_i2c.hpp"
|
#include "common/pimoroni_i2c.hpp"
|
||||||
|
|
||||||
//using namespace pimoroni;
|
using namespace pimoroni;
|
||||||
|
|
||||||
pimoroni::I2C i2c(4, 5);
|
I2C i2c(4, 5);
|
||||||
|
VL53L5CX vl53l5cx(&i2c);
|
||||||
VL53L5CX_Configuration configuration {
|
|
||||||
.platform = VL53L5CX_Platform{
|
|
||||||
.address = 0x29,
|
|
||||||
.i2c = i2c0
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
|
|
||||||
vl53l5cx_init(&configuration);
|
vl53l5cx.init();
|
||||||
vl53l5cx_set_ranging_mode(&configuration, VL53L5CX_RANGING_MODE_AUTONOMOUS);
|
vl53l5cx.set_ranging_mode(VL53L5CX::RANGING_MODE_AUTONOMOUS);
|
||||||
vl53l5cx_set_resolution(&configuration, VL53L5CX_RESOLUTION_4X4);
|
vl53l5cx.set_resolution(VL53L5CX::RESOLUTION_4X4);
|
||||||
vl53l5cx_start_ranging(&configuration);
|
vl53l5cx.start_ranging();
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
uint8_t is_ready;
|
if(vl53l5cx.data_ready()) {
|
||||||
if(vl53l5cx_check_data_ready(&configuration, &is_ready) == VL53L5CX_STATUS_OK) {
|
VL53L5CX::ResultsData result;
|
||||||
if(is_ready){
|
if(vl53l5cx.get_data(&result)) {
|
||||||
VL53L5CX_ResultsData result;
|
|
||||||
if(vl53l5cx_get_ranging_data(&configuration, &result) == VL53L5CX_STATUS_OK) {
|
|
||||||
printf("Distance: %dmm\n", result.distance_mm[0]);
|
printf("Distance: %dmm\n", result.distance_mm[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
sleep_ms(20);
|
sleep_ms(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue