-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathHX711.h
139 lines (112 loc) · 3.82 KB
/
HX711.h
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
// 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.
#ifndef HX711_HX711_H_670BFDCD_DA15_4F8B_A15C_0F0043905889
#define HX711_HX711_H_670BFDCD_DA15_4F8B_A15C_0F0043905889
#include <chrono>
#include <cstdint>
#include <mutex>
#include <unordered_map>
#include "Value.h"
namespace HX711 {
/**
* Datasheet
* https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf
*/
enum class Channel : unsigned char {
A,
B
};
/**
* Datasheet pg. 4
*/
enum class Gain : unsigned char {
GAIN_128,
GAIN_32,
GAIN_64
};
/**
* Datasheet pg. 3
* OTHER is to be used when an external clock (ie. crystal) is used
*/
enum class Rate : unsigned char {
HZ_10,
HZ_80,
OTHER
};
enum class Format : unsigned char {
MSB,
LSB
};
class HX711 {
protected:
static const unsigned char _BITS_PER_CONVERSION_PERIOD = 24;
static const std::unordered_map<const Gain, const unsigned char> _PULSES;
static constexpr auto _T1 = std::chrono::nanoseconds(100);
static constexpr auto _T2 = std::chrono::nanoseconds(100);
static constexpr auto _T3 = std::chrono::nanoseconds(200);
static constexpr auto _T4 = std::chrono::nanoseconds(200);
static constexpr auto _POWER_DOWN_TIMEOUT = std::chrono::microseconds(60);
static const std::unordered_map<const Rate, const std::chrono::milliseconds> _SETTLING_TIMES;
int _gpioHandle;
const int _dataPin;
const int _clockPin;
const Rate _rate;
std::mutex _commLock;
Channel _channel;
Gain _gain;
bool _strictTiming;
bool _useDelays;
Format _bitFormat;
static val_t _convertFromTwosComplement(const val_t val) noexcept;
static unsigned char _calculatePulses(const Gain g) noexcept;
void _setInputGainSelection();
bool _readBit() const;
void _readBits(val_t* const v);
public:
HX711(
const int dataPin,
const int clockPin,
const Rate rate = Rate::HZ_10) noexcept;
HX711(const HX711& that) = delete;
HX711& operator=(const HX711& that) = delete;
virtual ~HX711();
void connect();
void disconnect();
void setStrictTiming(const bool strict) noexcept;
bool isStrictTiming() const noexcept;
void useDelays(const bool use) noexcept;
bool isUsingDelays() const noexcept;
Format getFormat() const noexcept;
void setFormat(const Format bitFormat) noexcept;
int getDataPin() const noexcept;
int getClockPin() const noexcept;
Channel getChannel() const noexcept;
Gain getGain() const noexcept;
void setConfig(const Channel c = Channel::A, const Gain g = Gain::GAIN_128);
bool isReady() const;
virtual bool waitReady(const std::chrono::nanoseconds timeout = std::chrono::seconds(1)) const;
Value readValue();
void powerDown();
void powerUp();
};
};
#endif