vdiv: Added vdiv C source.

Multiple changes related to building the vdiv c source w/c example.

    * Renamed GroveVdiv to vdiv (all cases) throughout source, examples,
    directory names, and documentation.

    * Added C source.

    * Tested C sensor code on edison

    * Updated CMakeLists.txt for examples-c to build from
    <sensorname>-c.  This was a small change to get c examples to build
    for sensors in which the C++ does NOT wrap the C.

    * Added C example for vdiv.

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2016-09-01 16:32:49 -07:00
parent 668aa320b9
commit b608232cb9
20 changed files with 523 additions and 66 deletions

View File

@ -1,5 +0,0 @@
set (libname "grovevdiv")
set (libdescription "upm grove voltage divider module")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()

View File

@ -1,8 +0,0 @@
%module jsupm_grovevdiv
%include "../upm.i"
%{
#include "grovevdiv.hpp"
%}
%include "grovevdiv.hpp"

8
src/vdiv/CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
upm_mixed_module_init (NAME vdiv
DESCRIPTION "upm grove voltage divider module"
C_HDR vdiv.h
C_SRC vdiv.c
CPP_HDR vdiv.hpp
CPP_SRC vdiv.cxx
FTI_SRC vdiv_fti.c
REQUIRES mraa)

View File

@ -1,16 +1,16 @@
%module javaupm_grovevdiv
%module javaupm_vdiv
%include "../upm.i"
%{
#include "grovevdiv.hpp"
#include "vdiv.hpp"
%}
%include "grovevdiv.hpp"
%include "vdiv.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_grovevdiv");
System.loadLibrary("javaupm_vdiv");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);

8
src/vdiv/jsupm_vdiv.i Normal file
View File

@ -0,0 +1,8 @@
%module jsupm_vdiv
%include "../upm.i"
%{
#include "vdiv.hpp"
%}
%include "vdiv.hpp"

View File

@ -1,11 +1,11 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_grovevdiv
%module pyupm_vdiv
%include "../upm.i"
%feature("autodoc", "3");
%include "grovevdiv.hpp"
%include "vdiv.hpp"
%{
#include "grovevdiv.hpp"
#include "vdiv.hpp"
%}

122
src/vdiv/vdiv.c Normal file
View File

