temperature_sensor.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include "esp_err.h"
  9. #include "hal/temperature_sensor_types.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Type of temperature sensor driver handle
  15. */
  16. typedef struct temperature_sensor_obj_t *temperature_sensor_handle_t;
  17. /**
  18. * @brief Configuration of measurement range for the temperature sensor.
  19. *
  20. * @note If you see the log `the boundary you gave cannot meet the range of internal temperature sensor`. You may need to refer to
  21. * predefined range listed doc ``api-reference/peripherals/Temperature sensor``.
  22. */
  23. typedef struct {
  24. int range_min; /**< the minimum value of the temperature you want to test */
  25. int range_max; /**< the maximum value of the temperature you want to test */
  26. temperature_sensor_clk_src_t clk_src; /**< the clock source of the temperature sensor. */
  27. } temperature_sensor_config_t;
  28. /**
  29. * @brief temperature_sensor_config_t default constructure
  30. */
  31. #define TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(min, max) \
  32. { \
  33. .range_min = min, \
  34. .range_max = max, \
  35. .clk_src = TEMPERATURE_SENSOR_CLK_SRC_DEFAULT, \
  36. }
  37. /**
  38. * @brief Install temperature sensor driver
  39. *
  40. * @param tsens_config Pointer to config structure.
  41. * @param ret_tsens Return the pointer of temperature sensor handle.
  42. * @return
  43. * - ESP_OK if succeed
  44. */
  45. esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_config, temperature_sensor_handle_t *ret_tsens);
  46. /**
  47. * @brief Uninstall the temperature sensor driver
  48. *
  49. * @param tsens The handle created by `temperature_sensor_install()`.
  50. * @return
  51. * - ESP_OK if succeed.
  52. */
  53. esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens);
  54. /**
  55. * @brief Enable the temperature sensor
  56. *
  57. * @param tsens The handle created by `temperature_sensor_install()`.
  58. * @return
  59. * - ESP_OK Success
  60. * - ESP_ERR_INVALID_STATE if temperature sensor is enabled already.
  61. */
  62. esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens);
  63. /**
  64. * @brief Disable temperature sensor
  65. *
  66. * @param tsens The handle created by `temperature_sensor_install()`.
  67. * @return
  68. * - ESP_OK Success
  69. * - ESP_ERR_INVALID_STATE if temperature sensor is not enabled yet.
  70. */
  71. esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens);
  72. /**
  73. * @brief Read temperature sensor data that is converted to degrees Celsius.
  74. * @note Should not be called from interrupt.
  75. *
  76. * @param tsens The handle created by `temperature_sensor_install()`.
  77. * @param out_celsius The measure output value.
  78. * @return
  79. * - ESP_OK Success
  80. * - ESP_ERR_INVALID_ARG invalid arguments
  81. * - ESP_ERR_INVALID_STATE Temperature sensor is not enabled yet.
  82. * - ESP_FAIL Parse the sensor data into ambient temperature failed (e.g. out of the range).
  83. */
  84. esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius);
  85. #ifdef __cplusplus
  86. }
  87. #endif