-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSi7021.cpp
More file actions
52 lines (40 loc) · 1.05 KB
/
Si7021.cpp
File metadata and controls
52 lines (40 loc) · 1.05 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
#include "Si7021.h"
Si7021::Si7021(PinName sda, PinName scl):i2c(sda, scl)
{
i2c.frequency(FREQ);
}
Si7021::~Si7021()
{
}
int32_t Si7021::get_temperature()
{
return tData;
}
uint32_t Si7021::get_humidity()
{
return rhData;
}
bool Si7021::measure()
{
tx_buff[0] = READ_RH;
if(i2c.write(ADDR, (char*)tx_buff, 1) != 0) return 0;
if(i2c.read(ADDR, (char*)rx_buff, 2) != 0) return 0;
rhData = ((uint32_t)rx_buff[0] << 8) + (rx_buff[1]);
rhData = (((rhData) * 125/65536)) - 6;
tx_buff[0] = READ_TEMP;
if(i2c.write(ADDR, (char*)tx_buff, 1) != 0) return 0;
if(i2c.read(ADDR, (char*)rx_buff, 2) != 0) return 0;
tData = ((uint32_t)rx_buff[0] << 8) + (rx_buff[1]);
tData = (((tData) * 175.72/65536)) - 46.85;
return 1;
}
bool Si7021::check()
{
tx_buff[0] = READ_ID2_1;
tx_buff[1] = READ_ID2_2;
if(i2c.write(ADDR, (char*)tx_buff, 2) != 0) return 0;
if(i2c.read(ADDR, (char*)rx_buff, 8) != 0) return 0;
if(rx_buff[0] == DEVICE_ID)
return true;
else return 0;
}