Browse Source

microphone: added new sensor

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
81c8baa071
7 changed files with 281 additions and 0 deletions
  1. 3
    0
      examples/CMakeLists.txt
  2. 74
    0
      examples/mic-example.cxx
  3. 5
    0
      src/mic/CMakeLists.txt
  4. 7
    0
      src/mic/jsupm_mic.i
  5. 96
    0
      src/mic/mic.cxx
  6. 86
    0
      src/mic/mic.h
  7. 10
    0
      src/mic/pyupm_mic.i

+ 3
- 0
examples/CMakeLists.txt View File

@@ -19,6 +19,7 @@ add_executable (max31855-example max31855.cxx)
19 19
 add_executable (gy65-example gy65.cxx)
20 20
 add_executable (stepmotor-example stepmotor.cxx)
21 21
 add_executable (pulsensor-example pulsensor.cxx)
22
+add_executable (mic-example mic-example.cxx)
22 23
 
23 24
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
24 25
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -36,6 +37,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/max31855)
36 37
 include_directories (${PROJECT_SOURCE_DIR}/src/gy65)
37 38
 include_directories (${PROJECT_SOURCE_DIR}/src/stepmotor)
38 39
 include_directories (${PROJECT_SOURCE_DIR}/src/pulsensor)
40
+include_directories (${PROJECT_SOURCE_DIR}/src/mic)
39 41
 
40 42
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
41 43
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -58,3 +60,4 @@ target_link_libraries (max31855-example max31855 ${CMAKE_THREAD_LIBS_INIT})
58 60
 target_link_libraries (gy65-example gy65 ${CMAKE_THREAD_LIBS_INIT})
59 61
 target_link_libraries (stepmotor-example stepmotor ${CMAKE_THREAD_LIBS_INIT})
60 62
 target_link_libraries (pulsensor-example pulsensor ${CMAKE_THREAD_LIBS_INIT})
63
+target_link_libraries (mic-example mic ${CMAKE_THREAD_LIBS_INIT})

+ 74
- 0
examples/mic-example.cxx View File

@@ -0,0 +1,74 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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 "mic.h"
28
+#include <signal.h>
29
+#include <stdlib.h>
30
+#include <sys/time.h>
31
+
32
+int is_running = 0;
33
+uint16_t buffer [128];
34
+upm::Microphone *sensor = NULL;
35
+
36
+void
37
+sig_handler(int signo)
38
+{
39
+    printf("got signal\n");
40
+    if (signo == SIGINT) {
41
+        is_running = 1;
42
+    }
43
+}
44
+
45
+//! [Interesting]
46
+int
47
+main(int argc, char **argv)
48
+{
49
+    sensor = new upm::Microphone(0);
50
+    signal(SIGINT, sig_handler);
51
+
52
+    thresholdContext ctx;
53
+    ctx.averageReading = 0;
54
+    ctx.runningAverage = 0;
55
+    ctx.averagedOver   = 2;
56
+
57
+    while (!is_running) {
58
+        int len = sensor->getSampledWindow (2, 128, buffer);
59
+        if (len) {
60
+            int thresh = sensor->findThreshold (&ctx, 30, buffer, len);
61
+            sensor->printGraph(&ctx);
62
+            if (thresh) {
63
+                // do something ....
64
+            }
65
+        }
66
+    }
67
+
68
+    std::cout << "exiting application" << std::endl;
69
+
70
+    delete sensor;
71
+
72
+    return 0;
73
+}
74
+//! [Interesting]

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

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

+ 7
- 0
src/mic/jsupm_mic.i View File

@@ -0,0 +1,7 @@
1
+%module jsupm_mic
2
+
3
+%{
4
+    #include "mic.h"
5
+%}
6
+
7
+%include "mic.h"

+ 96
- 0
src/mic/mic.cxx View File

