Skip to content

Commit 75b9060

Browse files
committed
feat(aht20): add Sensor Hub support, new test app, and example
Updated AHT20 driver to support Sensor Hub integration. Added a dedicated test application to verify functionality with Sensor Hub Humiture Sensor APIs. Included a new example demonstrating how to use AHT20 through the Sensor Hub interface. On branch update_aht20 Changes to be committed: modified: components/sensors/humiture/aht20/CMakeLists.txt modified: components/sensors/humiture/aht20/aht20.c modified: components/sensors/humiture/aht20/idf_component.yml modified: components/sensors/humiture/aht20/include/aht20.h renamed: components/sensors/humiture/aht20/test_apps/CMakeLists.txt -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt renamed: components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/CMakeLists.txt renamed: components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/Kconfig.projbuild renamed: components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c renamed: components/sensors/humiture/aht20/test_apps/main/idf_component.yml -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml renamed: components/sensors/humiture/aht20/test_apps/pytest_aht20.py -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/pytest_aht20.py renamed: components/sensors/humiture/aht20/test_apps/sdkconfig.defaults -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/sdkconfig.defaults new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults new file: examples/sensors/aht20_Sensor_Hub/CMakeLists.txt new file: examples/sensors/aht20_Sensor_Hub/README.md new file: examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt new file: examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild new file: examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c new file: examples/sensors/aht20_Sensor_Hub/main/idf_component.yml modified: examples/sensors/aht20_demo/CMakeLists.txt modified: examples/sensors/aht20_demo/main/aht20_demo.c
1 parent 4f076d7 commit 75b9060

26 files changed

+503
-18
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
idf_component_register(SRCS "aht20.c"
22
INCLUDE_DIRS "include")
3+
4+
5+
if(CONFIG_SENSOR_INCLUDED_HUMITURE)
6+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u humiture_aht20_init")
7+
endif()
8+
9+
include(package_manager)
10+
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})

components/sensors/humiture/aht20/aht20.c

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616

1717
#include <stdio.h>
18+
19+
#include "iot_sensor_hub.h"
20+
1821
#include "aht20.h"
1922

2023
static const char *s_TAG = "AHT20";
@@ -300,9 +303,104 @@ aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address)
300303
return my_aht20_handle;
301304
}
302305

303-
void aht20_remove(aht20_handle_t *aht20ptr)
306+
esp_err_t aht20_remove(aht20_handle_t *aht20ptr)
304307
{
308+
if (*aht20ptr == NULL) {
309+
return ESP_ERR_INVALID_ARG;
310+
}
305311
i2c_bus_device_delete(&((*aht20ptr)->i2c_dev));
306312
free(*aht20ptr);
307313
*aht20ptr = NULL; // now AHT20 handle is not a dangling pointer
314+
return ESP_OK;
315+
}
316+
317+
#ifdef CONFIG_SENSOR_INCLUDED_HUMITURE
318+
319+
static aht20_handle_t aht20 = NULL;
320+
static bool is_init = false;
321+
322+
esp_err_t humiture_aht20_init(i2c_bus_handle_t i2c_bus, uint8_t addr)
323+
{
324+
if (is_init || !i2c_bus) {
325+
return ESP_FAIL;
326+
}
327+
328+
aht20 = aht20_create(i2c_bus, addr);
329+
330+
if (!aht20) {
331+
return ESP_FAIL;
332+
}
333+
334+
ESP_RETURN_ON_ERROR(aht20_init(aht20), s_TAG, "");
335+
336+
is_init = true;
337+
return ESP_OK;
338+
}
339+
340+
esp_err_t humiture_aht20_deinit(void)
341+
{
342+
if (!is_init) {
343+
return ESP_FAIL;
344+
}
345+
346+
ESP_RETURN_ON_ERROR(aht20_remove(&aht20), s_TAG, "");
347+
348+
is_init = false;
349+
return ESP_OK;
350+
}
351+
352+
esp_err_t humiture_aht20_test(void)
353+
{
354+
if (!is_init) {
355+
return ESP_FAIL;
356+
}
357+
358+
return ESP_OK;
359+
}
360+
361+
esp_err_t humiture_aht20_acquire_humidity(float *h)
362+
{
363+
if (!is_init) {
364+
return ESP_FAIL;
365+
}
366+
367+
float humidity = 0;
368+
esp_err_t ret = aht20_read_humidity(aht20, &humidity);
369+
370+
if (ret == ESP_OK) {
371+
*h = humidity;
372+
return ESP_OK;
373+
}
374+
*h = 0;
375+
return ESP_FAIL;
308376
}
377+
378+
esp_err_t humiture_aht20_acquire_temperature(float *t)
379+
{
380+
if (!is_init) {
381+
return ESP_FAIL;
382+
}
383+
384+
float temperature = 0;
385+
esp_err_t ret = aht20_read_temperature(aht20, &temperature);
386+
387+
if (ret == ESP_OK) {
388+
*t = temperature;
389+
return ESP_OK;
390+
}
391+
392+
*t = 0;
393+
return ESP_FAIL;
394+
}
395+
396+
static humiture_impl_t aht20_impl = {
397+
.init = humiture_aht20_init,
398+
.deinit = humiture_aht20_deinit,
399+
.test = humiture_aht20_test,
400+
.acquire_humidity = humiture_aht20_acquire_humidity,
401+
.acquire_temperature = humiture_aht20_acquire_temperature,
402+
};
403+
404+
SENSOR_HUB_DETECT_FN(HUMITURE_ID, aht20, &aht20_impl);
405+
406+
#endif

