1 /*
2  * Author: Rafael da Mata Neri <rafael.neri@gmail.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #pragma once
23 
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <mraa/gpio.h>
29 
30 namespace upm {
31      /**
32       * @brief HX711 24-bit ADC library
33       * @defgroup hx711 libupm-hx711
34       * @ingroup generic gpio electric
35       */
36 
37      /**
38       * @library hx711
39       * @sensor hx711
40       * @comname HX711 Analog-to-Digital Converter
41       * @type electric
42       * @man generic
43       * @web http://www.dfrobot.com/image/data/SEN0160/hx711_english.pdf
44       * @con gpio
45       *
46       * @brief API for the HX711 Analog-to-Digital Converter
47       *
48       * HX711 is a precision 24-bit analog-to-digital converter (ADC)
49       * designed for weight scales and industrial control applications to
50       * interface directly with a bridge sensor. This module was tested on
51       * the Intel(R) Galileo Gen 2 board.
52       *
53       * @image html hx711.jpeg
54       * @snippet hx711.cxx Interesting
55       */
56       class HX711 {
57       public:
58             /**
59             * HX711 constructor
60             *
61             * @param data Defines the data pin
62             * @param sck Defines the clock pin
63             * @param gain Defines the gain factor
64             * Valid values are 128 or 64 for channel A; channel B works with a 32-gain factor only
65             */
66             HX711(uint8_t data, uint8_t sck, uint8_t gain = 128);
67 
68             /**
69             * HX711 destructor
70             */
71             ~HX711();
72 
73             /**
74             * Waits for the chip to be ready and returns a reading
75             *
76             * @return Raw ADC reading
77             */
78             unsigned long read();
79 
80             /**
81             * Sets the gain factor; takes effect only after a call to read()
82             * channel A can be set for a 128 or 64 gain; channel B has a fixed 32-gain
83             * factor depending on the parameter; the channel is also set to either A or B
84             * @param gain Defines the gain factor
85             */
86             void setGain(uint8_t gain = 128);
87 
88             /**
89             * Returns an average reading
90             * @param times Defines how many reading to do
91             * @return Average reading
92             */
93             unsigned long readAverage(uint8_t times = 10);
94 
95             /**
96             * Returns (readAverage() - OFFSET)
97             * @param times Defines how many readings to do
98             * @return Current value without the tare weight
99             */
100             double getValue(uint8_t times = 10);
101 
102             /**
103             * Returns getValue() divided by SCALE
104             * @param times Defines how many readings to do
105             * @return Raw value divided by a value obtained via calibration
106             */
107             float getUnits(uint8_t times = 1);
108 
109             /**
110             * Sets the OFFSET value for the tare weight
111             * @param times Defines how many times to read the tare value
112             */
113             void tare(uint8_t times = 10);
114 
115             /**
116             * Sets the SCALE value
117             * This value is used to convert the raw data to human-readable data (measurement units)
118             * @param scale Value obtained via calibration
119             */
120             void setScale(float scale = 1.f);
121        private:
122             mraa_gpio_context m_sckPinCtx; // Power Down and Serial Clock Input Pin
123             mraa_gpio_context m_dataPinCtx; // Serial Data Output Pin
124 
125             uint8_t GAIN; // amplification factor
126             unsigned long OFFSET; // used for tare weight
127             float SCALE; // used to return weight in grams, kg, ounces, whatever
128 
129 
130             /**
131             * Sets the OFFSET value
132             * This value is subtracted from the actual reading (tare weight)
133             * @param scale Value obtained via calibration
134             */
135             void setOffset(long offset = 0);
136      };
137 
138 }
139