Browse Source

ldt0028: Added support for LDT0-028 PZT film-based sensors

Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
sknepper 10 years ago
parent
commit
c5369315e9

+ 3
- 0
examples/CMakeLists.txt View File

@@ -44,6 +44,7 @@ add_executable (nrf_ble_broadcast-example ble_broadcast.cxx)
44 44
 add_executable (tsl2561-example tsl2561.cxx)
45 45
 add_executable (htu21d-example htu21d.cxx)
46 46
 add_executable (mpl3115a2-example mpl3115a2.cxx)
47
+add_executable (ldt0028-example ldt0028.cxx)
47 48
 
48 49
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
49 50
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -79,6 +80,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/lol)
79 80
 include_directories (${PROJECT_SOURCE_DIR}/src/tsl2561)
80 81
 include_directories (${PROJECT_SOURCE_DIR}/src/htu21d)
81 82
 include_directories (${PROJECT_SOURCE_DIR}/src/mpl3115a2)
83
+include_directories (${PROJECT_SOURCE_DIR}/src/ldt0028)
82 84
 
83 85
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
84 86
 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
126 128
 target_link_libraries (tsl2561-example tsl2561 ${CMAKE_THREAD_LIBS_INIT})
127 129
 target_link_libraries (htu21d-example htu21d ${CMAKE_THREAD_LIBS_INIT})
128 130
 target_link_libraries (mpl3115a2-example mpl3115a2 ${CMAKE_THREAD_LIBS_INIT})
131
+target_link_libraries (ldt0028-example ldt0028 ${CMAKE_THREAD_LIBS_INIT})

+ 82
- 0
examples/javascript/ldt0028.js View File

@@ -0,0 +1,82 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+// Load Grove module
26
+var sensorModule = require('jsupm_ldt0028');
27
+
28
+var NUMBER_OF_SECONDS = 10;
29
+var MILLISECONDS_PER_SECOND = 1000;
30
+var SAMPLES_PER_SECOND = 50;
31
+var THRESHOLD = 100;
32
+
33
+// Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
34
+var sensor = new sensorModule.LDT0028(0);
35
+
36
+// Read the signal every 20 milliseconds for 10 seconds
37
+console.log("For the next " + NUMBER_OF_SECONDS + " seconds, " +
38
+            SAMPLES_PER_SECOND + " samples will be taken every second.");
39
+console.log("");
40
+var buffer = [];
41
+for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
42
+    buffer.push(sensor.getSample());
43
+    delay(MILLISECONDS_PER_SECOND / SAMPLES_PER_SECOND );
44
+}
45
+
46
+// Print the number of times the reading was greater than the threshold
47
+var count = 0;
48
+for (var i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
49
+    if (buffer[i] > THRESHOLD) {
50
+        count++;
51
+    }
52
+}
53
+console.log(sensor.name() + " exceeded the threshold value of " +
54
+        THRESHOLD + " a total of " + count + " times,");
55
+console.log("out of a total of " + NUMBER_OF_SECONDS*SAMPLES_PER_SECOND +
56
+            " readings.");
57
+console.log("");
58
+
59
+// Print a graphical representation of the average value sampled
60
+// each second for the past 10 seconds, using a scale factor of 15
61
+console.log("Now printing a graphical representation of the average reading ");
62
+console.log("each second for the last " + NUMBER_OF_SECONDS + " seconds.");
63
+var SCALE_FACTOR = 15;
64
+for (var i=0; i < NUMBER_OF_SECONDS; i++) {
65
+    var sum = 0;
66
+    for (var j=0; j < SAMPLES_PER_SECOND; j++) {
67
+        sum += buffer[i*SAMPLES_PER_SECOND+j];
68
+    }
69
+    var average = sum / SAMPLES_PER_SECOND;
70
+    var stars_to_print = Math.round(average / SCALE_FACTOR);
71
+    var string = "(" + ("    " + Math.round(average)).slice(-4) + ") | ";
72
+    for (var j=0; j < stars_to_print; j++) {
73
+        string += "*";
74
+    }
75
+    console.log(string);
76
+}
77
+
78
+function delay( milliseconds ) {
79
+    var startTime = Date.now();
80
+    while (Date.now() - startTime < milliseconds);
81
+}
82
+

+ 90
- 0
examples/ldt0028.cxx View File

