This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHS300x.cpp
193 lines (154 loc) · 4.12 KB
/
HS300x.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Module: Catena-HS300x.cpp
Function:
Code for Catena HS300x library
Copyright and License:
See accompanying LICENSE file.
Author:
Terry Moore, MCCI Corporation June 2019
*/
#include "HS300x.h"
using namespace HS300x;
bool cHS300x::begin(void)
{
std::uint8_t buf[2];
this->m_wire->begin();
/* do a data fetch to see whether we can see the device */
return this->readResponse(buf, sizeof(buf));
}
void cHS300x::end(void)
{
}
bool cHS300x::getTemperatureHumidity(cHS300x::Measurements &m) const
{
MeasurementsRaw mRaw;
bool fResult;
fResult = this->getTemperatureHumidityRaw(mRaw);
if (fResult)
{
/* set m from bits in mRaw */
m.set(mRaw);
}
else
{
m.Temperature = m.Humidity = NAN;
}
return fResult;
}
bool cHS300x::getTemperatureHumidityRaw(cHS300x::MeasurementsRaw &mRaw) const
{
uint32_t msDelay;
bool fResult;
fResult = true;
msDelay = this->startMeasurement();
if (msDelay == 0)
{
fResult = false;
}
if (fResult)
{
delay(msDelay);
uint32_t tStart = millis();
/* loop trying to read data */
do {
fResult = this->getMeasurementResultsRaw(mRaw);
} while (! fResult && (millis() - tStart) < this->kGetTemperatureTimeoutMs);
}
if (! fResult)
{
mRaw.TemperatureBits = mRaw.HumidityBits = 0;
}
return fResult;
}
std::uint32_t cHS300x::startMeasurement(void) const
{
std::uint8_t error;
this->m_wire->beginTransmission(this->getAddress());
error = this->m_wire->endTransmission();
if (error != 0)
{
if (this->isDebug())
{
//Serial.print("startMeasurement: can't select device: error ");
//Serial.println(unsigned(error));
}
return 0;
}
else
{
return this->kMeasurementDelayMs;
}
}
bool cHS300x::getMeasurementResults(cHS300x::Measurements &m) const
{
MeasurementsRaw mRaw;
bool fResult;
fResult = this->getMeasurementResultsRaw(mRaw);
if (fResult)
{
m.set(mRaw);
}
return fResult;
}
bool cHS300x::getMeasurementResultsRaw(cHS300x::MeasurementsRaw &mRaw) const
{
std::uint8_t buf[4];
bool fResult;
fResult = this->readResponse(buf, sizeof(buf));
if (fResult)
{
uint8_t status = buf[0] & 0xC0;
if (status != 0)
{
if (this->isDebug())
{
//Serial.print("getMeasurementResultsRaw: invalid data status: ");
//Serial.println(unsigned(status >> 6u));
}
fResult = false;
}
}
if (fResult)
{
mRaw.TemperatureBits = ((buf[2] << 8) | buf[3]) & 0xFFFCu;
mRaw.HumidityBits = (buf[0] << 10) | (buf[1] << 2);
}
return fResult;
}
bool cHS300x::readResponse(std::uint8_t *buf, size_t nBuf) const
{
bool ok;
unsigned nResult;
const std::int8_t addr = this->getAddress();
uint8_t nReadFrom;
if (buf == nullptr || nBuf > 32 || addr < 0)
{
if (this->isDebug())
//Serial.println("readResponse: invalid parameter");
return false;
}
nReadFrom = this->m_wire->requestFrom(std::uint8_t(addr), /* bytes */ std::uint8_t(nBuf));
if (nReadFrom != nBuf)
{
if (this->isDebug())
{
/*Serial.print("readResponse: nReadFrom(");
Serial.print(unsigned(nReadFrom));
Serial.print(") != nBuf(");
Serial.print(nBuf);
Serial.println(")");*/
}
}
nResult = this->m_wire->available();
for (unsigned i = 0; i < nResult; ++i)
buf[i] = this->m_wire->read();
if (nResult != nBuf && this->isDebug())
{
/*Serial.print("readResponse: nResult(");
Serial.print(nResult);
Serial.print(") != nBuf(");
Serial.print(nBuf);
Serial.println(")");*/
}
return (nResult == nBuf);
}