|
@@ -0,0 +1,148 @@
|
|
1
|
+/*
|
|
2
|
+*
|
|
3
|
+* Author: Rafael da Mata Neri <rafael.neri@gmail.com>
|
|
4
|
+* Copyright (c) 2015 Intel Corporation.
|
|
5
|
+*
|
|
6
|
+*
|
|
7
|
+* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
8
|
+* this software and associated documentation files (the "Software"), to deal in
|
|
9
|
+* the Software without restriction, including without limitation the rights to
|
|
10
|
+* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
11
|
+* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
12
|
+* subject to the following conditions:
|
|
13
|
+*
|
|
14
|
+* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+* copies or substantial portions of the Software.
|
|
16
|
+*
|
|
17
|
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
19
|
+* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
20
|
+* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
21
|
+* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
22
|
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+*/
|
|
24
|
+#include <iostream>
|
|
25
|
+#include <fstream>
|
|
26
|
+#include <unistd.h>
|
|
27
|
+#include <stdlib.h>
|
|
28
|
+#include <string.h>
|
|
29
|
+#include "hx711.h"
|
|
30
|
+
|
|
31
|
+using namespace upm;
|
|
32
|
+using namespace std;
|
|
33
|
+
|
|
34
|
+struct HX711Exception : public std::exception {
|
|
35
|
+ std::string message;
|
|
36
|
+ HX711Exception (std::string msg) : message (msg) { }
|
|
37
|
+ ~HX711Exception () throw () { }
|
|
38
|
+ const char* what() const throw () { return message.c_str(); }
|
|
39
|
+};
|
|
40
|
+
|
|
41
|
+HX711::HX711(uint8_t data, uint8_t sck, uint8_t gain) {
|
|
42
|
+ mraa_result_t error = MRAA_SUCCESS;
|
|
43
|
+
|
|
44
|
+ this->m_dataPinCtx = mraa_gpio_init(data);
|
|
45
|
+ if (this->m_dataPinCtx == NULL) {
|
|
46
|
+ throw HX711Exception ("Couldn't initilize DATA pin.");
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ this->m_sckPinCtx = mraa_gpio_init(sck);
|
|
50
|
+ if (this->m_sckPinCtx == NULL) {
|
|
51
|
+ throw HX711Exception ("Couldn't initilize CLOCK pin.");
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ error = mraa_gpio_dir (this->m_dataPinCtx, MRAA_GPIO_IN);
|
|
55
|
+ if (error != MRAA_SUCCESS) {
|
|
56
|
+ throw HX711Exception ("Couldn't set direction for DATA pin.");
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ error = mraa_gpio_dir (this->m_sckPinCtx, MRAA_GPIO_OUT);
|
|
60
|
+ if (error != MRAA_SUCCESS) {
|
|
61
|
+ throw HX711Exception ("Couldn't set direction for CLOCK pin.");
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ this->setGain(gain);
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+HX711::~HX711() {
|
|
68
|
+ mraa_result_t error = MRAA_SUCCESS;
|
|
69
|
+
|
|
70
|
+ error = mraa_gpio_close (this->m_dataPinCtx);
|
|
71
|
+ if (error != MRAA_SUCCESS) {
|
|
72
|
+ mraa_result_print(error);
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ error = mraa_gpio_close (this->m_sckPinCtx);
|
|
76
|
+ if (error != MRAA_SUCCESS) {
|
|
77
|
+ mraa_result_print(error);
|
|
78
|
+ }
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+unsigned long HX711::read() {
|
|
82
|
+ unsigned long Count = 0;
|
|
83
|
+
|
|
84
|
+ while (mraa_gpio_read(this->m_dataPinCtx));
|
|
85
|
+
|
|
86
|
+ for (int i=0; i<GAIN; i++)
|
|
87
|
+ {
|
|
88
|
+ mraa_gpio_write(this->m_sckPinCtx, 1);
|
|
89
|
+ Count = Count << 1;
|
|
90
|
+ mraa_gpio_write(this->m_sckPinCtx, 0);
|
|
91
|
+ if(mraa_gpio_read(this->m_dataPinCtx))
|
|
92
|
+ {
|
|
93
|
+ Count++;
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ mraa_gpio_write(this->m_sckPinCtx, 1);
|
|
98
|
+ Count = Count ^ 0x800000;
|
|
99
|
+ mraa_gpio_write(this->m_sckPinCtx, 0);
|
|
100
|
+
|
|
101
|
+ return (Count);
|
|
102
|
+}
|
|
103
|
+
|
|
104
|
+void HX711::setGain(uint8_t gain){
|
|
105
|
+ switch (gain) {
|
|
106
|
+ case 128: // channel A, gain factor 128
|
|
107
|
+ GAIN = 24;
|
|
108
|
+ break;
|
|
109
|
+ case 64: // channel A, gain factor 64
|
|
110
|
+ GAIN = 26;
|
|
111
|
+ break;
|
|
112
|
+ case 32: // channel B, gain factor 32
|
|
113
|
+ GAIN = 25;
|
|
114
|
+ break;
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ mraa_gpio_write(this->m_sckPinCtx, 0);
|
|
118
|
+ read();
|
|
119
|
+}
|
|
120
|
+
|
|
121
|
+unsigned long HX711::readAverage(uint8_t times){
|
|
122
|
+ unsigned long sum = 0;
|
|
123
|
+ for (uint8_t i = 0; i < times; i++) {
|
|
124
|
+ sum += read();
|
|
125
|
+ }
|
|
126
|
+ return sum / times;
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+double HX711::getValue(uint8_t times){
|
|
130
|
+ return readAverage(times) - OFFSET;
|
|
131
|
+}
|
|
132
|
+
|
|
133
|
+float HX711::getUnits(uint8_t times){
|
|
134
|
+ return getValue(times) / SCALE;
|
|
135
|
+}
|
|
136
|
+
|
|
137
|
+void HX711::tare(uint8_t times){
|
|
138
|
+ double sum = readAverage(times);
|
|
139
|
+ setOffset(sum);
|
|
140
|
+}
|
|
141
|
+
|
|
142
|
+void HX711::setScale(float scale){
|
|
143
|
+ SCALE = scale;
|
|
144
|
+}
|
|
145
|
+
|
|
146
|
+void HX711::setOffset(long offset){
|
|
147
|
+ OFFSET = offset;
|
|
148
|
+}
|