Browse Source

hyld9767: Initial implementation

This driver was developed using the DFRobot Loudness sensor

http://www.dfrobot.com/index.php?route=product/product&product_id=83

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Sisinty Sasmita Patra<sisinty.s.patra@intel.com>
Jon Trulson 9 years ago
parent
commit
ad1cc81c0d

+ 3
- 0
examples/c++/CMakeLists.txt View File

@@ -133,6 +133,7 @@ add_executable (mpu60x0-example mpu60x0.cxx)
133 133
 add_executable (ak8975-example ak8975.cxx)
134 134
 add_executable (lsm9ds0-example lsm9ds0.cxx)
135 135
 add_executable (eboled-example eboled.cxx)
136
+add_executable (hyld9767-example hyld9767.cxx)
136 137
 
137 138
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
138 139
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -239,6 +240,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/rgbringcoder)
239 240
 include_directories (${PROJECT_SOURCE_DIR}/src/hp20x)
240 241
 include_directories (${PROJECT_SOURCE_DIR}/src/pn532)
241 242
 include_directories (${PROJECT_SOURCE_DIR}/src/lsm9ds0)
243
+include_directories (${PROJECT_SOURCE_DIR}/src/hyld9767)
242 244
 
243 245
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
244 246
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -373,3 +375,4 @@ target_link_libraries (mpu60x0-example mpu9150 ${CMAKE_THREAD_LIBS_INIT})
373 375
 target_link_libraries (ak8975-example mpu9150 ${CMAKE_THREAD_LIBS_INIT})
374 376
 target_link_libraries (lsm9ds0-example lsm9ds0 ${CMAKE_THREAD_LIBS_INIT})
