Skip to content

Commit

Permalink
Fix compilation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hayschan committed Dec 30, 2023
1 parent 381bc8b commit b0bd196
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 92 deletions.
11 changes: 0 additions & 11 deletions examples/CMakelists.txt

This file was deleted.

16 changes: 13 additions & 3 deletions examples/default_example/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "." )

# 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"
)
3 changes: 3 additions & 0 deletions examples/default_example/main/CMakeLists_registry.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(SRCS "xgzf4000-poll-data.c"
INCLUDE_DIRS "." )

84 changes: 20 additions & 64 deletions examples/default_example/main/Kconfig
Original file line number Diff line number Diff line change
@@ -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
# endmenu

Empty file.
6 changes: 0 additions & 6 deletions examples/default_example/main/main.c

This file was deleted.

60 changes: 60 additions & 0 deletions examples/default_example/main/xgzf4000-poll-data.c
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions include/xgzf4000.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion priv_include/xgzf4000_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
28 changes: 24 additions & 4 deletions xgzf4000.c
Original file line number Diff line number Diff line change
@@ -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 <inttypes.h>
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b0bd196

Please sign in to comment.