소스 검색

guvas12d: Initial implementation

jrvandr: removing unnecessary mraa_init()

This module implements support for the Grove UV sensor (guvas12d).

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
Jon Trulson 10 년 전
부모
커밋
38a14eb068

+ 3
- 0
examples/CMakeLists.txt 파일 보기

@@ -63,6 +63,7 @@ add_executable (ta12200-example ta12200.cxx)
63 63
 add_executable (grovelinefinder-example grovelinefinder.cxx)
64 64
 add_executable (grovevdiv-example grovevdiv.cxx)
65 65
 add_executable (grovewater-example grovewater.cxx)
66
+add_executable (guvas12d-example guvas12d.cxx)
66 67
 
67 68
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
68 69
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -113,6 +114,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
113 114
 include_directories (${PROJECT_SOURCE_DIR}/src/grovelinefinder)
114 115
 include_directories (${PROJECT_SOURCE_DIR}/src/grovevdiv)
115 116
 include_directories (${PROJECT_SOURCE_DIR}/src/grovewater)
117
+include_directories (${PROJECT_SOURCE_DIR}/src/guvas12d)
116 118
 
117 119
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
118 120
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -179,3 +181,4 @@ target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})
179 181
 target_link_libraries (grovelinefinder-example grovelinefinder ${CMAKE_THREAD_LIBS_INIT})
180 182
 target_link_libraries (grovevdiv-example grovevdiv ${CMAKE_THREAD_LIBS_INIT})
181 183
 target_link_libraries (grovewater-example grovewater ${CMAKE_THREAD_LIBS_INIT})
184
+target_link_libraries (guvas12d-example guvas12d ${CMAKE_THREAD_LIBS_INIT})

+ 72
- 0
examples/guvas12d.cxx 파일 보기

@@ -0,0 +1,72 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <signal.h>
28
+#include "guvas12d.h"
29
+
30
+using namespace std;
31
+
32
+bool shouldRun = true;
33
+
34
+// analog voltage, usually 3.3 or 5.0
35
+#define GUVAS12D_AREF   5.0
36
+#define SAMPLES_PER_QUERY 1024
37
+
38
+void sig_handler(int signo)
39
+{
40
+  if (signo == SIGINT)
41
+    shouldRun = false;
42
+}
43
+
44
+int main()
45
+{
46
+  signal(SIGINT, sig_handler);
47
+
48
+//! [Interesting]
49
+  // The was tested with the Grove UV Sensor module.
50
+  // It has a sensing range from between 200-400nm.  It's strongest
51
+  // response is around 320-360nm.
52
+
53
+  // Instantiate a GUVAS12D on analog pin A0
54
+  upm::GUVAS12D *volts = new upm::GUVAS12D(0);
55
+  
56
+  // The higher the voltage the more intense the UV radiation.
57
+
58
+  while (shouldRun)
59
+    {
60
+      cout << "AREF: " << GUVAS12D_AREF 
61
+           << ", Voltage value (higher means more UV): " 
62
+           << volts->value(GUVAS12D_AREF, SAMPLES_PER_QUERY) << endl;
63
+      
64
+      sleep(1);
65
+    }
66
+//! [Interesting]
67
+
68
+  cout << "Exiting" << endl;
69
+
70
+  delete volts;
71
+  return 0;
72
+}

+ 56
- 0
examples/javascript/guvas12d.js 파일 보기

@@ -0,0 +1,56 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*global */
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2014 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
+var UVSensor = require('jsupm_guvas12d');
29
+
30
+// Instantiate a UV sensor on analog pin A0
31
+var myUVSensor = new UVSensor.GUVAS12D(0);
32
+
33
+// analog voltage, usually 3.3 or 5.0
34
+var g_GUVAS12D_AREF = 5.0;
35
+var g_SAMPLES_PER_QUERY = 1024;
36
+
37
+setInterval(function()
38
+{
39
+	var outputStr = "AREF: " + g_GUVAS12D_AREF 
40
+					+ ", Voltage value (higher means more UV): "
41
+					+ roundNum(myUVSensor.value(g_GUVAS12D_AREF, g_SAMPLES_PER_QUERY), 6);
42
+	console.log(outputStr);
43
+}, 1000);
44
+
45
+function roundNum(num, decimalPlaces)
46
+{
47
+	var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
48
+	return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
49
+}
50
+
51
+// Print message when exiting
52
+process.on('SIGINT', function()
53
+{
54
+	console.log("Exiting...");
55
+	process.exit(0);
56
+});

+ 5
- 0
src/guvas12d/CMakeLists.txt 파일 보기

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

+ 62
- 0
src/guvas12d/guvas12d.cxx 파일 보기

@@ -0,0 +1,62 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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
+
27
+#include "guvas12d.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+GUVAS12D::GUVAS12D(int pin)
33
+{
34
+  if ( !(m_aio = mraa_aio_init(pin)) )
35
+    {
36
+      cerr << __FUNCTION__ << ": mraa_aio_init() failed" << endl;
37
+      return;
38
+    }
39
+}
40
+
41
+GUVAS12D::~GUVAS12D()
42
+{
43
+  mraa_aio_close(m_aio);
44
+}
45
+
46
+float GUVAS12D::value(float aref, unsigned int samples)
47
+{
48
+  int val;
49
+  unsigned long sum = 0;
50
+
51
+  for (int i=0; i<samples; i++)
52
+    {
53
+      val = mraa_aio_read(m_aio);
54
+      sum += val;
55
+      usleep(2000);
56
+    }
57
+
58
+  sum = sum / samples;
59
+  float volts = (float)sum * aref / 1024.0;
60
+
61
+  return volts;
62
+}

+ 65
- 0
src/guvas12d/guvas12d.h 파일 보기

@@ -0,0 +1,65 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 C++ API for the GUVAS12D UV sensor module
33
+   *
34
+   * UPM module for the GUVAS12D UV Sensor
35
+   *
36
+   * @ingroup grove analog
37
+   * @snippet guvas12d.cxx Interesting
38
+   */
39
+  class GUVAS12D {
40
+  public:
41
+    /**
42
+     * GUVAS12D sensor constructor
43
+     *
44
+     * @param pin analog pin to use
45
+     */
46
+    GUVAS12D(int pin);
47
+    /**
48
+     * GUVAS12D Destructor
49
+     */
50
+    ~GUVAS12D();
51
+    /**
52
+     * Get the averaged voltage value from the sensor
53
+     *
54
+     * @param aref the reference voltage in use (5.0 or 3.3 usually)
55
+     * @param samples number of samples to average over
56
+     * @return the averaged voltage reading
57
+     */
58
+    float value(float aref, unsigned int samples);
59
+
60
+  private:
61
+    mraa_aio_context m_aio;
62
+  };
63
+}
64
+
65
+

+ 8
- 0
src/guvas12d/jsupm_guvas12d.i 파일 보기

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

+ 9
- 0
src/guvas12d/pyupm_guvas12d.i 파일 보기

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