Skip to content

Commit dbcc65e

Browse files
authored
Merge pull request #13 from maesoser/master
Updated SHT3X so it can support different and configurable I2C busses through TwoWire
2 parents 12898b2 + 471f686 commit dbcc65e

2 files changed

Lines changed: 55 additions & 79 deletions

File tree

src/SHT3X.cpp

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,45 @@
11
#include "SHT3X.h"
22

3-
/* Motor()
4-
5-
*/
6-
SHT3X::SHT3X(uint8_t address, uint8_t deviceType) {
7-
if (deviceType == 0)
8-
Wire.begin();
9-
else if (deviceType == 1)
10-
Wire.begin(0, 26);
11-
else if (deviceType == 2)
12-
Wire.begin(2, 1);
13-
_address = address;
3+
bool SHT3X::init(uint8_t slave_addr_in, TwoWire* wire_in)
4+
{
5+
_wire = wire_in;
6+
_address=slave_addr_in;
7+
return true;
148
}
159

16-
// /*! @Detecting whether a device is a Unit or a Hat
17-
// @return Return 0 for Unit, 1 for Hat */
18-
// bool SHT3X::detectDevice() {
19-
// unsigned int data[6];
20-
// Wire.begin();
21-
// Wire.beginTransmission(_address);
22-
// Wire.write(0x2C);
23-
// Wire.write(0x06);
24-
// // Stop I2C transmission
25-
// Wire.endTransmission();
26-
// Wire.requestFrom(_address, (uint8_t)1);
27-
// for (int i = 0; i < 6; i++) {
28-
// data[i] = Wire.read();
29-
// Serial.printf("data%d:%d ", i, data[i]);
30-
// };
31-
// if (data[1] == -1) {
32-
// Wire.begin(0, 26);
33-
// return 1;
34-
// }
35-
// return 0;
36-
// }
37-
38-
byte SHT3X::get() {
39-
unsigned int data[6];
40-
41-
// Start I2C Transmission
42-
Wire.beginTransmission(_address);
43-
// Send measurement command
44-
Wire.write(0x2C);
45-
Wire.write(0x06);
46-
// Stop I2C transmission
47-
if (Wire.endTransmission() != 0) return 1;
48-
49-
delay(200);
50-
51-
// Request 6 bytes of data
52-
Wire.requestFrom(_address, (uint8_t)6);
53-
54-
// Read 6 bytes of data
55-
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
56-
for (int i = 0; i < 6; i++) {
57-
data[i] = Wire.read();
58-
};
59-
60-
delay(50);
61-
62-
if (Wire.available() != 0) return 2;
63-
64-
// Convert the data
65-
cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
66-
fTemp = (cTemp * 1.8) + 32;
67-
humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);
68-
69-
return 0;
10+
byte SHT3X::get()
11+
{
12+
unsigned int data[6];
13+
14+
// Start I2C Transmission
15+
_wire->beginTransmission(_address);
16+
// Send measurement command
17+
_wire->write(0x2C);
18+
_wire->write(0x06);
19+
// Stop I2C transmission
20+
if (_wire->endTransmission()!=0)
21+
return 1;
22+
23+
delay(200);
24+
25+
// Request 6 bytes of data
26+
_wire->requestFrom(_address, (uint8_t) 6);
27+
28+
// Read 6 bytes of data
29+
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
30+
for (int i=0;i<6;i++) {
31+
data[i]=_wire->read();
32+
};
33+
34+
delay(50);
35+
36+
if (_wire->available()!=0)
37+
return 2;
38+
39+
// Convert the data
40+
cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
41+
fTemp = (cTemp * 1.8) + 32;
42+
humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);
43+
44+
return 0;
7045
}

src/SHT3X.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
#ifndef _SHT3X_H_
2-
#define _SHT3X_H_
1+
#ifndef __SHT3X_H
2+
#define __HT3X_H
3+
34

45
#if ARDUINO >= 100
5-
#include "Arduino.h"
6+
#include "Arduino.h"
67
#else
7-
#include "WProgram.h"
8+
#include "WProgram.h"
89
#endif
910

1011
#include "Wire.h"
1112

12-
class SHT3X {
13-
public:
14-
SHT3X(uint8_t address = 0x44, uint8_t deviceType = 0);
15-
byte get(void);
16-
bool detectDevice();
17-
float cTemp = 0;
18-
float fTemp = 0;
19-
float humidity = 0;
13+
class SHT3X{
14+
public:
15+
bool init(uint8_t slave_addr_in=0x44, TwoWire* wire_in = &Wire);
16+
byte get(void);
17+
float cTemp=0;
18+
float fTemp=0;
19+
float humidity=0;
2020

21-
private:
22-
uint8_t _address;
21+
private:
22+
uint8_t _address;
23+
TwoWire* _wire;
2324
};
2425

2526
#endif

0 commit comments

Comments
 (0)