@ -0,0 +1,122 @@
/*
* Author:
* Copyright (c) 2015 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 <string.h>
#include <stdlib.h>
#include "vdiv.h"
vdiv_context vdiv_init(int16_t pin, float voltage_ref)
{
vdiv_context dev = (vdiv_context)malloc(sizeof(struct _vdiv_context));
if (dev == NULL) return NULL;
/* Init aio pin */
dev->aio = mraa_aio_init(pin);
if (dev->aio == NULL) {
free(dev);
return NULL;
}
/* Set defaults */
dev->m_aRef = 5.0;
dev->m_scale = 1.0;
dev->m_offset = 0.0;
dev->m_vdiv_sw = 3;
return dev;
}
void vdiv_close(vdiv_context dev)
{
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t vdiv_set_scale(const vdiv_context dev, float scale)
{
dev->m_scale = scale;
return UPM_SUCCESS;
}
upm_result_t vdiv_set_offset(const vdiv_context dev, float offset)
{
dev->m_offset = offset;
return UPM_SUCCESS;
}
float vdiv_get_scale(const vdiv_context dev)
{
return dev->m_scale;
}
float vdiv_get_offset(const vdiv_context dev)
{
return dev->m_offset;
}
upm_result_t vdiv_set_divsw(const vdiv_context dev, int vdiv_sw)
{
dev->m_vdiv_sw = vdiv_sw;
return UPM_SUCCESS;
}
int vdiv_get_divsw(const vdiv_context dev)
{
return dev->m_vdiv_sw;
}
upm_result_t vdiv_get_raw_volts(const vdiv_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
/* Scale by the ADC reference voltage */
*value *= dev->m_aRef;
return UPM_SUCCESS;
}
upm_result_t vdiv_get_computed_volts(const vdiv_context dev, float *value)
{
// JET - this is wrong.
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
/* Apply raw scale */
*value *= dev->m_scale;
/* Scale to the ADC referecen then to vdiv gain */
*value *= dev->m_aRef * dev->m_vdiv_sw;
/* Apply the offset in volts */
*value += dev->m_offset;
return UPM_SUCCESS;
}

View File

@ -26,12 +26,12 @@
#include <string>
#include <stdexcept>
#include "grovevdiv.hpp"
#include "vdiv.hpp"
using namespace upm;
using namespace std;
GroveVDiv::GroveVDiv(int pin)
VDiv::VDiv(int pin)
{
if ( !(m_aio = mraa_aio_init(pin)) )
{
@ -41,12 +41,12 @@ GroveVDiv::GroveVDiv(int pin)
}
}
GroveVDiv::~GroveVDiv()
VDiv::~VDiv()
{
mraa_aio_close(m_aio);
}
unsigned int GroveVDiv::value(unsigned int samples)
unsigned int VDiv::value(unsigned int samples)
{
unsigned int sum = 0;
@ -60,7 +60,7 @@ unsigned int GroveVDiv::value(unsigned int samples)
return (sum / samples);
}
float GroveVDiv::computedValue(uint8_t gain, unsigned int val, int vref, int res)
float VDiv::computedValue(uint8_t gain, unsigned int val, int vref, int res)
{
return ((float(gain) * float(val) * float(vref) / float(res)) / 1000.0);

139
src/vdiv/vdiv.h Normal file
View File

@ -0,0 +1,139 @@
/*
* Author:
* Copyright (c) 2015 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 "upm.h"
#include "mraa/aio.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* device context
*/
typedef struct _vdiv_context {
/* mraa aio pin context */
mraa_aio_context aio;
/* ADC voltage reference */
float m_aRef;
/* Scale */
float m_scale;
/* Offset in sensor units */
float m_offset;
/* Offset in sensor units */
int m_vdiv_sw;
} *vdiv_context;
/**
* Initialize analog sensor
* @param pin is Analog pin
* @return sensor context as void pointer
*/
vdiv_context vdiv_init(int16_t pin, float voltage_ref);
/**
* Analog sensor destructor
* @param sensor context pointer deallocate memory
*/
void vdiv_close(vdiv_context dev);
/**
* Set sensor scale. This scale is applied to the return value:
* counts = counts * scale
* @param dev sensor context pointer
* @param scale count scale value used
* @return Function result code
*/
upm_result_t vdiv_set_scale(const vdiv_context dev, float scale);
/**
* Set sensor offset. This offset is applied to the return value:
* counts = counts + offset
* @param dev sensor context pointer
* @param offset count offset value used
* @return Function result code
*/
upm_result_t vdiv_set_offset(const vdiv_context dev, float offset);
/**
* Get sensor scale
* @param dev sensor context pointer
* @return Sensor scale
*/
float vdiv_get_scale(const vdiv_context dev);
/**
* Get sensor offset
* @param dev sensor context pointer
* @return Sensor offset
*/
float vdiv_get_offset(const vdiv_context dev);
/**
* Set sensor divide switch value
* @param dev sensor context pointer
* @param vdiv_sw Divide switch value
* @return Function result code
*/
upm_result_t vdiv_set_divsw(const vdiv_context dev, int vdiv_sw);
/**
* Get divide switch value
* @param dev sensor context pointer
* @return Sensor divide switch value
*/
int vdiv_get_divsw(const vdiv_context dev);
/**
* Read raw voltage from the sensor
* @param dev sensor context pointer
* @param *value Raw sensor voltage
* @return Function result code
*/
upm_result_t vdiv_get_raw_volts(const vdiv_context dev, float *value);
/**
* Gets the true voltage value from the sensor. Voltage divides by 3 or 10,
* depending on the toggle switch on the board. The dynamic range of the
* vdiv sensor is listed below.
*
* ADC Ref SW max VOL in
* ------- --- ----------
* 3.3v 3 8.5v
* 3.3v 10 28.4v
* 5.0v 3 12.9v
* 5.0v 10 43.0v
*
* @param dev sensor context pointer
* @param *value Voltage (v)
* @return Function result code
*/
upm_result_t vdiv_get_computed_volts(const vdiv_context dev, float *value);
#ifdef __cplusplus
}
#endif

View File

@ -29,47 +29,47 @@
#include <mraa/aio.h>
// reference voltage in millivolts
#define GROVEVDIV_VREF 4980
#define VDIV_VREF 4980
// default ADC resolution
#define GROVEVDIV_ADC_RES 1024
#define VDIV_ADC_RES 1024
namespace upm {
/**
* @brief Grove Voltage Divider Sensor library
* @defgroup grovevdiv libupm-grovevdiv
* @brief Voltage Divider Sensor library
* @defgroup vdiv libupm-vdiv
* @ingroup seeed analog electric robok
*/
/**
* @library grovevdiv
* @sensor grovevdiv
* @comname Grove Voltage Divider
* @library vdiv
* @sensor vdiv
* @comname Voltage Divider
* @type electric
* @man seeed
* @con analog
* @kit robok
*
* @brief API for the Grove Voltage Divider Sensor
* @brief API for the Voltage Divider Sensor
*
* UPM module for the Grove Voltage Divider sensor
* UPM module for the Voltage Divider sensor
*
* @image html grovevdiv.jpg
* @snippet grovevdiv.cxx Interesting
* @image html vdiv.jpg
* @snippet vdiv.cxx Interesting
*/
class GroveVDiv {
class VDiv {
public:
/**
* Grove Voltage Divider sensor constructor
* Voltage Divider sensor constructor
*
* @param pin Analog pin to use
*/
GroveVDiv(int pin);
VDiv(int pin);
/**
* Grove Voltage Divider destructor
* Voltage Divider destructor
*/
~GroveVDiv();
~VDiv();
/**
* Gets the conversion value from the sensor
@ -82,15 +82,15 @@ namespace upm {
/**
* Computes the measured voltage
*
* @param gain Gain switch, either 3 or 10 for Grove
* @param gain Gain switch, example: either 3 or 10 grove vdiv
* @param val Measured voltage (from value())
* @param vref Reference voltage in millivolts
* @param res ADC resolution
*
* @return Measured voltage
*/
float computedValue(uint8_t gain, unsigned int val, int vref=GROVEVDIV_VREF,
int res=GROVEVDIV_ADC_RES);
float computedValue(uint8_t gain, unsigned int val, int vref=VDIV_VREF,
int res=VDIV_ADC_RES);
private:
mraa_aio_context m_aio;

118
src/vdiv/vdiv_fti.c Normal file
View File

@ -0,0 +1,118 @@
/*
* Author:
* Copyright (c) 2015 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 <string.h>
#include <stdlib.h>
#include "vdiv.h"
#include "fti/upm_sensor.h"
#include "fti/upm_voltage.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_vdiv_name[] = "VDIV";
const char upm_vdiv_description[] = "Analog voltage divider sensor";
const upm_protocol_t upm_vdiv_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_vdiv_category[] = {UPM_VOLTAGE};
// forward declarations
const void* upm_vdiv_get_ft(upm_sensor_t sensor_type);
void* upm_vdiv_init_str(const char* protocol, const char* params);
void upm_vdiv_close(void* dev);
const upm_sensor_descriptor_t upm_vdiv_get_descriptor();
upm_result_t upm_vdiv_set_offset(const void* dev, float offset);
upm_result_t upm_vdiv_set_scale(const void* dev, float scale);
upm_result_t upm_vdiv_get_value(const void* dev, float *value);
/* This sensor implementes 2 function tables */
/* 1. Generic base function table */
static const upm_sensor_ft ft_gen =
{
.upm_sensor_init_name = &upm_vdiv_init_str,
.upm_sensor_close = &upm_vdiv_close,
.upm_sensor_get_descriptor = &upm_vdiv_get_descriptor
};
/* 2. VOLTAGE function table */
static const upm_voltage_ft ft_voltage =
{
.upm_voltage_set_offset = &upm_vdiv_set_offset,
.upm_voltage_set_scale = &upm_vdiv_set_scale,
.upm_voltage_get_value = &upm_vdiv_get_value
};
const void* upm_vdiv_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft_gen;
case UPM_VOLTAGE:
return &ft_voltage;
default:
return NULL;
}
}
void* upm_vdiv_init_str(const char* protocol, const char* params)
{
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__);
return vdiv_init(0, 5.0);
}
void upm_vdiv_close(void* dev)
{
vdiv_close((vdiv_context)dev);
}
const upm_sensor_descriptor_t upm_vdiv_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_vdiv_name;
usd.description = upm_vdiv_description;
usd.protocol_size = 1;
usd.protocol = upm_vdiv_protocol;
usd.category_size = 1;
usd.category = upm_vdiv_category;
return usd;
}
upm_result_t upm_vdiv_set_offset(const void* dev, float offset)
{
return vdiv_set_offset((vdiv_context)dev, offset);
}
upm_result_t upm_vdiv_set_scale(const void* dev, float scale)
{
return vdiv_set_scale((vdiv_context)dev, scale);
}
upm_result_t upm_vdiv_get_value(const void* dev, float *value)
{
return vdiv_get_computed_volts((vdiv_context)dev, value);
}