@@ -0,0 +1,90 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include <iomanip>
28
+#include <cmath>
29
+#include "ldt0028.h"
30
+
31
+int
32
+main(int argc, char **argv)
33
+{
34
+//! [Interesting]
35
+    const int NUMBER_OF_SECONDS = 10;
36
+    const int MICROSECONDS_PER_SECOND = 1000000;
37
+    const int SAMPLES_PER_SECOND = 50;
38
+    const int THRESHOLD = 100;
39
+
40
+    // Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
41
+    upm::LDT0028* sensor = new upm::LDT0028(0);
42
+
43
+    // Read the signal every 20 milliseconds for 10 seconds
44
+    std::cout << "For the next " << NUMBER_OF_SECONDS << " seconds, " 
45
+              << SAMPLES_PER_SECOND << " samples will be taken every second." 
46
+              << std::endl << std::endl;
47
+    uint16_t* buffer = new uint16_t[NUMBER_OF_SECONDS * SAMPLES_PER_SECOND];
48
+    for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
49
+        buffer[i] = (uint16_t) sensor->getSample();
50
+        usleep(MICROSECONDS_PER_SECOND / SAMPLES_PER_SECOND);
51
+    }
52
+
53
+    // Print the number of times the reading was greater than the threshold
54
+    int count = 0;
55
+    for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) {
56
+        if (buffer[i] > THRESHOLD) {
57
+            count++;
58
+        }
59
+    }
60
+    std::cout << sensor->name() << " exceeded the threshold value of " <<
61
+        THRESHOLD << " a total of " << count << " times," << std::endl
62
+        << "out of a total of " << NUMBER_OF_SECONDS*SAMPLES_PER_SECOND 
63
+        << " readings." << std::endl << std::endl;
64
+
65
+    // Print a graphical representation of the average value sampled
66
+    // each second for the past 10 seconds, using a scale factor of 15
67
+    std::cout << "Now printing a graphical representation of the average reading "
68
+        << std::endl << "each second for the last " 
69
+        << NUMBER_OF_SECONDS << " seconds." << std::endl;
70
+    const int SCALE_FACTOR = 15;
71
+    for (int i=0; i < NUMBER_OF_SECONDS; i++) {
72
+        long sum = 0;
73
+        for (int j=0; j < SAMPLES_PER_SECOND; j++) {
74
+            sum += buffer[i*SAMPLES_PER_SECOND + j];
75
+        }
76
+        double average = (double) sum / (double) SAMPLES_PER_SECOND;
77
+        int stars_to_print = (int) round(average / SCALE_FACTOR);
78
+        std::cout << "(" << std::setw(4) << (int) round(average) << ") | ";
79
+        for (int j=0; j<stars_to_print; j++) {
80
+            std::cout << "*";
81
+        }
82
+        std::cout << std::endl;
83
+    }
84
+
85
+    // Delete the sensor object
86
+    delete sensor;
87
+//! [Interesting]
88
+
89
+    return 0;
90
+}

+ 66
- 0
examples/python/ldt0028.py View File