375 377
 target_link_libraries (eboled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
378
+target_link_libraries (hyld9767-example hyld9767 ${CMAKE_THREAD_LIBS_INIT})

+ 68
- 0
examples/c++/hyld9767.cxx View File

@@ -0,0 +1,68 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 <signal.h>
28
+#include "hyld9767.h"
29
+
30
+using namespace std;
31
+
32
+bool shouldRun = true;
33
+
34
+#define HYLD9767_AREF   5.0
35
+
36
+void sig_handler(int signo)
37
+{
38
+  if (signo == SIGINT)
39
+    shouldRun = false;
40
+}
41
+
42
+int main()
43
+{
44
+  signal(SIGINT, sig_handler);
45
+
46
+//! [Interesting]
47
+
48
+  // Instantiate a HYLD9767 on analog pin A0, with an analog
49
+  // reference voltage of HYLD9767_AREF
50
+  upm::HYLD9767 *loud = new upm::HYLD9767(0, HYLD9767_AREF);
51
+  
52
+  // Every tenth of a second, sample the loudness and output it's
53
+  // corresponding analog voltage. 
54
+
55
+  while (shouldRun)
56
+    {
57
+      cout << "Detected loudness (volts): " << loud->loudness() << endl;
58
+      
59
+      usleep(100000);
60
+    }
61
+
62
+//! [Interesting]
63
+
64
+  cout << "Exiting" << endl;
65
+
66
+  delete loud;
67
+  return 0;
68
+}

+ 52
- 0
examples/javascript/hyld9767.js View File

@@ -0,0 +1,52 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+ * Author: Jon Trulson <jtrulson@ics.com>
6
+ * Copyright (c) 2015 Intel Corporation.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+
29
+var sensorObj = require('jsupm_hyld9767');
30
+
31
+// Instantiate a HYLD9767 on analog pin A0, with an analog
32
+// reference voltage of 5.0
33
+var sensor = new sensorObj.HYLD9767(0, 5.0);
34
+
35
+// Every tenth of a second, sample the loudness and output it's
36
+// corresponding analog voltage. 
37
+
38
+setInterval(function()
39
+{
40
+    console.log("Detected loudness (volts): " + sensor.loudness());
41
+}, 100);
42
+
43
+// exit on ^C
44
+process.on('SIGINT', function()
45
+{
46
+    sensor = null;
47
+    sensorObj.cleanUp();
48
+    sensorObj = null;
49
+    console.log("Exiting.");
50
+    process.exit(0);
51
+});
52
+

+ 50
- 0
examples/python/hyld9767.py View File

@@ -0,0 +1,50 @@
1
+#!/usr/bin/python
2
+# Author: Jon Trulson <jtrulson@ics.com>
3
+# Copyright (c) 2015 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
+import time, sys, signal, atexit
25
+import pyupm_hyld9767 as sensorObj
26
+
27
+# Instantiate a HYLD9767 on analog pin A0, with an analog
28
+# reference voltage of 5.0
29
+sensor = sensorObj.HYLD9767(0, 5.0)
30
+
31
+## Exit handlers ##
32
+# This function stops python from printing a stacktrace when you hit control-C
33
+def SIGINTHandler(signum, frame):
34
+	raise SystemExit
35
+
36
+# This function lets you run code on exit
37
+def exitHandler():
38
+	print "Exiting"
39
+	sys.exit(0)
40
+
41
+# Register exit handlers
42
+atexit.register(exitHandler)
43
+signal.signal(signal.SIGINT, SIGINTHandler)
44
+
45
+# Every tenth of a second, sample the loudness and output it's
46
+# corresponding analog voltage. 
47
+
48
+while (1):
49
+        print "Detected loudness (volts): ", sensor.loudness()
50
+	time.sleep(.1)

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

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

+ 48
- 0
src/hyld9767/hyld9767.cxx View File

@@ -0,0 +1,48 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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
+
27
+#include "hyld9767.h"
28
+
29
+using namespace std;
30
+using namespace upm;
31
+
32
+HYLD9767::HYLD9767(int pin, float aref) :
33
+  m_aio(pin)
34
+{
35
+  m_aRes = m_aio.getBit();
36
+  m_aref = aref;
37
+}
38
+
39
+HYLD9767::~HYLD9767()
40
+{
41
+}
42
+
43
+float HYLD9767::loudness()
44
+{
45
+  int val = m_aio.read();
46
+
47
+  return(val * (m_aref / float(1 << m_aRes)));
48
+}

+ 96
- 0
src/hyld9767/hyld9767.h View File

@@ -0,0 +1,96 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 <iostream>
27
+#include <string>
28
+#include <mraa/aio.hpp>
29
+
30
+// EZ series is volts/512
31
+#define HYLD9767_RES  512
32
+
33
+namespace upm {
34
+  /**
35
+   * @brief DFRobot Loudness Sensor V2
36
+   * @defgroup hyld9767 libupm-hyld9767
37
+   * @ingroup dfrobot analog sound
38
+   */
39
+
40
+  /**
41
+   * @library hyld9767
42
+   * @sensor hyld9767
43
+   * @comname DFRobot Loudness Sensor V2
44
+   * @altname HYLD9767
45
+   * @type sound
46
+   * @man dfrobot
47
+   * @web http://www.dfrobot.com/index.php?route=product/product&product_id=83
48
+   * @con analog
49
+   *
50
+   * @brief API for the DFRobot Loudness Sensor V2
51
+   *
52
+   * This sensor returns an analog voltage proportional to the
53
+   * loudness of the ambient environment.  It's output does not
54
+   * correspond to a particular sound level in decibels.
55
+   *
56
+   * This device uses an HYLD9767 electret microphone for sound input.
57
+   *
58
+   * This driver was developed using the DFRobot Loudness Sensor V2
59
+   *
60
+   * @snippet hyld9767.cxx Interesting
61
+   */
62
+
63
+  class HYLD9767 {
64
+  public:
65
+
66
+    /**
67
+     * HYLD9767 constructor
68
+     *
69
+     * @param pin Analog pin to use
70
+     * @param aref Analog reference voltage; default is 5.0 V
71
+     */
72
+    HYLD9767(int pin, float aref=5.0);
73
+
74
+    /**
75
+     * HYLD9767 destructor
76
+     */
77
+    ~HYLD9767();
78
+
79
+    /**
80
+     * Returns the voltage detected on the analog pin
81
+     *
82
+     * @return The detected voltage
83
+     */
84
+    float loudness();
85
+
86
+  protected:
87
+    mraa::Aio m_aio;
88
+
89
+  private:
90
+    float m_aref;
91
+    // ADC resolution
92
+    int m_aRes;
93
+  };
94
+}
95
+
96
+

+ 8
- 0
src/hyld9767/javaupm_hyld9767.i View File

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

+ 8
- 0
src/hyld9767/jsupm_hyld9767.i View File

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

+ 9
- 0
src/hyld9767/pyupm_hyld9767.i View File

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

+ 7
- 1
src/upm.h View File

@@ -278,6 +278,12 @@
278 278
  * @ingroup byman
279 279
  */
280 280
 
281
+/**
282
+ * @brief DFRobot
283
+ * @defgroup dfrobot DFRobot
284
+ * @ingroup byman
285
+ */
286
+
281 287
 /**
282 288
  * @brief EpicTinker
283 289
  * @defgroup epict EpicTinker
@@ -358,4 +364,4 @@
358 364
  * @brief Robotics Kit - Sensors for your robot
359 365
  * @defgroup robok Robotics Kit
360 366
  * @ingroup bykit
361
- */
367
+ */