-
Notifications
You must be signed in to change notification settings - Fork 7k
/
temperature_sensor.h
186 lines (164 loc) · 7.16 KB
/
temperature_sensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "hal/temperature_sensor_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Type of temperature sensor driver handle
*/
typedef struct temperature_sensor_obj_t *temperature_sensor_handle_t;
/**
* @brief Configuration of measurement range for the temperature sensor.
*
* @note If you see the log `the boundary you gave cannot meet the range of internal temperature sensor`. You may need to refer to
* predefined range listed doc ``api-reference/peripherals/Temperature sensor``.
*/
typedef struct {
int range_min; /**< the minimum value of the temperature you want to test */
int range_max; /**< the maximum value of the temperature you want to test */
temperature_sensor_clk_src_t clk_src; /**< the clock source of the temperature sensor. */
} temperature_sensor_config_t;
/**
* @brief temperature_sensor_config_t default constructure
*/
#define TEMPERATURE_SENSOR_CONFIG_DEFAULT(min, max) \
{ \
.range_min = min, \
.range_max = max, \
.clk_src = TEMPERATURE_SENSOR_CLK_SRC_DEFAULT, \
}
/**
* @brief Install temperature sensor driver
*
* @param tsens_config Pointer to config structure.
* @param ret_tsens Return the pointer of temperature sensor handle.
* @return
* - ESP_OK if succeed
*/
esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_config, temperature_sensor_handle_t *ret_tsens);
/**
* @brief Uninstall the temperature sensor driver
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @return
* - ESP_OK if succeed.
*/
esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens);
/**
* @brief Enable the temperature sensor
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_STATE if temperature sensor is enabled already.
*/
esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens);
/**
* @brief Disable temperature sensor
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_STATE if temperature sensor is not enabled yet.
*/
esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens);
/**
* @brief Read temperature sensor data that is converted to degrees Celsius.
* @note Should not be called from interrupt.
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @param out_celsius The measure output value.
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG invalid arguments
* - ESP_ERR_INVALID_STATE Temperature sensor is not enabled yet.
* - ESP_FAIL Parse the sensor data into ambient temperature failed (e.g. out of the range).
*/
esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, float *out_celsius);
#if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
/**
* @brief Temperature sensor event data
*/
typedef struct {
int celsius_value; /**< Celsius value in interrupt callback. */
} temperature_sensor_threshold_event_data_t;
/**
* @brief Callback for temperature sensor threshold interrupt.
*
* @param[in] tsens The handle created by `temperature_sensor_install()`.
* @param[in] edata temperature sensor event data, fed by driver.
* @param[in] user_data User data, set in `temperature_sensor_register_callbacks()`.
* @return Whether a high priority task has been waken up by this function.
*/
typedef bool (*temperature_thres_cb_t)(temperature_sensor_handle_t tsens, const temperature_sensor_threshold_event_data_t *edata, void *user_data);
/**
* @brief Group of temperature sensor callback functions, all of them will be run in ISR.
*/
typedef struct {
temperature_thres_cb_t on_threshold; /**< Temperature value interrupt callback */
} temperature_sensor_event_callbacks_t;
/**
* @brief Config options for temperature value absolute interrupt.
*/
typedef struct {
float high_threshold; /**< High threshold value(Celsius). Interrupt will be triggered if temperature value is higher than this value */
float low_threshold; /**< Low threshold value(Celsius). Interrupt will be triggered if temperature value is lower than this value */
} temperature_sensor_abs_threshold_config_t;
/**
* @brief Set temperature sensor absolute mode automatic monitor.
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @param abs_cfg Configuration of temperature sensor absolute mode interrupt, see `temperature_sensor_abs_threshold_config_t`.
* @note This function should not be called with `temperature_sensor_set_delta_threshold`.
*
* @return
* - ESP_OK: Set absolute threshold successfully.
* - ESP_ERR_INVALID_STATE: Set absolute threshold failed because of wrong state.
* - ESP_ERR_INVALID_ARG: Set absolute threshold failed because of invalid argument.
*/
esp_err_t temperature_sensor_set_absolute_threshold(temperature_sensor_handle_t tsens, const temperature_sensor_abs_threshold_config_t *abs_cfg);
/**
* @brief Config options for temperature value delta interrupt.
*/
typedef struct {
float increase_delta; /**< Interrupt will be triggered if the temperature increment of two consecutive samplings if larger than `increase_delta` */
float decrease_delta; /**< Interrupt will be triggered if the temperature decrement of two consecutive samplings if smaller than `decrease_delta` */
} temperature_sensor_delta_threshold_config_t;
/**
* @brief Set temperature sensor differential mode automatic monitor.
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @param delta_cfg Configuration of temperature sensor delta mode interrupt, see `temperature_sensor_delta_threshold_config_t`.
* @note This function should not be called with `temperature_sensor_set_absolute_threshold`
*
* @return
* - ESP_OK: Set differential value threshold successfully.
* - ESP_ERR_INVALID_STATE: Set absolute threshold failed because of wrong state.
* - ESP_ERR_INVALID_ARG: Set differential value threshold failed because of invalid argument.
*/
esp_err_t temperature_sensor_set_delta_threshold(temperature_sensor_handle_t tsens, const temperature_sensor_delta_threshold_config_t *delta_cfg);
/**
* @brief Install temperature sensor interrupt callback. Temperature sensor interrupt will be enabled at same time
*
* @param tsens The handle created by `temperature_sensor_install()`.
* @param cbs Pointer to the group of temperature sensor interrupt callbacks.
* @param user_arg Callback argument.
*
* @return
* - ESP_OK: Set event callbacks successfully
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
* - ESP_FAIL: Set event callbacks failed because of other error
*/
esp_err_t temperature_sensor_register_callbacks(temperature_sensor_handle_t tsens, const temperature_sensor_event_callbacks_t *cbs, void *user_arg);
#endif // SOC_TEMPERATURE_SENSOR_INTR_SUPPORT
#ifdef __cplusplus
}
#endif