From 38a14eb06817f4fa24ba1884161cdd45c2466b7d Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 11 Dec 2014 12:52:29 -0700 Subject: [PATCH] guvas12d: Initial implementation jrvandr: removing unnecessary mraa_init() This module implements support for the Grove UV sensor (guvas12d). Signed-off-by: Jon Trulson Signed-off-by: Zion Orent Signed-off-by: John Van Drasek --- examples/CMakeLists.txt | 3 ++ examples/guvas12d.cxx | 72 +++++++++++++++++++++++++++++++++ examples/javascript/guvas12d.js | 56 +++++++++++++++++++++++++ src/guvas12d/CMakeLists.txt | 5 +++ src/guvas12d/guvas12d.cxx | 62 ++++++++++++++++++++++++++++ src/guvas12d/guvas12d.h | 65 +++++++++++++++++++++++++++++ src/guvas12d/jsupm_guvas12d.i | 8 ++++ src/guvas12d/pyupm_guvas12d.i | 9 +++++ 8 files changed, 280 insertions(+) create mode 100644 examples/guvas12d.cxx create mode 100644 examples/javascript/guvas12d.js create mode 100644 src/guvas12d/CMakeLists.txt create mode 100644 src/guvas12d/guvas12d.cxx create mode 100644 src/guvas12d/guvas12d.h create mode 100644 src/guvas12d/jsupm_guvas12d.i create mode 100644 src/guvas12d/pyupm_guvas12d.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 9c61a46d..246cc7f5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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}) diff --git a/examples/guvas12d.cxx b/examples/guvas12d.cxx new file mode 100644 index 00000000..3351741c --- /dev/null +++ b/examples/guvas12d.cxx @@ -0,0 +1,72 @@ +/* + * Author: Jon Trulson + * 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 +#include +#include +#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; +} diff --git a/examples/javascript/guvas12d.js b/examples/javascript/guvas12d.js new file mode 100644 index 00000000..54107771 --- /dev/null +++ b/examples/javascript/guvas12d.js @@ -0,0 +1,56 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ +/*global */ +/* +* Author: Zion Orent +* 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); +}); diff --git a/src/guvas12d/CMakeLists.txt b/src/guvas12d/CMakeLists.txt new file mode 100644 index 00000000..3485e0ed --- /dev/null +++ b/src/guvas12d/CMakeLists.txt @@ -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() diff --git a/src/guvas12d/guvas12d.cxx b/src/guvas12d/guvas12d.cxx new file mode 100644 index 00000000..90a65413 --- /dev/null +++ b/src/guvas12d/guvas12d.cxx @@ -0,0 +1,62 @@ +/* + * Author: Jon Trulson + * 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 + +#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 + * 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 +#include + +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; + }; +} + + diff --git a/src/guvas12d/jsupm_guvas12d.i b/src/guvas12d/jsupm_guvas12d.i new file mode 100644 index 00000000..96607545 --- /dev/null +++ b/src/guvas12d/jsupm_guvas12d.i @@ -0,0 +1,8 @@ +%module jsupm_guvas12d +%include "../upm.i" + +%{ + #include "guvas12d.h" +%} + +%include "guvas12d.h" diff --git a/src/guvas12d/pyupm_guvas12d.i b/src/guvas12d/pyupm_guvas12d.i new file mode 100644 index 00000000..3b8ee1ba --- /dev/null +++ b/src/guvas12d/pyupm_guvas12d.i @@ -0,0 +1,9 @@ +%module pyupm_guvas12d +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "guvas12d.h" +%{ + #include "guvas12d.h" +%}