From 637f6d18ca2b70e42108eedc93258b68c7636425 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 19 Apr 2022 17:24:06 +0100 Subject: [PATCH] VL53L5CX: Class wrapper --- drivers/vl53l5cx/vl53l5cx.cpp | 61 ++++++++++++++++++++ drivers/vl53l5cx/vl53l5cx.hpp | 53 +++++++++++++++++ examples/breakout_vl53l5cx/vl53l5cx_demo.cpp | 33 ++++------- 3 files changed, 126 insertions(+), 21 deletions(-) diff --git a/drivers/vl53l5cx/vl53l5cx.cpp b/drivers/vl53l5cx/vl53l5cx.cpp index e69de29b..78e0b33e 100644 --- a/drivers/vl53l5cx/vl53l5cx.cpp +++ b/drivers/vl53l5cx/vl53l5cx.cpp @@ -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; + } +} \ No newline at end of file diff --git a/drivers/vl53l5cx/vl53l5cx.hpp b/drivers/vl53l5cx/vl53l5cx.hpp index 811297e6..5b3990cf 100644 --- a/drivers/vl53l5cx/vl53l5cx.hpp +++ b/drivers/vl53l5cx/vl53l5cx.hpp @@ -2,4 +2,57 @@ extern "C" { #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; + }; } \ No newline at end of file diff --git a/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp b/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp index c47ba126..d6e6f5ff 100644 --- a/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp +++ b/examples/breakout_vl53l5cx/vl53l5cx_demo.cpp @@ -5,37 +5,28 @@ #include "common/pimoroni_i2c.hpp" -//using namespace pimoroni; +using namespace pimoroni; -pimoroni::I2C i2c(4, 5); - -VL53L5CX_Configuration configuration { - .platform = VL53L5CX_Platform{ - .address = 0x29, - .i2c = i2c0 - }, -}; +I2C i2c(4, 5); +VL53L5CX vl53l5cx(&i2c); int main() { stdio_init_all(); - vl53l5cx_init(&configuration); - vl53l5cx_set_ranging_mode(&configuration, VL53L5CX_RANGING_MODE_AUTONOMOUS); - vl53l5cx_set_resolution(&configuration, VL53L5CX_RESOLUTION_4X4); - vl53l5cx_start_ranging(&configuration); + vl53l5cx.init(); + vl53l5cx.set_ranging_mode(VL53L5CX::RANGING_MODE_AUTONOMOUS); + vl53l5cx.set_resolution(VL53L5CX::RESOLUTION_4X4); + vl53l5cx.start_ranging(); while(true) { - uint8_t is_ready; - if(vl53l5cx_check_data_ready(&configuration, &is_ready) == VL53L5CX_STATUS_OK) { - if(is_ready){ - VL53L5CX_ResultsData result; - if(vl53l5cx_get_ranging_data(&configuration, &result) == VL53L5CX_STATUS_OK) { - printf("Distance: %dmm\n", result.distance_mm[0]); - } + if(vl53l5cx.data_ready()) { + VL53L5CX::ResultsData result; + if(vl53l5cx.get_data(&result)) { + printf("Distance: %dmm\n", result.distance_mm[0]); } } sleep_ms(20); } return 0; -} +} \ No newline at end of file