Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file app_main.c
- * @author Ryotaro Onuki ([email protected])
- * @brief VL53L0X driver example
- * @date 2020-12-07
- * @copyright Copyright (c) 2020 Ryotaro Onuki
- */
- #include "VL53L0X.h"
- #include "esp_log.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #define TAG "app_main"
- /* config */
- #define I2C_PORT I2C_NUM_0
- #define PIN_SDA GPIO_NUM_21
- #define PIN_SCL GPIO_NUM_22
- void app_main(void) {
- VL53L0X dev;
- dev.i2c_port = I2C_PORT;
- dev.gpio_xshut = GPIO_NUM_MAX;
- dev.gpio_gpio1 = GPIO_NUM_MAX;
- dev.xSemaphore = xSemaphoreCreateBinary();
- if (dev.xSemaphore == NULL) {
- ESP_LOGE(TAG, "Failed to create semaphore");
- vTaskDelay(portMAX_DELAY);
- }
- /* Initialization */
- VL53L0X_I2CMasterInit(&dev, PIN_SDA, PIN_SCL, 100000); // 100kHz I2C frequency
- if (!VL53L0X_Init(&dev)) {
- ESP_LOGE(TAG, "Failed to initialize VL53L0X :(");
- vTaskDelay(portMAX_DELAY);
- }
- while (1) {
- /* Measurement */
- uint16_t result_mm = 0;
- TickType_t tick_start = xTaskGetTickCount();
- bool res = VL53L0X_Read(&dev, &result_mm);
- TickType_t tick_end = xTaskGetTickCount();
- int took_ms = ((int)tick_end - tick_start) / portTICK_PERIOD_MS;
- if (res) {
- ESP_LOGI(TAG, "Range: %d [mm] took %d [ms]", (int)result_mm, took_ms);
- } else {
- ESP_LOGE(TAG, "Failed to measure :(");
- }
- vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment