mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
guvas12d: C implementation; FTI; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
9be920dbcd
commit
3eb3a0b825
@ -33,40 +33,40 @@ bool shouldRun = true;
|
|||||||
|
|
||||||
// analog voltage, usually 3.3 or 5.0
|
// analog voltage, usually 3.3 or 5.0
|
||||||
#define GUVAS12D_AREF 5.0
|
#define GUVAS12D_AREF 5.0
|
||||||
#define SAMPLES_PER_QUERY 1024
|
|
||||||
|
|
||||||
void sig_handler(int signo)
|
void sig_handler(int signo)
|
||||||
{
|
{
|
||||||
if (signo == SIGINT)
|
if (signo == SIGINT)
|
||||||
shouldRun = false;
|
shouldRun = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
signal(SIGINT, sig_handler);
|
signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
// The was tested with the Grove UV Sensor module.
|
// This was tested with the Grove UV Sensor module.
|
||||||
// It has a sensing range from between 200-400nm. It's strongest
|
// It has a sensing range from between 240-370nm. It's strongest
|
||||||
// response is around 320-360nm.
|
// response is around 320-360nm.
|
||||||
|
|
||||||
// Instantiate a GUVAS12D on analog pin A0
|
// Instantiate a GUVAS12D on analog pin A0
|
||||||
upm::GUVAS12D *volts = new upm::GUVAS12D(0);
|
upm::GUVAS12D *volts = new upm::GUVAS12D(0);
|
||||||
|
|
||||||
// The higher the voltage the more intense the UV radiation.
|
|
||||||
|
|
||||||
while (shouldRun)
|
// The higher the voltage the more intense the UV radiation.
|
||||||
|
|
||||||
|
while (shouldRun)
|
||||||
{
|
{
|
||||||
cout << "AREF: " << GUVAS12D_AREF
|
cout << "Volts: " << volts->volts()
|
||||||
<< ", Voltage value (higher means more UV): "
|
<< ", Intensity: " << volts->intensity()
|
||||||
<< volts->value(GUVAS12D_AREF, SAMPLES_PER_QUERY) << endl;
|
<< " mW/m^2"
|
||||||
|
<< endl;
|
||||||
sleep(1);
|
|
||||||
|
sleep(1);
|
||||||
}
|
}
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
|
|
||||||
cout << "Exiting" << endl;
|
cout << "Exiting" << endl;
|
||||||
|
|
||||||
delete volts;
|
delete volts;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,7 @@ add_example (uln200xa)
|
|||||||
add_example (mma7660)
|
add_example (mma7660)
|
||||||
add_example (buzzer)
|
add_example (buzzer)
|
||||||
add_example (ppd42ns)
|
add_example (ppd42ns)
|
||||||
|
add_example (guvas12d)
|
||||||
|
|
||||||
# Custom examples
|
# Custom examples
|
||||||
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
||||||
|
89
examples/c/guvas12d.c
Normal file
89
examples/c/guvas12d.c
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* 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 <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <upm_utilities.h>
|
||||||
|
#include <guvas12d.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
|
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
|
||||||
* Copyright (c) 2015 Intel Corporation.
|
* Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2015-2016 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -23,24 +24,26 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class GUVAS12DSample {
|
public class GUVAS12DSample {
|
||||||
// analog voltage, usually 3.3 or 5.0
|
// analog voltage, usually 3.3 or 5.0
|
||||||
private static final float GUVAS12D_AREF = 5;
|
private static final float GUVAS12D_AREF = 5;
|
||||||
private static final int SAMPLES_PER_QUERY = 1024;
|
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
// ! [Interesting]
|
// ! [Interesting]
|
||||||
// Instantiate a GUVAS12D on analog pin A3
|
// Instantiate a GUVAS12D on analog pin A0
|
||||||
upm_guvas12d.GUVAS12D volts = new upm_guvas12d.GUVAS12D(3);
|
upm_guvas12d.GUVAS12D volts = new upm_guvas12d.GUVAS12D(0,
|
||||||
|
GUVAS12D_AREF);
|
||||||
|
|
||||||
while (true) {
|
while (true)
|
||||||
float value = volts.value(GUVAS12D_AREF, SAMPLES_PER_QUERY);
|
{
|
||||||
|
System.out.println("Volts: "
|
||||||
|
+ volts.volts()
|
||||||
|
+ ", Intensity: "
|
||||||
|
+ volts.intensity()
|
||||||
|
+ " mW/m^2");
|
||||||
|
|
||||||
System.out.println("AREF: " + GUVAS12D_AREF
|
Thread.sleep(1000);
|
||||||
+ ", Voltage value (higher means more UV): " + value);
|
}
|
||||||
|
// ! [Interesting]
|
||||||
|
}
|
||||||
|
|
||||||
Thread.sleep(1000);
|
}
|
||||||
}
|
|
||||||
// ! [Interesting]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Zion Orent <zorent@ics.com>
|
* Author: Zion Orent <zorent@ics.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2014-2016 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -24,26 +25,22 @@
|
|||||||
|
|
||||||
var UVSensor = require('jsupm_guvas12d');
|
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
|
// analog voltage, usually 3.3 or 5.0
|
||||||
var g_GUVAS12D_AREF = 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()
|
setInterval(function()
|
||||||
{
|
{
|
||||||
var outputStr = "AREF: " + g_GUVAS12D_AREF
|
var outputStr = "Volts: "
|
||||||
+ ", Voltage value (higher means more UV): "
|
+ myUVSensor.volts()
|
||||||
+ roundNum(myUVSensor.value(g_GUVAS12D_AREF, g_SAMPLES_PER_QUERY), 6);
|
+ ", Intensity: "
|
||||||
console.log(outputStr);
|
+ myUVSensor.intensity()
|
||||||
}, 1000);
|
+ " mW/m^2";
|
||||||
|
|
||||||
function roundNum(num, decimalPlaces)
|
console.log(outputStr);
|
||||||
{
|
}, 1000);
|
||||||
var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
|
|
||||||
return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print message when exiting
|
// Print message when exiting
|
||||||
process.on('SIGINT', function()
|
process.on('SIGINT', function()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# Author: Zion Orent <zorent@ics.com>
|
# Author: Zion Orent <zorent@ics.com>
|
||||||
# Copyright (c) 2015 Intel Corporation.
|
# Jon Trulson <jtrulson@ics.com>
|
||||||
|
# Copyright (c) 2015-2016 Intel Corporation.
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
# a copy of this software and associated documentation files (the
|
# 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
|
from upm import pyupm_guvas12d as upmUV
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Instantiate a UV sensor on analog pin A0
|
|
||||||
myUVSensor = upmUV.GUVAS12D(0);
|
|
||||||
|
|
||||||
# analog voltage, usually 3.3 or 5.0
|
# analog voltage, usually 3.3 or 5.0
|
||||||
GUVAS12D_AREF = 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 ##
|
## Exit handlers ##
|
||||||
# This function stops python from printing a stacktrace when you hit control-C
|
# This function stops python from printing a stacktrace when you hit control-C
|
||||||
@ -48,10 +48,8 @@ def main():
|
|||||||
signal.signal(signal.SIGINT, SIGINTHandler)
|
signal.signal(signal.SIGINT, SIGINTHandler)
|
||||||
|
|
||||||
while(1):
|
while(1):
|
||||||
s = ("AREF: {0}, "
|
s = ("Volts: {0}, Intensity: {1} mW/m^2".format(myUVSensor.volts(),
|
||||||
"Voltage value (higher means more UV): "
|
myUVSensor.intensity()))
|
||||||
"{1}".format(GUVAS12D_AREF,
|
|
||||||
myUVSensor.value(GUVAS12D_AREF, SAMPLES_PER_QUERY)))
|
|
||||||
print(s)
|
print(s)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
set (libname "guvas12d")
|
upm_mixed_module_init (NAME guvas12d
|
||||||
set (libdescription "Guvas12d UV sensor module")
|
DESCRIPTION "Guvas12d analog UV sensor"
|
||||||
set (module_src ${libname}.cxx)
|
C_HDR guvas12d.h
|
||||||
set (module_hpp ${libname}.hpp)
|
C_SRC guvas12d.c
|
||||||
upm_module_init()
|
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
128
src/guvas12d/guvas12d.c
Normal 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;
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Jon Trulson <jtrulson@ics.com>
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -31,36 +31,63 @@
|
|||||||
using namespace upm;
|
using namespace upm;
|
||||||
using namespace std;
|
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__) +
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
": mraa_aio_init() failed, invalid pin?");
|
": guvas12d_init() failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GUVAS12D::~GUVAS12D()
|
GUVAS12D::~GUVAS12D()
|
||||||
{
|
{
|
||||||
mraa_aio_close(m_aio);
|
guvas12d_close(m_guvas12d);
|
||||||
}
|
}
|
||||||
|
|
||||||
float GUVAS12D::value(float aref, unsigned int samples)
|
float GUVAS12D::value(float aref, unsigned int samples)
|
||||||
{
|
{
|
||||||
int val;
|
(void)(samples); // unused, this method is deprecated.
|
||||||
unsigned long sum = 0;
|
|
||||||
|
|
||||||
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);
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
if (val == -1) return -1;
|
": guvas12d_get_volts() failed");
|
||||||
sum += val;
|
|
||||||
usleep(2000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sum = sum / samples;
|
return volts;
|
||||||
float volts = (float)sum * aref / 1024.0;
|
}
|
||||||
|
|
||||||
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
108
src/guvas12d/guvas12d.h
Normal 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
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Jon Trulson <jtrulson@ics.com>
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -23,55 +23,96 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <guvas12d.h>
|
||||||
#include <mraa/aio.h>
|
|
||||||
|
|
||||||
namespace upm {
|
namespace upm {
|
||||||
/**
|
/**
|
||||||
* @brief GUVA-S12D UV sensor library
|
* @brief GUVA-S12D UV sensor library
|
||||||
* @defgroup guvas12d libupm-guvas12d
|
* @defgroup guvas12d libupm-guvas12d
|
||||||
* @ingroup seeed analog light eak
|
* @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
|
* @brief API for the GUVA-S12D UV Sensor
|
||||||
*/
|
|
||||||
GUVAS12D(int pin);
|
|
||||||
/**
|
|
||||||
* GUVAS12D destructor
|
|
||||||
*/
|
|
||||||
~GUVAS12D();
|
|
||||||
/**
|
|
||||||
* Gets the average voltage value from the sensor
|
|
||||||
*
|
*
|
||||||
* @param aref Reference voltage in use (usually 5.0 V or 3.3 V)
|
* UPM module for the GUVA-S12D UV sensor
|
||||||
* @param samples Number of samples to average over
|
*
|
||||||
* @return Average voltage reading
|
* @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
119
src/guvas12d/guvas12d_fti.c
Normal 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);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user