summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/hx711/hx711.cxx
blob: cf6c987c8f4277a131a54b47ca76393d552e548b (plain)
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
/*
 * Author: Rafael da Mata Neri <rafael.neri@gmail.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * 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 <iostream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#include "hx711.hpp"

using namespace upm;
using namespace std;

HX711::HX711(uint8_t data, uint8_t sck, uint8_t gain) {
    mraa_result_t error = MRAA_SUCCESS;

    this->m_dataPinCtx = mraa_gpio_init(data);
    if (this->m_dataPinCtx == NULL) {
        throw std::invalid_argument(std::string(__FUNCTION__) + 
                                    ": Couldn't initialize DATA pin.");
    }

    this->m_sckPinCtx = mraa_gpio_init(sck);
    if (this->m_sckPinCtx == NULL) {
        throw std::invalid_argument(std::string(__FUNCTION__) + 
                                    ": Couldn't initialize CLOCK pin.");
    }

    error = mraa_gpio_dir (this->m_dataPinCtx, MRAA_GPIO_IN);
    if (error != MRAA_SUCCESS) {
        throw std::invalid_argument(std::string(__FUNCTION__) + 
                                    ": Couldn't set direction for DATA pin.");
    }

    error = mraa_gpio_dir (this->m_sckPinCtx, MRAA_GPIO_OUT);
    if (error != MRAA_SUCCESS) {
        throw std::invalid_argument(std::string(__FUNCTION__) + 
                                    ": Couldn't set direction for CLOCK pin.");
    }

    this->setGain(gain);
}

HX711::~HX711() {
    mraa_result_t error = MRAA_SUCCESS;

    error = mraa_gpio_close (this->m_dataPinCtx);
    if (error != MRAA_SUCCESS) {
        mraa_result_print(error);
    }

    error = mraa_gpio_close (this->m_sckPinCtx);
    if (error != MRAA_SUCCESS) {
        mraa_result_print(error);
    }
}

unsigned long HX711::read() {
    unsigned long Count = 0;

    while (mraa_gpio_read(this->m_dataPinCtx));

    for (int i=0; i<GAIN; i++)
    {
        mraa_gpio_write(this->m_sckPinCtx, 1);
        Count = Count << 1;
        mraa_gpio_write(this->m_sckPinCtx, 0);
        if(mraa_gpio_read(this->m_dataPinCtx))
        {
            Count++;
        }
    }

    mraa_gpio_write(this->m_sckPinCtx, 1);
    Count = Count ^ 0x800000;
    mraa_gpio_write(this->m_sckPinCtx, 0);

    return (Count);
}

void HX711::setGain(uint8_t gain){
    switch (gain) {
        case 128:       // channel A, gain factor 128
            GAIN = 24;
            break;
        case 64:        // channel A, gain factor 64
            GAIN = 26;
            break;
        case 32:        // channel B, gain factor 32
            GAIN = 25;
            break;
    }

    mraa_gpio_write(this->m_sckPinCtx, 0);
    read();
}

unsigned long HX711::readAverage(uint8_t times){
    unsigned long sum = 0;
    for (uint8_t i = 0; i < times; i++) {
        sum += read();
    }
    return sum / times;
}

double HX711::getValue(uint8_t times){
    return readAverage(times) - OFFSET;
}

float HX711::getUnits(uint8_t times){
    return getValue(times) / SCALE;
}

void HX711::tare(uint8_t times){
    double sum = readAverage(times);
    setOffset(sum);
}

void HX711::setScale(float scale){
    SCALE = scale;
}

void HX711::setOffset(long offset){
    OFFSET = offset;
}