Skip to content

Commit 3719c16

Browse files
committed
test(aht20): update test app for new driver APIs and configurations
Updated the test application to work with the new version of the AHT20 driver (v1.1.0). Modified test code to include the new APIs for reading sensor values. Changes to be committed: modified: test_apps/CMakeLists.txt modified: test_apps/main/CMakeLists.txt modified: test_apps/main/Kconfig.projbuild new file: test_apps/main/aht20_test_app.c new file: test_apps/main/idf_component.yml modified: test_apps/pytest_aht20.py
1 parent 378c8c8 commit 3719c16

File tree

6 files changed

+145
-10
lines changed

6 files changed

+145
-10
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# The following lines of boilerplate have to be in your project's CMakeLists
22
# in this exact order for cmake to work correctly
3-
cmake_minimum_required(VERSION 3.5)
3+
cmake_minimum_required(VERSION 3.16)
44

5-
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
6-
"../../aht20")
5+
set(COMPONENTS main)
76
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
87
project(aht20_test)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
idf_component_register(SRC_DIRS "."
2-
PRIV_INCLUDE_DIRS "."
3-
PRIV_REQUIRES unity test_utils aht20)
1+
idf_component_register(SRCS "aht20_test_app.c"
2+
INCLUDE_DIRS "."
3+
PRIV_REQUIRES unity test_utils aht20)

components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ menu "Example Configuration"
22

33
config I2C_MASTER_SCL
44
int "SCL GPIO Num"
5-
default 40
5+
default 22
66
help
77
GPIO number for I2C Master clock line.
88

99
config I2C_MASTER_SDA
1010
int "SDA GPIO Num"
11-
default 41
11+
default 21
1212
help
1313
GPIO number for I2C Master data line.
1414

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/*-
7+
* @File: aht20_test_app.c
8+
*
9+
* @brief: AHT20 driver unity test app
10+
*
11+
* @Date: May 2, 2025
12+
*
13+
* @Author: Rohan Jeet <[email protected]>
14+
*
15+
*/
16+
17+
#include <stdio.h>
18+
#include "unity.h"
19+
#include "esp_system.h"
20+
#include "aht20.h"
21+
22+
#define TEST_MEMORY_LEAK_THRESHOLD (-400)
23+
24+
// I2C config
25+
#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL
26+
#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA
27+
#define I2C_MASTER_NUM I2C_NUM_0
28+
#define I2C_MASTER_FREQ_HZ 100000
29+
30+
// Global handles
31+
i2c_master_bus_handle_t my_i2c_bus_handle = NULL;
32+
aht20_handle_t aht20_handle = NULL;
33+
34+
/*******************************Memory Leak Checks****************************/
35+
36+
static size_t before_free_8bit;
37+
static size_t before_free_32bit;
38+
static size_t after_free_8bit;
39+
static size_t after_free_32bit;
40+
41+
static void check_leak(size_t before_free, size_t after_free, const char *type)
42+
{
43+
ssize_t delta = after_free - before_free;
44+
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
45+
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
46+
}
47+
48+
void setUp(void)
49+
{
50+
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
51+
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
52+
}
53+
54+
void tearDown(void)
55+
{
56+
after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
57+
after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
58+
}
59+
60+
/************************** Memory Leak Checks Completed********************/
61+
62+
/*******************************I2C Master Bus Initialization****************************/
63+
void i2c_master_init(void)
64+
{
65+
i2c_master_bus_config_t i2c_mst_config = {
66+
.clk_source = I2C_CLK_SRC_DEFAULT,
67+
.i2c_port = I2C_MASTER_NUM,
68+
.scl_io_num = I2C_MASTER_SCL_IO,
69+
.sda_io_num = I2C_MASTER_SDA_IO,
70+
.glitch_ignore_cnt = 7,
71+
.flags.enable_internal_pullup = true,
72+
};
73+
74+
printf("Requesting I2C bus handle\n");
75+
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle));
76+
printf("I2C bus handle acquired\n");
77+
}
78+
/*******************************I2C Master Bus Initialization Over****************************/
79+
80+
/*******************************AHT20 Device Initialization****************************/
81+
esp_err_t aht20_init_test()
82+
{
83+
i2c_master_init();
84+
aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW);
85+
86+
printf("Initializing AHT20 sensor\n");
87+
while (aht20_init(aht20_handle) != ESP_OK) {
88+
vTaskDelay(100 / portTICK_PERIOD_MS);
89+
}
90+
printf("AHT20 initialized\n");
91+
92+
return ESP_OK;
93+
}
94+
/*******************************AHT20 Device Initialization Over****************************/
95+
96+
/*******************************AHT20 Device Deinitializtion****************************/
97+
void aht20_deinit_test(void)
98+
{
99+
aht20_remove(&aht20_handle);
100+
}
101+
/*******************************AHT20 Device Deinitializtion Over****************************/
102+
103+
/*******************************AHT20 Read sensor****************************/
104+
void aht20_read_test(void)
105+
{
106+
vTaskDelay(400 / portTICK_PERIOD_MS);
107+
108+
TEST_ASSERT(ESP_OK == aht20_read_humiture(aht20_handle));
109+
110+
printf("Temperature = %.2f°C, Humidity = %.3f%%\n",
111+
aht20_handle->humiture.temperature,
112+
aht20_handle->humiture.humidity);
113+
114+
vTaskDelay(1000 / portTICK_PERIOD_MS);
115+
}
116+
/*******************************AHT20 AHT20 Read sensor Over*****************************/
117+
118+
/*************************************Test Case**************************/
119+
120+
TEST_CASE("AHT20 Sensor", "[aht20][sensor]")
121+
{
122+
aht20_init_test();
123+
aht20_read_test();
124+
aht20_deinit_test();
125+
}
126+
/*************************************Test Case Over**************************/
127+
128+
void app_main(void)
129+
{
130+
printf("\n=== AHT20 Sensor Test Menu ===\n");
131+
unity_run_menu(); // Run test selection menu in flash monitor
132+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies:
2+
aht20:
3+
version: "*"
4+
override_path: "../../"

components/sensors/humiture/aht20/test_apps/pytest_aht20.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
- Build
77
- . ${IDF_PATH}/export.sh
88
- pip install idf_build_apps
9-
- python tools/build_apps.py components/sensor/radar/at581x/test_apps -t esp32s3
9+
- python tools/build_apps.py components/sensors/humiture/aht20/test_apps -t esp32s3
1010
- Test
1111
- pip install -r tools/requirements/requirement.pytest.txt
12-
- pytest components/sensor/radar/at581x/test_apps --target esp32s3
12+
- pytest components/sensors/humiture/aht20/test_apps --target esp32s3
1313
'''
1414

1515
import pytest

0 commit comments

Comments
 (0)