guvas12d: C implementation; FTI; C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-11-04 12:51:42 -06:00
parent 9be920dbcd
commit 3eb3a0b825
12 changed files with 643 additions and 128 deletions

View File

@ -1,5 +1,9 @@
set (libname "guvas12d")
set (libdescription "Guvas12d UV sensor module")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()
upm_mixed_module_init (NAME guvas12d
DESCRIPTION "Guvas12d analog UV sensor"
C_HDR guvas12d.h
C_SRC guvas12d.c
CPP_HDR guvas12d.hpp
CPP_SRC guvas12d.cxx
FTI_SRC guvas12d_fti.c
CPP_WRAPS_C
REQUIRES mraa)

128
src/guvas12d/guvas12d.c Normal file
View File

@ -0,0 +1,128 @@
/*
* Author: Jon Trulson <jtrulson@ics.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 <assert.h>
#include <string.h>
#include <upm_utilities.h>
#include "guvas12d.h"
guvas12d_context guvas12d_init(int pin, float aref)
{
guvas12d_context dev =
(guvas12d_context)malloc(sizeof(struct _guvas12d_context));
if (!dev)
return NULL;
memset((void *)dev, 0, sizeof(struct _guvas12d_context));
// make sure MRAA is initialized
int mraa_rv;
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
{
printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
guvas12d_close(dev);
return NULL;
}
// initialize the MRAA context
if (!(dev->aio = mraa_aio_init(pin)))
{
printf("%s: mraa_aio_init failed.\n", __FUNCTION__);
guvas12d_close(dev);
return NULL;
}
dev->ares = (float)((1 << mraa_aio_get_bit(dev->aio)) - 1);
dev->aref = aref;
dev->scale = 1.0;
dev->offset = 0.0;
return dev;
}
void guvas12d_close(guvas12d_context dev)
{
assert(dev != NULL);
if (dev->aio)
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t guvas12d_get_volts(const guvas12d_context dev, float *volts)
{
assert(dev != NULL);
int val;
val = mraa_aio_read(dev->aio);
if (val < 0)
{
printf("%s: mraa_aio_read() failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
*volts = (float)val * (dev->aref / dev->ares);
return UPM_SUCCESS;
}
upm_result_t guvas12d_get_intensity(const guvas12d_context dev,
float *intensity)
{
assert(dev != NULL);
float volts;
if (guvas12d_get_volts(dev, &volts))
{
printf("%s: guvas12d_get_volts() failed\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
// Seeed magic number 307.0
*intensity = volts * 307.0;
*intensity = *intensity * dev->scale + (dev->offset * dev->scale);
return UPM_SUCCESS;
}
void guvas12d_set_offset(const guvas12d_context dev, float offset)
{
assert(dev != NULL);
dev->offset = offset;
}
void guvas12d_set_scale(const guvas12d_context dev, float scale)
{
assert(dev != NULL);
dev->scale = scale;
}

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
* 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
@ -31,36 +31,63 @@
using namespace upm;
using namespace std;
GUVAS12D::GUVAS12D(int pin)
GUVAS12D::GUVAS12D(int pin, float aref) :
m_guvas12d(guvas12d_init(pin, aref))
{
if ( !(m_aio = mraa_aio_init(pin)) )
if (!m_guvas12d)
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_aio_init() failed, invalid pin?");
throw std::runtime_error(std::string(__FUNCTION__) +
": guvas12d_init() failed");
return;
}
}
GUVAS12D::~GUVAS12D()
{
mraa_aio_close(m_aio);
guvas12d_close(m_guvas12d);
}
float GUVAS12D::value(float aref, unsigned int samples)
{
int val;
unsigned long sum = 0;
(void)(samples); // unused, this method is deprecated.
for (unsigned int i=0; i<samples; i++)
// this is a hack, but this function should go away anyway
if (aref != m_guvas12d->aref)
m_guvas12d->aref = aref;
return volts();
}
float GUVAS12D::volts()
{
float volts;
if (guvas12d_get_volts(m_guvas12d, &volts))
{
val = mraa_aio_read(m_aio);
if (val == -1) return -1;
sum += val;
usleep(2000);
throw std::runtime_error(std::string(__FUNCTION__) +
": guvas12d_get_volts() failed");
}
sum = sum / samples;
float volts = (float)sum * aref / 1024.0;
return volts;
return volts;
}
float GUVAS12D::intensity()
{
float i;
if (guvas12d_get_intensity(m_guvas12d, &i))
{
throw std::runtime_error(std::string(__FUNCTION__) +
": guvas12d_get_intensity() failed");
}
return i;
}
void GUVAS12D::setScale(float scale)
{
guvas12d_set_scale(m_guvas12d, scale);
}
void GUVAS12D::setOffset(float offset)
{
guvas12d_set_offset(m_guvas12d, offset);
}

108
src/guvas12d/guvas12d.h Normal file
View File

@ -0,0 +1,108 @@
/*
* Author: Jon Trulson <jtrulson@ics.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.
*/
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <upm.h>
#include <mraa/aio.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file guvas12d.h
* @library guvas12d
* @brief C API for the guvas12d driver
*
* @include guvas12d.c
*/
/**
* Device context
*/
typedef struct _guvas12d_context {
mraa_aio_context aio;
// ADC reference voltage
float aref;
// ADC resolution
float ares;
float scale;
float offset;
} *guvas12d_context;
/**
* GUVA-S12D UV sensor constructor
*
* @param pin Analog pin to use
*/
guvas12d_context guvas12d_init(int pin, float aref);
/**
* GUVAS12D destructor
*/
void guvas12d_close(guvas12d_context dev);
/**
* Gets the voltage value from the sensor
*
* @param volts Pointer to average voltage reading
* @return UPM status.
*/
upm_result_t guvas12d_get_volts(const guvas12d_context dev, float *volts);
/**
* Gets the illumination intensity in mW/m^2
*
* @param volts Pointer to average voltage reading
* @return UPM status.
* @return illumination intensity in mW/m^2
*/
upm_result_t guvas12d_get_intensity(const guvas12d_context dev,
float *intensity);
/**
* Set sensor offset. This offset is applied to the illumination
* intensity value before scaling. Default is 0.0.
*
* @param dev sensor context pointer
* @param offset Offset to apply.
*/
void guvas12d_set_offset(const guvas12d_context dev, float offset);
/**
* Set sensor scale. This offset is applied to the illumination
* intensity value before scaling. Default is 1.0.
*
* @param dev sensor context pointer
* @param scale Scale to apply.
*/
void guvas12d_set_scale(const guvas12d_context dev, float scale);
#ifdef __cplusplus
}
#endif

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
* 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
@ -23,55 +23,96 @@
*/
#pragma once
#include <string>
#include <mraa/aio.h>
#include <guvas12d.h>
namespace upm {
/**
* @brief GUVA-S12D UV sensor library
* @defgroup guvas12d libupm-guvas12d
* @ingroup seeed analog light eak
*/
/**
* @brief GUVA-S12D UV sensor library
* @defgroup guvas12d libupm-guvas12d
* @ingroup seeed analog light eak
*/
/**
* @library guvas12d
* @sensor guvas12d
* @comname Grove UV Sensor
* @altname GUVA-S12D UV Sensor
* @type light
* @man seeed
* @con analog
* @kit eak
*
* @brief API for the GUVA-S12D UV Sensor
*
* UPM module for the GUVA-S12D UV sensor
*
* @image html guvas12d.jpg
* @snippet guvas12d.cxx Interesting
*/
class GUVAS12D {
public:
/**
* GUVA-S12D UV sensor constructor
* @library guvas12d
* @sensor guvas12d
* @comname Grove UV Sensor
* @altname GUVA-S12D UV Sensor
* @type light
* @man seeed
* @con analog
* @kit eak
*
* @param pin Analog pin to use
*/
GUVAS12D(int pin);
/**
* GUVAS12D destructor
*/
~GUVAS12D();
/**
* Gets the average voltage value from the sensor
* @brief API for the GUVA-S12D UV Sensor
*
* @param aref Reference voltage in use (usually 5.0 V or 3.3 V)
* @param samples Number of samples to average over
* @return Average voltage reading
* UPM module for the GUVA-S12D UV sensor
*
* @image html guvas12d.jpg
* @snippet guvas12d.cxx Interesting
*/
float value(float aref, unsigned int samples);
class GUVAS12D {
public:
private:
mraa_aio_context m_aio;
};
/**
* GUVA-S12D UV sensor constructor
*
* @param pin Analog pin to use
* @param aref Analog reference voltage to use
*/
GUVAS12D(int pin, float aref=5.0);
/**
* GUVAS12D destructor
*/
~GUVAS12D();
/**
* @deprecated This method is being replaced by the volts()
* and illumination() methods.
*
* Gets the average voltage value from the sensor
*
* @param aref Reference voltage in use (usually 5.0 V or 3.3 V)
* @param samples Number of samples to average over (currently
* ignored)
* @return Average voltage reading
*
*/
float value(float aref, unsigned int samples);
/**
* Gets the voltage value from the sensor
*
* @return Voltage reading
*
*/
float volts();
/**
* Gets the computed illumination intensity from the sensor in
* mW/m^2.
*
* @return Intensity over the sensitive wavelengths in mW/m^2
*
*/
float intensity();
/**
* Set sensor scale. This scale is applied to the intensity value
* before the offset is applied. Default is 1.0.
*
* @param scale Scale to apply.
*/
void setScale(float scale);
/**
* Set sensor offset. This offset is applied to the intensity
* value before scaling. Default is 0.0.
*
* @param offset Offset to apply.
*/
void setOffset(float offset);
private:
guvas12d_context m_guvas12d;
};
}

