Library for Sensirion Environmental Sensor Shield for SGP30 & SHTC1

Dependents:   example-sensirion-ublox-c030

Committer:
Haseeb Khalid
Date:
Fri Dec 21 17:24:52 2018 +0500
Revision:
0:3e97001a43f8
Sensirion-ess library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Haseeb Khalid 0:3e97001a43f8 1 /*
Haseeb Khalid 0:3e97001a43f8 2 * Copyright (c) 2017-2018, Sensirion AG
Haseeb Khalid 0:3e97001a43f8 3 * All rights reserved.
Haseeb Khalid 0:3e97001a43f8 4 *
Haseeb Khalid 0:3e97001a43f8 5 * Redistribution and use in source and binary forms, with or without
Haseeb Khalid 0:3e97001a43f8 6 * modification, are permitted provided that the following conditions are met:
Haseeb Khalid 0:3e97001a43f8 7 *
Haseeb Khalid 0:3e97001a43f8 8 * * Redistributions of source code must retain the above copyright notice, this
Haseeb Khalid 0:3e97001a43f8 9 * list of conditions and the following disclaimer.
Haseeb Khalid 0:3e97001a43f8 10 *
Haseeb Khalid 0:3e97001a43f8 11 * * Redistributions in binary form must reproduce the above copyright notice,
Haseeb Khalid 0:3e97001a43f8 12 * this list of conditions and the following disclaimer in the documentation
Haseeb Khalid 0:3e97001a43f8 13 * and/or other materials provided with the distribution.
Haseeb Khalid 0:3e97001a43f8 14 *
Haseeb Khalid 0:3e97001a43f8 15 * * Neither the name of Sensirion AG nor the names of its
Haseeb Khalid 0:3e97001a43f8 16 * contributors may be used to endorse or promote products derived from
Haseeb Khalid 0:3e97001a43f8 17 * this software without specific prior written permission.
Haseeb Khalid 0:3e97001a43f8 18 *
Haseeb Khalid 0:3e97001a43f8 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Haseeb Khalid 0:3e97001a43f8 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Haseeb Khalid 0:3e97001a43f8 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Haseeb Khalid 0:3e97001a43f8 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Haseeb Khalid 0:3e97001a43f8 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Haseeb Khalid 0:3e97001a43f8 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Haseeb Khalid 0:3e97001a43f8 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Haseeb Khalid 0:3e97001a43f8 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Haseeb Khalid 0:3e97001a43f8 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Haseeb Khalid 0:3e97001a43f8 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Haseeb Khalid 0:3e97001a43f8 29 */
Haseeb Khalid 0:3e97001a43f8 30
Haseeb Khalid 0:3e97001a43f8 31 #ifndef SENSIRION_ESS
Haseeb Khalid 0:3e97001a43f8 32 #define SENSIRION_ESS
Haseeb Khalid 0:3e97001a43f8 33 #include "mbed.h"
Haseeb Khalid 0:3e97001a43f8 34 #define LED_RED D9
Haseeb Khalid 0:3e97001a43f8 35 #define LED_YEL D10
Haseeb Khalid 0:3e97001a43f8 36 #define LED_GRN D11
Haseeb Khalid 0:3e97001a43f8 37 class SensirionESS
Haseeb Khalid 0:3e97001a43f8 38 {
Haseeb Khalid 0:3e97001a43f8 39 public:
Haseeb Khalid 0:3e97001a43f8 40 SensirionESS(void *interface);
Haseeb Khalid 0:3e97001a43f8 41
Haseeb Khalid 0:3e97001a43f8 42 int initSensors();
Haseeb Khalid 0:3e97001a43f8 43
Haseeb Khalid 0:3e97001a43f8 44 int measureRHT();
Haseeb Khalid 0:3e97001a43f8 45 int measureIAQ();
Haseeb Khalid 0:3e97001a43f8 46
Haseeb Khalid 0:3e97001a43f8 47 bool isInitialized();
Haseeb Khalid 0:3e97001a43f8 48
Haseeb Khalid 0:3e97001a43f8 49 float getTemperature() const;
Haseeb Khalid 0:3e97001a43f8 50 float getHumidity() const;
Haseeb Khalid 0:3e97001a43f8 51
Haseeb Khalid 0:3e97001a43f8 52 int getProductType() const;
Haseeb Khalid 0:3e97001a43f8 53 int getFeatureSetVersion() const;
Haseeb Khalid 0:3e97001a43f8 54
Haseeb Khalid 0:3e97001a43f8 55 float getTVOC() const;
Haseeb Khalid 0:3e97001a43f8 56 float getECO2() const; // SGP30 only
Haseeb Khalid 0:3e97001a43f8 57
Haseeb Khalid 0:3e97001a43f8 58 const char* getError() const;
Haseeb Khalid 0:3e97001a43f8 59
Haseeb Khalid 0:3e97001a43f8 60 void setLedRYG(int r, int y, int g);
Haseeb Khalid 0:3e97001a43f8 61
Haseeb Khalid 0:3e97001a43f8 62 int remainingWaitTimeMS();
Haseeb Khalid 0:3e97001a43f8 63
Haseeb Khalid 0:3e97001a43f8 64 const static int PRODUCT_TYPE_SGP30 = 0;
Haseeb Khalid 0:3e97001a43f8 65 const static int PRODUCT_TYPE_SGPC3 = 1;
Haseeb Khalid 0:3e97001a43f8 66
Haseeb Khalid 0:3e97001a43f8 67 private:
Haseeb Khalid 0:3e97001a43f8 68 int8_t i2c_write(uint8_t addr, const uint8_t* data, uint16_t count);
Haseeb Khalid 0:3e97001a43f8 69 int8_t i2c_read(uint8_t addr, uint8_t* data, uint16_t count);
Haseeb Khalid 0:3e97001a43f8 70 static uint8_t crc8(const uint8_t* data, uint8_t len);
Haseeb Khalid 0:3e97001a43f8 71
Haseeb Khalid 0:3e97001a43f8 72 inline void setError(const char* error);
Haseeb Khalid 0:3e97001a43f8 73 int readFeatureSetInt();
Haseeb Khalid 0:3e97001a43f8 74 int measureRHTInt();
Haseeb Khalid 0:3e97001a43f8 75 int initSGP();
Haseeb Khalid 0:3e97001a43f8 76 void setLedRYGInt(int r, int y, int g);
Haseeb Khalid 0:3e97001a43f8 77
Haseeb Khalid 0:3e97001a43f8 78 const static int ERROR_BUF_LENGTH = 255;
Haseeb Khalid 0:3e97001a43f8 79 const static int CMD_LENGTH = 2;
Haseeb Khalid 0:3e97001a43f8 80
Haseeb Khalid 0:3e97001a43f8 81 const static int SHT_DATA_LENGTH = 6;
Haseeb Khalid 0:3e97001a43f8 82 const static int SHT_I2C_ADDR = 0x70;
Haseeb Khalid 0:3e97001a43f8 83 const static int SHT_MEASURE_DELAY = 15; // SHTC1 worst case
Haseeb Khalid 0:3e97001a43f8 84
Haseeb Khalid 0:3e97001a43f8 85 const static int SGP30_DATA_LENGTH = 6;
Haseeb Khalid 0:3e97001a43f8 86 const static int SGPC3_DATA_LENGTH = 3;
Haseeb Khalid 0:3e97001a43f8 87 const static int SGP_I2C_ADDR = 0x58;
Haseeb Khalid 0:3e97001a43f8 88 const static int SGP_MEASURE_DELAY = 50;
Haseeb Khalid 0:3e97001a43f8 89
Haseeb Khalid 0:3e97001a43f8 90 const static int SGP_RED_THRESHOLD = 150;
Haseeb Khalid 0:3e97001a43f8 91 const static int SGP_YEL_THRESHOLD = 125;
Haseeb Khalid 0:3e97001a43f8 92
Haseeb Khalid 0:3e97001a43f8 93 const static int SGPC3_INTERMEASURE_DELAY = 2000;
Haseeb Khalid 0:3e97001a43f8 94 const static int SGP30_INTERMEASURE_DELAY = 1000;
Haseeb Khalid 0:3e97001a43f8 95
Haseeb Khalid 0:3e97001a43f8 96 int SGP_INTERMEASURE_DELAY = SGP30_INTERMEASURE_DELAY;
Haseeb Khalid 0:3e97001a43f8 97 int SGP_DATA_LENGTH = SGP30_DATA_LENGTH;
Haseeb Khalid 0:3e97001a43f8 98
Haseeb Khalid 0:3e97001a43f8 99 float mTemperature = -250;
Haseeb Khalid 0:3e97001a43f8 100 float mHumidity = -1;
Haseeb Khalid 0:3e97001a43f8 101 float mTVOC = -1;
Haseeb Khalid 0:3e97001a43f8 102 float mECO2 = -1;
Haseeb Khalid 0:3e97001a43f8 103
Haseeb Khalid 0:3e97001a43f8 104 int mProductType = 0;
Haseeb Khalid 0:3e97001a43f8 105 int mFeatureSetVersion = 0;
Haseeb Khalid 0:3e97001a43f8 106
Haseeb Khalid 0:3e97001a43f8 107 unsigned long mSGPMeasurementTimestamp = 0;
Haseeb Khalid 0:3e97001a43f8 108
Haseeb Khalid 0:3e97001a43f8 109 bool mInitialized = false;
Haseeb Khalid 0:3e97001a43f8 110 char mErrorBuf[ERROR_BUF_LENGTH];
Haseeb Khalid 0:3e97001a43f8 111 uint8_t mDataBuf[SHT_DATA_LENGTH]; // max(SHT_DATA_LENGTH, SGP_DATA_LENGTH)
Haseeb Khalid 0:3e97001a43f8 112 I2C *i2c_connection;
Haseeb Khalid 0:3e97001a43f8 113 Timer *t;
Haseeb Khalid 0:3e97001a43f8 114
Haseeb Khalid 0:3e97001a43f8 115
Haseeb Khalid 0:3e97001a43f8 116 };
Haseeb Khalid 0:3e97001a43f8 117
Haseeb Khalid 0:3e97001a43f8 118 #endif /* SENSIRION_ESS */