Skip to content

Commit 0a781d9

Browse files
Francesco Stefanniwinkj
authored andcommitted
Supported custom I2C instance
1 parent 25693d2 commit 0a781d9

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

SHTSensor.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
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

5453
const 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()
146146
class SHTC1Sensor : public SHTI2cSensor
147147
{
148148
public:
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[] = {
282282
const float SHTSensor::TEMPERATURE_INVALID = NAN;
283283
const 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
{

SHTSensor.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define SHTSENSOR_H
3131

3232
#include <inttypes.h>
33+
#include <Wire.h>
3334

3435
// Forward declaration
3536
class SHTSensorDriver;
@@ -116,7 +117,7 @@ class SHTSensor
116117
*
117118
* Returns true if communication with a sensor on the bus was successful, false otherwise
118119
*/
119-
bool init();
120+
bool init(TwoWire & wire = Wire);
120121

121122
/**
122123
* Read new values from the sensor
@@ -153,7 +154,7 @@ class SHTSensor
153154
private:
154155
void cleanup();
155156

156-
157+
157158
SHTSensorDriver *mSensor;
158159
float mTemperature;
159160
float mHumidity;
@@ -201,7 +202,7 @@ class SHTSensorDriver
201202
class SHTI2cSensor : public SHTSensorDriver {
202203
public:
203204
/** Size of i2c commands to send */
204-
205+
205206

206207
/** Size of i2c replies to expect */
207208
static const uint8_t EXPECTED_DATA_SIZE;
@@ -219,9 +220,11 @@ class SHTI2cSensor : public SHTSensorDriver {
219220
*/
220221
SHTI2cSensor(uint8_t i2cAddress, uint16_t i2cCommand, uint8_t duration,
221222
float a, float b, float c,
222-
float x, float y, float z, uint8_t cmd_Size)
223+
float x, float y, float z, uint8_t cmd_Size,
224+
TwoWire & wire = Wire)
223225
: mI2cAddress(i2cAddress), mI2cCommand(i2cCommand), mDuration(duration),
224-
mA(a), mB(b), mC(c), mX(x), mY(y), mZ(z), mCmd_Size(cmd_Size)
226+
mA(a), mB(b), mC(c), mX(x), mY(y), mZ(z), mCmd_Size(cmd_Size),
227+
mWire(wire)
225228
{
226229
}
227230

@@ -241,10 +244,12 @@ class SHTI2cSensor : public SHTSensorDriver {
241244
float mY;
242245
float mZ;
243246
uint8_t mCmd_Size;
247+
TwoWire & mWire;
244248

245249
private:
246250
static uint8_t crc8(const uint8_t *data, uint8_t len);
247-
static bool readFromI2c(uint8_t i2cAddress,
251+
static bool readFromI2c(TwoWire & wire,
252+
uint8_t i2cAddress,
248253
const uint8_t *i2cCommand,
249254
uint8_t commandLength, uint8_t *data,
250255
uint8_t dataLength, uint8_t duration);

0 commit comments

Comments
 (0)