From 3eb3a0b82555d18096278bf4b99827b736c9cfbb Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 4 Nov 2016 12:51:42 -0600 Subject: [PATCH] guvas12d: C implementation; FTI; C++ wraps C Signed-off-by: Jon Trulson --- examples/c++/guvas12d.cxx | 40 ++++----- examples/c/CMakeLists.txt | 1 + examples/c/guvas12d.c | 89 +++++++++++++++++++++ examples/java/GUVAS12DSample.java | 39 ++++----- examples/javascript/guvas12d.js | 27 +++---- examples/python/guvas12d.py | 16 ++-- src/guvas12d/CMakeLists.txt | 14 ++-- src/guvas12d/guvas12d.c | 128 +++++++++++++++++++++++++++++ src/guvas12d/guvas12d.cxx | 61 ++++++++++---- src/guvas12d/guvas12d.h | 108 +++++++++++++++++++++++++ src/guvas12d/guvas12d.hpp | 129 ++++++++++++++++++++---------- src/guvas12d/guvas12d_fti.c | 119 +++++++++++++++++++++++++++ 12 files changed, 643 insertions(+), 128 deletions(-) create mode 100644 examples/c/guvas12d.c create mode 100644 src/guvas12d/guvas12d.c create mode 100644 src/guvas12d/guvas12d.h create mode 100644 src/guvas12d/guvas12d_fti.c diff --git a/examples/c++/guvas12d.cxx b/examples/c++/guvas12d.cxx index a0aa7403..8e88e00f 100644 --- a/examples/c++/guvas12d.cxx +++ b/examples/c++/guvas12d.cxx @@ -33,40 +33,40 @@ bool shouldRun = true; // analog voltage, usually 3.3 or 5.0 #define GUVAS12D_AREF 5.0 -#define SAMPLES_PER_QUERY 1024 void sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } int main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); //! [Interesting] - // The was tested with the Grove UV Sensor module. - // It has a sensing range from between 200-400nm. It's strongest - // response is around 320-360nm. + // This was tested with the Grove UV Sensor module. + // It has a sensing range from between 240-370nm. It's strongest + // response is around 320-360nm. - // Instantiate a GUVAS12D on analog pin A0 - upm::GUVAS12D *volts = new upm::GUVAS12D(0); - - // The higher the voltage the more intense the UV radiation. + // Instantiate a GUVAS12D on analog pin A0 + upm::GUVAS12D *volts = new upm::GUVAS12D(0); - while (shouldRun) + // The higher the voltage the more intense the UV radiation. + + while (shouldRun) { - cout << "AREF: " << GUVAS12D_AREF - << ", Voltage value (higher means more UV): " - << volts->value(GUVAS12D_AREF, SAMPLES_PER_QUERY) << endl; - - sleep(1); + cout << "Volts: " << volts->volts() + << ", Intensity: " << volts->intensity() + << " mW/m^2" + << endl; + + sleep(1); } //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete volts; - return 0; + delete volts; + return 0; } diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 51d34c9c..a1fcebc9 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -133,6 +133,7 @@ add_example (uln200xa) add_example (mma7660) add_example (buzzer) add_example (ppd42ns) +add_example (guvas12d) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/guvas12d.c b/examples/c/guvas12d.c new file mode 100644 index 00000000..9c984f43 --- /dev/null +++ b/examples/c/guvas12d.c @@ -0,0 +1,89 @@ +/* + * Author: Jon Trulson + * 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 + +#include +#include + +bool shouldRun = true; + +// analog voltage, usually 3.3 or 5.0 +#define GUVAS12D_AREF 5.0 + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // This was tested with the Grove UV Sensor module. + // It has a sensing range from between 240-370nm. It's strongest + // response is around 320-360nm. + + // Instantiate a GUVAS12D on analog pin A0 + guvas12d_context uv = guvas12d_init(0, GUVAS12D_AREF); + + if (!uv) + { + printf("guvas12d_init() failed\n"); + return 1; + } + + // The higher the voltage the more intense the UV radiation. + while (shouldRun) + { + float volts = 0; + float intensity = 0; + + if (guvas12d_get_volts(uv, &volts)) + { + printf("guvas12d_get_volts() failed\n"); + return 1; + } + + if (guvas12d_get_intensity(uv, &intensity)) + { + printf("guvas12d_get_intensity() failed\n"); + return 1; + } + + printf("Volts: %f, Intensity %f mW/m^2\n", volts, intensity); + + upm_delay(1); + } + + printf("Exiting\n"); + + guvas12d_close(uv); +//! [Interesting] + return 0; +} diff --git a/examples/java/GUVAS12DSample.java b/examples/java/GUVAS12DSample.java index 0cd41156..89c9f081 100644 --- a/examples/java/GUVAS12DSample.java +++ b/examples/java/GUVAS12DSample.java @@ -1,6 +1,7 @@ /* * Author: Stefan Andritoiu - * Copyright (c) 2015 Intel Corporation. + * Jon Trulson + * Copyright (c) 2015-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,24 +24,26 @@ */ public class GUVAS12DSample { - // analog voltage, usually 3.3 or 5.0 - private static final float GUVAS12D_AREF = 5; - private static final int SAMPLES_PER_QUERY = 1024; + // analog voltage, usually 3.3 or 5.0 + private static final float GUVAS12D_AREF = 5; - public static void main(String[] args) throws InterruptedException { - // ! [Interesting] - // Instantiate a GUVAS12D on analog pin A3 - upm_guvas12d.GUVAS12D volts = new upm_guvas12d.GUVAS12D(3); + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a GUVAS12D on analog pin A0 + upm_guvas12d.GUVAS12D volts = new upm_guvas12d.GUVAS12D(0, + GUVAS12D_AREF); - while (true) { - float value = volts.value(GUVAS12D_AREF, SAMPLES_PER_QUERY); + while (true) + { + System.out.println("Volts: " + + volts.volts() + + ", Intensity: " + + volts.intensity() + + " mW/m^2"); - System.out.println("AREF: " + GUVAS12D_AREF - + ", Voltage value (higher means more UV): " + value); + Thread.sleep(1000); + } + // ! [Interesting] + } - Thread.sleep(1000); - } - // ! [Interesting] - } - -} \ No newline at end of file +} diff --git a/examples/javascript/guvas12d.js b/examples/javascript/guvas12d.js index 93e440fc..28105be3 100644 --- a/examples/javascript/guvas12d.js +++ b/examples/javascript/guvas12d.js @@ -1,6 +1,7 @@ /* * Author: Zion Orent -* Copyright (c) 2014 Intel Corporation. +* Jon Trulson +* 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 @@ -24,26 +25,22 @@ var UVSensor = require('jsupm_guvas12d'); -// Instantiate a UV sensor on analog pin A0 -var myUVSensor = new UVSensor.GUVAS12D(0); - // analog voltage, usually 3.3 or 5.0 var g_GUVAS12D_AREF = 5.0; -var g_SAMPLES_PER_QUERY = 1024; + +// Instantiate a UV sensor on analog pin A0 +var myUVSensor = new UVSensor.GUVAS12D(0, g_GUVAS12D_AREF); setInterval(function() { - var outputStr = "AREF: " + g_GUVAS12D_AREF - + ", Voltage value (higher means more UV): " - + roundNum(myUVSensor.value(g_GUVAS12D_AREF, g_SAMPLES_PER_QUERY), 6); - console.log(outputStr); -}, 1000); + var outputStr = "Volts: " + + myUVSensor.volts() + + ", Intensity: " + + myUVSensor.intensity() + + " mW/m^2"; -function roundNum(num, decimalPlaces) -{ - var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000)); - return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces)); -} + console.log(outputStr); +}, 1000); // Print message when exiting process.on('SIGINT', function() diff --git a/examples/python/guvas12d.py b/examples/python/guvas12d.py index 095a331c..6da0f7fe 100755 --- a/examples/python/guvas12d.py +++ b/examples/python/guvas12d.py @@ -1,6 +1,7 @@ #!/usr/bin/python # Author: Zion Orent -# Copyright (c) 2015 Intel Corporation. +# Jon Trulson +# Copyright (c) 2015-2016 Intel Corporation. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -26,12 +27,11 @@ import time, sys, signal, atexit from upm import pyupm_guvas12d as upmUV def main(): - # Instantiate a UV sensor on analog pin A0 - myUVSensor = upmUV.GUVAS12D(0); - # analog voltage, usually 3.3 or 5.0 GUVAS12D_AREF = 5.0; - SAMPLES_PER_QUERY = 1024; + + # Instantiate a UV sensor on analog pin A0 + myUVSensor = upmUV.GUVAS12D(0, GUVAS12D_AREF); ## Exit handlers ## # This function stops python from printing a stacktrace when you hit control-C @@ -48,10 +48,8 @@ def main(): signal.signal(signal.SIGINT, SIGINTHandler) while(1): - s = ("AREF: {0}, " - "Voltage value (higher means more UV): " - "{1}".format(GUVAS12D_AREF, - myUVSensor.value(GUVAS12D_AREF, SAMPLES_PER_QUERY))) + s = ("Volts: {0}, Intensity: {1} mW/m^2".format(myUVSensor.volts(), + myUVSensor.intensity())) print(s) time.sleep(1) diff --git a/src/guvas12d/CMakeLists.txt b/src/guvas12d/CMakeLists.txt index ec79dbde..7ab99144 100644 --- a/src/guvas12d/CMakeLists.txt +++ b/src/guvas12d/CMakeLists.txt @@ -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) diff --git a/src/guvas12d/guvas12d.c b/src/guvas12d/guvas12d.c new file mode 100644 index 00000000..e63f2fe3 --- /dev/null +++ b/src/guvas12d/guvas12d.c @@ -0,0 +1,128 @@ +/* + * Author: Jon Trulson + * 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 + +#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; +} diff --git a/src/guvas12d/guvas12d.cxx b/src/guvas12d/guvas12d.cxx index f3624c5a..2ac51971 100644 --- a/src/guvas12d/guvas12d.cxx +++ b/src/guvas12d/guvas12d.cxx @@ -1,6 +1,6 @@ /* * Author: Jon Trulson - * 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; iaref) + 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); } diff --git a/src/guvas12d/guvas12d.h b/src/guvas12d/guvas12d.h new file mode 100644 index 00000000..f4d8658b --- /dev/null +++ b/src/guvas12d/guvas12d.h @@ -0,0 +1,108 @@ +/* + * Author: Jon Trulson + * 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 +#include +#include +#include + +#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 diff --git a/src/guvas12d/guvas12d.hpp b/src/guvas12d/guvas12d.hpp index 9ca2f5c1..0b5e7af5 100644 --- a/src/guvas12d/guvas12d.hpp +++ b/src/guvas12d/guvas12d.hpp @@ -1,6 +1,6 @@ /* * Author: Jon Trulson - * 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 -#include +#include 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; + }; } diff --git a/src/guvas12d/guvas12d_fti.c b/src/guvas12d/guvas12d_fti.c new file mode 100644 index 00000000..1b3a3379 --- /dev/null +++ b/src/guvas12d/guvas12d_fti.c @@ -0,0 +1,119 @@ +/* + * Author: Jon Trulson + * 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 "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); +}