forked from tinkertanker/microDriver_SHT2x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_sht2x.cpp
79 lines (61 loc) · 2.29 KB
/
driver_sht2x.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "driver_sht2x.h"
using namespace pxt;
namespace SHT2xDriver
{
static MicroBit uBit;
static int sht_i2c_address = SHT2X_I2C_ADDR_DEFAULT;
/* Write given command to SHT2x, returning the reply recieved, <0 on error
* Actual data size 8bit - 14bit, depending on command */
uint16_t read_sht2x(SHTCommand cmd)
{
MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
if(i2c.write(sht_i2c_address, (const char *)&cmd, sizeof(uint8_t) * 1)\
!= MICROBIT_OK)
{
dprint("Failed to write command to SHT2x");
uBit.panic(SHT2X_PANIC_CODE);
}
dprintf("read_sht2x(): sent command to SHT2x: %x\r\n", cmd);
uint8_t read_buf[2] = { 0 , 0 };
uint16_t reply = 0;
for(int i = 0; i < 4; i ++)
{
if(i2c.read(sht_i2c_address, (char *)&read_buf, sizeof(uint16_t) * 1)\
== MICROBIT_OK)
{
//Data Preprocessing
reply = (read_buf[0] << 8) | (read_buf[1]); //Convert to LSB first from MSB first
reply&= ~0x0003; //Remove status bits: last 2 bits
dprintf("read_sht2x(): obtained reply from SHT2x: %hx\r\n", reply);
return reply;
}
fiber_sleep(30);
}
//Read Failure: No response from sensor before timeout
dprint("Read Failure: No response from sensor before timeout");
uBit.panic(SHT2X_PANIC_CODE);
return -1;
}
int read_humidity()
{
uint16_t read_rst = read_sht2x(sht_command_humidity);
int humidity = (int) SHT_CONV_HUMID(read_rst);
dprintf("read_humidity(): Calculated %% Relative Humidity: %d %%\r\n", \
humidity);
return humidity;
}
int read_temperature()
{
uint16_t read_rst = read_sht2x(sht_command_temperature);
int temperature = (int) SHT_CONV_TEMP(read_rst);
dprintf("read_temperature(): Calculated temperature: %d dg C\r\n", \
temperature);
return temperature;
}
void set_i2c_address(int address)
{
if(address < 0 || address > 128)
dprint("Error: set_i2c_address(): Invaild i2c address");
sht_i2c_address = address;
}
}