119
src/guvas12d/guvas12d_fti.c Normal file
View File

@ -0,0 +1,119 @@
/*
* Author: Jon Trulson <jtrulson@ics.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 <string.h>
#include <stdlib.h>
#include "guvas12d.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_guvas12d_name[] = "GUVAS12D";
const char upm_guvas12d_description[] = "Analog UV sensor";
const upm_protocol_t upm_guvas12d_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_guvas12d_category[] = {UPM_VOLTAGE};
// forward declarations
void* upm_guvas12d_init_str(const char* protocol, const char* params);
void upm_guvas12d_close(void* dev);
const void* upm_guvas12d_get_ft(upm_sensor_t sensor_type);
const upm_sensor_descriptor_t upm_guvas12d_get_descriptor();
upm_result_t upm_guvas12d_set_offset(const void* dev, float offset);
upm_result_t upm_guvas12d_set_scale(const void* dev, float scale);
upm_result_t upm_guvas12d_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_guvas12d_init_str,
.upm_sensor_close = &upm_guvas12d_close,
.upm_sensor_get_descriptor = &upm_guvas12d_get_descriptor
};
/* 2. VOLTAGE function table */
static const upm_voltage_ft ft_voltage =
{
.upm_voltage_set_offset = &upm_guvas12d_set_offset,
.upm_voltage_set_scale = &upm_guvas12d_set_scale,
.upm_voltage_get_value = &upm_guvas12d_get_value
};
const void* upm_guvas12d_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_guvas12d_init_str(const char* protocol, const char* params)
{
return NULL;
}
void upm_guvas12d_close(void* dev)
{
guvas12d_close((guvas12d_context)dev);
}
const upm_sensor_descriptor_t upm_guvas12d_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_guvas12d_name;
usd.description = upm_guvas12d_description;
usd.protocol_size = 1;
usd.protocol = upm_guvas12d_protocol;
usd.category_size = 1;
usd.category = upm_guvas12d_category;
return usd;
}
upm_result_t upm_guvas12d_set_offset(const void* dev, float offset)
{
guvas12d_set_offset((guvas12d_context)dev, offset);
return UPM_SUCCESS;
}
upm_result_t upm_guvas12d_set_scale(const void* dev, float scale)
{
guvas12d_set_scale((guvas12d_context)dev, scale);
return UPM_SUCCESS;
}
upm_result_t upm_guvas12d_get_value(const void* dev, float *value)
{
return guvas12d_get_volts((guvas12d_context)dev, value);
}