upm/docs/max31855.md
Noel Eck 20aa4962f0 SWIG: Moved common SWIG syntax to ${libname}.i
This commit moves common SWIG syntax to a ${libname}.i for sensor
libraries.  Much of the swig content was originally duplicated for
each wrapper language which has lead to inconsistencies between wrappers
over time.  This commit moves all swig syntax to a common file.  Language
specific swig syntax can be added with #ifdef SWIG<LANGUAGE>.

The src/CMakeLists.txt will look first for a language-specific .i file,
then fall back to ${libname}.i.  In this way, it's possible to override
the common ${libname}.i file.  If a fallback .i file does NOT exist,
UPM CMake will generate a simple interface file for all languages.

Example:
    If no src/abp/pyupm_abp.i and no src/abp/abp.i then
    generate ${CMAKE_CURRENT_BINARY_DIR}/abp.i

When src/CMakeLists.txt uses a common ${libname}.i, it adds a -module
<language>upm_${libname} to the swig command line.

In the example below, a -module argument is provided for both Java and
Javascript, while the python module takes all syntax from pyupm_abp.i.

    SWIG FILE              Language       CMake added SWIG args
    ---------------        ----------     ---------------------
    src/abp/abp.i          java           -module javaupm_abp
    src/abp/abp.i          javascript     -module jsupm_abp
    src/abp/pyupm_abp.i    python

This commit removes ~4500 redundant lines for the UPM repository and
helps promote uniformity for the SWIG'ed languages.

Signed-off-by: Noel Eck <noel.eck@intel.com>
2018-02-21 10:51:44 -08:00

97 lines
3.6 KiB
Markdown

Making a UPM module for MAX31855 {#max31855}
================================
The Maxim Integrated MAX31855 is a thermocouple amplifier allowing you to read
from a K type thermocouple. My board comes from the Pmod kit form Maxim
(MAX31855PMB1) but you can get this from many different sources. The adafruit
people made arduino code already so we'll use that as a
[reference](https://github.com/adafruit/Adafruit-MAX31855-library/blob/master/Adafruit_MAX31855.cpp).
### Basics
This is a spi module so we will use the mraa spi functions to build our module.
First thing to do is to create a tree structure like this in upm/src/max31855:
* max31855.cxx
* max31855.hpp
* max31855.i
* CMakeLists.txt
And then an example file to use & test our lib with in upm/examples/max31855.cxx.
### Swig
The .i files are used by swig, there is one for each python & javascript. They
contain essentially the same thing and are very simple. The only thing to
change between the javascript & node.js one is the argument to %module.
@snippet jsupm_max31855.i Interesting
The %include parameter defines which functions will be available to the
node/python module created, Whilst the headers inside %{} will be explicitly
required during compilation. Typically only the top level header is required in
either of those args. The upm.i is just a shortcut to include some commonly
used swig wrappers for UPM sensors, it's not obligatory but recommended.
### API
Then we create the header (max31855.hpp) , a very simple header in our case we
will have only a very basic api. We provide a getTemp() function which will
return the same type as in the arduino library, a double.
@snippet max31855.hpp Interesting
Note that the header contains both the io that we will use, the gpio is in this
case used as the chip select pin.
### Implementing our API
In the adafruit library the read function (our chip is a 3pin SPI so only read
is possible), the spiread32() does all the work. It starts by setting up the io
so we will do the same in our constructor.
Note unlike on Arduino, we'll just set a 2Mhz clock and let the chip do the
work.
@snippet src/max31855/max31855.cxx Constructor
Then we also need to implement a nice cleanup in our destructor.
@snippet src/max31855/max31855.cxx Destructor
Then to read data, we will use spi_write_buf which will allow us to write a
whole uint32_t in order to get one back, which is what the arduino code does in
spiread32. Obviously we set our chip select to low first. Here is the start of
the implementation of MAX31855::getTemp()
@snippet src/max31855/max31855.cxx spi
Then using the arduino code as reference we simply reconstruct form the 4
uint8_t values a 32bit int value and select only the valuable parts of
information from that. The MAX31855 datasheet explains exactly which bits are
useful, we will just do the same as the adafruit code, first checking the error
bit and then scrapping everything but the 14bit of thermocouple data that are
useful to us and converting it to a double.
@snippet src/max31855/max31855.cxx conversion
### Finalizing
Our final example, very easy to use API!
@snippet examples/c++/max31855.cxx Interesting
### Building
The we need to add it to the examples/CMakeLists.txt. Only three lines are required
~~~~~~~~~~~
add_executable (max31855-example max31855.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/max31855)
target_link_libraries (max31855-example max31855 ${CMAKE_THREAD_LIBS_INIT})
~~~~~~~~~~~
Note you don't have to rebuild everything, cmake keeps target lists so if you
named your example target modulename-example you can simply do make
max31855-example and both the library & example will build.