From c5369315e95896dbfd6f169123c5d65302cb6f27 Mon Sep 17 00:00:00 2001 From: sknepper Date: Tue, 18 Nov 2014 14:38:36 -0800 Subject: [PATCH] ldt0028: Added support for LDT0-028 PZT film-based sensors Signed-off-by: Sarah Knepper Signed-off-by: Brendan Le Foll --- examples/CMakeLists.txt | 3 ++ examples/javascript/ldt0028.js | 82 +++++++++++++++++++++++++++++++ examples/ldt0028.cxx | 90 ++++++++++++++++++++++++++++++++++ examples/python/ldt0028.py | 66 +++++++++++++++++++++++++ src/ldt0028/CMakeLists.txt | 5 ++ src/ldt0028/jsupm_ldt0028.i | 8 +++ src/ldt0028/ldt0028.cxx | 46 +++++++++++++++++ src/ldt0028/ldt0028.h | 78 +++++++++++++++++++++++++++++ src/ldt0028/pyupm_ldt0028.i | 10 ++++ 9 files changed, 388 insertions(+) create mode 100644 examples/javascript/ldt0028.js create mode 100644 examples/ldt0028.cxx create mode 100644 examples/python/ldt0028.py create mode 100644 src/ldt0028/CMakeLists.txt create mode 100644 src/ldt0028/jsupm_ldt0028.i create mode 100644 src/ldt0028/ldt0028.cxx create mode 100644 src/ldt0028/ldt0028.h create mode 100644 src/ldt0028/pyupm_ldt0028.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 42ab252b..f26414b2 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -44,6 +44,7 @@ add_executable (nrf_ble_broadcast-example ble_broadcast.cxx) add_executable (tsl2561-example tsl2561.cxx) add_executable (htu21d-example htu21d.cxx) add_executable (mpl3115a2-example mpl3115a2.cxx) +add_executable (ldt0028-example ldt0028.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -79,6 +80,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/lol) include_directories (${PROJECT_SOURCE_DIR}/src/tsl2561) include_directories (${PROJECT_SOURCE_DIR}/src/htu21d) include_directories (${PROJECT_SOURCE_DIR}/src/mpl3115a2) +include_directories (${PROJECT_SOURCE_DIR}/src/ldt0028) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -126,3 +128,4 @@ target_link_libraries (nrf_ble_broadcast-example nrf24l01 ${CMAKE_THREAD_LIBS_IN target_link_libraries (tsl2561-example tsl2561 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (htu21d-example htu21d ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mpl3115a2-example mpl3115a2 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (ldt0028-example ldt0028 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/javascript/ldt0028.js b/examples/javascript/ldt0028.js new file mode 100644 index 00000000..e57dbc6e --- /dev/null +++ b/examples/javascript/ldt0028.js @@ -0,0 +1,82 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var sensorModule = require('jsupm_ldt0028'); + +var NUMBER_OF_SECONDS = 10; +var MILLISECONDS_PER_SECOND = 1000; +var SAMPLES_PER_SECOND = 50; +var THRESHOLD = 100; + +// Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0 +var sensor = new sensorModule.LDT0028(0); + +// Read the signal every 20 milliseconds for 10 seconds +console.log("For the next " + NUMBER_OF_SECONDS + " seconds, " + + SAMPLES_PER_SECOND + " samples will be taken every second."); +console.log(""); +var buffer = []; +for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { + buffer.push(sensor.getSample()); + delay(MILLISECONDS_PER_SECOND / SAMPLES_PER_SECOND ); +} + +// Print the number of times the reading was greater than the threshold +var count = 0; +for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { + if (buffer[i] > THRESHOLD) { + count++; + } +} +console.log(sensor.name() + " exceeded the threshold value of " + + THRESHOLD + " a total of " + count + " times,"); +console.log("out of a total of " + NUMBER_OF_SECONDS*SAMPLES_PER_SECOND + + " readings."); +console.log(""); + +// Print a graphical representation of the average value sampled +// each second for the past 10 seconds, using a scale factor of 15 +console.log("Now printing a graphical representation of the average reading "); +console.log("each second for the last " + NUMBER_OF_SECONDS + " seconds."); +var SCALE_FACTOR = 15; +for (var i=0; i < NUMBER_OF_SECONDS; i++) { + var sum = 0; + for (var j=0; j < SAMPLES_PER_SECOND; j++) { + sum += buffer[i*SAMPLES_PER_SECOND+j]; + } + var average = sum / SAMPLES_PER_SECOND; + var stars_to_print = Math.round(average / SCALE_FACTOR); + var string = "(" + (" " + Math.round(average)).slice(-4) + ") | "; + for (var j=0; j < stars_to_print; j++) { + string += "*"; + } + console.log(string); +} + +function delay( milliseconds ) { + var startTime = Date.now(); + while (Date.now() - startTime < milliseconds); +} + diff --git a/examples/ldt0028.cxx b/examples/ldt0028.cxx new file mode 100644 index 00000000..8d9c8b7b --- /dev/null +++ b/examples/ldt0028.cxx @@ -0,0 +1,90 @@ +/* + * Author: Sarah Knepper + * 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 +#include "ldt0028.h" + +int +main(int argc, char **argv) +{ +//! [Interesting] + const int NUMBER_OF_SECONDS = 10; + const int MICROSECONDS_PER_SECOND = 1000000; + const int SAMPLES_PER_SECOND = 50; + const int THRESHOLD = 100; + + // Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0 + upm::LDT0028* sensor = new upm::LDT0028(0); + + // Read the signal every 20 milliseconds for 10 seconds + std::cout << "For the next " << NUMBER_OF_SECONDS << " seconds, " + << SAMPLES_PER_SECOND << " samples will be taken every second." + << std::endl << std::endl; + uint16_t* buffer = new uint16_t[NUMBER_OF_SECONDS * SAMPLES_PER_SECOND]; + for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { + buffer[i] = (uint16_t) sensor->getSample(); + usleep(MICROSECONDS_PER_SECOND / SAMPLES_PER_SECOND); + } + + // Print the number of times the reading was greater than the threshold + int count = 0; + for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { + if (buffer[i] > THRESHOLD) { + count++; + } + } + std::cout << sensor->name() << " exceeded the threshold value of " << + THRESHOLD << " a total of " << count << " times," << std::endl + << "out of a total of " << NUMBER_OF_SECONDS*SAMPLES_PER_SECOND + << " readings." << std::endl << std::endl; + + // Print a graphical representation of the average value sampled + // each second for the past 10 seconds, using a scale factor of 15 + std::cout << "Now printing a graphical representation of the average reading " + << std::endl << "each second for the last " + << NUMBER_OF_SECONDS << " seconds." << std::endl; + const int SCALE_FACTOR = 15; + for (int i=0; i < NUMBER_OF_SECONDS; i++) { + long sum = 0; + for (int j=0; j < SAMPLES_PER_SECOND; j++) { + sum += buffer[i*SAMPLES_PER_SECOND + j]; + } + double average = (double) sum / (double) SAMPLES_PER_SECOND; + int stars_to_print = (int) round(average / SCALE_FACTOR); + std::cout << "(" << std::setw(4) << (int) round(average) << ") | "; + for (int j=0; j +# 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. + +import time +import array +import pyupm_ldt0028 as ldt0028 + +NUMBER_OF_SECONDS = 10 +SAMPLES_PER_SECOND = 50 +THRESHOLD = 100 + +# Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0 +sensor = ldt0028.LDT0028(0) + +# Read the signal every 20 milliseconds for 10 seconds +print 'For the next', NUMBER_OF_SECONDS, 'seconds,', \ + SAMPLES_PER_SECOND, 'samples will be taken every second.\n' +buffer = array.array('H') +for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND): + buffer.append(sensor.getSample()) + time.sleep(1.0/SAMPLES_PER_SECOND) + +# Print the number of times the reading was greater than the threshold +count = 0 +for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND): + if buffer[i] > THRESHOLD: + count += 1 +print sensor.name(), ' exceeded the threshold value of', \ + THRESHOLD, 'a total of', count, 'times,' +print 'out of a total of', NUMBER_OF_SECONDS*SAMPLES_PER_SECOND, \ + 'reading.\n' + +# Print a graphical representation of the average value sampled +# each second for the past 10 seconds, using a scale factor of 15 +print 'Now printing a graphical representation of the average reading ' +print 'each second for the last', NUMBER_OF_SECONDS, 'seconds.' +SCALE_FACTOR = 15 +for i in range(0, NUMBER_OF_SECONDS): + sum = 0 + for j in range(0, SAMPLES_PER_SECOND): + sum += buffer[i*SAMPLES_PER_SECOND+j] + average = sum / SAMPLES_PER_SECOND + stars_to_print = int(round(average / SCALE_FACTOR)) + print '(' + repr(int(round(average))).rjust(4) + ') |', '*' * stars_to_print + +# Delete the sensor object +del sensor diff --git a/src/ldt0028/CMakeLists.txt b/src/ldt0028/CMakeLists.txt new file mode 100644 index 00000000..e066e351 --- /dev/null +++ b/src/ldt0028/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "ldt0028") +set (libdescription "upm ldt0028") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/ldt0028/jsupm_ldt0028.i b/src/ldt0028/jsupm_ldt0028.i new file mode 100644 index 00000000..7682fb20 --- /dev/null +++ b/src/ldt0028/jsupm_ldt0028.i @@ -0,0 +1,8 @@ +%module jsupm_ldt0028 +%include "../upm.i" + +%{ + #include "ldt0028.h" +%} + +%include "ldt0028.h" diff --git a/src/ldt0028/ldt0028.cxx b/src/ldt0028/ldt0028.cxx new file mode 100644 index 00000000..a66f1344 --- /dev/null +++ b/src/ldt0028/ldt0028.cxx @@ -0,0 +1,46 @@ +/* + * Author: Sarah Knepper + * 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 "ldt0028.h" + +using namespace upm; + +LDT0028::LDT0028(unsigned int pin) { + // initialize analog input + m_pin = mraa_aio_init(pin); + m_name = "ldt0-028"; +} + +LDT0028::~LDT0028() { + // close analog input + mraa_aio_close(m_pin); +} + +std::string LDT0028::name() { + return m_name; +} + +int LDT0028::getSample() { + return mraa_aio_read(m_pin); +} diff --git a/src/ldt0028/ldt0028.h b/src/ldt0028/ldt0028.h new file mode 100644 index 00000000..23e2e622 --- /dev/null +++ b/src/ldt0028/ldt0028.h @@ -0,0 +1,78 @@ +/* + * Author: Sarah Knepper + * 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 LDT0-028 Piezo Vibration sensor library + * @defgroup ldt0028 libupm-ldt0028 + */ + +/** + * @brief C++ API for LDT0-028 PZT film-based sensors, + * such as the Grove Piezo Vibration sensor + * + * This file defines the LDT0028 C++ interface for libupm-ldt0028 + * + * @ingroup ldt0028 aio + * @snippet ldt0028.cxx Interesting + */ +class LDT0028 { + public: + /** + * LDT0028 Piezo Vibration sensor constructor + * + * @param pin AIO pin where sensor is connected + */ + LDT0028(unsigned int pin); + + /** + * LDT0028 destructor + */ + ~LDT0028(); + + /** + * Return name of this sensor + * + * @return the name of this sensor + */ + std::string name(); + + /** + * Return one sample from this sensor + * + * @return one value from this sensor + */ + int getSample(); + + protected: + std::string m_name; //!< name of this sensor + mraa_aio_context m_pin; //!< AIO pin +}; + +} diff --git a/src/ldt0028/pyupm_ldt0028.i b/src/ldt0028/pyupm_ldt0028.i new file mode 100644 index 00000000..5c4208a3 --- /dev/null +++ b/src/ldt0028/pyupm_ldt0028.i @@ -0,0 +1,10 @@ +%module pyupm_ldt0028 +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "ldt0028.h" +%{ + #include "ldt0028.h" +%} +