From b0bd1962a6508b11fff302fed797011eb637f968 Mon Sep 17 00:00:00 2001 From: Hays Chan <25737801+hayschan@users.noreply.github.com> Date: Sat, 30 Dec 2023 15:03:28 +0800 Subject: [PATCH] Fix compilation bug --- examples/CMakelists.txt | 11 --- examples/default_example/main/CMakeLists.txt | 16 +++- .../main/CMakeLists_registry.txt | 3 + examples/default_example/main/Kconfig | 84 +++++-------------- .../default_example/main/idf_component.yml | 0 examples/default_example/main/main.c | 6 -- .../default_example/main/xgzf4000-poll-data.c | 60 +++++++++++++ idf_component.yml | 2 +- include/xgzf4000.h | 3 +- priv_include/xgzf4000_reg.h | 2 +- xgzf4000.c | 28 ++++++- 11 files changed, 123 insertions(+), 92 deletions(-) delete mode 100644 examples/CMakelists.txt create mode 100644 examples/default_example/main/CMakeLists_registry.txt create mode 100644 examples/default_example/main/idf_component.yml delete mode 100644 examples/default_example/main/main.c create mode 100644 examples/default_example/main/xgzf4000-poll-data.c diff --git a/examples/CMakelists.txt b/examples/CMakelists.txt deleted file mode 100644 index 950ed97..0000000 --- a/examples/CMakelists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# For more information about build system see -# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -add_compile_options("-Wno-format") -get_filename_component(ProjectId ${CMAKE_CURRENT_LIST_DIR} NAME) -string(REPLACE " " "_" ProjectId ${ProjectId}) -project(${ProjectId}) diff --git a/examples/default_example/main/CMakeLists.txt b/examples/default_example/main/CMakeLists.txt index 0228bd0..21c85d6 100644 --- a/examples/default_example/main/CMakeLists.txt +++ b/examples/default_example/main/CMakeLists.txt @@ -1,3 +1,13 @@ -idf_component_register(SRCS "main.c" - INCLUDE_DIRS "." ) - \ No newline at end of file +# Be aware this file should not be takes as inspiration on how to set up compilation with the CMake build system when using ESP-IDF, because it directly includes the implementation files. +# This has to be done because the examples are build to test if they are still working and to automatically inform the library if a pull request would break examples. +# To actually include the library in your ESP-IDF project read the documentation especially the Installation section +set(srcs + xgzf4000-poll-data.c + ../../../xgzf4000.c +) + +idf_component_register( + SRCS ${srcs} + INCLUDE_DIRS "../../../include" + PRIV_INCLUDE_DIRS "../../../priv_include" +) \ No newline at end of file diff --git a/examples/default_example/main/CMakeLists_registry.txt b/examples/default_example/main/CMakeLists_registry.txt new file mode 100644 index 0000000..dfe4da5 --- /dev/null +++ b/examples/default_example/main/CMakeLists_registry.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "xgzf4000-poll-data.c" + INCLUDE_DIRS "." ) + \ No newline at end of file diff --git a/examples/default_example/main/Kconfig b/examples/default_example/main/Kconfig index 8042cc6..77ce26c 100644 --- a/examples/default_example/main/Kconfig +++ b/examples/default_example/main/Kconfig @@ -1,68 +1,24 @@ -menu "NTC Temperature" +# menu "XGZF4000 Configuration" - config RESISTANCE_VOLTAGE_DIVIDER_OHM - int "Resistance of Voltage Divider in Ohm" - default 4700 - help "This resistance can be adjusted to output different voltage" - - config EXP_VOLTAGE - string "EXP32 Voltage" - default "3.3" - help "This voltage is the supply voltage of the single-chip microcomputer" - - config BETA - int "" "Maximum of the output ADC raw digital reading result" - default 3950 - help - "This is 4095 under Single Read mode, 8191 under Continuous Read mode. - 2 ^ 12 - 1 = 4095, 2 ^ 13 - 1 = 8191" - - config COEFFICIENT_A - string "HardwareA_coefficient" - default "0.0007207995816" - help - "Coefficient A value for the Steinhart-Hart model of the NTC thermistor. - This coefficient affects the temperature calculation." +# config FLOW_RATE_UNIT +# string "Unit for Flow Rate Measurement" +# default "L/min" +# option "L/min" if FLOW_RATE_UNIT_LMIN +# option "m^3/h" if FLOW_RATE_UNIT_CMH +# help +# Choose the unit for measuring the flow rate: +# - L/min: Liters per minute +# - m^3/h: Cubic meters per hour - config COEFFICIENT_B - string "HardwareB_coefficient" - default "0.0002171490624" - help - "Coefficient B value for the Steinhart-Hart model of the NTC thermistor. - This coefficient affects the temperature calculation." +# config FLOW_RATE_UNIT_LMIN +# bool "Liters per minute (L/min)" +# help +# Select this option to measure the flow rate in liters per minute. - config COEFFICIENT_C - string "HardwareC_coefficient" - default "0.00000008956835924" - help - " Coefficient C value for the Steinhart-Hart model of the NTC thermistor. - This coefficient affects the temperature calculation." +# config FLOW_RATE_UNIT_CMH +# bool "Cubic meters per hour (m³/h)" +# help +# Select this option to measure the flow rate in cubic meters per hour. - config CONSTANT_SAMPLING - int "CONSTANT_SAMPLING" - default 5 - help - "CONSTANT_SAMPLING represents the number of a constant sample. - It plays a role in the NTC reading temperature." - - config SAMPLING_DELAY - int "Time delay" - default 300 - help - "Represents the delay time of the sample,can be used to set the delay time of a task." - - config SUPPORT_SIMULATION_TEMPERATURE_OFFSET - bool "Offset Access" - default n - help - "123456 SIMULATION_TEMPERATURE_OFFSET" - - config SIMULATION_TEMPERATURE_OFFSET_VALUE - depends on SUPPORT_SIMULATION_TEMPERATURE_OFFSET - int "Offset temperature" - default 0 - help - "123456 SIMULATION_TEMPERATURE_OFFSET_VALUE" - - -endmenu # NTC Temperature ADC \ No newline at end of file +# endmenu + \ No newline at end of file diff --git a/examples/default_example/main/idf_component.yml b/examples/default_example/main/idf_component.yml new file mode 100644 index 0000000..e69de29 diff --git a/examples/default_example/main/main.c b/examples/default_example/main/main.c deleted file mode 100644 index 2f777a6..0000000 --- a/examples/default_example/main/main.c +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include "template.h" -void app_main(void) -{ - test(); -} \ No newline at end of file diff --git a/examples/default_example/main/xgzf4000-poll-data.c b/examples/default_example/main/xgzf4000-poll-data.c new file mode 100644 index 0000000..6e24cd2 --- /dev/null +++ b/examples/default_example/main/xgzf4000-poll-data.c @@ -0,0 +1,60 @@ +#include "esp_log.h" +#include "xgzf4000.h" + +static const char *TAG = "XGZF4000_APP"; + +#define I2C_XGZF4000_ADDR 0x50 // default I2C address of the XGZF4000 sensor +#define I2C_MASTER_SCL_IO 19 // Assign the SCL pin number +#define I2C_MASTER_SDA_IO 18 // Assign the SDA pin number +#define I2C_MASTER_NUM I2C_NUM_0 +#define I2C_MASTER_FREQ_HZ 100000 // I2C master frequency + +static xgzf4000_dev_handle_t xgzf4000; + +static void poll_air_flow_data(void) { + uint32_t flow_rate_raw; + float flow_rate; + + if (xgzf4000_read_air_flow(xgzf4000, &flow_rate_raw, &flow_rate) == ESP_OK) { + // Convert raw flow rate based on the selected unit + #ifdef CONFIG_FLOW_RATE_UNIT_LMIN + flow_rate *= LITERS_PER_MINUTE_FACTOR; + #elif CONFIG_FLOW_RATE_UNIT_CMH + flow_rate *= CUBIC_METERS_PER_HOUR_FACTOR; + #endif + + // Log the flow rate in the selected unit + #ifdef CONFIG_FLOW_RATE_UNIT_LMIN + ESP_LOGI(TAG, "Raw Flow Rate: %u, Flow Rate: %.2f L/min", flow_rate_raw, flow_rate); + #elif CONFIG_FLOW_RATE_UNIT_CMH + ESP_LOGI(TAG, "Raw Flow Rate: %u, Flow Rate: %.2f m³/h", flow_rate_raw, flow_rate); + #endif + } else { + ESP_LOGE(TAG, "Failed to read air flow data"); + } +} + +void app_main(void) { + int ret = ESP_OK; + + xgzf4000_i2c_config_t i2c_config = { + .i2c_port = I2C_MASTER_NUM, + .i2c_addr = I2C_XGZF4000_ADDR, + }; + + ESP_ERROR_CHECK(xgzf4000_new_sensor(I2C_MASTER_NUM, I2C_SDA_PIN, I2C_SCL_PIN)); + + + if (init_xgzf4000_sensor() != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize XGZF4000 sensor"); + return; + } + + while (1) { + poll_air_flow_data(); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + + // Clean up + xgzf4000_del_sensor(xgzf4000); +} diff --git a/idf_component.yml b/idf_component.yml index 14e538b..202bf5b 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -1,4 +1,4 @@ -version: "0.9.1" +version: "0.9.2" license: "MIT" description: I2C driver for XGZF4000 air flow sensor url: https://github.com/hayschan/esp-idf-XGZF4000/tree/main diff --git a/include/xgzf4000.h b/include/xgzf4000.h index 5cf5861..a25b840 100644 --- a/include/xgzf4000.h +++ b/include/xgzf4000.h @@ -71,8 +71,7 @@ esp_err_t xgzf4000_del_sensor(xgzf4000_dev_handle_t handle); * - ESP_OK on successful reading of the flow rate data * - ESP_FAIL on failure to read the flow rate data */ -esp_err_t xgzf4000_read_air_flow(xgzf4000_dev_handle_t handle, uint32_t *flow_rate_raw, float *flow_rate) - +esp_err_t xgzf4000_read_air_flow(xgzf4000_dev_handle_t handle, uint32_t *flow_rate_raw, float *flow_rate); #ifdef __cplusplus } diff --git a/priv_include/xgzf4000_reg.h b/priv_include/xgzf4000_reg.h index 4600c42..b88333d 100644 --- a/priv_include/xgzf4000_reg.h +++ b/priv_include/xgzf4000_reg.h @@ -16,7 +16,7 @@ * @brief chip information definition */ #define CHIP_NAME "CFSENSOR XGZF4000" /**< chip name */ -#define SUPPLY_VOLTAGE_MIN (4f) /**< chip min supply voltage */ +#define SUPPLY_VOLTAGE_MIN (4.0f) /**< chip min supply voltage */ #define SUPPLY_VOLTAGE_MAX (5.5f) /**< chip max supply voltage */ #define TEMPERATURE_MIN (0.0f) /**< chip min working temperature */ #define TEMPERATURE_MAX (50.0f) /**< chip max working temperature */ diff --git a/xgzf4000.c b/xgzf4000.c index 7d3c104..f6dc97d 100644 --- a/xgzf4000.c +++ b/xgzf4000.c @@ -1,7 +1,13 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * File: xgzf4000.c + * Author: Hays Chan + * Year: 2023 * - * SPDX-License-Identifier: Apache-2.0 + * This file is part of the XGZF4000 Air Flow Sensor Driver project. + * + * SPDX-FileCopyrightText: 2023 Hays Chan + * + * SPDX-License-Identifier: MIT */ #include @@ -14,6 +20,8 @@ #include "xgzf4000_reg.h" +#define K_FACTOR 1000 // Proportionality factor for converting raw flow data to actual flow rate, either 1000 or 1000 + const static char *TAG = "XGZF4000"; typedef struct { @@ -72,11 +80,23 @@ esp_err_t xgzf4000_read_air_flow(xgzf4000_dev_handle_t handle, uint32_t *flow_ra return ESP_OK; } +esp_err_t adafruit_stemma_soil_sensor_init(i2c_port_t i2c_num, int sda_pin, int scl_pin) +{ + i2c_config_t conf; + conf.mode = I2C_MODE_MASTER; + conf.sda_io_num = sda_pin; + conf.sda_pullup_en = GPIO_PULLUP_ENABLE; + conf.scl_io_num = scl_pin; + conf.scl_pullup_en = GPIO_PULLUP_ENABLE; + conf.master.clk_speed = I2C_MASTER_FREQ_HZ; + conf.clk_flags = 0; + i2c_param_config(i2c_num, &conf); + return i2c_driver_install(i2c_num, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); +} -esp_err_t xgzf4000_new_sensor(const xgzf4000_i2c_config_t *i2c_conf, xgzf4000_dev_handle_t *handle_out) +esp_err_t xgzf4000_new_sensor(i2c_port_t i2c_num, int sda_pin, int scl_pin) { ESP_LOGI(TAG, "Initializing XGZF4000 Air Flow Sensor"); - ESP_LOGI(TAG, "%-15s: %d.%d.%d", CHIP_NAME, XGZF4000_VER_MAJOR, XGZF4000_VER_MINOR, XGZF4000_VER_PATCH); ESP_LOGI(TAG, "%-15s: %1.1f - %1.1fV", "SUPPLY_VOLTAGE", SUPPLY_VOLTAGE_MIN, SUPPLY_VOLTAGE_MAX); ESP_LOGI(TAG, "%-15s: %.2f - %.2f℃", "TEMPERATURE", TEMPERATURE_MIN, TEMPERATURE_MAX); ESP_LOGI(TAG, "%-15s: %.2fMPa", "PRESSURE", PRESSURE_MAX);