temperature: added C sources and removed grove dependency

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Mihai Tudor Panu 2016-09-09 18:33:30 -07:00 committed by Noel Eck
parent af61f51e48
commit 67d01a9393
16 changed files with 472 additions and 10 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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)

View File

@ -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);
}
}
%}

View File

@ -0,0 +1,7 @@
%module jsupm_temperature
%include "../upm.i"
%include "temperature.hpp"
%{
#include "temperature.hpp"
%}

View File

@ -0,0 +1,9 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_temperature
%include "../upm.i"
%include "temperature.hpp"
%{
#include "temperature.hpp"
%}

View File

@ -0,0 +1,77 @@
/*
* Author: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#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;
}

View File

@ -0,0 +1,67 @@
/*
* Authors: Brendan Le Foll <brendan.le.foll@intel.com>
* Mihai Tudor Panu <mihai.tudor.panu@intel.com>
* Sarah Knepper <sarah.knepper@intel.com>
* 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 <iostream>
#include <string>
#include <stdexcept>
#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);
}

View File

@ -0,0 +1,52 @@
/*
* Author: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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 <stdlib.h>
#include <unistd.h>
#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_ */

View File

@ -0,0 +1,104 @@
/*
* Authors: Brendan Le Foll <brendan.le.foll@intel.com>
* Mihai Tudor Panu <mihai.tudor.panu@intel.com>
* Sarah Knepper <sarah.knepper@intel.com>
* 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 <string>
#include <mraa/aio.hpp>
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;
};
}

View File

@ -0,0 +1,118 @@
/*
* Author: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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<stdio.h>
#include <stdlib.h>
#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;
}