BayEOS-Arduino  1.8.0_0.0.4
Sensirion.h
1 /* ========================================================================== */
2 /* Sensirion.h - Library for Sensirion SHT1x & SHT7x family temperature */
3 /* and humidity sensors */
4 /* Created by Markus Schatzl, November 28, 2008 */
5 /* Released into the public domain */
6 /* */
7 /* Revised (v1.1) by Carl Jackson, August 4, 2010 */
8 /* Rewritten (v2.0) by Carl Jackson, December 10, 2010 */
9 /* Rewritten (v3.01) by Thierry Couquillou, June 3, 2016 */
10 /* Modified by Stefan Holzheu, November 17, 2017 */
11 /* See README.txt file for details */
12 /* ========================================================================== */
13 
14 
15 #ifndef Sensirion_h
16 #define Sensirion_h
17 
18 #include <stdint.h>
19 #include <Sleep.h>
20 
21 // Clock pulse timing macros
22 // Lengthening these may assist communication over long wires
23 #define PULSE_LONG delayMicroseconds(30)
24 #define PULSE_SHORT delayMicroseconds(15)
25 
26 // User constants DO NOT CHANGE
27 const uint8_t TEMP = 0;
28 const uint8_t HUMI = 1;
29 
30 // Status register bit definitions
31 const uint8_t LOW_RES = 0x01; // 12-bit Temp / 8-bit RH (vs. 14 / 12)
32 const uint8_t NORELOAD = 0x02; // No reload of calibrarion data
33 const uint8_t HEAT_ON = 0x04; // Built-in heater on
34 const uint8_t BATT_LOW = 0x40; // VDD < 2.47V
35 
36 // Function return code definitions
37 const int8_t S_Err_TO = -4; // Timeout
38 const int8_t S_Err_CRC = -3; // CRC failure
39 const int8_t S_Err_NoACK = -2; // ACK expected but not received
40 const int8_t S_Err_Param = -1; // Parameter error in function call
41 const int8_t S_Meas_Wait = 0; // Wait for sensor cooling down
42 const int8_t S_Temp_Req = 1; // Temperature request (pulse)
43 const int8_t S_Temp_Wait = 2; // Wait for temperature measurement
44 const int8_t S_Humi_Req = 3; // Humidity request (pulse) means temperature measurement was successfull
45 const int8_t S_Humi_Wait = 4; // Wait for humidity measurement
46 const int8_t S_Calc_Run = 5; // Calculation in progress
47 const int8_t S_Meas_Rdy = 6; // All measurement was successfull (pulse)
48 
49 
50 class Sensirion
51 {
52  private:
53  uint8_t _pinData; // Pin interface
54  uint8_t _pinClock;
55  uint8_t _step; // Step of read cycle 0/1/2/3/4/5
56  uint8_t _stat_reg; // Local copy of status register
57  uint8_t _shtaddress; // Address of SHTxx module
58  uint8_t _shtnohold; // Hold Master mode for SHT2x/3x module
59  uint8_t _sht1x; // With Address we can know if it's SHT1x/2x/3x
60  uint8_t _sht2x;
61  uint8_t _sht3x;
62  uint8_t _shtprefix; // prefix for SHT2x and SHT3x command
63  uint32_t _time; // For delay and Time out calculation
64  uint16_t _meas[2]; // meas[0] = Temperature, meas[1] = Humidity
65  uint8_t _crc;
66 
67  int8_t getResult(uint8_t cmd);
68  int8_t meas(uint8_t cmd);
69  int8_t measRdy(uint8_t cmd);
70  int8_t putByte(uint8_t value);
71  uint8_t getByte(bool ack);
72  uint8_t bitrev(uint8_t value);
73  void startTransmission(void);
74  void stopTransmission(void);
75  void resetConnection(void);
76  void calcCRC(uint8_t value, uint8_t *crc);
77 
78  public:
79  Sensirion(uint8_t dataPin, uint8_t clockPin, uint8_t address = 0x00, bool noholdmaster = false);
80 
81  int8_t measureSleep(float *temp, float *humi,uint8_t timeoutcounter=30);
82  int8_t measure(float *temp = NULL, float *humi = NULL, float *dew = NULL, float temp0 = 0, float *humi0 = NULL);
83  int8_t writeSR(uint8_t value);
84  int8_t readSR(uint8_t *result);
85  int8_t reset(void);
86  float calcTemp(uint16_t rawData);
87  float calcHumi(uint16_t rawData, float temp);
88  float calcHumi(float dewpoint, float temp);
89  float calcDewpoint(float humi, float temp);
90 };
91 
92 #endif // #ifndef Sensirion_h