upm
0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
|
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.
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:
And then an example file to use & test our lib with in upm/examples/max31855.cxx.
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.
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.
Then we create the header (max31855.h) , 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.
Note that the header contains both the io that we will use, the gpio is in this case used as the chip select pin.
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.
Then we also need to implement a nice cleanup in our 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()
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.
Our final example, very easy to use API!
The we need to add it to the examples/CMakeLists.txt. Only three lines are required
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.