max31855: add sensor and documentation on creation of a UPM sensor

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-06-13 16:44:48 +01:00
parent 3e12ed6719
commit 2c1baf66b5
10 changed files with 340 additions and 2 deletions

View File

@ -0,0 +1,5 @@
set (libname "max31855")
set (libdescription "K type thermistor amplifier")
set (module_src ${libname}.cxx)
set (module_h ${libname}.h)
upm_module_init()

View File

@ -0,0 +1,9 @@
//! [Interesting]
%module jsupm_max31855
%{
#include "max31855.h"
%}
%include "max31855.h"
//! [Interesting]

103
src/max31855/max31855.cxx Normal file
View File

@ -0,0 +1,103 @@
/*
* 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 <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
m_gpio = maa_gpio_init(cs);
maa_gpio_dir(m_gpio, MAA_GPIO_OUT);
// initialise the spi bus with a 2Mhz clock
m_sensor = maa_spi_init(bus);
maa_spi_frequency(m_sensor, 2000000);
}
//! [Constructor]
//! [Destructor]
MAX31855::~MAX31855()
{
// close both m_sensor & m_gpio cleanly
maa_result_t error;
error = maa_spi_stop(m_sensor);
if (error != MAA_SUCCESS) {
maa_result_print(error);
}
error = maa_gpio_close(m_gpio);
if (error != MAA_SUCCESS) {
maa_result_print(error);
}
}
//! [Destructor]
double
MAX31855::getTemp()
{
//! [spi]
// set chip select low
maa_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 = maa_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];;
// maa_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
maa_gpio_write(m_gpio, 1);
return c;
}

69
src/max31855/max31855.h Normal file
View File

@ -0,0 +1,69 @@
/*
* 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.
*/
#pragma once
#include <string>
#include <maa/spi.h>
#include <maa/gpio.h>
namespace upm {
/**
* @brief C++ API for MAX31855
*
* This file defines the max31855 SPI sensor
*
* @snippet examples/max31855.cxx Interesting
*
*/
//! [Interesting]
class MAX31855 {
public:
/**
* Instanciates a MAX31855 object
*
* @param bus The spi bus to use
* @param cs The chip select pin
*/
MAX31855(int bus, int cs);
/**
* MAX31855 object destructor
*/
~MAX31855();
/**
* Get the distance from the sensor
*
* @return value in degrees celcius
*/
double getTemp();
private:
maa_spi_context m_sensor;
maa_gpio_context m_gpio;
};
//! [Interesting]
}

View File

@ -0,0 +1,10 @@
%module pyupm_max31855
%include "stdint.i"
%feature("autodoc", "3");
%include "max31855.h"
%{
#include "max31855.h"
%}