suraj0152

main.c

Jul 23rd, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | Software | 0 0
  1. /**
  2.  * @file app_main.c
  3.  * @author Ryotaro Onuki ([email protected])
  4.  * @brief VL53L0X driver example
  5.  * @date 2020-12-07
  6.  * @copyright Copyright (c) 2020 Ryotaro Onuki
  7.  */
  8.  
  9. #include "VL53L0X.h"
  10. #include "esp_log.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13.  
  14. #define TAG "app_main"
  15.  
  16. /* config */
  17. #define I2C_PORT I2C_NUM_0
  18. #define PIN_SDA GPIO_NUM_21
  19. #define PIN_SCL GPIO_NUM_22
  20.  
  21. void app_main(void) {
  22.     VL53L0X dev;
  23.     dev.i2c_port = I2C_PORT;
  24.     dev.gpio_xshut = GPIO_NUM_MAX;
  25.     dev.gpio_gpio1 = GPIO_NUM_MAX;
  26.     dev.xSemaphore = xSemaphoreCreateBinary();
  27.     if (dev.xSemaphore == NULL) {
  28.         ESP_LOGE(TAG, "Failed to create semaphore");
  29.         vTaskDelay(portMAX_DELAY);
  30.     }
  31.  
  32.     /* Initialization */
  33.     VL53L0X_I2CMasterInit(&dev, PIN_SDA, PIN_SCL, 100000); // 100kHz I2C frequency
  34.     if (!VL53L0X_Init(&dev)) {
  35.         ESP_LOGE(TAG, "Failed to initialize VL53L0X :(");
  36.         vTaskDelay(portMAX_DELAY);
  37.     }
  38.  
  39.     while (1) {
  40.         /* Measurement */
  41.         uint16_t result_mm = 0;
  42.         TickType_t tick_start = xTaskGetTickCount();
  43.         bool res = VL53L0X_Read(&dev, &result_mm);
  44.         TickType_t tick_end = xTaskGetTickCount();
  45.         int took_ms = ((int)tick_end - tick_start) / portTICK_PERIOD_MS;
  46.  
  47.         if (res) {
  48.             ESP_LOGI(TAG, "Range: %d [mm] took %d [ms]", (int)result_mm, took_ms);
  49.         } else {
  50.             ESP_LOGE(TAG, "Failed to measure :(");
  51.         }
  52.  
  53.         vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment