|
| 1 | +#include <chrono> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include "i2c.hpp" |
| 5 | +#include "logger.hpp" |
| 6 | +#include "timer.hpp" |
| 7 | +#include "vl53l.hpp" |
| 8 | + |
| 9 | +using namespace std::chrono_literals; |
| 10 | + |
| 11 | +extern "C" void app_main(void) { |
| 12 | + static espp::Logger logger({.tag = "VL53L4CX example", .level = espp::Logger::Verbosity::INFO}); |
| 13 | + // This example shows using the i2c adc (vl53l) |
| 14 | + { |
| 15 | + logger.info("Starting example!"); |
| 16 | + |
| 17 | + //! [vl53l example] |
| 18 | + // make the i2c we'll use to communicate |
| 19 | + static constexpr auto i2c_port = I2C_NUM_0; |
| 20 | + static constexpr auto i2c_clock_speed = CONFIG_EXAMPLE_I2C_CLOCK_SPEED_HZ; |
| 21 | + static constexpr gpio_num_t i2c_sda = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO; |
| 22 | + static constexpr gpio_num_t i2c_scl = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO; |
| 23 | + logger.info("Creating I2C on port {} with SDA {} and SCL {}", i2c_port, i2c_sda, i2c_scl); |
| 24 | + logger.info("I2C clock speed: {} Hz", i2c_clock_speed); |
| 25 | + espp::I2c i2c({.port = i2c_port, |
| 26 | + .sda_io_num = i2c_sda, |
| 27 | + .scl_io_num = i2c_scl, |
| 28 | + .sda_pullup_en = GPIO_PULLUP_ENABLE, |
| 29 | + .scl_pullup_en = GPIO_PULLUP_ENABLE, |
| 30 | + .clk_speed = i2c_clock_speed}); |
| 31 | + |
| 32 | + // make the actual test object |
| 33 | + espp::Vl53l vl53l( |
| 34 | + espp::Vl53l::Config{.device_address = espp::Vl53l::DEFAULT_ADDRESS, |
| 35 | + .write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, |
| 36 | + std::placeholders::_2, std::placeholders::_3), |
| 37 | + .read = std::bind(&espp::I2c::read, &i2c, std::placeholders::_1, |
| 38 | + std::placeholders::_2, std::placeholders::_3), |
| 39 | + .log_level = espp::Logger::Verbosity::WARN}); |
| 40 | + |
| 41 | + std::error_code ec; |
| 42 | + // set the timing budget to 10ms, which must be shorter than the |
| 43 | + // inter-measurement period. We'll log every 20ms so this guarantees we get |
| 44 | + // new data every time |
| 45 | + if (!vl53l.set_timing_budget_ms(10, ec)) { |
| 46 | + logger.error("Failed to set inter measurement period: {}", ec.message()); |
| 47 | + return; |
| 48 | + } |
| 49 | + // set the inter-measurement period to 10ms, so we should be sure to get new |
| 50 | + // data each measurement |
| 51 | + if (!vl53l.set_inter_measurement_period_ms(10, ec)) { |
| 52 | + logger.error("Failed to set inter measurement period: {}", ec.message()); |
| 53 | + return; |
| 54 | + } |
| 55 | + // tell it to start ranging |
| 56 | + if (!vl53l.start_ranging(ec)) { |
| 57 | + logger.error("Failed to start ranging: {}", ec.message()); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // make the task which will read the vl53l |
| 62 | + fmt::print("%time (s), distance (m)\n"); |
| 63 | + auto read_task_fn = [&vl53l]() { |
| 64 | + auto now = esp_timer_get_time(); |
| 65 | + static auto start = now; |
| 66 | + float elapsed = (float)(now - start) / 1e6; |
| 67 | + std::error_code ec; |
| 68 | + // wait for the data to be ready |
| 69 | + while (!vl53l.is_data_ready(ec)) { |
| 70 | + std::this_thread::sleep_for(1ms); |
| 71 | + } |
| 72 | + // clear the interrupt so we can get another reading |
| 73 | + if (!vl53l.clear_interrupt(ec)) { |
| 74 | + logger.error("Failed to clear interrupt: {}", ec.message()); |
| 75 | + return false; |
| 76 | + } |
| 77 | + auto meters = vl53l.get_distance_meters(ec); |
| 78 | + if (ec) { |
| 79 | + logger.error("Failed to get distance: {}", ec.message()); |
| 80 | + return false; |
| 81 | + } |
| 82 | + fmt::print("{:.3f}, {:.3f}\n", elapsed, meters); |
| 83 | + // we don't want to stop, so return false |
| 84 | + return false; |
| 85 | + }; |
| 86 | + |
| 87 | + espp::Timer timer({.period = 20ms, |
| 88 | + .callback = read_task_fn, |
| 89 | + .task_config = |
| 90 | + { |
| 91 | + .name = "VL53L4CX", |
| 92 | + .stack_size_bytes{4 * 1024}, |
| 93 | + }, |
| 94 | + .log_level = espp::Logger::Verbosity::INFO}); |
| 95 | + //! [vl53l example] |
| 96 | + |
| 97 | + while (true) { |
| 98 | + std::this_thread::sleep_for(100ms); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + logger.info("Example complete!"); |
| 103 | + |
| 104 | + while (true) { |
| 105 | + std::this_thread::sleep_for(1s); |
| 106 | + } |
| 107 | +} |
0 commit comments