@@ -0,0 +1,66 @@
1
+# Author: Sarah Knepper <sarah.knepper@intel.com>
2
+# Copyright (c) 2014 Intel Corporation.
3
+#
4
+# Permission is hereby granted, free of charge, to any person obtaining
5
+# a copy of this software and associated documentation files (the
6
+# "Software"), to deal in the Software without restriction, including
7
+# without limitation the rights to use, copy, modify, merge, publish,
8
+# distribute, sublicense, and/or sell copies of the Software, and to
9
+# permit persons to whom the Software is furnished to do so, subject to
10
+# the following conditions:
11
+#
12
+# The above copyright notice and this permission notice shall be
13
+# included in all copies or substantial portions of the Software.
14
+#
15
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+import time
24
+import array
25
+import pyupm_ldt0028 as ldt0028
26
+
27
+NUMBER_OF_SECONDS = 10
28
+SAMPLES_PER_SECOND = 50
29
+THRESHOLD = 100
30
+
31
+# Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0
32
+sensor = ldt0028.LDT0028(0)
33
+
34
+# Read the signal every 20 milliseconds for 10 seconds
35
+print 'For the next', NUMBER_OF_SECONDS, 'seconds,', \
36
+      SAMPLES_PER_SECOND, 'samples will be taken every second.\n'
37
+buffer = array.array('H')
38
+for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
39
+    buffer.append(sensor.getSample())
40
+    time.sleep(1.0/SAMPLES_PER_SECOND)
41
+
42
+# Print the number of times the reading was greater than the threshold
43
+count = 0
44
+for i in range(0, NUMBER_OF_SECONDS * SAMPLES_PER_SECOND):
45
+    if buffer[i] > THRESHOLD:
46
+        count += 1
47
+print sensor.name(), ' exceeded the threshold value of', \
48
+        THRESHOLD, 'a total of', count, 'times,'
49
+print 'out of a total of', NUMBER_OF_SECONDS*SAMPLES_PER_SECOND, \
50
+        'reading.\n'
51
+
52
+# Print a graphical representation of the average value sampled
53
+# each second for the past 10 seconds, using a scale factor of 15
54
+print 'Now printing a graphical representation of the average reading '
55
+print 'each second for the last', NUMBER_OF_SECONDS, 'seconds.'
56
+SCALE_FACTOR = 15
57
+for i in range(0, NUMBER_OF_SECONDS):
58
+    sum = 0
59
+    for j in range(0, SAMPLES_PER_SECOND):
60
+        sum += buffer[i*SAMPLES_PER_SECOND+j]
61
+    average = sum / SAMPLES_PER_SECOND
62
+    stars_to_print = int(round(average / SCALE_FACTOR))
63
+    print '(' + repr(int(round(average))).rjust(4) + ') |', '*' * stars_to_print
64
+
65
+# Delete the sensor object
66
+del sensor

+ 5
- 0
src/ldt0028/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "ldt0028")
2
+set (libdescription "upm ldt0028")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/ldt0028/jsupm_ldt0028.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_ldt0028
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "ldt0028.h"
6
+%}
7
+
8
+%include "ldt0028.h"

+ 46
- 0
src/ldt0028/ldt0028.cxx View File

@@ -0,0 +1,46 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include "ldt0028.h"
26
+
27
+using namespace upm;
28
+
29
+LDT0028::LDT0028(unsigned int pin) {
30
+    // initialize analog input
31
+    m_pin = mraa_aio_init(pin);
32
+    m_name = "ldt0-028";
33
+}
34
+
35
+LDT0028::~LDT0028() {
36
+    // close analog input
37
+    mraa_aio_close(m_pin);
38
+}
39
+
40
+std::string LDT0028::name() {
41
+    return m_name;
42
+}
43
+
44
+int LDT0028::getSample() {
45
+    return mraa_aio_read(m_pin);
46
+}

+ 78
- 0
src/ldt0028/ldt0028.h View File

@@ -0,0 +1,78 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+#pragma once
25
+
26
+#include <string>
27
+#include <mraa/aio.h>
28
+
29
+namespace upm {
30
+
31
+/**
32
+ * @brief LDT0-028 Piezo Vibration sensor library
33
+ * @defgroup ldt0028 libupm-ldt0028
34
+ */
35
+
36
+/**
37
+ * @brief C++ API for LDT0-028 PZT film-based sensors,
38
+ * such as the Grove Piezo Vibration sensor
39
+ *
40
+ * This file defines the LDT0028 C++ interface for libupm-ldt0028
41
+ *
42
+ * @ingroup ldt0028 aio
43
+ * @snippet ldt0028.cxx Interesting
44
+ */
45
+class LDT0028 {
46
+    public:
47
+        /**
48
+         * LDT0028 Piezo Vibration sensor constructor
49
+         *
50
+         * @param pin AIO pin where sensor is connected
51
+         */
52
+        LDT0028(unsigned int pin);
53
+
54
+        /**
55
+         * LDT0028 destructor
56
+         */
57
+        ~LDT0028();
58
+
59
+        /**
60
+         * Return name of this sensor
61
+         *
62
+         * @return the name of this sensor
63
+         */
64
+        std::string name();
65
+
66
+        /**
67
+         * Return one sample from this sensor
68
+         *
69
+         * @return one value from this sensor
70
+         */
71
+        int getSample();
72
+
73
+    protected:
74
+        std::string         m_name; //!< name of this sensor
75
+        mraa_aio_context    m_pin;  //!< AIO pin
76
+};
77
+
78
+}

+ 10
- 0
src/ldt0028/pyupm_ldt0028.i View File

@@ -0,0 +1,10 @@
1
+%module pyupm_ldt0028
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "ldt0028.h"
7
+%{
8
+    #include "ldt0028.h"
9
+%}
10
+