SHT3x driver minor changes

- type sht3x_values_t replaced by separate float values
- additional repeatability parameter defined for function sht3x_start_measurement
- parameter of function sht3x_get_measurement_duration changed from
  sht3x_sensort_t to sht3_repeat_t
- sensor modes and repeatability levels extended by prefix sht3x_
This commit is contained in:
Gunar Schorcht 2017-10-21 19:09:31 +02:00
parent 6cd46833e9
commit a53586eb75
5 changed files with 111 additions and 115 deletions

View file

@ -15,7 +15,7 @@
* +------------------------+ +----------+
*/
// #define SINGLE_SHOT_MODE
#define SINGLE_SHOT_MODE
#include "espressif/esp_common.h"
#include "esp/uart.h"
@ -36,28 +36,28 @@ static sht3x_sensor_t* sensor; // sensor device data structure
#ifdef SINGLE_SHOT_MODE
/*
* User task that triggers a measurement every 5 seconds. Due to
* power efficiency reasons, it uses the SHT3x *single_shot*.
* power efficiency reasons, it uses the SHT3x *sht3x_single_shot*.
*/
void user_task (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
TickType_t last_wakeup = xTaskGetTickCount();
while (1)
{
// Trigger one measurement in single shot mode.
sht3x_start_measurement (sensor, single_shot);
// Trigger one measurement in single shot mode with high repeatability.
sht3x_start_measurement (sensor, 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(sensor));
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
// retrieve the values and do something with them
if (sht3x_get_results (sensor, &values))
if (sht3x_get_results (sensor, &temperature, &humidity))
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
(double)sdk_system_get_time()*1e-3, temperature, humidity);
// wait until 5 seconds are over
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
@ -67,28 +67,28 @@ void user_task (void *pvParameters)
/*
* User task that fetches latest measurement results of sensor every 2
* seconds. It starts the SHT3x in periodic mode with 1 measurements per
* second (*periodic_1mps*).
* second (*sht3x_periodic_1mps*).
*/
void user_task (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
// Start periodic measurements with 1 measurement per second.
sht3x_start_measurement (sensor, periodic_1mps);
sht3x_start_measurement (sensor, sht3x_periodic_1mps, sht3x_high);
// Wait until first measurement is ready (constant time of at least 30 ms
// or the duration returned from *sht3x_get_measurement_duration*).
vTaskDelay (sht3x_get_measurement_duration(sensor));
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
TickType_t last_wakeup = xTaskGetTickCount();
while (1)
{
// Get the values and do something with them.
if (sht3x_get_results (sensor, &values))
if (sht3x_get_results (sensor, &temperature, &humidity))
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
(double)sdk_system_get_time()*1e-3, temperature, humidity);
// Wait until 2 seconds (cycle time) are over.
vTaskDelayUntil(&last_wakeup, 2000 / portTICK_PERIOD_MS);

View file

@ -42,28 +42,28 @@ static sht3x_sensor_t* sensor2;
/*
* User task that triggers measurements of sensor1 every 5 seconds. Due to
* power efficiency reasons, it uses the SHT3x *single_shot* mode.
* power efficiency reasons, it uses the SHT3x *sht3x_single_shot* mode.
*/
void user_task_sensor1 (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
TickType_t last_wakeup = xTaskGetTickCount();
while (1)
{
// Trigger one measurement in single shot mode.
sht3x_start_measurement (sensor1, single_shot);
// 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(sensor1));
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
// retrieve the values and do something with them
if (sht3x_get_results (sensor1, &values))
printf("%.3f SHT3x Sensor1: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
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);
@ -73,28 +73,28 @@ void user_task_sensor1 (void *pvParameters)
/*
* User task that fetches latest measurement results of sensor2 every 2
* seconds. It starts the SHT3x in periodic mode with 10 measurements per
* second (*periodic_10mps*).
* second (*sht3x_periodic_10mps*).
*/
void user_task_sensor2 (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
// start periodic measurement mode
sht3x_start_measurement (sensor2, periodic_10mps);
// 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(sensor2));
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, &values))
printf("%.3f SHT3x Sensor2: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
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);

View file

@ -60,13 +60,13 @@ There are two different error levels that are ORed into one single ```error_code
## Repeatability
The SHT3x sensor supports **three levels of repeatability** (low, medium and high). Repeatability is the variation in measurement results taken by the sensor under the same conditions, and in a short period of time. It is a measure for the noise on the physical sensor output. The higher the repeatability the smaller are changes in the output subsequent measurements.
The SHT3x sensor supports **three levels of repeatability** (low, medium and high). Repeatability is the variation in measurement results taken by the sensor under the same conditions, and in a short period of time. It is a measure for the noise on the physical sensor output. The higher the repeatability the smaller are changes in the output of subsequent measurements.
The repeatability settings influences the measurement duration as well as the power consumption of the sensor. The measurement takes 3 ms with low repeatability, 5 ms with medium repeatability and 13.5 ms with high repeatability. That is, the measurement produces a noticeable delay in execution.
While the sensor measures at the lowest repeatability, the average current consumption is 800 μA. That is, the higher the repeatability level, the longer the measurement takes and the higher the power consumption. The sensor consumes only 0.2 μA in standby mode.
The driver uses high repeatability by default. The user task can change this by setting member ```repeatability``` of the sensor device data structure before function ```sht3x_start_measurement``` is called.
The repeatability used for a measurement is specified as parameter of ```sht3x_start_measurement```.
## Usage
@ -111,24 +111,24 @@ Thus, in this mode the user task could look like the following:
```
void user_task (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
// Start periodic measurements with 1 measurement per second.
sht3x_start_measurement (sensor, periodic_1mps);
sht3x_start_measurement (sensor, sht3x_periodic_1mps, sht3x_high);
// Wait until first measurement is ready (constant time of at least 30 ms
// or the duration returned from *sht3x_get_measurement_duration*).
vTaskDelay (sht3x_get_measurement_duration(sensor));
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
TickType_t last_wakeup = xTaskGetTickCount();
while (1)
{
// Get the values and do something with them.
if (sht3x_get_results (sensor, &values))
if (sht3x_get_results (sensor, &temperature, &humidity))
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
(double)sdk_system_get_time()*1e-3, temperature, humidity);
// Wait until 2 seconds (cycle time) are over.
vTaskDelayUntil(&last_wakeup, 2000 / portTICK_PERIOD_MS);
@ -137,7 +137,7 @@ void user_task (void *pvParameters)
```
At the beginning of the task, the periodic measurement is started with function ```sht3x_start_measurement``` and a rate of 1 measurement per second. The task is then delayed with function ```vTaskDelay``` to wait for first measurement results. The duration can be either a constant time of at least 30 ms or the duration returned by ```sht3x_get_measurement_duration```, as in the example. Inside the task loop, simply the measurement results are fetched periodically using function ```sht3x_get_results``` every 2 seconds.
At the beginning of the task, the periodic measurement is started with function ```sht3x_start_measurement``` at high repeatability level and a rate of 1 measurement per second. The task is then delayed with function ```vTaskDelay``` to wait for first measurement results. The duration can be either a constant time of at least 30 ms or the duration returned by ```sht3x_get_measurement_duration```, as in the example. Inside the task loop, simply the measurement results are fetched periodically using function ```sht3x_get_results``` every 2 seconds.
**Please note:** The rate of fetching the measurement results must be not greater than the rate of periodic measurements of the sensor, however, it *should be less* to avoid conflicts caused by the timing tolerance of the sensor.
@ -149,24 +149,24 @@ Thus the user task could look like the following:
```
void user_task (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
TickType_t last_wakeup = xTaskGetTickCount();
while (1)
{
// Trigger one measurement in single shot mode.
sht3x_start_measurement (sensor, single_shot);
// Trigger one measurement in single shot mode with high repeatability.
sht3x_start_measurement (sensor, 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(sensor));
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
// retrieve the values and do something with them
if (sht3x_get_results (sensor, &values))
if (sht3x_get_results (sensor, &temperature, &humidity))
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
(double)sdk_system_get_time()*1e-3, temperature, humidity);
// wait until 5 seconds are over
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
@ -222,35 +222,34 @@ static sht3x_sensor_t* sensor; // sensor device data structure
/*
* User task that triggers a measurement every 5 seconds. Due to
* power efficiency reasons, it uses the SHT3x *single_shot*.
* power efficiency reasons, it uses the SHT3x *sht3x_single_shot*.
*/
void user_task (void *pvParameters)
{
sht3x_values_t values;
float temperature;
float humidity;
TickType_t last_wakeup = xTaskGetTickCount();
while (1)
{
// Trigger one measurement in single shot mode.
sht3x_start_measurement (sensor, single_shot);
// Trigger one measurement in single shot mode with high repeatability.
sht3x_start_measurement (sensor, 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(sensor));
vTaskDelay (sht3x_get_measurement_duration(sht3x_high));
// retrieve the values and do something with them
if (sht3x_get_results (sensor, &values))
if (sht3x_get_results (sensor, &temperature, &humidity))
printf("%.3f SHT3x Sensor: %.2f °C, %.2f %%\n",
(double)sdk_system_get_time()*1e-3,
values.temperature, values.humidity);
(double)sdk_system_get_time()*1e-3, temperature, humidity);
// wait until 5 seconds are over
vTaskDelayUntil(&last_wakeup, 5000 / portTICK_PERIOD_MS);
}
}
void user_init(void)
{
// Set UART Parameter.

View file

@ -55,8 +55,6 @@
#include "espressif/esp_common.h"
#include "espressif/sdk_private.h"
// #define SHT3X_CLOCK_STRETCHING
#define SHT3x_STATUS_CMD 0xF32D
#define SHT3x_CLEAR_STATUS_CMD 0x3041
#define SHT3x_RESET_CMD 0x30A2
@ -134,8 +132,7 @@ sht3x_sensor_t* sht3x_init_sensor(uint8_t bus, uint8_t addr)
// inititalize sensor data structure
dev->bus = bus;
dev->addr = addr;
dev->mode = single_shot;
dev->repeatability = high;
dev->mode = sht3x_single_shot;
dev->meas_start_time = 0;
dev->meas_started = false;
dev->meas_first = false;
@ -163,15 +160,16 @@ sht3x_sensor_t* sht3x_init_sensor(uint8_t bus, uint8_t addr)
}
bool sht3x_start_measurement (sht3x_sensor_t* dev, sht3x_mode_t mode)
bool sht3x_start_measurement (sht3x_sensor_t* dev, sht3x_mode_t mode, sht3x_repeat_t repeat)
{
if (!dev) return false;
dev->error_code = SHT3x_OK;
dev->mode = mode;
dev->repeatability = repeat;
// start measurement according to selected mode and return an duration estimate
if (!sht3x_send_command(dev, SHT3x_MEASURE_CMD[dev->mode][dev->repeatability]))
if (!sht3x_send_command(dev, SHT3x_MEASURE_CMD[mode][repeat]))
{
error_dev ("could not send start measurment command", __FUNCTION__, dev);
dev->error_code |= SHT3x_SEND_MEAS_CMD_FAILED;
@ -186,11 +184,9 @@ bool sht3x_start_measurement (sht3x_sensor_t* dev, sht3x_mode_t mode)
}
uint8_t sht3x_get_measurement_duration (sht3x_sensor_t* dev)
uint8_t sht3x_get_measurement_duration (sht3x_repeat_t repeat)
{
if (!dev) return 0;
return SHT3x_MEAS_DURATION_TICKS[dev->repeatability]; // in RTOS ticks
return SHT3x_MEAS_DURATION_TICKS[repeat]; // in RTOS ticks
}
@ -234,7 +230,7 @@ bool sht3x_get_raw_data(sht3x_sensor_t* dev, sht3x_raw_data_t raw_data)
dev->meas_first = false;
// reset measurement started flag in single shot mode
if (dev->mode == single_shot)
if (dev->mode == sht3x_single_shot)
dev->meas_started = false;
// check temperature crc
@ -257,27 +253,30 @@ bool sht3x_get_raw_data(sht3x_sensor_t* dev, sht3x_raw_data_t raw_data)
}
bool sht3x_compute_values (sht3x_raw_data_t raw_data, sht3x_values_t* values)
bool sht3x_compute_values (sht3x_raw_data_t raw_data, float* temperature, float* humidity)
{
if (!raw_data || !values) return false;
if (!raw_data) return false;
values->temperature = ((((raw_data[0] * 256.0) + raw_data[1]) * 175) / 65535.0) - 45;
values->humidity = ((((raw_data[3] * 256.0) + raw_data[4]) * 100) / 65535.0);
if (temperature)
*temperature = ((((raw_data[0] * 256.0) + raw_data[1]) * 175) / 65535.0) - 45;
if (humidity)
*humidity = ((((raw_data[3] * 256.0) + raw_data[4]) * 100) / 65535.0);
return true;
}
bool sht3x_get_results (sht3x_sensor_t* dev, sht3x_values_t* values)
bool sht3x_get_results (sht3x_sensor_t* dev, float* temperature, float* humidity)
{
if (!dev || !values) return false;
if (!dev || (!temperature && !humidity)) return false;
sht3x_raw_data_t raw_data;
if (!sht3x_get_raw_data (dev, raw_data))
return false;
return sht3x_compute_values (raw_data, values);
return sht3x_compute_values (raw_data, temperature, humidity);
}
/* Functions for internal use only */

View file

@ -93,25 +93,17 @@ extern "C" {
*/
typedef uint8_t sht3x_raw_data_t [SHT3x_RAW_DATA_SIZE];
/**
* @brief sensor values type
*/
typedef struct {
float temperature; // temperature in degree Celcius
float humidity; // humidity in percent
} sht3x_values_t;
/**
* @brief possible measurement modes
*/
typedef enum {
single_shot = 0, // one single measurement
periodic_05mps, // periodic with 0.5 measurements per second (mps)
periodic_1mps, // periodic with 1 measurements per second (mps)
periodic_2mps, // periodic with 2 measurements per second (mps)
periodic_4mps, // periodic with 4 measurements per second (mps)
periodic_10mps // periodic with 10 measurements per second (mps)
sht3x_single_shot = 0, // one single measurement
sht3x_periodic_05mps, // periodic with 0.5 measurements per second (mps)
sht3x_periodic_1mps, // periodic with 1 measurements per second (mps)
sht3x_periodic_2mps, // periodic with 2 measurements per second (mps)
sht3x_periodic_4mps, // periodic with 4 measurements per second (mps)
sht3x_periodic_10mps // periodic with 10 measurements per second (mps)
} sht3x_mode_t;
@ -119,9 +111,9 @@ typedef enum {
* @brief possible repeatability modes
*/
typedef enum {
high = 0,
medium,
low
sht3x_high = 0,
sht3x_medium,
sht3x_low
} sht3x_repeat_t;
/**
@ -129,7 +121,7 @@ typedef enum {
*/
typedef struct {
uint32_t error_code; //
uint32_t error_code; // combined error codes
uint8_t bus; // I2C bus at which sensor is connected
uint8_t addr; // I2C slave address of the sensor
@ -161,7 +153,8 @@ sht3x_sensor_t* sht3x_init_sensor (uint8_t bus, uint8_t addr);
* @brief Start single shot or periodic measurements
*
* The function starts the measurement either in *single shot mode*
* (exactly one measurement) or *periodic mode* (periodic measurements).
* (exactly one measurement) or *periodic mode* (periodic measurements)
* with given repeatabilty.
*
* In the *single shot mode*, this function has to be called for each
* measurement. The measurement duration has to be waited every time
@ -176,25 +169,27 @@ sht3x_sensor_t* sht3x_init_sensor (uint8_t bus, uint8_t addr);
* results at a lower rate. The rate of the periodic measurements is defined
* by the parameter *mode*.
*
* @param dev pointer to sensor device data structure
* @param mode measurement mode, see type *sht3x_mode_t*
* @return measurement duration given in RTOS ticks or -1 on error
* @param dev pointer to sensor device data structure
* @param mode measurement mode, see type *sht3x_mode_t*
* @param repeat repeatability, see type *sht3x_repeat_t*
* @return true on success, false on error
*/
bool sht3x_start_measurement (sht3x_sensor_t* dev, sht3x_mode_t mode);
bool sht3x_start_measurement (sht3x_sensor_t* dev, sht3x_mode_t mode,
sht3x_repeat_t repeat);
/**
* @brief Get the duration of a measurement in RTOS ticks.
*
* The function returns the duration in RTOS ticks required by the sensor to
* perform a measurement with configured repeatability. Once a measurement is
* perform a measurement for the given repeatability. Once a measurement is
* started with function *sht3x_start_measurement* the user task can use this
* duration in RTOS ticks directly to wait with function *vTaskDelay* until
* the measurement results can be fetched.
*
* @param dev pointer to sensor device data structure
* @return measurement duration given in RTOS ticks
* @param repeat repeatability, see type *sht3x_repeat_t*
* @return measurement duration given in RTOS ticks
*/
uint8_t sht3x_get_measurement_duration (sht3x_sensor_t* dev);
uint8_t sht3x_get_measurement_duration (sht3x_repeat_t repeat);
/**
@ -212,21 +207,22 @@ uint8_t sht3x_get_measurement_duration (sht3x_sensor_t* dev);
*
* In case that there are no new data that can be read, the function fails.
*
* @param dev pointer to sensor device data structure
* @param raw_data byte array in which raw data are stored
* @return true on success, false on error
* @param dev pointer to sensor device data structure
* @param raw_data byte array in which raw data are stored
* @return true on success, false on error
*/
bool sht3x_get_raw_data(sht3x_sensor_t* dev, sht3x_raw_data_t raw_data);
/**
* @brief Computes sensor values from raw data
*
* @param raw_data byte array that contains raw data
* @param values pointer to data structure in which results are stored
* @return true on success, false on error
* @param raw_data byte array that contains raw data
* @param temperature returns temperature in degree Celsius
* @param humidity returns humidity in percent
* @return true on success, false on error
*/
bool sht3x_compute_values (sht3x_raw_data_t raw_data,
sht3x_values_t* values);
float* temperature, float* humidity);
/**
* @brief Get measurement results in form of sensor values
@ -236,11 +232,13 @@ bool sht3x_compute_values (sht3x_raw_data_t raw_data,
*
* In case that there are no results that can be read, the function fails.
*
* @param dev pointer to sensor device data structure
* @param values pointer to data structure in which results are stored
* @return true on success, false on error
* @param dev pointer to sensor device data structure
* @param temperature returns temperature in degree Celsius
* @param humidity returns humidity in percent
* @return true on success, false on error
*/
bool sht3x_get_results (sht3x_sensor_t* dev, sht3x_values_t* values);
bool sht3x_get_results (sht3x_sensor_t* dev,
float* temperature, float* humidity);
#ifdef __cplusplus