mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
guvas12d: Initial implementation
jrvandr: removing unnecessary mraa_init() This module implements support for the Grove UV sensor (guvas12d). Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Zion Orent <zorent@ics.com> Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:
parent
9784f6e6ab
commit
38a14eb068
@ -63,6 +63,7 @@ add_executable (ta12200-example ta12200.cxx)
|
||||
add_executable (grovelinefinder-example grovelinefinder.cxx)
|
||||
add_executable (grovevdiv-example grovevdiv.cxx)
|
||||
add_executable (grovewater-example grovewater.cxx)
|
||||
add_executable (guvas12d-example guvas12d.cxx)
|
||||
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||
@ -113,6 +114,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grovelinefinder)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grovevdiv)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grovewater)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/guvas12d)
|
||||
|
||||
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
||||
@ -179,3 +181,4 @@ target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (grovelinefinder-example grovelinefinder ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (grovevdiv-example grovevdiv ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (grovewater-example grovewater ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (guvas12d-example guvas12d ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
72
examples/guvas12d.cxx
Normal file
72
examples/guvas12d.cxx
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2014 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 <iostream>
|
||||
#include <signal.h>
|
||||
#include "guvas12d.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
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.
|
||||
|
||||
// Instantiate a GUVAS12D on analog pin A0
|
||||
upm::GUVAS12D *volts = new upm::GUVAS12D(0);
|
||||
|
||||
// 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);
|
||||
}
|
||||
//! [Interesting]
|
||||
|
||||
cout << "Exiting" << endl;
|
||||
|
||||
delete volts;
|
||||
return 0;
|
||||
}
|
56
examples/javascript/guvas12d.js
Normal file
56
examples/javascript/guvas12d.js
Normal file
@ -0,0 +1,56 @@
|
||||
/*jslint node:true, vars:true, bitwise:true, unparam:true */
|
||||
/*jshint unused:true */
|
||||
/*global */
|
||||
/*
|
||||
* Author: Zion Orent <zorent@ics.com>
|
||||
* Copyright (c) 2014 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.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
// Print message when exiting
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
5
src/guvas12d/CMakeLists.txt
Normal file
5
src/guvas12d/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
set (libname "guvas12d")
|
||||
set (libdescription "upm guvas12d UV sensor module")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_h ${libname}.h)
|
||||
upm_module_init()
|
62
src/guvas12d/guvas12d.cxx
Normal file
62
src/guvas12d/guvas12d.cxx
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2014 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 <iostream>
|
||||
|
||||
#include "guvas12d.h"
|
||||
|
||||
using namespace upm;
|
||||
using namespace std;
|
||||
|
||||
GUVAS12D::GUVAS12D(int pin)
|
||||
{
|
||||
if ( !(m_aio = mraa_aio_init(pin)) )
|
||||
{
|
||||
cerr << __FUNCTION__ << ": mraa_aio_init() failed" << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
GUVAS12D::~GUVAS12D()
|
||||
{
|
||||
mraa_aio_close(m_aio);
|
||||
}
|
||||
|
||||
float GUVAS12D::value(float aref, unsigned int samples)
|
||||
{
|
||||
int val;
|
||||
unsigned long sum = 0;
|
||||
|
||||
for (int i=0; i<samples; i++)
|
||||
{
|
||||
val = mraa_aio_read(m_aio);
|
||||
sum += val;
|
||||
usleep(2000);
|
||||
}
|
||||
|
||||
sum = sum / samples;
|
||||
float volts = (float)sum * aref / 1024.0;
|
||||
|
||||
return volts;
|
||||
}
|
65
src/guvas12d/guvas12d.h
Normal file
65
src/guvas12d/guvas12d.h
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2014 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 <string>
|
||||
#include <mraa/aio.h>
|
||||
|
||||
namespace upm {
|
||||
|
||||
/**
|
||||
* @brief C++ API for the GUVAS12D UV sensor module
|
||||
*
|
||||
* UPM module for the GUVAS12D UV Sensor
|
||||
*
|
||||
* @ingroup grove analog
|
||||
* @snippet guvas12d.cxx Interesting
|
||||
*/
|
||||
class GUVAS12D {
|
||||
public:
|
||||
/**
|
||||
* GUVAS12D sensor constructor
|
||||
*
|
||||
* @param pin analog pin to use
|
||||
*/
|
||||
GUVAS12D(int pin);
|
||||
/**
|
||||
* GUVAS12D Destructor
|
||||
*/
|
||||
~GUVAS12D();
|
||||
/**
|
||||
* Get the averaged voltage value from the sensor
|
||||
*
|
||||
* @param aref the reference voltage in use (5.0 or 3.3 usually)
|
||||
* @param samples number of samples to average over
|
||||
* @return the averaged voltage reading
|
||||
*/
|
||||
float value(float aref, unsigned int samples);
|
||||
|
||||
private:
|
||||
mraa_aio_context m_aio;
|
||||
};
|
||||
}
|
||||
|
||||
|
8
src/guvas12d/jsupm_guvas12d.i
Normal file
8
src/guvas12d/jsupm_guvas12d.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_guvas12d
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "guvas12d.h"
|
||||
%}
|
||||
|
||||
%include "guvas12d.h"
|
9
src/guvas12d/pyupm_guvas12d.i
Normal file
9
src/guvas12d/pyupm_guvas12d.i
Normal file
@ -0,0 +1,9 @@
|
||||
%module pyupm_guvas12d
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "guvas12d.h"
|
||||
%{
|
||||
#include "guvas12d.h"
|
||||
%}
|
Loading…
x
Reference in New Issue
Block a user