components/sensors/humiture/aht20/idf_component.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ dependencies:
1818
public: true
1919
override_path: "../../../i2c_bus"
2020

21+
sensor_hub:
22+
public: true
23+
override_path: "../../sensor_hub"
24+
2125
idf: ">=4.0"
2226

2327
examples:
2428
- path: ../../../examples/sensors/aht20_demo
29+
- path: ../../../examples/sensors/aht20_Sensor_Hub
2530

2631
targets:
2732
- esp32

components/sensors/humiture/aht20/include/aht20.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,13 @@ aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address);
196196
/**
197197
* @brief free the resources associated with AHT20 device
198198
*
199-
* @param[in] aht20_handler address of
200-
AHT20 device handle
199+
* @param[in] aht20_handler address of AHT20 device handle
201200
*
201+
* @return
202+
* - ESP_ERR_INVALID_ARG: Invalid argument
203+
* - ESP_OK : successful
202204
*/
203-
void aht20_remove(aht20_handle_t *aht20_handler);
205+
esp_err_t aht20_remove(aht20_handle_t *aht20_handler);
204206

205207
#ifdef __cplusplus
206208
}

components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c renamed to components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define I2C_MASTER_FREQ_HZ 100000
2929

3030
// Global handles
31-
i2c_master_bus_handle_t my_i2c_bus_handle = NULL;
31+
i2c_bus_handle_t my_i2c_bus_handle = NULL;
3232
aht20_handle_t aht20_handle = NULL;
3333

3434
/*******************************Memory Leak Checks****************************/
@@ -64,17 +64,17 @@ void tearDown(void)
6464
/*******************************I2C Master Bus Initialization****************************/
6565
void i2c_master_init(void)
6666
{
67-
i2c_master_bus_config_t i2c_mst_config = {
68-
.clk_source = I2C_CLK_SRC_DEFAULT,
69-
.i2c_port = I2C_MASTER_NUM,
70-
.scl_io_num = I2C_MASTER_SCL_IO,
67+
const i2c_config_t conf = {
68+
.mode = I2C_MODE_MASTER,
7169
.sda_io_num = I2C_MASTER_SDA_IO,
72-
.glitch_ignore_cnt = 7,
73-
.flags.enable_internal_pullup = true,
70+
.sda_pullup_en = GPIO_PULLUP_ENABLE,
71+
.scl_io_num = I2C_MASTER_SCL_IO,
72+
.scl_pullup_en = GPIO_PULLUP_ENABLE,
73+
.master.clk_speed = I2C_MASTER_FREQ_HZ,
7474
};
7575

7676
printf("Requesting I2C bus handle\n");
77-
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle));
77+
my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf);
7878
printf("I2C bus handle acquired\n");
7979
}
8080
/*******************************I2C Master Bus Initialization Over****************************/
@@ -99,7 +99,7 @@ esp_err_t aht20_init_test()
9999
void aht20_deinit_test(void)
100100
{
101101
aht20_remove(&aht20_handle);
102-
i2c_del_master_bus(my_i2c_bus_handle);
102+
i2c_bus_delete(&my_i2c_bus_handle);
103103
}
104104
/*******************************AHT20 Device Deinitializtion Over****************************/
105105

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dependencies:
22
aht20:
33
version: "*"
4-
override_path: "../../"
4+
override_path: "../../../"

0 commit comments

Comments
 (0)