From 2eb00476438a25c20e88caa54c3fc06e6af7b403 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 12 Dec 2014 10:55:53 -0700 Subject: [PATCH] groveloudness: Initial implementation This module implements support for the Grove Loudness sensor. Signed-off-by: Jon Trulson Signed-off-by: Zion Orent Signed-off-by: Sarah Knepper --- examples/CMakeLists.txt | 3 ++ examples/groveloudness.cxx | 63 ++++++++++++++++++++++++ examples/javascript/groveloudness.js | 43 ++++++++++++++++ src/groveloudness/CMakeLists.txt | 5 ++ src/groveloudness/groveloudness.cxx | 46 +++++++++++++++++ src/groveloudness/groveloudness.h | 65 +++++++++++++++++++++++++ src/groveloudness/jsupm_groveloudness.i | 8 +++ src/groveloudness/pyupm_groveloudness.i | 9 ++++ 8 files changed, 242 insertions(+) create mode 100644 examples/groveloudness.cxx create mode 100644 examples/javascript/groveloudness.js create mode 100644 src/groveloudness/CMakeLists.txt create mode 100644 src/groveloudness/groveloudness.cxx create mode 100644 src/groveloudness/groveloudness.h create mode 100644 src/groveloudness/jsupm_groveloudness.i create mode 100644 src/groveloudness/pyupm_groveloudness.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 246cc7f5..bc76034e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -64,6 +64,7 @@ add_executable (grovelinefinder-example grovelinefinder.cxx) add_executable (grovevdiv-example grovevdiv.cxx) add_executable (grovewater-example grovewater.cxx) add_executable (guvas12d-example guvas12d.cxx) +add_executable (groveloudness-example groveloudness.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -115,6 +116,7 @@ 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) +include_directories (${PROJECT_SOURCE_DIR}/src/groveloudness) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -182,3 +184,4 @@ target_link_libraries (grovelinefinder-example grovelinefinder ${CMAKE_THREAD_LI 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}) +target_link_libraries (groveloudness-example groveloudness ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/groveloudness.cxx b/examples/groveloudness.cxx new file mode 100644 index 00000000..dbfceb68 --- /dev/null +++ b/examples/groveloudness.cxx @@ -0,0 +1,63 @@ +/* + * 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 "groveloudness.h" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a Grove Loudness sensor on analog pin A0 + upm::GroveLoudness* loudness = new upm::GroveLoudness(0); + + // Print the loudness value every 0.1 seconds + while (shouldRun) + { + int val = loudness->value(); + cout << "Loudness value (higher is louder): " << val << endl; + + usleep(100000); + } +//! [Interesting] + + cout << "Exiting" << endl; + + delete loudness; + return 0; +} diff --git a/examples/javascript/groveloudness.js b/examples/javascript/groveloudness.js new file mode 100644 index 00000000..a12f8e6d --- /dev/null +++ b/examples/javascript/groveloudness.js @@ -0,0 +1,43 @@ +/*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 loudnessSensor = require('jsupm_groveloudness'); +// Instantiate a Grove Loudness sensor on analog pin A0 +var myLoudnessSensor = new loudnessSensor.GroveLoudness(0); + +// Print the loudness value every 0.1 seconds +setInterval(function() +{ + console.log("Loudness value (higher is louder): " + myLoudnessSensor.value()); +}, 100); + +// Print message when exiting +process.on('SIGINT', function() +{ + console.log("Exiting..."); + process.exit(0); +}); diff --git a/src/groveloudness/CMakeLists.txt b/src/groveloudness/CMakeLists.txt new file mode 100644 index 00000000..890000c4 --- /dev/null +++ b/src/groveloudness/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "groveloudness") +set (libdescription "upm grove loudness module") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/groveloudness/groveloudness.cxx b/src/groveloudness/groveloudness.cxx new file mode 100644 index 00000000..273e0738 --- /dev/null +++ b/src/groveloudness/groveloudness.cxx @@ -0,0 +1,46 @@ +/* + * 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 "groveloudness.h" + +using namespace upm; + +GroveLoudness::GroveLoudness(int pin) +{ + mraa_init(); + + m_aio = mraa_aio_init(pin); +} + +GroveLoudness::~GroveLoudness() +{ + mraa_aio_close(m_aio); +} + +int GroveLoudness::value() +{ + return mraa_aio_read(m_aio); +} diff --git a/src/groveloudness/groveloudness.h b/src/groveloudness/groveloudness.h new file mode 100644 index 00000000..c4fd21b7 --- /dev/null +++ b/src/groveloudness/groveloudness.h @@ -0,0 +1,65 @@ +/* + * 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. + */ +#pragma once + +#include +#include + +namespace upm { + + /** + * @brief C++ API for the Grove Loudness Sensor + * + * UPM module for the Grove Loudness Sensor. This sensor + * detects how loud the surrounding environment is. + * The higher the output analog value, the louder the sound. + * + * @ingroup grove analog + * @snippet groveloudness.cxx Interesting + */ + class GroveLoudness { + public: + /** + * Grove analog loudness sensor constructor + * + * @param pin analog pin to use + */ + GroveLoudness(int pin); + /** + * GroveLoudness Destructor + */ + ~GroveLoudness(); + /** + * Get the loudness value from the sensor + * + * @return the loudness reading + */ + int value(); + + private: + mraa_aio_context m_aio; + }; +} + + diff --git a/src/groveloudness/jsupm_groveloudness.i b/src/groveloudness/jsupm_groveloudness.i new file mode 100644 index 00000000..8f18ea08 --- /dev/null +++ b/src/groveloudness/jsupm_groveloudness.i @@ -0,0 +1,8 @@ +%module jsupm_groveloudness +%include "../upm.i" + +%{ + #include "groveloudness.h" +%} + +%include "groveloudness.h" diff --git a/src/groveloudness/pyupm_groveloudness.i b/src/groveloudness/pyupm_groveloudness.i new file mode 100644 index 00000000..8630e958 --- /dev/null +++ b/src/groveloudness/pyupm_groveloudness.i @@ -0,0 +1,9 @@ +%module pyupm_groveloudness +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "groveloudness.h" +%{ + #include "groveloudness.h" +%}