@@ -0,0 +1,96 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@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 <iostream>
26
+#include <unistd.h>
27
+#include <stdlib.h>
28
+#include <functional>
29
+#include <string.h>
30
+#include "mic.h"
31
+
32
+using namespace upm;
33
+
34
+Microphone::Microphone(int micPin) {
35
+    // initialise analog mic input
36
+    m_micCtx = maa_aio_init(micPin);
37
+
38
+
39
+}
40
+
41
+Microphone::~Microphone() {
42
+    // close analog input
43
+    maa_result_t error;
44
+    error = maa_aio_close(m_micCtx);
45
+    if (error != MAA_SUCCESS) {
46
+        maa_result_print(error);
47
+    }
48
+}
49
+
50
+int
51
+Microphone::getSampledWindow (unsigned int freqMS, unsigned int numberOfSamples,
52
+                            uint16_t * buffer) {
53
+    int sampleIdx = 0;
54
+
55
+    // must have freq
56
+    if (!freqMS) {
57
+        return 0;
58
+    }
59
+
60
+    // too much samples
61
+    if (numberOfSamples > 0xFFFFFF) {
62
+        return 0;
63
+    }
64
+
65
+    while (sampleIdx < numberOfSamples) {
66
+        buffer[sampleIdx++] = maa_aio_read (m_micCtx);
67
+        usleep(freqMS * 1000);
68
+    }
69
+
70
+    return sampleIdx;
71
+}
72
+
73
+int
74
+Microphone::findThreshold (thresholdContext* ctx, unsigned int threshold,
75
+                                uint16_t * buffer, unsigned int len) {
76
+    long sum = 0;
77
+    for (unsigned int i = 0; i < len; i++) {
78
+        sum += buffer[i];
79
+    }
80
+
81
+    ctx->averageReading = sum / len;
82
+    ctx->runningAverage = (((ctx->averagedOver-1) * ctx->runningAverage) + ctx->averageReading) / ctx->averagedOver;
83
+
84
+    if (ctx->runningAverage > threshold) {
85
+        return ctx->runningAverage;
86
+    } else {
87
+        return 0;
88
+    }
89
+}
90
+
91
+void
92
+Microphone::printGraph (thresholdContext* ctx) {
93
+    for (int i = 0; i < ctx->runningAverage; i++)
94
+        std::cout << ".";
95
+    std::cout << std::endl;
96
+}

+ 86
- 0
src/mic/mic.h View File

@@ -0,0 +1,86 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@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 <maa/gpio.h>
28
+#include <maa/aio.h>
29
+
30
+struct thresholdContext {
31
+    long averageReading;
32
+    long runningAverage;
33
+    int averagedOver;
34
+};
35
+
36
+namespace upm {
37
+
38
+/**
39
+ * @brief C++ API for Microphone
40
+ *
41
+ * This file defines the Microphone Analog sensor
42
+ *
43
+ * @snippet mic-example.cxx Interesting
44
+ *
45
+ */
46
+class Microphone {
47
+    public:
48
+        /**
49
+         * Instanciates a Microphone object
50
+         *
51
+         * @param micPin pin where microphone is connected
52
+         */
53
+        Microphone(int micPin);
54
+
55
+        /**
56
+         * MAX31723 object destructor
57
+         */
58
+        ~Microphone();
59
+
60
+        /**
61
+         * Get samples from microphone according to provided window and
62
+         * number of samples
63
+         *
64
+         * @return freqMS time between each sample (in microseconds)
65
+         * @return numberOfSamples number of sample to sample for this window
66
+         * @return buffer bufer with sampled data
67
+         */
68
+        int getSampledWindow (unsigned int freqMS, unsigned int numberOfSamples, uint16_t * buffer);
69
+
70
+        /**
71
+         * Given sampled buffer this method will return TRUE/FALSE if threshold
72
+         * was reached
73
+         *
74
+         * @return threshold sample threshold
75
+         * @return buffer buffer with samples
76
+         * @return len bufer len
77
+         */
78
+        int findThreshold (thresholdContext* ctx, unsigned int threshold, uint16_t * buffer, unsigned int len);
79
+
80
+        void printGraph (thresholdContext* ctx);
81
+
82
+    private:
83
+        maa_aio_context    m_micCtx;
84
+};
85
+
86
+}

+ 10
- 0
src/mic/pyupm_mic.i View File

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