-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHT4X.cpp
58 lines (51 loc) · 1.23 KB
/
SHT4X.cpp
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
#include <Wire.h>
#include "SHT4X.h"
extern int init_status;
static int32_t sht40_lastCommandTime = -1;
bool triggerSHT40()
{
if (sht40_lastCommandTime != -1 || (init_status & STATUS_ESHT40)) {
return false;
}
Wire.beginTransmission(SHT4X_ADDR);
Wire.write(SHT4X_LPM);
if (Wire.endTransmission() == 0) {
sht40_lastCommandTime = millis();
return true;
}
return false;
}
// temperature is in deg. Celsius * 10
// (one decimal place fixed point)
bool readSHT40(int *out_t, int *out_rh)
{
if (sht40_lastCommandTime == -1 || (init_status & STATUS_ESHT40)) {
return false;
}
uint8_t buff[6];
uint32_t elapsed = millis() - sht40_lastCommandTime;
if (elapsed > 10) {
// read data from sensor
sht40_lastCommandTime = -1;
if (Wire.requestFrom(SHT4X_ADDR, sizeof(buff)) != sizeof(buff)) {
return false;
}
Wire.readBytes(buff, sizeof(buff));
if (out_t) {
int val = (buff[0] << 8) | buff[1];
*out_t = -450 + 1750 * val / 65535;
}
if (out_rh) {
int val = (buff[3] << 8) | buff[4];
int lrh = -60 + 1250 * val / 65535;
if (lrh > 1000) {
lrh = 1000;
}
if (lrh < 0) {
lrh = 0;
}
*out_rh = lrh;
}
}
return true;
}