upmc: Updates for building C modules w/base UPM

Test commit for building C UPM modules.

    * Added C include directory
    * Added C utilities directory
    * Rename C++ upm.h -> upm.hpp to make room for C upm.h
    * Added upm_mixed_module_init function to src/CMakeLists.txt.  This
      function takes filesnames similar to upm_module_init and does a
      bit of processing before calling upm_module_init.
    * Added c example directory.  Changed c++ example names.
    * Added dfrph implemention for testing (C++ wraps C).  Added mraa
      to .pc requires for dfrph.  Tested against stand-alone project.
      Added dfrph c example.
    * Update implemention of pkg-config file generation.
    * Added two cmake cache variables: BUILDCPP and BUILDFTI
    * Removed src from swig_add_module calls, added libname to
      swig_link_libraries calls.  Shrinks swig'ed binaries by ~13%.
    * Added install target in upm/CMakeLists.txt to install C header,
      directory.  Is this where we want this?
    * C FTI header directory is include/fti

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2016-08-17 17:58:21 -07:00
parent d866b25f85
commit c1f9d15f67
46 changed files with 1945 additions and 87 deletions

View File

@ -1,5 +1,9 @@
set (libname "dfrph")
set (libdescription "upm dfrobot pH sensors")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()
upm_mixed_module_init (NAME dfrph
DESCRIPTION "upm dfrobot pH sensor"
C_HDR dfrph.h
C_SRC dfrph.c
CPP_HDR dfrph.hpp
CPP_SRC dfrph.cxx
FTI_SRC dfrph_fti.c
CPP_WRAPS_C
REQUIRES mraa)

103
src/dfrph/dfrph.c Normal file
View File

@ -0,0 +1,103 @@
/*
* 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 "dfrph.h"
dfrph_context dfrph_init(int16_t pin)
{
dfrph_context dev =
(dfrph_context) malloc(sizeof(struct _dfrph_context));
if(dev == NULL) return NULL;
/* Init aio pin */
dev->aio = mraa_aio_init(pin);
/* Set the ref, offset, and scale */
dev->m_aref = 5.0;
dev->m_count_offset = 0.0;
dev->m_count_scale = 1.0;
if(dev->aio == NULL) {
free(dev);
return NULL;
}
return dev;
}
void dfrph_close(dfrph_context dev)
{
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t dfrph_set_offset(const dfrph_context dev, float offset)
{
dev->m_count_offset = offset;
return UPM_SUCCESS;
}
upm_result_t dfrph_set_scale(const dfrph_context dev, float scale)
{
dev->m_count_scale = scale;
return UPM_SUCCESS;
}
upm_result_t dfrph_get_raw_volts(const dfrph_context dev, float *volts)
{
*volts = mraa_aio_read_float(dev->aio);
if (*volts == -1.0) return UPM_ERROR_OPERATION_FAILED;
/* Scale by aref */
*volts *= dev->m_aref;
return UPM_SUCCESS;
}
upm_result_t dfrph_get_ph(const dfrph_context dev, float *value)
{
/* Read counts */
int counts = mraa_aio_read(dev->aio);
/* Get max adc value range 1023, 2047, 4095, etc... */
float max_adc = (1 << mraa_aio_get_bit(dev->aio)) - 1;
/* Apply raw scale */
*value = counts * dev->m_count_scale;
/* Apply raw offset */
*value += dev->m_count_offset * dev->m_count_scale;
/* Normalize the value */
*value /= max_adc;
/* Vmax for sensor is 0.8 * Vref, so scale by 1/0.8 = 1.25 */
*value *= 1.25 * 14; /* Convert to pH */
return UPM_SUCCESS;
}

View File

@ -23,52 +23,54 @@
*/
#include <iostream>
#include <stdexcept>
#include "dfrph.hpp"
using namespace std;
using namespace upm;
DFRPH::DFRPH(int pin, float aref) :
m_aio(pin)
DFRPH::DFRPH(int pin, float vref) : _dev(dfrph_init(pin))
{
m_aRes = (1 << m_aio.getBit());
m_aref = aref;
m_offset = 0.0;
if (_dev == NULL)
throw std::invalid_argument(std::string(__FUNCTION__) +
": dfrph_init() failed, invalid pin?");
}
DFRPH::~DFRPH()
{
}
float DFRPH::volts()
{
int val = m_aio.read();
return(val * (m_aref / m_aRes));
dfrph_close(_dev);
}
void DFRPH::setOffset(float offset)
{
m_offset = offset;
dfrph_set_offset(_dev, offset);
}
void DFRPH::setScale(float scale)
{
dfrph_set_scale(_dev, scale);
}
float DFRPH::volts()
{
float volts = 0.0;
dfrph_get_raw_volts(_dev, &volts);
return volts;
}
float DFRPH::pH(unsigned int samples)
{
if (!samples)
samples = 1;
float ph_avg = 0.0;
float sum = 0.0;
// Read at least 1 sample
if (samples == 0) samples = 1;
for (int i=0; i<samples; i++)
float ph = 0.0;
for (int i =0; i < samples; i++)
{
sum += volts();
usleep(20000);
dfrph_get_ph(_dev, &ph);
ph_avg += ph;
}
sum /= samples;
// 3.5 is a 'magic' DFRobot number. Seems to work though :)
return (3.5 * sum + m_offset);
return ph_avg/samples;
}

97
src/dfrph/dfrph.h Normal file
View File

@ -0,0 +1,97 @@
/*
* 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 _dfrph_context {
/* mraa aio pin context */
mraa_aio_context aio;
/* ADC reference */
float m_aref;
/* Raw count offset */
float m_count_offset;
/* Raw count scale */
float m_count_scale;
} *dfrph_context;
/**
* Initialize analog sensor
* @param pin is Analog pin
* @return sensor context as void pointer
*/
dfrph_context dfrph_init(int16_t pin);
/**
* Analog sensor destructor
* @param sensor context pointer deallocate memory
*/
void dfrph_close(dfrph_context dev);
/**
* 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 dfrph_set_offset(const dfrph_context dev, float offset);
/**
* 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 dfrph_set_scale(const dfrph_context dev, float scale);
/**
* Get raw volts
* @param dev sensor context pointer
* @param volts Raw sensor voltage
* @return Function result code
*/
upm_result_t dfrph_get_raw_volts(const dfrph_context dev, float *volts);
/**
* Read value from sensor
* @param dev sensor context pointer
* @param value pointer to returned pH value from sensor
* @return Function result code
*/
upm_result_t dfrph_get_ph(const dfrph_context dev, float *value);
#ifdef __cplusplus
}
#endif

