summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/max31855/max31855.cxx
blob: 3037aaf3f90843496c7d7dc5f662d2ba2828d7a8 (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
/*
 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
 * Copyright (c) 2014 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 <string>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>
#include <functional>
#include <string.h>

#include "max31855.h"

using namespace upm;

//! [Constructor]
MAX31855::MAX31855(int bus, int cs)
{
    // initialise chip select as a normal gpio
    if ( !(m_gpio = mraa_gpio_init(cs)) ) 
      {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_gpio_init(cs) failed, invalid pin?");
        return;
      }
    mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);

    // initialise the spi bus with a 2Mhz clock
    m_sensor = mraa_spi_init(bus);
    mraa_spi_frequency(m_sensor, 2000000);
}
//! [Constructor]

//! [Destructor]
MAX31855::~MAX31855()
{
    // close both m_sensor & m_gpio cleanly
    mraa_result_t error;
    error = mraa_spi_stop(m_sensor);
    if (error != MRAA_SUCCESS) {
        mraa_result_print(error);
    }
    error = mraa_gpio_close(m_gpio);
    if (error != MRAA_SUCCESS) {
        mraa_result_print(error);
    }
}
//! [Destructor]

double
MAX31855::getTemp()
{
//! [spi]
    // set chip select low
    mraa_gpio_write(m_gpio, 0);

    uint8_t buf[4];

    // set our input buffer to 0, this is clean but not required
    memset(buf, 0, sizeof(uint8_t)*4);

    // Write buffer to the spi slave
    uint8_t* x = mraa_spi_write_buf(m_sensor, buf, 4);
//! [spi]

//! [conversion]
    // Endian correct way of making our char array into an 32bit int
    int32_t temp = (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3];;

    // mraa_spi_write_buf does not free the return buffer
    free(x);

    if (temp & 0x7) {
        std::cerr << "Something went very wrong!" << std::endl;
    }

    // scrap all the data we dont care about
    temp >>= 18;

    // LSB = 0.25 degrees C
    double c = (double) temp;
    c *= 0.25;
//! [conversion]

    // set chip select high
    mraa_gpio_write(m_gpio, 1);

    return c;
}