From d6a73f616e725b461749fddc8d5c0b5e54bd53e0 Mon Sep 17 00:00:00 2001 From: kuba Date: Tue, 20 Feb 2024 20:17:43 +0100 Subject: [PATCH] renamed anemometer to impulse-sensor --- README.md | 4 +- components/anemometer/anemometer.c | 238 ------------------ .../{anemometer => impulse_sensor}/.eil.yml | 4 +- .../CMakeLists.txt | 2 +- .../{anemometer => impulse_sensor}/LICENSE | 0 .../component.mk | 0 components/impulse_sensor/impulse_sensor.c | 234 +++++++++++++++++ .../impulse_sensor.h} | 47 ++-- docs/source/groups/impulse_sensor.rst | 9 + docs/source/index.rst | 1 + .../default/CMakeLists.txt | 2 +- .../default/Makefile | 2 +- .../default/README.md | 6 +- .../default/main/CMakeLists.txt | 0 .../default/main/Kconfig.projbuild | 2 +- .../default/main/component.mk | 0 .../default/main/main.c | 20 +- .../default/sdkconfig.defaults.esp8266 | 0 18 files changed, 289 insertions(+), 282 deletions(-) delete mode 100644 components/anemometer/anemometer.c rename components/{anemometer => impulse_sensor}/.eil.yml (74%) rename components/{anemometer => impulse_sensor}/CMakeLists.txt (93%) rename components/{anemometer => impulse_sensor}/LICENSE (100%) rename components/{anemometer => impulse_sensor}/component.mk (100%) create mode 100644 components/impulse_sensor/impulse_sensor.c rename components/{anemometer/anemometer.h => impulse_sensor/impulse_sensor.h} (64%) create mode 100644 docs/source/groups/impulse_sensor.rst rename examples/{anemometer => impulse_sensor}/default/CMakeLists.txt (90%) rename examples/{anemometer => impulse_sensor}/default/Makefile (85%) rename examples/{anemometer => impulse_sensor}/default/README.md (50%) rename examples/{anemometer => impulse_sensor}/default/main/CMakeLists.txt (100%) rename examples/{anemometer => impulse_sensor}/default/main/Kconfig.projbuild (89%) rename examples/{anemometer => impulse_sensor}/default/main/component.mk (100%) rename examples/{anemometer => impulse_sensor}/default/main/main.c (68%) rename examples/{anemometer => impulse_sensor}/default/sdkconfig.defaults.esp8266 (100%) diff --git a/README.md b/README.md index b23b5c31..1164667e 100644 --- a/README.md +++ b/README.md @@ -232,10 +232,10 @@ or [GitLab examples](https://gitlab.com/UncleRus/esp-idf-lib/tree/master/example | Component | Description | License | Supported on | Thread safety | |--------------------------|----------------------------------------------------------------------------------|---------|--------------------|---------------| -| **anemometer** | Driver for impulse wind speed sensors(anemometers) | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | no | | **ds3502** | Driver for nonvolatile digital potentiometer DS3502 | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes | | **example** | An example component | ISC | esp32, esp8266, esp32s2, esp32c3 | n/a | | **hd44780** | Driver for HD44780 compatible LCD text displays | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | no | +| **impulse_sensor** | Driver for impulse output sensors | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | no | | **pca9685** | Driver for 16-channel, 12-bit PWM PCA9685 | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes | | **rda5807m** | Driver for single-chip broadcast FM radio tuner RDA5807M | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes | | **tca9548** | Driver for TCA9548A/PCA9548A low-voltage 8-channel I2C switch | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes | @@ -321,7 +321,7 @@ or [GitLab examples](https://gitlab.com/UncleRus/esp-idf-lib/tree/master/example - [Grupo de Pesquisa em Cultura Digital](http://gepid.upf.br/): `mpu6050` - GrzegorzH: `ds18x20` - [Gunar Schorcht](https://github.com/gschorcht): `bme680` `ccs811` `sht3x` `sts3x` -- [Jakub Turek](https://github.com/QB4-dev): `anemometer` `l3gx` `lsm303` +- [Jakub Turek](https://github.com/QB4-dev): `impulse_sensor` `l3gx` `lsm303` - [Jan Veeh](https://github.com/janveeh): `icm42670` - [Jeff Rowberg](https://www.i2cdevlib.com/): `mpu6050` - [Jose Manuel Perez](https://github.com/jmpmscorp): `lc709203f` `sgm58031` diff --git a/components/anemometer/anemometer.c b/components/anemometer/anemometer.c deleted file mode 100644 index c06b4183..00000000 --- a/components/anemometer/anemometer.c +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (c) 2024 Jakub Turek - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of itscontributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file anemometer.c - * - * ESP-IDF driver for impulse wind speed sensors(anemometers) - * - * Copyright (c) 2024 Jakub Turek - * - * BSD Licensed as described in the file LICENSE - */ -#include "anemometer.h" - -#include -#include -#include -#include -#include - -#if defined(HELPER_TARGET_IS_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) \ - && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) - -#define ESP_PCNT_SUPPORTED (1) -#include -#else -#define ESP_PCNT_SUPPORTED (0) -#endif - -#if HELPER_TARGET_IS_ESP32 -static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; -#define PORT_ENTER_CRITICAL() portENTER_CRITICAL(&mux) -#define PORT_EXIT_CRITICAL() portEXIT_CRITICAL(&mux) - -#elif HELPER_TARGET_IS_ESP8266 -#define PORT_ENTER_CRITICAL() portENTER_CRITICAL() -#define PORT_EXIT_CRITICAL() portEXIT_CRITICAL() - -#else -#error cannot identify the target -#endif - -#define CHECK_ARG(VAL) do { if (!(VAL)) return ESP_ERR_INVALID_ARG; } while (0) - -typedef struct { - gpio_num_t input_pin; //!< GPIO input pin - float sf; //!< scale factor - int pps; //!< measured pulses count per second - esp_timer_handle_t timer; //!< periodic timer to reset pps count - -#if ESP_PCNT_SUPPORTED - pcnt_unit_handle_t pcnt_unit; //!< hardware pulse counter - pcnt_channel_handle_t pcnt_ch; //!< hardware pulse counter channel -#else - int pulse_count; //!< pulses counter -#endif -} anemometer_priv_t; - -#if ESP_PCNT_SUPPORTED - -static esp_err_t anemometer_pulse_counter_init(anemometer_priv_t *priv) -{ - pcnt_unit_config_t unit_config = { - .high_limit = 2048, - .low_limit = -1 - }; - - pcnt_chan_config_t ch_config = { - .edge_gpio_num = priv->input_pin, - .level_gpio_num = -1 - }; - - ESP_ERROR_CHECK(pcnt_new_unit(&unit_config,&priv->pcnt_unit)); - ESP_ERROR_CHECK(pcnt_new_channel(priv->pcnt_unit,&ch_config,&priv->pcnt_ch)); - ESP_ERROR_CHECK(pcnt_channel_set_edge_action(priv->pcnt_ch, - PCNT_CHANNEL_EDGE_ACTION_INCREASE,PCNT_CHANNEL_EDGE_ACTION_HOLD)); - ESP_ERROR_CHECK(pcnt_unit_enable(priv->pcnt_unit)); - ESP_ERROR_CHECK(pcnt_unit_clear_count(priv->pcnt_unit)); - ESP_ERROR_CHECK(pcnt_unit_start(priv->pcnt_unit)); - return ESP_OK; -} - -static esp_err_t anemometer_pulse_counter_deinit(anemometer_priv_t *priv) -{ - ESP_ERROR_CHECK(pcnt_unit_stop(priv->pcnt_unit)); - ESP_ERROR_CHECK(pcnt_unit_disable(priv->pcnt_unit)); - ESP_ERROR_CHECK(pcnt_del_channel(priv->pcnt_ch)); - ESP_ERROR_CHECK(pcnt_del_unit(priv->pcnt_unit)); - return ESP_OK; -} - -static void timer_event_callback(void *arg) -{ - anemometer_priv_t *priv = (anemometer_priv_t *)arg; - - pcnt_unit_get_count(priv->pcnt_unit,&priv->pps); - pcnt_unit_clear_count(priv->pcnt_unit); -} - -#else - -static void IRAM_ATTR gpio_isr_handler(void* arg) -{ - anemometer_priv_t *priv = (anemometer_priv_t *)arg; - priv->pulse_count++; -} - -static esp_err_t anemometer_pulse_counter_init(anemometer_priv_t *priv) -{ - /* enable interrupts */ - esp_err_t rc = gpio_install_isr_service(0); - if (rc != ESP_OK && rc != ESP_ERR_INVALID_STATE){ - return rc; - } - /* setup GPIO */ - gpio_config_t io_conf = { - .intr_type = GPIO_INTR_POSEDGE, - .pin_bit_mask = (1 << priv->input_pin), - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_ENABLE - }; - ESP_ERROR_CHECK(gpio_config(&io_conf)); - ESP_ERROR_CHECK(gpio_isr_handler_add(priv->input_pin, gpio_isr_handler,priv)); - return ESP_OK; -} - -static esp_err_t anemometer_pulse_counter_deinit(anemometer_priv_t *priv) -{ - ESP_ERROR_CHECK(gpio_isr_handler_remove(priv->input_pin)); - return ESP_OK; -} - -static void timer_event_callback(void *arg) -{ - anemometer_priv_t *priv = (anemometer_priv_t *)arg; - - PORT_ENTER_CRITICAL(); - priv->pps = priv->pulse_count; - priv->pulse_count = 0; - PORT_EXIT_CRITICAL(); -} - -#endif - -static esp_err_t anemometer_timer_setup(anemometer_priv_t *priv) -{ - esp_timer_create_args_t timer_args = { - .name = "anemometer", - .dispatch_method = ESP_TIMER_TASK, - .callback = timer_event_callback, - .arg = priv, - }; - - ESP_ERROR_CHECK(esp_timer_create(&timer_args,&priv->timer)); - ESP_ERROR_CHECK(esp_timer_start_periodic(priv->timer,1000*1000)); - return ESP_OK; -} - -esp_err_t anemometer_init(const anemometer_config_t *conf, anemometer_t *anemometer) -{ - CHECK_ARG(conf); - CHECK_ARG(anemometer); - - anemometer_priv_t *priv; - esp_err_t rc; - - priv = (anemometer_priv_t *)calloc(1,sizeof(anemometer_priv_t)); - if(priv == NULL){ - return ESP_ERR_NO_MEM; - } - - priv->input_pin = conf->input_pin; - priv->sf = conf->scale_factor ? conf->scale_factor : ANEMOMETER_DEFAULT_SF; - - rc = anemometer_pulse_counter_init(priv); - if (rc != ESP_OK){ - free(priv); - return rc; - } - - rc = anemometer_timer_setup(priv); - if (rc != ESP_OK){ - anemometer_pulse_counter_deinit(priv); - free(priv); - return rc; - } - *anemometer = priv; - return ESP_OK; -} - -esp_err_t anemometer_deinit(anemometer_t *anemometer) -{ - CHECK_ARG(anemometer); - anemometer_priv_t *priv = (anemometer_priv_t *)anemometer; - - ESP_ERROR_CHECK(esp_timer_stop(priv->timer)); - anemometer_pulse_counter_deinit(priv); - free(priv); - return ESP_OK; -} - -esp_err_t anemometer_get_wind_speed(anemometer_t *anemometer, float *speed) -{ - CHECK_ARG(anemometer); - CHECK_ARG(speed); - - anemometer_priv_t *priv = (anemometer_priv_t *)anemometer; - - PORT_ENTER_CRITICAL(); - *speed = priv->sf * priv->pps; - PORT_EXIT_CRITICAL(); - return ESP_OK; -} - diff --git a/components/anemometer/.eil.yml b/components/impulse_sensor/.eil.yml similarity index 74% rename from components/anemometer/.eil.yml rename to components/impulse_sensor/.eil.yml index ede80ae3..36c0d772 100644 --- a/components/anemometer/.eil.yml +++ b/components/impulse_sensor/.eil.yml @@ -1,5 +1,5 @@ -name: anemometer -description: Driver for impulse wind speed sensors(anemometers) +name: impulse_sensor +description: Driver for impulse output sensors version: 1.0.0 groups: - misc diff --git a/components/anemometer/CMakeLists.txt b/components/impulse_sensor/CMakeLists.txt similarity index 93% rename from components/anemometer/CMakeLists.txt rename to components/impulse_sensor/CMakeLists.txt index 0b7ee61a..3d58bbbd 100644 --- a/components/anemometer/CMakeLists.txt +++ b/components/impulse_sensor/CMakeLists.txt @@ -7,7 +7,7 @@ else() endif() idf_component_register( - SRCS anemometer.c + SRCS impulse_sensor.c INCLUDE_DIRS . REQUIRES ${req} ) diff --git a/components/anemometer/LICENSE b/components/impulse_sensor/LICENSE similarity index 100% rename from components/anemometer/LICENSE rename to components/impulse_sensor/LICENSE diff --git a/components/anemometer/component.mk b/components/impulse_sensor/component.mk similarity index 100% rename from components/anemometer/component.mk rename to components/impulse_sensor/component.mk diff --git a/components/impulse_sensor/impulse_sensor.c b/components/impulse_sensor/impulse_sensor.c new file mode 100644 index 00000000..fba83f9b --- /dev/null +++ b/components/impulse_sensor/impulse_sensor.c @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2024 Jakub Turek + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of itscontributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file impulse_sensor.c + * + * ESP-IDF driver for impulse sensors + * + * Copyright (c) 2024 Jakub Turek + * + * BSD Licensed as described in the file LICENSE + */ +#include "impulse_sensor.h" + +#include +#include +#include +#include +#include + +#if defined(HELPER_TARGET_IS_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3) && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + +#define ESP_PCNT_SUPPORTED (1) +#include +#else +#define ESP_PCNT_SUPPORTED (0) +#endif + +#if HELPER_TARGET_IS_ESP32 +static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; +#define PORT_ENTER_CRITICAL() portENTER_CRITICAL(&mux) +#define PORT_EXIT_CRITICAL() portEXIT_CRITICAL(&mux) + +#elif HELPER_TARGET_IS_ESP8266 +#define PORT_ENTER_CRITICAL() portENTER_CRITICAL() +#define PORT_EXIT_CRITICAL() portEXIT_CRITICAL() + +#else +#error cannot identify the target +#endif + +#define CHECK_ARG(VAL) \ + do \ + { \ + if (!(VAL)) \ + return ESP_ERR_INVALID_ARG; \ + } \ + while (0) + +typedef struct +{ + gpio_num_t input_pin; //!< GPIO input pin + float sf; //!< scale factor + int pps; //!< measured pulses count per second + esp_timer_handle_t timer; //!< periodic timer to reset pps count + +#if ESP_PCNT_SUPPORTED + pcnt_unit_handle_t pcnt_unit; //!< hardware pulse counter + pcnt_channel_handle_t pcnt_ch; //!< hardware pulse counter channel +#else + int pulse_count; //!< pulses counter +#endif +} imp_sensor_priv_t; + +#if ESP_PCNT_SUPPORTED + +static esp_err_t pulse_counter_init(imp_sensor_priv_t *priv) +{ + pcnt_unit_config_t unit_config = { .high_limit = SHRT_MAX, .low_limit = -1 }; + + pcnt_chan_config_t ch_config = { .edge_gpio_num = priv->input_pin, .level_gpio_num = -1 }; + + ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &priv->pcnt_unit)); + ESP_ERROR_CHECK(pcnt_new_channel(priv->pcnt_unit, &ch_config, &priv->pcnt_ch)); + ESP_ERROR_CHECK(pcnt_channel_set_edge_action(priv->pcnt_ch, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD)); + ESP_ERROR_CHECK(pcnt_unit_enable(priv->pcnt_unit)); + ESP_ERROR_CHECK(pcnt_unit_clear_count(priv->pcnt_unit)); + ESP_ERROR_CHECK(pcnt_unit_start(priv->pcnt_unit)); + return ESP_OK; +} + +static esp_err_t pulse_counter_deinit(imp_sensor_priv_t *priv) +{ + ESP_ERROR_CHECK(pcnt_unit_stop(priv->pcnt_unit)); + ESP_ERROR_CHECK(pcnt_unit_disable(priv->pcnt_unit)); + ESP_ERROR_CHECK(pcnt_del_channel(priv->pcnt_ch)); + ESP_ERROR_CHECK(pcnt_del_unit(priv->pcnt_unit)); + return ESP_OK; +} + +static void timer_event_callback(void *arg) +{ + imp_sensor_priv_t *priv = (imp_sensor_priv_t *)arg; + + pcnt_unit_get_count(priv->pcnt_unit, &priv->pps); + pcnt_unit_clear_count(priv->pcnt_unit); +} + +#else + +static void IRAM_ATTR gpio_isr_handler(void *arg) +{ + imp_sensor_priv_t *priv = (imp_sensor_priv_t *)arg; + priv->pulse_count++; +} + +static esp_err_t pulse_counter_init(imp_sensor_priv_t *priv) +{ + /* enable interrupts */ + esp_err_t rc = gpio_install_isr_service(0); + if (rc != ESP_OK && rc != ESP_ERR_INVALID_STATE) + { + return rc; + } + /* setup GPIO */ + gpio_config_t io_conf = { .intr_type = GPIO_INTR_POSEDGE, .pin_bit_mask = (1 << priv->input_pin), .mode = GPIO_MODE_INPUT, .pull_up_en = GPIO_PULLUP_ENABLE }; + ESP_ERROR_CHECK(gpio_config(&io_conf)); + ESP_ERROR_CHECK(gpio_isr_handler_add(priv->input_pin, gpio_isr_handler, priv)); + return ESP_OK; +} + +static esp_err_t pulse_counter_deinit(imp_sensor_priv_t *priv) +{ + ESP_ERROR_CHECK(gpio_isr_handler_remove(priv->input_pin)); + return ESP_OK; +} + +static void timer_event_callback(void *arg) +{ + imp_sensor_priv_t *priv = (imp_sensor_priv_t *)arg; + + PORT_ENTER_CRITICAL(); + priv->pps = priv->pulse_count; + priv->pulse_count = 0; + PORT_EXIT_CRITICAL(); +} + +#endif + +static esp_err_t timer_setup(imp_sensor_priv_t *priv, const uint32_t period) +{ + esp_timer_create_args_t timer_args = { + .name = "imp-sensor", + .dispatch_method = ESP_TIMER_TASK, + .callback = timer_event_callback, + .arg = priv, + }; + + ESP_ERROR_CHECK(esp_timer_create(&timer_args, &priv->timer)); + ESP_ERROR_CHECK(esp_timer_start_periodic(priv->timer, period * 1000)); + return ESP_OK; +} + +esp_err_t imp_sensor_init(const imp_sensor_config_t *conf, imp_sensor_t *imp_sensor) +{ + CHECK_ARG(conf); + CHECK_ARG(imp_sensor); + + imp_sensor_priv_t *priv; + esp_err_t rc; + const uint32_t timer_period_ms = conf->meas_period ? conf->meas_period : IMP_SENSOR_DEFAULT_MEAS_PERIOD; + + priv = (imp_sensor_priv_t *)calloc(1, sizeof(imp_sensor_priv_t)); + if (priv == NULL) + return ESP_ERR_NO_MEM; + + priv->input_pin = conf->input_pin; + priv->sf = conf->scale_factor ? conf->scale_factor : IMP_SENSOR_DEFAULT_SF; + + rc = pulse_counter_init(priv); + if (rc != ESP_OK) + { + free(priv); + return rc; + } + + rc = timer_setup(priv, timer_period_ms); + if (rc != ESP_OK) + { + pulse_counter_deinit(priv); + free(priv); + return rc; + } + *imp_sensor = priv; + return ESP_OK; +} + +esp_err_t imp_sensor_deinit(imp_sensor_t *imp_sensor) +{ + CHECK_ARG(imp_sensor); + imp_sensor_priv_t *priv = (imp_sensor_priv_t *)imp_sensor; + + ESP_ERROR_CHECK(esp_timer_stop(priv->timer)); + pulse_counter_deinit(priv); + free(priv); + return ESP_OK; +} + +esp_err_t imp_sensor_get_value(imp_sensor_t *imp_sensor, float *value) +{ + CHECK_ARG(imp_sensor); + CHECK_ARG(value); + + imp_sensor_priv_t *priv = (imp_sensor_priv_t *)imp_sensor; + + PORT_ENTER_CRITICAL(); + *value = priv->sf * priv->pps; + PORT_EXIT_CRITICAL(); + return ESP_OK; +} diff --git a/components/anemometer/anemometer.h b/components/impulse_sensor/impulse_sensor.h similarity index 64% rename from components/anemometer/anemometer.h rename to components/impulse_sensor/impulse_sensor.h index c92f4c9e..96b09377 100644 --- a/components/anemometer/anemometer.h +++ b/components/impulse_sensor/impulse_sensor.h @@ -26,18 +26,18 @@ */ /** - * @file anemometer.h - * @defgroup anemometer + * @file impulse_sensor.h + * @defgroup impulse_sensor * @{ * - * ESP-IDF driver for impulse wind speed sensors(anemometers) + * ESP-IDF driver for impulse sensors * * Copyright (c) 2024 Jakub Turek * * BSD Licensed as described in the file LICENSE */ -#ifndef __ANEMOMETER_H__ -#define __ANEMOMETER_H__ +#ifndef __IMPULSE_SENSOR_H__ +#define __IMPULSE_SENSOR_H__ #include #include @@ -46,45 +46,46 @@ extern "C" { #endif -#define ANEMOMETER_DEFAULT_SF (1.75/20) //!< 1.75 m/s = 20 pps +#define IMP_SENSOR_DEFAULT_SF 1.0 ///< default scale factor +#define IMP_SENSOR_DEFAULT_MEAS_PERIOD 1000 ///< default measurement period[1sec] /** * Device descriptor */ -typedef void *anemometer_t; +typedef void *imp_sensor_t; typedef struct { - gpio_num_t input_pin; //!< GPIO input pin - const float scale_factor; //!< scale factor -} anemometer_config_t; + gpio_num_t input_pin; //!< GPIO input pin + const float scale_factor; //!< scale factor + const uint32_t meas_period; //!< measurement period[msecs] +} imp_sensor_config_t; /** - * @brief Init anemometer sensor + * @brief Init impulse sensor * - * @param config Pointer to the device config - * @param[out] Pointer to created anemometer device + * @param config Pointer to sensor config + * @param[out] Pointer to created sensor object * @return `ESP_OK` on success */ -esp_err_t anemometer_init(const anemometer_config_t *conf, anemometer_t *anemometer); +esp_err_t imp_sensor_init(const imp_sensor_config_t *conf, imp_sensor_t *imp_sensor); /** - * @brief Deinit anemometer sensor + * @brief Deinit impulse sensor * - * @param anemometer Pointer to the anemometer device + * @param imp_sensor Pointer to sensor device * @return `ESP_OK` on success */ -esp_err_t anemometer_deinit(anemometer_t *anemometer); +esp_err_t imp_sensor_deinit(imp_sensor_t *imp_sensor); /** - * @brief Deinit anemometer sensor + * @brief Deinit impulse sensor * - * @param anemometer Pointer to the anemometer device - * @param[out] speed calculated wind speed in [m/s] + * @param imp_sensor Pointer to sensor device + * @param[out] value output value multiplied by scale factor * @return `ESP_OK` on success */ -esp_err_t anemometer_get_wind_speed(anemometer_t *anemometer, float *speed); - +esp_err_t imp_sensor_get_value(imp_sensor_t *imp_sensor, float *value); #ifdef __cplusplus } @@ -92,4 +93,4 @@ esp_err_t anemometer_get_wind_speed(anemometer_t *anemometer, float *speed); /**@}*/ -#endif /* __ANEMOMETER_H__ */ +#endif /* __IMPULSE_SENSOR_H__ */ diff --git a/docs/source/groups/impulse_sensor.rst b/docs/source/groups/impulse_sensor.rst new file mode 100644 index 00000000..40eb23c4 --- /dev/null +++ b/docs/source/groups/impulse_sensor.rst @@ -0,0 +1,9 @@ +.. _impulse_sensor: + +impulse_sensor - Driver for sensors with impulse output like +fluid flow, shaft rotation, windspeed sensors. +================================================================= + +.. doxygengroup:: impulse_sensor + :members: + diff --git a/docs/source/index.rst b/docs/source/index.rst index b135cd4b..2f39f6b0 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -252,6 +252,7 @@ Other groups/tca9548 groups/ds3502 groups/wiegand + groups/impulse_sensor =========== diff --git a/examples/anemometer/default/CMakeLists.txt b/examples/impulse_sensor/default/CMakeLists.txt similarity index 90% rename from examples/anemometer/default/CMakeLists.txt rename to examples/impulse_sensor/default/CMakeLists.txt index f581ab54..baf540f8 100644 --- a/examples/anemometer/default/CMakeLists.txt +++ b/examples/impulse_sensor/default/CMakeLists.txt @@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.5) set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../../components) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(example-anemometer) +project(example-impulse-sensor) diff --git a/examples/anemometer/default/Makefile b/examples/impulse_sensor/default/Makefile similarity index 85% rename from examples/anemometer/default/Makefile rename to examples/impulse_sensor/default/Makefile index 2751be41..22b6f8da 100644 --- a/examples/anemometer/default/Makefile +++ b/examples/impulse_sensor/default/Makefile @@ -1,5 +1,5 @@ #V := 1 -PROJECT_NAME := example-anemometer +PROJECT_NAME := example-impulse-sensor EXTRA_COMPONENT_DIRS := $(CURDIR)/../../../components diff --git a/examples/anemometer/default/README.md b/examples/impulse_sensor/default/README.md similarity index 50% rename from examples/anemometer/default/README.md rename to examples/impulse_sensor/default/README.md index 964da8f8..ca724cdc 100644 --- a/examples/anemometer/default/README.md +++ b/examples/impulse_sensor/default/README.md @@ -1,8 +1,8 @@ -# Example for `anemometer` driver +# Example for `impulse_counter` driver ## What it does -It shows anemometer impulse wind sensor sensor data in a loop. +It shows anemometer - impulse wind speed sensor data in a loop. ## Wiring @@ -11,7 +11,7 @@ resistors. | Name | Description | Defaults | |------|-------------|----------| -| `CONFIG_EXAMPLE_ANEMOMETER_GPIO` | GPIO number for `DATA` | "5" for `esp8266`, "6" for `esp32c3`, "19" for `esp32`, `esp32s2`, and `esp32s3` | +| `CONFIG_EXAMPLE_DATA_GPIO` | GPIO number for `DATA` | "5" for `esp8266`, "6" for `esp32c3`, "19" for `esp32`, `esp32s2`, and `esp32s3` | ## Notes diff --git a/examples/anemometer/default/main/CMakeLists.txt b/examples/impulse_sensor/default/main/CMakeLists.txt similarity index 100% rename from examples/anemometer/default/main/CMakeLists.txt rename to examples/impulse_sensor/default/main/CMakeLists.txt diff --git a/examples/anemometer/default/main/Kconfig.projbuild b/examples/impulse_sensor/default/main/Kconfig.projbuild similarity index 89% rename from examples/anemometer/default/main/Kconfig.projbuild rename to examples/impulse_sensor/default/main/Kconfig.projbuild index e0def6ac..a1fc2407 100644 --- a/examples/anemometer/default/main/Kconfig.projbuild +++ b/examples/impulse_sensor/default/main/Kconfig.projbuild @@ -1,5 +1,5 @@ menu "Example configuration" - config EXAMPLE_ANEMOMETER_GPIO + config EXAMPLE_DATA_GPIO int "DATA GPIO Number" default 2 if IDF_TARGET_ESP8266 default 6 if IDF_TARGET_ESP32C3 diff --git a/examples/anemometer/default/main/component.mk b/examples/impulse_sensor/default/main/component.mk similarity index 100% rename from examples/anemometer/default/main/component.mk rename to examples/impulse_sensor/default/main/component.mk diff --git a/examples/anemometer/default/main/main.c b/examples/impulse_sensor/default/main/main.c similarity index 68% rename from examples/anemometer/default/main/main.c rename to examples/impulse_sensor/default/main/main.c index 6ccf2c68..6a805fca 100644 --- a/examples/anemometer/default/main/main.c +++ b/examples/impulse_sensor/default/main/main.c @@ -18,26 +18,26 @@ #include #include #include +#include -#include +static const char *TAG = "APP"; -static const char *TAG="APP"; +static imp_sensor_t anemometer; -static anemometer_t anemometer; - -static anemometer_config_t config = { - .input_pin = CONFIG_EXAMPLE_ANEMOMETER_GPIO, - .scale_factor = ANEMOMETER_DEFAULT_SF +static imp_sensor_config_t config = { + .input_pin = CONFIG_EXAMPLE_DATA_GPIO, + .scale_factor = (1.75 / 20) //!< 1.75 m/s = 20 pps, }; void app_main() { float val; ESP_LOGI(TAG, "Anemometer test"); - ESP_ERROR_CHECK(anemometer_init(&config,&anemometer)); + ESP_ERROR_CHECK(imp_sensor_init(&config, &anemometer)); - while(1){ - anemometer_get_wind_speed(anemometer,&val); + while (1) + { + imp_sensor_get_value(anemometer, &val); ESP_LOGI(TAG, "Wind speed = %.2f m/s", val); vTaskDelay(pdMS_TO_TICKS(1000)); } diff --git a/examples/anemometer/default/sdkconfig.defaults.esp8266 b/examples/impulse_sensor/default/sdkconfig.defaults.esp8266 similarity index 100% rename from examples/anemometer/default/sdkconfig.defaults.esp8266 rename to examples/impulse_sensor/default/sdkconfig.defaults.esp8266