forked from pyrlouit/SHT2x-Arduino-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHT2x.cpp
More file actions
173 lines (144 loc) · 4.82 KB
/
Copy pathSHT2x.cpp
File metadata and controls
173 lines (144 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
SHT2x - A Humidity Library for Arduino.
Supported Sensor modules:
SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
SHT2x-Breakout Module - http://www.misenso.com/products/001
Created by Christopher Ladden at Modern Device on December 2009.
Modified by Paul Badger March 2010
Modified by www.misenso.com on October 2011:
- code optimisation
- compatibility with Arduino 1.0
Modified by Patrick Thompson
- Added NON Blocking Functions
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <inttypes.h>
#include "wire.h"
#include "Arduino.h"
#include "SHT2x.h"
uint8_t ReadingStatus = SHT2X_Status::eReady;
/******************************************************************************
* Global Functions
******************************************************************************/
/**********************************************************
* GetHumidity
* Gets the current humidity from the sensor.
*
* @return float - The relative humidity in %RH
**********************************************************/
float SHT2xClass::GetHumidity()
{
float result = readSensor(eRHumidityHoldCmd);
return (-6.0 + 125.0 / 65536.0 * (float)(readSensor(result)));
}
/**********************************************************
* StartSensorRead
* SensorToRead = Which Sensor to Start Reading
* Starts A Sensor Read
* @return uint8_t - Code to Look for Errors 1 = Success
**********************************************************/
uint8_t SHT2xClass::StartSensorRead(uint8_t SensorToRead)
{
uint8_t returnCode = 0;
if (ReadingStatus == SHT2X_Status::eReady)
{
ReadingStatus = SensorToRead;
Wire.beginTransmission(eSHT2xAddress);
if (SensorToRead == SHT2X_Status::eTempature)
{
Wire.write(eTempNoHoldCmd);
}
if (SensorToRead == SHT2X_Status::eHumidity)
{
Wire.write(eRHumidityNoHoldCmd);
}
Wire.endTransmission();
delayMicroseconds(20);
returnCode = 1;
}
return returnCode;
}
/**********************************************************
* SensorDataReady
* Checks to see if sensor data is ready
*
* @return bool - True if Ready to read data
**********************************************************/
bool SHT2xClass::SensorDataReady()
{
bool result = false;
if (ReadingStatus > 0)
{
if (Wire.requestFrom(eSHT2xAddress,3) >= 3)
{
result = true;
}
}
return result;
}
/**********************************************************
* ReadSensorData
* Reads incoming Sensor Data and returns it;
*
* @return uint8_t = Sensor that was read
**********************************************************/
uint8_t SHT2xClass::ReadSensorData(float* result)
{
uint8_t returnCode = ReadingStatus;
uint16_t rawData;
rawData = ((Wire.read()) << 8);
rawData += Wire.read();
rawData &= ~0x0003; // clear two low bits (status bits)
if (ReadingStatus == SHT2X_Status::eHumidity)
{
*result = (-6.0 + 125.0 / 65536.0 * (float)(rawData));
}
if (ReadingStatus == SHT2X_Status::eTempature)
{
*result = (-46.85 + 175.72 / 65536.0 * (float)(rawData));
}
ReadingStatus = SHT2X_Status::eReady;
return returnCode;
}
/**********************************************************
* GetTemperature
* Gets the current temperature from the sensor.
*
* @return float - The temperature in Deg C
**********************************************************/
float SHT2xClass::GetTemperature()
{
float result = (readSensor(eTempHoldCmd));
return (-46.85 + 175.72 / 65536.0 * (float)(result));
}
/******************************************************************************
* Private Functions
******************************************************************************/
uint16_t SHT2xClass::readSensor(uint8_t command)
{
uint16_t result;
Wire.beginTransmission(eSHT2xAddress); //begin
Wire.write(command); //send the pointer location
delay(100);
Wire.endTransmission(); //end
Wire.requestFrom(eSHT2xAddress, 3);
while(Wire.available() < 3) {
; //wait
}
//Store the result
result = ((Wire.read()) << 8);
result += Wire.read();
result &= ~0x0003; // clear two low bits (status bits)
return result;
}
SHT2xClass SHT2x;