View File

@ -25,7 +25,8 @@
#include <iostream>
#include <string>
#include <mraa/aio.hpp>
#include "dfrph.h"
namespace upm {
/**
@ -94,22 +95,15 @@ namespace upm {
* DFRPH constructor
*
* @param pin Analog pin to use
* @param aref Analog reference voltage; default is 5.0 V
* @param vref Analog reference voltage; default is 5.0 V
*/
DFRPH(int pin, float aref=5.0);
DFRPH(int pin, float vref = 5.0);
/**
* DFRPH destructor
*/
~DFRPH();
/**
* Returns the voltage detected on the analog pin
*
* @return The detected voltage
*/
float volts();
/**
* Specifies the offset determined from calibration. The default
* is 0.0.
@ -118,6 +112,16 @@ namespace upm {
*/
void setOffset(float offset);
/**
* Specifies the scale determined from calibration. The default
* is 1.0.
*
* @param scale The scale value to use
*/
void setScale(float scale);
float volts();
/**
* Take a number of samples and return the detected pH value. The
* default number of samples is 15.
@ -125,18 +129,10 @@ namespace upm {
* @param samples The number of samples to average over, default 15
* @return The pH value detected
*/
float pH(unsigned int samples=15);
protected:
mraa::Aio m_aio;
float pH(unsigned int samples = 15);
private:
float m_aref;
// ADC resolution
int m_aRes;
// voltage offset
float m_offset;
dfrph_context _dev;
};
}

118
src/dfrph/dfrph_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 "dfrph.h"
#include "upm_fti.h"
#include "fti/upm_sensor.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_dfrph_name[] = "DFRPH";
const char upm_dfrph_description[] = "Analog pH Meter Pro";
const upm_protocol_t upm_dfrph_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_dfrph_category[] = {UPM_PH};
// forward declarations
const void* upm_dfrph_get_ft(upm_sensor_t sensor_type);
void* upm_dfrph_init_str(const char* protocol, const char* params);
void upm_dfrph_close(void* dev);
const upm_sensor_descriptor_t upm_dfrph_get_descriptor();
upm_result_t upm_dfrph_set_offset(const void* dev, float offset);
upm_result_t upm_dfrph_set_scale(const void* dev, float scale);
upm_result_t upm_dfrph_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_dfrph_init_str,
.upm_sensor_close = &upm_dfrph_close,
.upm_sensor_get_descriptor = &upm_dfrph_get_descriptor
};
/* 2. PH function table */
static const upm_ph_ft ft_ph =
{
.upm_ph_set_offset = &upm_dfrph_set_offset,
.upm_ph_set_scale = &upm_dfrph_set_scale,
.upm_ph_get_value = &upm_dfrph_get_value
};
const void* upm_dfrph_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft_gen;
case UPM_PH:
return &ft_ph;
default:
return NULL;
}
}
void* upm_dfrph_init_str(const char* protocol, const char* params)
{
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__);
return dfrph_init(0);
}
void upm_dfrph_close(void* dev)
{
dfrph_close(dev);
}
const upm_sensor_descriptor_t upm_dfrph_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_dfrph_name;
usd.description = upm_dfrph_description;
usd.protocol_size = 1;
usd.protocol = upm_dfrph_protocol;
usd.category_size = 1;
usd.category = upm_dfrph_category;
return usd;
}
upm_result_t upm_dfrph_set_offset(const void* dev, float offset)
{
return dfrph_set_offset((dfrph_context)dev, offset);
}
upm_result_t upm_dfrph_set_scale(const void* dev, float scale)
{
return dfrph_set_scale((dfrph_context)dev, scale);
}
upm_result_t upm_dfrph_get_value(const void* dev, float *value)
{
return dfrph_get_ph((dfrph_context)dev, value);
}