-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHX711.cpp
371 lines (292 loc) · 9.93 KB
/
HX711.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// MIT License
//
// Copyright (c) 2021 Daniel Robertson
//
// 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 <chrono>
#include <cstdint>
#include <mutex>
#include <stdexcept>
#include <unordered_map>
#include <utility>
#include "../include/GpioException.h"
#include "../include/HX711.h"
#include "../include/IntegrityException.h"
#include "../include/TimeoutException.h"
#include "../include/Utility.h"
#include "../include/Value.h"
namespace HX711 {
constexpr std::chrono::nanoseconds HX711::_T1;
constexpr std::chrono::nanoseconds HX711::_T2;
constexpr std::chrono::nanoseconds HX711::_T3;
constexpr std::chrono::nanoseconds HX711::_T4;
constexpr std::chrono::microseconds HX711::_POWER_DOWN_TIMEOUT;
/**
* Used to select the correct number of clock pulses depending on the
* gain
* Datasheet pg. 4
*/
const std::unordered_map<const Gain, const unsigned char> HX711::_PULSES({
{ Gain::GAIN_128, 25 },
{ Gain::GAIN_32, 26 },
{ Gain::GAIN_64, 27 }
});
/**
* Used to select the correct settling time depending on rate
* Datasheet pg. 3
*/
const std::unordered_map<const Rate, const std::chrono::milliseconds>
HX711::_SETTLING_TIMES({
{ Rate::HZ_10, std::chrono::milliseconds(400) },
{ Rate::HZ_80, std::chrono::milliseconds(50) }
});
std::int32_t HX711::_convertFromTwosComplement(const std::int32_t val) noexcept {
return -(val & 0x800000) + (val & 0x7fffff);
}
unsigned char HX711::_calculatePulses(const Gain g) noexcept {
return _PULSES.at(g) - _BITS_PER_CONVERSION_PERIOD;
}
void HX711::_setInputGainSelection() {
const auto pulses = _calculatePulses(this->_gain);
for(auto i = decltype(pulses){0}; i < pulses; ++i) {
this->_readBit();
}
}
bool HX711::_readBit() const {
//first, clock pin is set high to make DOUT ready to be read from
//and the current ACTUAL time is noted for later
const auto startNanos = Utility::getnanos();
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::HIGH);
//then delay for sufficient time to allow DOUT to be ready (0.1us)
//and min amount of time between the high to low clock pulse. Note
//the overlap between T2 and T3
if(this->_useDelays) {
Utility::delay(std::max(_T2, _T3));
}
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::LOW);
const auto diff = Utility::getnanos() - startNanos;
//at this point, according to the documentation, if the clock pin
//was held high for longer than 60us, the chip will have entered
//power down mode. This means the currently read bit, and
//consequently the entire value, is unreliable.
if(this->_strictTiming && diff >= _POWER_DOWN_TIMEOUT) {
throw IntegrityException("bit integrity failure");
}
//at this stage, DOUT is ready so read the bit value
const auto bit = Utility::readGpio(this->_gpioHandle, this->_dataPin);
//Assuming everything was OK, the datasheet requires a further
//delay before the next pulse
if(this->_useDelays) {
Utility::delay(_T4);
}
return static_cast<bool>(bit);
}
void HX711::_readBits(std::int32_t* const v) {
std::lock_guard<std::mutex> lock(this->_commLock);
//The datasheet notes a tiny delay between DOUT going low and the
//initial clock pin change
if(this->_useDelays) {
Utility::delay(_T1);
}
//msb first
for(auto i = decltype(_BITS_PER_CONVERSION_PERIOD){0};
i < _BITS_PER_CONVERSION_PERIOD;
++i) {
*v <<= 1;
*v |= this->_readBit();
}
this->_setInputGainSelection();
}
HX711::HX711(const int dataPin, const int clockPin, const Rate rate) noexcept :
_gpioHandle(-1),
_dataPin(dataPin),
_clockPin(clockPin),
_rate(rate),
_channel(Channel::A),
_gain(Gain::GAIN_128),
_strictTiming(false),
_useDelays(false),
_bitFormat(Format::MSB) {
}
HX711::~HX711() {
try {
this->disconnect();
}
catch(...) {
//do not allow propagation
}
}
void HX711::connect() {
if(this->_gpioHandle >= 0) {
return;
}
this->_gpioHandle = Utility::openGpioHandle(0);
Utility::openGpioInput(this->_gpioHandle, this->_dataPin);
Utility::openGpioOutput(this->_gpioHandle, this->_clockPin);
this->setConfig(this->_channel, this->_gain);
}
void HX711::disconnect() {
if(this->_gpioHandle < 0) {
return;
}
Utility::closeGpioPin(this->_gpioHandle, this->_clockPin);
Utility::closeGpioPin(this->_gpioHandle, this->_dataPin);
Utility::closeGpioHandle(this->_gpioHandle);
this->_gpioHandle = -1;
}
void HX711::setStrictTiming(const bool strict) noexcept {
std::lock_guard<std::mutex> lock(this->_commLock);
this->_strictTiming = strict;
}
bool HX711::isStrictTiming() const noexcept {
return this->_strictTiming;
}
void HX711::useDelays(const bool use) noexcept {
this->_useDelays = use;
}
bool HX711::isUsingDelays() const noexcept {
return this->_useDelays;
}
void HX711::setFormat(const Format bitFormat) noexcept {
std::lock_guard<std::mutex> lock(this->_commLock);
this->_bitFormat = bitFormat;
}
Format HX711::getFormat() const noexcept {
return this->_bitFormat;
}
int HX711::getDataPin() const noexcept {
return this->_dataPin;
}
int HX711::getClockPin() const noexcept {
return this->_clockPin;
}
Channel HX711::getChannel() const noexcept {
return this->_channel;
}
Gain HX711::getGain() const noexcept {
return this->_gain;
}
void HX711::setConfig(const Channel c, const Gain g) {
if(c == Channel::A && g == Gain::GAIN_32) {
throw std::invalid_argument("Channel A can only use a gain of 128 or 64");
}
else if(c == Channel::B && g != Gain::GAIN_32) {
throw std::invalid_argument("Channel B can only use a gain of 32");
}
const auto backupChannel = this->_channel;
const auto backupGain = this->_gain;
this->_channel = c;
this->_gain = g;
/**
* If the attempt to set the gain fails, it should
* revert back to whatever it was before
*/
try {
/**
* A read must take place to set the gain at the
* hardware level. See datasheet pg. 4 "Serial
* Interface".
*
* TODO: this is an inefficient busy-wait. Solutions?
*/
while(!this->isReady());
this->readValue();
/**
* If PD_SCK pulse number is changed during
* the current conversion period, power down should
* be executed after current conversion period is
* completed. This is to ensure that the change is
* saved. When chip returns back to normal
* operation from power down, it will return to the
* set up conditions of the last change.
*
* Datasheet pg. 5
*/
this->powerDown();
this->powerUp();
}
catch(const TimeoutException& e) {
this->_channel = backupChannel;
this->_gain = backupGain;
throw;
}
}
bool HX711::isReady() const {
/**
* HX711 will be "ready" when DOUT is low.
* "Ready" means "data is ready for retrieval".
* Datasheet pg. 4
*
* This should be a one-shot test. Any follow-ups
* or looping for checking if the sensor is ready
* over time can/should be done by other calling code
*/
try {
return Utility::readGpio(
this->_gpioHandle,
this->_dataPin) == GpioLevel::LOW;
}
catch(const GpioException& ex) {
return false;
}
}
Value HX711::readValue() {
std::int32_t v = 0;
this->_readBits(&v);
if(this->_bitFormat == Format::LSB) {
v = Utility::reverseBits(v);
}
return Value(_convertFromTwosComplement(v));
}
void HX711::powerDown() {
std::lock_guard<std::mutex> lock(this->_commLock);
/**
* The delay between low to high is probably not necessary, but it
* should help to keep the underlying code from optimising it away -
* if does at all.
*/
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::LOW);
Utility::delay(std::chrono::microseconds(1));
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::HIGH);
/**
* "When PD_SCK pin changes from low to high
* and stays at high for longer than 60µs, HX711
* enters power down mode (Fig.3)."
* Datasheet pg. 5
*/
Utility::sleep(_POWER_DOWN_TIMEOUT);
}
void HX711::powerUp() {
std::lock_guard<std::mutex> lock(this->_commLock);
/**
* "When PD_SCK returns to low,
* chip will reset and enter normal operation mode"
* Datasheet pg. 5
*/
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::LOW);
/**
* "Settling time refers to the time from power up, reset,
* input channel change and gain change to valid stable output data."
* Datasheet pg. 3
*/
if(this->_rate != Rate::OTHER) {
Utility::sleep(_SETTLING_TIMES.at(this->_rate));
}
}
};