SHT3x driver minor changes
- new high level function sht3x_measure added, it comprises all three steps to perform one measurement in only one function - example with two sensors removed
This commit is contained in:
parent
a53586eb75
commit
b9add1ee48
9 changed files with 138 additions and 167 deletions
3
examples/sht3x/Makefile
Normal file
3
examples/sht3x/Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=SHT3x
|
||||
EXTRA_COMPONENTS = extras/i2c extras/sht3x
|
||||
include ../../common.mk
|
||||
|
|
@ -33,14 +33,4 @@ If you want to run examples with **two sensors**, you could do this with only on
|
|||
|
||||
## Example description
|
||||
|
||||
Examples show how to use the driver in single shot as well as in periodic mode.
|
||||
|
||||
|
||||
### _sht3x_one_sensor_
|
||||
|
||||
The simple example show how to use the driver with one SHT3x sensor. It demonstrates two different user task implementations, one in *single shot mode* and one in *periodic mode*.
|
||||
|
||||
### _sht3x_two_sensors_
|
||||
|
||||
This simple example shows how to use the driver for two sensors. As with the example _sht3x_one_sensor_, it uses two different user task implementations.
|
||||
|
||||
It shows different user task implementations in *single shot mode* and *periodic mode*. In *single shot* mode either low level or high level functions are used. Constants SINGLE_SHOT_LOW_LEVEL and SINGLE_SHOT_HIGH_LEVEL controls which task implementation is used.
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
/**
|
||||
* Simple example with one SHT3x sensor.
|
||||
* Simple example with SHT3x sensor.
|
||||
*
|
||||
* It shows two different user task implementations, one in *single shot mode*
|
||||
* and one in *periodic mode*.
|
||||
* It shows different user task implementations in *single shot mode* and
|
||||
* *periodic mode*. In *single shot* mode either low level or high level
|
||||
* functions are used.
|
||||
*
|
||||
* Which implementation is used is defined by constant SINGLE_SHOT_MODE.
|
||||
* Constants SINGLE_SHOT_LOW_LEVEL and SINGLE_SHOT_HIGH_LEVEL controls which
|
||||
* task implementation is used.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
|
|
@ -15,7 +17,8 @@
|
|||
* +------------------------+ +----------+
|
||||
*/
|
||||
|
||||
#define SINGLE_SHOT_MODE
|
||||
// #define SINGLE_SHOT_LOW_LEVEL
|
||||
// #define SINGLE_SHOT_HIGH_LEVEL
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
|
@ -33,10 +36,11 @@
|
|||
|
||||
static sht3x_sensor_t* sensor; // sensor device data structure
|
||||
|
||||
#ifdef SINGLE_SHOT_MODE
|
||||
#if defined(SINGLE_SHOT_HIGH_LEVEL)
|
||||
/*
|
||||
* User task that triggers a measurement every 5 seconds. Due to
|
||||
* power efficiency reasons, it uses the SHT3x *sht3x_single_shot*.
|
||||
* User task that triggers a measurement every 5 seconds. Due to power
|
||||
* efficiency reasons it uses *single shot* mode. In this example it uses the
|
||||
* high level function *sht3x_measure* to perform one measurement in each cycle.
|
||||
*/
|
||||
void user_task (void *pvParameters)
|
||||
{
|
||||
|
|
@ -45,6 +49,35 @@ void user_task (void *pvParameters)
|
|||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// perform one measurement and do something with the results
|
||||
if (sht3x_measure (sensor, &temperature, &humidity))
|
||||
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3, temperature, humidity);
|
||||
|
||||
// wait until 5 seconds are over
|
||||
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(SINGLE_SHOT_LOW_LEVEL)
|
||||
/*
|
||||
* User task that triggers a measurement every 5 seconds. Due to power
|
||||
* efficiency reasons it uses *single shot* mode. In this example it starts the
|
||||
* measurement, waits for the results and fetches the results using separate
|
||||
* functions
|
||||
*/
|
||||
void user_task (void *pvParameters)
|
||||
{
|
||||
float temperature;
|
||||
float humidity;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
// get the measurement duration for high repeatability;
|
||||
uint8_t duration = sht3x_get_measurement_duration(sht3x_high);
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Trigger one measurement in single shot mode with high repeatability.
|
||||
|
|
@ -52,7 +85,7 @@ void user_task (void *pvParameters)
|
|||
|
||||
// Wait until measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
|
||||
vTaskDelay (duration);
|
||||
|
||||
// retrieve the values and do something with them
|
||||
if (sht3x_get_results (sensor, &temperature, &humidity))
|
||||
|
|
@ -63,6 +96,7 @@ void user_task (void *pvParameters)
|
|||
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
#else // PERIODIC MODE
|
||||
/*
|
||||
* User task that fetches latest measurement results of sensor every 2
|
||||
|
|
@ -108,7 +142,7 @@ void user_init(void)
|
|||
// (different busses are possible).
|
||||
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Create the sensors.
|
||||
// Create the sensors, multiple sensors are possible.
|
||||
if ((sensor = sht3x_init_sensor (I2C_BUS, SHT3x_ADDR_2)))
|
||||
{
|
||||
// Create a user task that uses the sensors.
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
PROGRAM=SHT3x_One_Sensor
|
||||
EXTRA_COMPONENTS = extras/i2c extras/sht3x
|
||||
include ../../../common.mk
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
PROGRAM=SHT3x_Two_Sensors
|
||||
EXTRA_COMPONENTS = extras/i2c extras/sht3x
|
||||
include ../../../common.mk
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
/**
|
||||
* Simple example with two SHT3x sensors.
|
||||
*
|
||||
* It shows two different user task implementations, one in *single shot mode*
|
||||
* and one in *periodic mode*.
|
||||
*
|
||||
* Harware configuration:
|
||||
*
|
||||
* +------------------------+ +----------+
|
||||
* | ESP8266 Bus 0 | | SHT3x_1 |
|
||||
* | GPIO 5 (SCL) ------> SCL |
|
||||
* | GPIO 4 (SDA) ------- SDA |
|
||||
* | | +----------+
|
||||
* | Bus 1 | | SHT3x_2 |
|
||||
* | GPIO 14 (SCL) ------> SCL |
|
||||
* | GPIO 12 (SDA) ------- SDA |
|
||||
* +------------------------+ +----------+
|
||||
*/
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
// include SHT3x driver
|
||||
|
||||
#include "sht3x/sht3x.h"
|
||||
|
||||
// define I2C interfaces at which SHTx3 sensors are connected
|
||||
|
||||
#define I2C_1_BUS 0
|
||||
#define I2C_1_SCL_PIN GPIO_ID_PIN((5))
|
||||
#define I2C_1_SDA_PIN GPIO_ID_PIN((4))
|
||||
|
||||
#define I2C_2_BUS 1
|
||||
#define I2C_2_SCL_PIN GPIO_ID_PIN((14))
|
||||
#define I2C_2_SDA_PIN GPIO_ID_PIN((12))
|
||||
|
||||
static sht3x_sensor_t* sensor1;
|
||||
static sht3x_sensor_t* sensor2;
|
||||
|
||||
/*
|
||||
* User task that triggers measurements of sensor1 every 5 seconds. Due to
|
||||
* power efficiency reasons, it uses the SHT3x *sht3x_single_shot* mode.
|
||||
*/
|
||||
void user_task_sensor1 (void *pvParameters)
|
||||
{
|
||||
float temperature;
|
||||
float humidity;
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Trigger one measurement in single shot mode with high repeatability.
|
||||
sht3x_start_measurement (sensor1, sht3x_single_shot, sht3x_high);
|
||||
|
||||
// Wait until measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
|
||||
|
||||
// retrieve the values and do something with them
|
||||
if (sht3x_get_results (sensor1, &temperature, &humidity))
|
||||
printf("%.3f SHT3x Sensor 1: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3, temperature, humidity);
|
||||
|
||||
// wait until 5 seconds are over
|
||||
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* User task that fetches latest measurement results of sensor2 every 2
|
||||
* seconds. It starts the SHT3x in periodic mode with 10 measurements per
|
||||
* second (*sht3x_periodic_10mps*).
|
||||
*/
|
||||
void user_task_sensor2 (void *pvParameters)
|
||||
{
|
||||
float temperature;
|
||||
float humidity;
|
||||
|
||||
// Start periodic measurement mode with high repeatability.
|
||||
sht3x_start_measurement (sensor2, sht3x_periodic_10mps, sht3x_high);
|
||||
|
||||
// Wait until measurement is ready (constant time of at least 30 ms
|
||||
// or the duration returned from *sht3x_get_measurement_duration*).
|
||||
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
|
||||
|
||||
TickType_t last_wakeup = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Retrieve the values and do something with them.
|
||||
if (sht3x_get_results (sensor2, &temperature, &humidity))
|
||||
printf("%.3f SHT3x Sensor 2: %.2f °C, %.2f %%\n",
|
||||
(double)sdk_system_get_time()*1e-3, temperature, humidity);
|
||||
|
||||
// Wait until 2 seconds are over.
|
||||
vTaskDelayUntil(&last_wakeup, 2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
// Set UART Parameter
|
||||
uart_set_baud(0, 115200);
|
||||
|
||||
// Give the UART some time to settle.
|
||||
sdk_os_delay_us(500);
|
||||
|
||||
// Init I2C bus interfaces at which SHT3x sensors are connected
|
||||
// (different busses are possible).
|
||||
i2c_init(I2C_1_BUS, I2C_1_SCL_PIN, I2C_1_SDA_PIN, I2C_FREQ_100K);
|
||||
i2c_init(I2C_2_BUS, I2C_2_SCL_PIN, I2C_2_SDA_PIN, I2C_FREQ_100K);
|
||||
|
||||
// Create sensors.
|
||||
sensor1 = sht3x_init_sensor (I2C_1_BUS, SHT3x_ADDR_2);
|
||||
sensor2 = sht3x_init_sensor (I2C_2_BUS, SHT3x_ADDR_1);
|
||||
|
||||
if (sensor1 && sensor2)
|
||||
{
|
||||
// Create the tasks that use the sensors.
|
||||
xTaskCreate(user_task_sensor1, "user_task_sensor1", 256, NULL, 2, 0);
|
||||
xTaskCreate(user_task_sensor2, "user_task_sensor2", 256, NULL, 2, 0);
|
||||
}
|
||||
|
||||
// That's it.
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue