temp_sensor: added enable/disable functions

The previous start/stop functions have been rename to enable/disable.
This commit is contained in:
morris
2022-04-24 18:14:17 +08:00
parent d67888b92b
commit 3a5fdfe35a
8 changed files with 123 additions and 117 deletions

View File

@@ -3,9 +3,9 @@
# Temperature Sensor Example
The ESP32-S2/C3/S3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.
The ESP32-S2/C3/S3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.
The conversion relationship is the first two columns of the table below. Among them, `offset = 0`(default) is the main measurement option, and other values are extended measurement options.
The conversion relationship is the first two columns of the table below. Among them, `offset = 0`(default) is the main measurement option, and other values are extended measurement options.
| DAC level | offset | measure range(℃) | measure error(℃) |
| :-------: | :----: | :--------------: | :--------------: |
@@ -37,18 +37,17 @@ See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/l
## Example Output
```
I (276) example: Initializing Temperature sensor
I (276) temperature_sensor: temperature range [-10°C ~ 80°C], error < 1°C
I (286) example: Temperature sensor started
I (1286) example: Temperature out celsius 21.64
I (2286) example: Temperature out celsius 21.64
I (3286) example: Temperature out celsius 21.64
I (4286) example: Temperature out celsius 22.08
I (5286) example: Temperature out celsius 22.08
I (6286) example: Temperature out celsius 22.08
I (7286) example: Temperature out celsius 22.08
I (8286) example: Temperature out celsius 22.08
I (9286) example: Temperature out celsius 22.08
I (0) cpu_start: Starting scheduler on APP CPU.
I (303) example: Install temperature sensor, expected temp ranger range: 10~50 ℃
I (303) temperature_sensor: Range [-10°C ~ 80°C], error < 1°C
I (313) example: Enable temperature sensor
I (323) example: Read temperature
I (323) example: Temperature value 26.06 ℃
I (1323) example: Temperature value 26.06 ℃
I (2323) example: Temperature value 26.06 ℃
I (3323) example: Temperature value 26.06 ℃
I (4323) example: Temperature value 26.06 ℃
I (5323) example: Temperature value 26.49 ℃
```

View File

@@ -4,34 +4,29 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp_log.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/temperature_sensor.h"
static const char *TAG = "example";
void tempsensor_example(void)
{
// Initialize touch pad peripheral, it will start a timer to run a filter
ESP_LOGI(TAG, "Initializing Temperature sensor");
float tsens_out;
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
temperature_sensor_handle_t temp_handle = NULL;
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
ESP_ERROR_CHECK(temperature_sensor_start(temp_handle));
ESP_LOGI(TAG, "Temperature sensor started");
int cnt = 20; //read value for 20 times
while (cnt) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
ESP_LOGI(TAG, "Temperature out celsius %.02f", tsens_out);
cnt--;
}
}
void app_main(void)
{
tempsensor_example();
ESP_LOGI(TAG, "Install temperature sensor, expected temp ranger range: 10~50 ℃");
temperature_sensor_handle_t temp_sensor = NULL;
temperature_sensor_config_t temp_sensor_config = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
ESP_LOGI(TAG, "Enable temperature sensor");
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
ESP_LOGI(TAG, "Read temperature");
int cnt = 20;
float tsens_value;
while (cnt--) {
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

View File

@@ -10,8 +10,11 @@ from pytest_embedded.dut import Dut
@pytest.mark.esp32s3
@pytest.mark.generic
def test_temp_sensor_example(dut: Dut) -> None:
dut.expect_exact('Initializing Temperature sensor')
dut.expect_exact('Temperature sensor started')
temp_value = dut.expect(r'Temperature out celsius (\d+\.\d+)', timeout=30)
dut.expect_exact('Install temperature sensor')
dut.expect_exact('Enable temperature sensor')
dut.expect_exact('Read temperature')
temp_value = dut.expect(r'Temperature value (\d+\.\d+) .*', timeout=5)
# Because the example test only run in the normal temperature environment. So this assert range is meaningful
assert 0 < float(temp_value.group(1)) < 45
assert 0 < float(temp_value.group(1).decode('utf8')) < 50
temp_value = dut.expect(r'Temperature value (\d+\.\d+) .*', timeout=5)
assert 0 < float(temp_value.group(1).decode('utf8')) < 50