-
Notifications
You must be signed in to change notification settings - Fork 0
/
Software_HDC1080.cpp
153 lines (119 loc) · 3.89 KB
/
Software_HDC1080.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Arduino Library for Texas Instruments HDC1080 Digital Humidity and Temperature Sensor
Written by AA for Software
---
The MIT License (MIT)
Copyright (c) 2016-2017 ClosedCube Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "Software_HDC1080.h"
Software_HDC1080::Software_HDC1080()
{
}
void Software_HDC1080::begin(uint8_t address, SoftWire &sw) {
_sw = &sw;
_address = address;
_sw->begin();
setResolution(HDC1080_RESOLUTION_14BIT, HDC1080_RESOLUTION_14BIT);
}
void Software_HDC1080::setResolution(HDC1080_MeasurementResolution humidity, HDC1080_MeasurementResolution temperature) {
HDC1080_Registers reg;
reg.HumidityMeasurementResolution = 0;
reg.TemperatureMeasurementResolution = 0;
if (temperature == HDC1080_RESOLUTION_11BIT)
reg.TemperatureMeasurementResolution = 0x01;
switch (humidity)
{
case HDC1080_RESOLUTION_8BIT:
reg.HumidityMeasurementResolution = 0x02;
break;
case HDC1080_RESOLUTION_11BIT:
reg.HumidityMeasurementResolution = 0x01;
break;
default:
break;
}
writeRegister(reg);
}
HDC1080_SerialNumber Software_HDC1080::readSerialNumber() {
HDC1080_SerialNumber sernum;
sernum.serialFirst = readData(HDC1080_SERIAL_ID_FIRST);
sernum.serialMid = readData(HDC1080_SERIAL_ID_MID);
sernum.serialLast = readData(HDC1080_SERIAL_ID_LAST);
return sernum;
}
HDC1080_Registers Software_HDC1080::readRegister() {
HDC1080_Registers reg;
reg.rawData = (readData(HDC1080_CONFIGURATION) >> 8);
return reg;
}
void Software_HDC1080::writeRegister(HDC1080_Registers reg) {
_sw->beginTransmission(_address);
_sw->write(HDC1080_CONFIGURATION);
_sw->write(reg.rawData);
_sw->write(0x00);
_sw->endTransmission();
delay(10);
}
void Software_HDC1080::heatUp(uint8_t seconds) {
HDC1080_Registers reg = readRegister();
reg.Heater = 1;
reg.ModeOfAcquisition = 1;
writeRegister(reg);
uint8_t buf[4];
for (int i = 1; i < (seconds*66); i++) {
_sw->beginTransmission(_address);
_sw->write(0x00);
_sw->endTransmission();
delay(20);
_sw->requestFrom(_address, (uint8_t)4);
_sw->readBytes(buf, (size_t)4);
}
reg.Heater = 0;
reg.ModeOfAcquisition = 0;
writeRegister(reg);
}
double Software_HDC1080::readT() {
return readTemperature();
}
double Software_HDC1080::readTemperature() {
uint16_t rawT = readData(HDC1080_TEMPERATURE);
return (rawT / pow(2, 16)) * 165.0 - 40.0;
}
double Software_HDC1080::readH() {
return readHumidity();
}
double Software_HDC1080::readHumidity() {
uint16_t rawH = readData(HDC1080_HUMIDITY);
return (rawH / pow(2, 16)) * 100.0;
}
uint16_t Software_HDC1080::readManufacturerId() {
return readData(HDC1080_MANUFACTURER_ID);
}
uint16_t Software_HDC1080::readDeviceId() {
return readData(HDC1080_DEVICE_ID);
}
uint16_t Software_HDC1080::readData(uint8_t pointer) {
_sw->beginTransmission(_address);
_sw->write(pointer);
_sw->endTransmission();
delay(9);
_sw->requestFrom(_address, (uint8_t)2);
byte msb = _sw->read();
byte lsb = _sw->read();
return msb << 8 | lsb;
}