|
| 1 | +/* |
| 2 | + * @File: aht20_test_app.c |
| 3 | + * |
| 4 | + * @brief: AHT20 driver unity test app |
| 5 | + * |
| 6 | + * @Date: April 20, 2025 |
| 7 | + * |
| 8 | + * Copyright 2025 Rohan Jeet <jeetrohan92@gmail.com> |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include "freertos/FreeRTOS.h" |
| 25 | +#include "freertos/task.h" |
| 26 | +#include "unity.h" |
| 27 | +#include "esp_system.h" |
| 28 | +#include "aht20.h" |
| 29 | + |
| 30 | + |
| 31 | +// I2C config |
| 32 | +#define I2C_MASTER_SCL_IO 22 |
| 33 | +#define I2C_MASTER_SDA_IO 21 |
| 34 | +#define I2C_MASTER_NUM I2C_NUM_0 |
| 35 | +#define I2C_MASTER_FREQ_HZ 400000 |
| 36 | + |
| 37 | +// Global handles |
| 38 | +i2c_master_bus_handle_t my_i2c_bus_handle = NULL; |
| 39 | +aht20_handle_t aht20_handle = NULL; |
| 40 | + |
| 41 | +void i2c_master_init(void) |
| 42 | +{ |
| 43 | + i2c_master_bus_config_t i2c_mst_config = { |
| 44 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 45 | + .i2c_port = I2C_MASTER_NUM, |
| 46 | + .scl_io_num = I2C_MASTER_SCL_IO, |
| 47 | + .sda_io_num = I2C_MASTER_SDA_IO, |
| 48 | + .glitch_ignore_cnt = 7, |
| 49 | + .flags.enable_internal_pullup = true, |
| 50 | + }; |
| 51 | + |
| 52 | + printf("Requesting I2C bus handle\n"); |
| 53 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle)); |
| 54 | + printf("I2C bus handle acquired\n"); |
| 55 | +} |
| 56 | + |
| 57 | +void aht20_init_test() |
| 58 | +{ |
| 59 | + i2c_master_init(); |
| 60 | + aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW); |
| 61 | + |
| 62 | + printf("Initializing AHT20 sensor\n"); |
| 63 | + while (aht20_init(aht20_handle) != ESP_OK) { |
| 64 | + vTaskDelay(100 / portTICK_PERIOD_MS); |
| 65 | + } |
| 66 | + printf("AHT20 initialized\n"); |
| 67 | +} |
| 68 | + |
| 69 | +void aht20_deinit_test(void) |
| 70 | +{ |
| 71 | + aht20_remove(&aht20_handle); |
| 72 | +} |
| 73 | + |
| 74 | +void aht20_read_test(void) |
| 75 | +{ |
| 76 | + vTaskDelay(400 / portTICK_PERIOD_MS); |
| 77 | + |
| 78 | + for (int i = 0; i < 5; i++) { |
| 79 | + if (aht20_read_humiture(aht20_handle) == ESP_OK) { |
| 80 | + printf("Temperature = %.2f°C, Humidity = %.3f%%\n", |
| 81 | + aht20_handle->humiture.temperature, |
| 82 | + aht20_handle->humiture.humidity); |
| 83 | + } else { |
| 84 | + printf("Failed to read data from AHT20 sensor\n"); |
| 85 | + } |
| 86 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +TEST_CASE("AHT20 Init", "[aht20][init]") |
| 92 | +{ |
| 93 | + TEST_ASSERT_NULL(aht20_handle); |
| 94 | + aht20_init_test(); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_CASE("AHT20 Read", "[aht20][read]") |
| 98 | +{ |
| 99 | + TEST_ASSERT_NOT_NULL(aht20_handle); |
| 100 | + |
| 101 | + aht20_read_test(); |
| 102 | +} |
| 103 | + |
| 104 | +TEST_CASE("AHT20 Deinit", "[aht20][deinit]") |
| 105 | +{ |
| 106 | + TEST_ASSERT_NOT_NULL(aht20_handle); |
| 107 | + |
| 108 | + aht20_deinit_test(); |
| 109 | + TEST_ASSERT_NULL(aht20_handle); |
| 110 | + |
| 111 | + printf("AHT20 deinitialized\n"); |
| 112 | +} |
| 113 | + |
| 114 | +void app_main(void) |
| 115 | +{ |
| 116 | + printf("\n=== AHT20 Sensor Test Menu ===\n"); |
| 117 | + unity_run_menu(); // Run test selection menu in flash monitor |
| 118 | +} |
0 commit comments