environment2 2.0.0.0
environment2.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef ENVIRONMENT2_H
29#define ENVIRONMENT2_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52
73#define ENVIRONMENT2_SGP40_CMD_MEASURE_RAW 0x260F
74#define ENVIRONMENT2_SGP40_CMD_MEASURE_TEST 0x280E
75#define ENVIRONMENT2_SGP40_CMD_HEATER_OFF 0x3615
76#define ENVIRONMENT2_SGP40_CMD_SOFT_RESET 0x0006
77
82#define ENVIRONMENT2_SHT40_CMD_MEASURE_T_RH_HIGH_PRECISION 0xFD
83#define ENVIRONMENT2_SHT40_CMD_MEASURE_T_RH_MEDIUM_PRECISION 0xF6
84#define ENVIRONMENT2_SHT40_CMD_MEASURE_T_RH_LOWEST_PRECISION 0xE0
85#define ENVIRONMENT2_SHT40_CMD_READ_SERIAL 0x89
86#define ENVIRONMENT2_SHT40_CMD_SOFT_RESET 0x94
87#define ENVIRONMENT2_SHT40_CMD_ACTIVATE_HIGHEST_HEATER_1SEC 0x39
88#define ENVIRONMENT2_SHT40_CMD_ACTIVATE_HIGHEST_HEATER_0_1SEC 0x32
89#define ENVIRONMENT2_SHT40_CMD_ACTIVATE_MEDIUM_HEATER_1SEC 0x2F
90#define ENVIRONMENT2_SHT40_CMD_ACTIVATE_MEDIUM_HEATER_0_1SEC 0x24
91#define ENVIRONMENT2_SHT40_CMD_ACTIVATE_LOWEST_HEATER_1SEC 0x1E
92#define ENVIRONMENT2_SHT40_CMD_ACTIVATE_LOWEST_HEATER_0_1SEC 0x15
93
94 // environment2_cmd
95
111#define ENVIRONMENT2_SGP40_SET_DEV_ADDR 0x59
112#define ENVIRONMENT2_SHT40_SET_DEV_ADDR 0x44
113
119#define ENVIRONMENT2_SEL_SGP40 0x00
120#define ENVIRONMENT2_SEL_SHT40 0x01
121
127#define F16(x) \
128 ((fix16_t)(((x) >= 0) ? ((x)*65536.0 + 0.5) : ((x)*65536.0 - 0.5)))
129#define VocAlgorithm_SAMPLING_INTERVAL (1.)
130#define VocAlgorithm_INITIAL_BLACKOUT (45.)
131#define VocAlgorithm_VOC_INDEX_GAIN (230.)
132#define VocAlgorithm_SRAW_STD_INITIAL (50.)
133#define VocAlgorithm_SRAW_STD_BONUS (220.)
134#define VocAlgorithm_TAU_MEAN_VARIANCE_HOURS (12.)
135#define VocAlgorithm_TAU_INITIAL_MEAN (20.)
136#define VocAlgorithm_INIT_DURATION_MEAN ((3600. * 0.75))
137#define VocAlgorithm_INIT_TRANSITION_MEAN (0.01)
138#define VocAlgorithm_TAU_INITIAL_VARIANCE (2500.)
139#define VocAlgorithm_INIT_DURATION_VARIANCE ((3600. * 1.45))
140#define VocAlgorithm_INIT_TRANSITION_VARIANCE (0.01)
141#define VocAlgorithm_GATING_THRESHOLD (340.)
142#define VocAlgorithm_GATING_THRESHOLD_INITIAL (510.)
143#define VocAlgorithm_GATING_THRESHOLD_TRANSITION (0.09)
144#define VocAlgorithm_GATING_MAX_DURATION_MINUTES ((60. * 3.))
145#define VocAlgorithm_GATING_MAX_RATIO (0.3)
146#define VocAlgorithm_SIGMOID_L (500.)
147#define VocAlgorithm_SIGMOID_K (-0.0065)
148#define VocAlgorithm_SIGMOID_X0 (213.)
149#define VocAlgorithm_VOC_INDEX_OFFSET_DEFAULT (100.)
150#define VocAlgorithm_LP_TAU_FAST (20.0)
151#define VocAlgorithm_LP_TAU_SLOW (500.0)
152#define VocAlgorithm_LP_ALPHA (-0.2)
153#define VocAlgorithm_PERSISTENCE_UPTIME_GAMMA ((3. * 3600.))
154#define VocAlgorithm_MEAN_VARIANCE_ESTIMATOR__GAMMA_SCALING (64.)
155#define VocAlgorithm_MEAN_VARIANCE_ESTIMATOR__FIX16_MAX (32767.)
156
157 // environment2_set
158
168#define ENVIRONMENT2_SGP40_TEST_PASSED 0xD400
169#define ENVIRONMENT2_SGP40_TEST_FAILED 0x4B00
170
171
172 // environment2_get
173
188#define ENVIRONMENT2_MAP_MIKROBUS( cfg, mikrobus ) \
189 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
190 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA )
191
192 // environment2_map
193 // environment2
194
199typedef struct
200{
201 // Modules
202
203 i2c_master_t i2c;
205 // I2C slave address
206
210
215typedef struct
216{
217 pin_name_t scl;
218 pin_name_t sda;
220 uint32_t i2c_speed;
221 uint8_t i2c_address;
224
225typedef int32_t fix16_t;
226
227
266
277
296
314
331err_t environment2_generic_write ( environment2_t *ctx, uint8_t select_device, uint16_t cmd );
332
350err_t environment2_generic_read ( environment2_t *ctx, uint8_t select_device, uint16_t cmd, uint8_t *rx_buf );
351
370err_t environment2_get_temp_hum ( environment2_t *ctx, float *humidity, float *temperature );
371
388err_t environment2_get_air_quality ( environment2_t *ctx, uint16_t *air_quality );
389
408
425
443
457
471
489err_t environment2_voc_algorithm_process ( environment2_voc_algorithm_params *params, int32_t sraw, int32_t *voc_index );
490
508err_t environment2_measure_voc_index_with_rh_t ( environment2_t *ctx, int32_t *voc_index, int32_t *relative_humidity, int32_t *temperature );
509
527err_t environment2_get_voc_index ( environment2_t *ctx, int32_t *voc_index );
528
529#ifdef __cplusplus
530}
531#endif
532#endif // ENVIRONMENT2_H
533
534 // environment2
535
536// ------------------------------------------------------------------------ END
environment2_return_value_t
Environment 2 Click return value data.
Definition environment2.h:272
@ ENVIRONMENT2_OK
Definition environment2.h:273
@ ENVIRONMENT2_ERROR
Definition environment2.h:274
int32_t fix16_t
Definition environment2.h:225
err_t environment2_get_temp_hum(environment2_t *ctx, float *humidity, float *temperature)
Environment 2 get temperature and relative humidity function.
err_t environment2_config_sensors(void)
Environment 2 sensors configuration function.
void environment2_cfg_setup(environment2_cfg_t *cfg)
Environment 2 configuration object setup function.
err_t environment2_voc_algorithm_configuration(environment2_voc_algorithm_params *params)
Environment 2 VOC algorithm configuration function.
err_t environment2_generic_read(environment2_t *ctx, uint8_t select_device, uint16_t cmd, uint8_t *rx_buf)
Environment 2 I2C reading function.
uint16_t environment2_sgp40_measure_test(environment2_t *ctx)
Environment 2 SGP40 measurement test function.
err_t environment2_get_air_quality(environment2_t *ctx, uint16_t *air_quality)
Environment 2 get air quality data function.
err_t environment2_sgp40_heater_off(environment2_t *ctx)
Environment 2 heater off function.
err_t environment2_get_voc_index(environment2_t *ctx, int32_t *voc_index)
Environment 2 get VOC index function.
err_t environment2_measure_voc_index_with_rh_t(environment2_t *ctx, int32_t *voc_index, int32_t *relative_humidity, int32_t *temperature)
Environment 2 measure VOC index with relative humidity and temperature function.
err_t environment2_generic_write(environment2_t *ctx, uint8_t select_device, uint16_t cmd)
Environment 2 I2C writing function.
err_t environment2_voc_algorithm_process(environment2_voc_algorithm_params *params, int32_t sraw, int32_t *voc_index)
Environment 2 VOC algorithm process function.
err_t environment2_init(environment2_t *ctx, environment2_cfg_t *cfg)
Environment 2 initialization function.
err_t environment2_sgp40_soft_reset(environment2_t *ctx)
Environment 2 soft reset function.
Environment 2 Click configuration object.
Definition environment2.h:216
uint32_t i2c_speed
Definition environment2.h:220
pin_name_t scl
Definition environment2.h:217
pin_name_t sda
Definition environment2.h:218
uint8_t i2c_address
Definition environment2.h:221
Environment 2 Click context object.
Definition environment2.h:200
i2c_master_t i2c
Definition environment2.h:203
uint8_t slave_address
Definition environment2.h:207
Environment 2 Click VOC algorithm object.
Definition environment2.h:232
fix16_t mVoc_Index_Offset
Definition environment2.h:233
fix16_t m_Mean_Variance_Estimator___Sigmoid__L
Definition environment2.h:253
fix16_t mTau_Mean_Variance_Hours
Definition environment2.h:234
fix16_t m_Mean_Variance_Estimator__Gamma_Variance
Definition environment2.h:249
fix16_t mSraw_Std_Initial
Definition environment2.h:236
fix16_t m_Adaptive_Lowpass__A1
Definition environment2.h:259
fix16_t m_Mean_Variance_Estimator___Gamma
Definition environment2.h:245
fix16_t mUptime
Definition environment2.h:237
fix16_t m_Adaptive_Lowpass___X3
Definition environment2.h:264
fix16_t mSraw
Definition environment2.h:238
fix16_t m_Mean_Variance_Estimator___Sraw_Offset
Definition environment2.h:243
fix16_t m_Mean_Variance_Estimator___Std
Definition environment2.h:244
fix16_t m_Mean_Variance_Estimator___Uptime_Gating
Definition environment2.h:251
fix16_t m_Mox_Model__Sraw_Std
Definition environment2.h:256
fix16_t m_Mean_Variance_Estimator__Gamma_Mean
Definition environment2.h:248
fix16_t m_Mean_Variance_Estimator___Uptime_Gamma
Definition environment2.h:250
fix16_t m_Adaptive_Lowpass__A2
Definition environment2.h:260
fix16_t m_Mean_Variance_Estimator___Sigmoid__K
Definition environment2.h:254
fix16_t m_Adaptive_Lowpass___X2
Definition environment2.h:263
fix16_t mGating_Max_Duration_Minutes
Definition environment2.h:235
fix16_t m_Mox_Model__Sraw_Mean
Definition environment2.h:257
fix16_t m_Mean_Variance_Estimator___Gating_Duration_Minutes
Definition environment2.h:252
fix16_t m_Mean_Variance_Estimator___Sigmoid__X0
Definition environment2.h:255
fix16_t m_Mean_Variance_Estimator__Gating_Max_Duration_Minutes
Definition environment2.h:240
bool m_Mean_Variance_Estimator___Initialized
Definition environment2.h:241
fix16_t m_Mean_Variance_Estimator___Gamma_Initial_Mean
Definition environment2.h:246
fix16_t m_Sigmoid_Scaled__Offset
Definition environment2.h:258
fix16_t m_Adaptive_Lowpass___X1
Definition environment2.h:262
fix16_t mVoc_Index
Definition environment2.h:239
fix16_t m_Mean_Variance_Estimator___Mean
Definition environment2.h:242
fix16_t m_Mean_Variance_Estimator___Gamma_Initial_Variance
Definition environment2.h:247
bool m_Adaptive_Lowpass___Initialized
Definition environment2.h:261