2727 */
2828
2929#include < inttypes.h>
30- #include < Wire.h>
3130#include < Arduino.h>
3231
3332#include " SHTSensor.h"
@@ -53,34 +52,35 @@ bool SHTSensorDriver::readSample()
5352
5453const uint8_t SHTI2cSensor::EXPECTED_DATA_SIZE = 6 ;
5554
56- bool SHTI2cSensor::readFromI2c (uint8_t i2cAddress,
55+ bool SHTI2cSensor::readFromI2c (TwoWire & wire,
56+ uint8_t i2cAddress,
5757 const uint8_t *i2cCommand,
5858 uint8_t commandLength, uint8_t *data,
5959 uint8_t dataLength,
6060 uint8_t duration)
6161{
62- Wire .beginTransmission (i2cAddress);
62+ wire .beginTransmission (i2cAddress);
6363 for (int i = 0 ; i < commandLength; ++i) {
64- if (Wire .write (i2cCommand[i]) != 1 ) {
64+ if (wire .write (i2cCommand[i]) != 1 ) {
6565 return false ;
6666 }
6767 }
6868
69- if (Wire .endTransmission () != 0 ) {
69+ if (wire .endTransmission () != 0 ) {
7070 return false ;
7171 }
7272
7373 delay (duration);
7474
75- Wire .requestFrom (i2cAddress, dataLength);
75+ wire .requestFrom (i2cAddress, dataLength);
7676
7777 // check if the same number of bytes are received that are requested.
78- if (Wire .available () != dataLength) {
78+ if (wire .available () != dataLength) {
7979 return false ;
8080 }
8181
8282 for (int i = 0 ; i < dataLength; ++i) {
83- data[i] = Wire .read ();
83+ data[i] = wire .read ();
8484 }
8585 return true ;
8686}
@@ -115,7 +115,7 @@ bool SHTI2cSensor::readSample()
115115 // is omitted for SHT4x Sensors
116116 cmd[1 ] = mI2cCommand & 0xff ;
117117
118- if (!readFromI2c (mI2cAddress , cmd, mCmd_Size , data,
118+ if (!readFromI2c (mWire , mI2cAddress , cmd, mCmd_Size , data,
119119 EXPECTED_DATA_SIZE, mDuration )) {
120120 return false ;
121121 }
@@ -135,8 +135,8 @@ bool SHTI2cSensor::readSample()
135135 val = (data[3 ] << 8 ) + data[4 ];
136136 mHumidity = mX + mY * (val / mZ );
137137
138- return true ;
139-
138+ return true ;
139+
140140}
141141
142142//
@@ -146,9 +146,9 @@ bool SHTI2cSensor::readSample()
146146class SHTC1Sensor : public SHTI2cSensor
147147{
148148public:
149- SHTC1Sensor ()
149+ SHTC1Sensor (TwoWire & wire )
150150 // clock stretching disabled, high precision, T first
151- : SHTI2cSensor(0x70 , 0x7866 , 15 , -45 , 175 , 65535 , 0 , 100 , 65535 , 2 )
151+ : SHTI2cSensor(0x70 , 0x7866 , 15 , -45 , 175 , 65535 , 0 , 100 , 65535 , 2 , wire )
152152 {
153153 }
154154};
@@ -173,10 +173,10 @@ class SHT3xSensor : public SHTI2cSensor
173173 static const uint8_t SHT3X_I2C_ADDRESS_44 = 0x44 ;
174174 static const uint8_t SHT3X_I2C_ADDRESS_45 = 0x45 ;
175175
176- SHT3xSensor (uint8_t i2cAddress = SHT3X_I2C_ADDRESS_44)
176+ SHT3xSensor (TwoWire & wire, uint8_t i2cAddress = SHT3X_I2C_ADDRESS_44)
177177 : SHTI2cSensor(i2cAddress, SHT3X_ACCURACY_HIGH,
178178 SHT3X_ACCURACY_HIGH_DURATION,
179- -45 , 175 , 65535 , 0 , 100 , 65535 , 2 )
179+ -45 , 175 , 65535 , 0 , 100 , 65535 , 2 , wire )
180180 {
181181 }
182182
@@ -222,10 +222,10 @@ class SHT4xSensor : public SHTI2cSensor
222222 static const uint8_t SHT4X_I2C_ADDRESS_44 = 0x44 ;
223223 static const uint8_t SHT4X_I2C_ADDRESS_45 = 0x45 ;
224224
225- SHT4xSensor (uint8_t i2cAddress = SHT4X_I2C_ADDRESS_44)
225+ SHT4xSensor (TwoWire & wire, uint8_t i2cAddress = SHT4X_I2C_ADDRESS_44)
226226 : SHTI2cSensor(i2cAddress, SHT4X_ACCURACY_HIGH,
227227 SHT4X_ACCURACY_HIGH_DURATION,
228- -45 , 175 , 65535 , -6 , 125 , 65535 , 1 )
228+ -45 , 175 , 65535 , -6 , 125 , 65535 , 1 , wire )
229229 {
230230 }
231231
@@ -282,29 +282,29 @@ const SHTSensor::SHTSensorType SHTSensor::AUTO_DETECT_SENSORS[] = {
282282const float SHTSensor::TEMPERATURE_INVALID = NAN;
283283const float SHTSensor::HUMIDITY_INVALID = NAN;
284284
285- bool SHTSensor::init ()
285+ bool SHTSensor::init (TwoWire & wire )
286286{
287287 if (mSensor != NULL ) {
288288 cleanup ();
289289 }
290290
291291 switch (mSensorType ) {
292292 case SHT3X:
293- mSensor = new SHT3xSensor ();
293+ mSensor = new SHT3xSensor (wire );
294294 break ;
295295
296296 case SHT3X_ALT:
297- mSensor = new SHT3xSensor (SHT3xSensor::SHT3X_I2C_ADDRESS_45);
297+ mSensor = new SHT3xSensor (wire, SHT3xSensor::SHT3X_I2C_ADDRESS_45);
298298 break ;
299299
300300 case SHTW1:
301301 case SHTW2:
302302 case SHTC1:
303303 case SHTC3:
304- mSensor = new SHTC1Sensor ();
304+ mSensor = new SHTC1Sensor (wire );
305305 break ;
306306 case SHT4X:
307- mSensor = new SHT4xSensor ();
307+ mSensor = new SHT4xSensor (wire );
308308 break ;
309309 case AUTO_DETECT:
310310 {
0 commit comments