From 67d01a9393a62306adda2854a504e27a56fb9bf3 Mon Sep 17 00:00:00 2001 From: Mihai Tudor Panu Date: Fri, 9 Sep 2016 18:33:30 -0700 Subject: [PATCH] temperature: added C sources and removed grove dependency Signed-off-by: Mihai Tudor Panu --- examples/c++/CMakeLists.txt | 2 +- .../c++/{grovetemp.cxx => temperature.cxx} | 2 +- examples/java/CMakeLists.txt | 2 +- ...TempSample.java => TemperatureSample.java} | 4 +- .../{grovetemp.js => temperature.js} | 6 +- .../python/{grovetemp.py => temperature.py} | 4 +- include/fti/upm_temperature.h | 1 + src/temperature/CMakeLists.txt | 8 ++ src/temperature/javaupm_temperature.i | 19 +++ src/temperature/jsupm_temperature.i | 7 ++ src/temperature/pyupm_temperature.i | 9 ++ src/temperature/temperature.c | 77 ++++++++++++ src/temperature/temperature.cxx | 67 ++++++++++ src/temperature/temperature.h | 52 ++++++++ src/temperature/temperature.hpp | 104 +++++++++++++++ src/temperature/temperature_fti.c | 118 ++++++++++++++++++ 16 files changed, 472 insertions(+), 10 deletions(-) rename examples/c++/{grovetemp.cxx => temperature.cxx} (97%) rename examples/java/{GroveTempSample.java => TemperatureSample.java} (93%) rename examples/javascript/{grovetemp.js => temperature.js} (94%) rename examples/python/{grovetemp.py => temperature.py} (96%) create mode 100644 src/temperature/CMakeLists.txt create mode 100644 src/temperature/javaupm_temperature.i create mode 100644 src/temperature/jsupm_temperature.i create mode 100644 src/temperature/pyupm_temperature.i create mode 100644 src/temperature/temperature.c create mode 100644 src/temperature/temperature.cxx create mode 100644 src/temperature/temperature.h create mode 100644 src/temperature/temperature.hpp create mode 100644 src/temperature/temperature_fti.c diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 0b2cf3e8..6a4e7673 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -90,7 +90,7 @@ add_example (hmc5883l) add_example (groveled) add_example (groverelay) add_example (light) -add_example (grovetemp) +add_example (temperature) add_example (grovebutton) add_example (groverotary) add_example (slide) diff --git a/examples/c++/grovetemp.cxx b/examples/c++/temperature.cxx similarity index 97% rename from examples/c++/grovetemp.cxx rename to examples/c++/temperature.cxx index 2a21bc15..93754828 100644 --- a/examples/c++/grovetemp.cxx +++ b/examples/c++/temperature.cxx @@ -34,7 +34,7 @@ main(int argc, char **argv) //! [Interesting] // Create the temperature sensor object using AIO pin 0 - upm::GroveTemp* temp = new upm::GroveTemp(0); + upm::Temperature* temp = new upm::Temperature(0); std::cout << temp->name() << std::endl; // Read the temperature ten times, printing both the Celsius and diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index 9d718fb7..55c9a9fb 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -51,7 +51,7 @@ add_example(GroveRotarySample grove) add_example(GROVESCAMSample grovescam) add_example(SlideSample grove) add_example(GroveSpeakerSample grovespeaker) -add_example(GroveTempSample grove) +add_example(TemperatureSample temperature) add_example(VDivSample vdiv) add_example(GroveWaterSample grovewater) add_example(GroveWFSSample grovewfs) diff --git a/examples/java/GroveTempSample.java b/examples/java/TemperatureSample.java similarity index 93% rename from examples/java/GroveTempSample.java rename to examples/java/TemperatureSample.java index 68788273..8cfe059f 100644 --- a/examples/java/GroveTempSample.java +++ b/examples/java/TemperatureSample.java @@ -22,10 +22,10 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -public class GroveTempSample { +public class TemperatureSample { public static void main (String args[]) throws InterruptedException { //! [Interesting] - upm_grove.GroveTemp temp = new upm_grove.GroveTemp(3); + upm_temperature.Temperature temp = new upm_temperature.Temperature(3); for (int i = 0; i < 10; ++i) { diff --git a/examples/javascript/grovetemp.js b/examples/javascript/temperature.js similarity index 94% rename from examples/javascript/grovetemp.js rename to examples/javascript/temperature.js index dba3954c..93340fc1 100644 --- a/examples/javascript/grovetemp.js +++ b/examples/javascript/temperature.js @@ -22,11 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -// Load Grove module -var groveSensor = require('jsupm_grove'); +// Load temperature module +var upm = require('jsupm_temperature'); // Create the temperature sensor object using AIO pin 0 -var temp = new groveSensor.GroveTemp(0); +var temp = new upm.Temperature(0); console.log(temp.name()); // Read the temperature ten times, printing both the Celsius and diff --git a/examples/python/grovetemp.py b/examples/python/temperature.py similarity index 96% rename from examples/python/grovetemp.py rename to examples/python/temperature.py index bd9cc244..7182349b 100644 --- a/examples/python/grovetemp.py +++ b/examples/python/temperature.py @@ -22,10 +22,10 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import time -import pyupm_grove as grove +import pyupm_temperature as upm # Create the temperature sensor object using AIO pin 0 -temp = grove.GroveTemp(0) +temp = upm.Temperature(0) print temp.name() # Read the temperature ten times, printing both the Celsius and diff --git a/include/fti/upm_temperature.h b/include/fti/upm_temperature.h index e793ce08..c031885d 100644 --- a/include/fti/upm_temperature.h +++ b/include/fti/upm_temperature.h @@ -34,6 +34,7 @@ typedef enum _upm_temperature_u {CELSIUS, FAHRENHEIT, KELVIN} upm_temperature_u; // Temperature function table typedef struct _upm_temperature_ft { upm_result_t (*upm_temperature_set_scale) (void* dev, float scale); + upm_result_t (*upm_temperature_set_offset) (void* dev, float offset); upm_result_t (*upm_temperature_get_value) (void* dev, float* value, upm_temperature_u unit); } upm_temperature_ft; diff --git a/src/temperature/CMakeLists.txt b/src/temperature/CMakeLists.txt new file mode 100644 index 00000000..7125e459 --- /dev/null +++ b/src/temperature/CMakeLists.txt @@ -0,0 +1,8 @@ +upm_mixed_module_init (NAME temperature + DESCRIPTION "Temperature Sensor" + C_HDR temperature.h + C_SRC temperature.c + CPP_HDR temperature.hpp + CPP_SRC temperature.cxx + FTI_SRC temperature_fti.c + REQUIRES mraa) \ No newline at end of file diff --git a/src/temperature/javaupm_temperature.i b/src/temperature/javaupm_temperature.i new file mode 100644 index 00000000..567b7f7b --- /dev/null +++ b/src/temperature/javaupm_temperature.i @@ -0,0 +1,19 @@ +%module (docstring="Temperature sensor") javaupm_temperature + +%include "../upm.i" + +%include "temperature.hpp" +%{ + #include "temperature.hpp" +%} + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_temperature"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/temperature/jsupm_temperature.i b/src/temperature/jsupm_temperature.i new file mode 100644 index 00000000..41f3277d --- /dev/null +++ b/src/temperature/jsupm_temperature.i @@ -0,0 +1,7 @@ +%module jsupm_temperature +%include "../upm.i" + +%include "temperature.hpp" +%{ + #include "temperature.hpp" +%} diff --git a/src/temperature/pyupm_temperature.i b/src/temperature/pyupm_temperature.i new file mode 100644 index 00000000..1242d4d7 --- /dev/null +++ b/src/temperature/pyupm_temperature.i @@ -0,0 +1,9 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_temperature +%include "../upm.i" + +%include "temperature.hpp" +%{ + #include "temperature.hpp" +%} diff --git a/src/temperature/temperature.c b/src/temperature/temperature.c new file mode 100644 index 00000000..19e1a78d --- /dev/null +++ b/src/temperature/temperature.c @@ -0,0 +1,77 @@ +/* + * Author: Sisinty Sasmita Patra + * + * Copyright (c) 2016 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 +#include + +#include "temperature.h" +#include "upm.h" + +temperature_context temperature_init(int pin) +{ + temperature_context dev = + (temperature_context)malloc(sizeof(struct _temperature_context)); + + if (dev == NULL) return NULL; + + dev->aio = mraa_aio_init(pin); + if(dev->aio == NULL) + { + free(dev); + return NULL; + } + + dev->m_aRes = (1 << mraa_aio_get_bit(dev->aio)); + + return dev; +} + +void temperature_close(temperature_context dev) +{ + mraa_aio_close(dev->aio); + free(dev); +} + +upm_result_t temperature_get_value(temperature_context dev, + float* tempval) +{ + float val = 0.0; + val = (float)mraa_aio_read(dev->aio); + + if (val == -1.0) + { + return UPM_ERROR_OPERATION_FAILED; + } + + // From old C++ UPM code, needs rework!!! + float r = ((float)dev->m_aRes - val) * 10000.0 / val; + float t = 1.0 / (log(r / 10000.0) / 3975.0 + 1.0 / 298.15) -273.15; + + // Celsius + *tempval = t; + + return UPM_SUCCESS; +} diff --git a/src/temperature/temperature.cxx b/src/temperature/temperature.cxx new file mode 100644 index 00000000..0e6973a3 --- /dev/null +++ b/src/temperature/temperature.cxx @@ -0,0 +1,67 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 +#include +#include + +#include "temperature.hpp" +#include "math.h" + +using namespace upm; + +Temperature::Temperature(unsigned int pin, float scale, int r0, int b) +{ + if ( !(m_aio = mraa_aio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_aio_init() failed, invalid pin?"); + return; + } + m_scale = scale; + m_r0 = r0; + m_b = b; +} + +Temperature::~Temperature() +{ + mraa_aio_close(m_aio); +} + +int Temperature::value () +{ + float a = (float) mraa_aio_read(m_aio); + if (a == -1.0) return -1; + // Apply scale factor after error check + a *= m_scale; + float r = (float)(1023.0-a)*(float)m_r0/a; + float t = 1.0/(log(r/(float)m_r0)/(float)m_b + 1.0/298.15)-273.15; + return (int) round(t); +} + +float Temperature::raw_value() +{ + return (float) mraa_aio_read(m_aio); +} diff --git a/src/temperature/temperature.h b/src/temperature/temperature.h new file mode 100644 index 00000000..2eaaa556 --- /dev/null +++ b/src/temperature/temperature.h @@ -0,0 +1,52 @@ +/* + * Author: Sisinty Sasmita Patra + * + * Copyright (c) 2016 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. + */ + +#ifndef TEMPERATURE_H_ +#define TEMPERATURE_H_ + +#include +#include + +#include "upm.h" +#include "mraa/aio.h" + +/** + * driver context + */ +typedef struct _temperature_context { + mraa_aio_context aio; + int16_t m_aRes; +} *temperature_context; + + +temperature_context temperature_init(int pin); + +void temperature_close(temperature_context dev); + +// Celsius +upm_result_t temperature_get_value(temperature_context dev, + float* tempval); + +#endif /* TEMPERATURE_H_ */ diff --git a/src/temperature/temperature.hpp b/src/temperature/temperature.hpp new file mode 100644 index 00000000..1c602a63 --- /dev/null +++ b/src/temperature/temperature.hpp @@ -0,0 +1,104 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 +#include + +namespace upm { +/** + * @brief Temperature sensor library + * @defgroup temperature libupm-temperature + * @ingroup seeed analog temp gsk + */ +/** + * @library temperature + * @sensor temperature + * @comname Temperature Sensor + * @altname Grove Temperature Sensor + * @type temp + * @man seeed + * @con analog + * @kit gsk + * + * @brief API for the Grove Temperature Sensor + * + * Basic UPM module for the Grove temperature sensor on analog. This sensor + * uses a thermistor to measure ambient temperature. The conversion formula has + * been updated to work with versions 1.1 and 1.2 of the sensor. For the older + * v1.0 sensor you will have to specify R0 and B values when initializing the + * device. The range of this sensor is -40 to 125 C and accuracy is +/- 1.5 C. + * + * @image html grovetemp.jpg + * @snippet temperature.cxx Interesting + */ +class Temperature { + public: + /** + * Grove analog temperature sensor constructor + * + * @param pin Analog pin to use + * @param scale Scaling factor for raw analog value from the ADC, + * useful for mixed 3.3V/5V boards, default 1.0 + * @param r0 zero power resistance, this is 100K (default) for + * v1.1-v1.2 and 10K for v1.0 of the sensor + * @param b thermistor nominal B constant, this is 4275 (default) for + * v1.1-v1.2 and 3975 for v1.0 of the sensor + */ + Temperature(unsigned int pin, float scale = 1.0, int r0 = 100000, int b = 4275); + /** + * Temperature destructor + */ + ~Temperature(); + /** + * Gets the raw value from the AIO pin + * + * @return Raw value from the ADC + */ + float raw_value(); + /* Provided for compatibility with old grove base class + * + * @return Sensor name + */ + std::string name() + { + return "Temperature sensor"; + } + /** + * Gets the temperature in Celsius from the sensor + * + * @return Normalized temperature in Celsius + */ + int value(); + private: + mraa_aio_context m_aio; + float m_scale; + int m_r0; + int m_b; + +}; +} diff --git a/src/temperature/temperature_fti.c b/src/temperature/temperature_fti.c new file mode 100644 index 00000000..6a1da381 --- /dev/null +++ b/src/temperature/temperature_fti.c @@ -0,0 +1,118 @@ +/* + * Author: Sisinty Sasmita Patra + * + * Copyright (c) 2016 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 +#include + +#include "temperature.h" +#include "upm_fti.h" +#include "upm_sensor.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_temperature_name[] = "Temperature Sensor"; +const char upm_temperature_description[] = "Analog Temperature Sensor"; +const upm_protocol_t upm_temperature_protocol[] = {UPM_ANALOG}; +const upm_sensor_t upm_temperature_category[] = {UPM_TEMPERATURE}; + +// forward declarations +const upm_sensor_descriptor_t upm_temperature_get_descriptor(); +const void* upm_temperature_get_ft(upm_sensor_t sensor_type); +upm_result_t upm_temperature_get_value_temperature(void* dev, float* tempval, + upm_temperature_u unit); +void* upm_temperature_init_name(); +void upm_temperature_close(void* dev); + +const upm_sensor_descriptor_t upm_temperature_get_descriptor() { + upm_sensor_descriptor_t usd; + usd.name = upm_temperature_name; + usd.description = upm_temperature_description; + usd.protocol_size = 1; + usd.protocol = upm_temperature_protocol; + usd.category_size = 1; + usd.category = upm_temperature_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_temperature_init_name, + .upm_sensor_close = &upm_temperature_close, + .upm_sensor_get_descriptor = &upm_temperature_get_descriptor +}; + +static const upm_temperature_ft tft = +{ + .upm_temperature_get_value = &upm_temperature_get_value_temperature +}; + +const void* upm_temperature_get_ft(upm_sensor_t sensor_type) { + if(sensor_type == UPM_SENSOR) { + return &ft; + } + if(sensor_type == UPM_TEMPERATURE) { + return &tft; + } + return NULL; +} + +void* upm_temperature_init_name(){ + return NULL; +} + +void upm_temperature_close(void* dev) +{ + temperature_close((temperature_context)dev); +} + +upm_result_t upm_temperature_get_value_temperature(void* dev, float* tempval, + upm_temperature_u unit) +{ + float temp = 0.0; + upm_result_t rv = temperature_get_value((temperature_context)dev, &temp); + + if (rv != UPM_SUCCESS) + return rv; + + switch (unit) + { + case CELSIUS: + *tempval = temp; + return UPM_SUCCESS; + + case KELVIN: + *tempval = temp + 273.15; + return UPM_SUCCESS; + + case FAHRENHEIT: + *tempval = temp * (9.0/5.0) + 32.0; + return UPM_SUCCESS; + } + + return UPM_ERROR_INVALID_PARAMETER; +}