瀏覽代碼

rpr220: Initial implementation

The module implements the RPR220 IR Reflective Sensor.  It was tested
with the Grove IR Reflective Sensor.

It includes 2 examples: rpr220.cxx, demonstrating the simple use case
of querying the current status.

rpr220-intr.cxx demonstrates the use of this class to register an
Interrupt Service Routine (ISR) to count transitions, which might be
more appropriate for some use cases, such as measuring RPM's.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 10 年之前
父節點
當前提交
ae7b2ad04f

+ 5
- 0
examples/CMakeLists.txt 查看文件

@@ -78,6 +78,8 @@ add_executable (my9221-example my9221.cxx)
78 78
 add_executable (grove_mcfled-example grove_mcfled.cxx)
79 79
 add_executable (rotaryencoder-example rotaryencoder.cxx)
80 80
 add_executable (adxl345-example adxl345.cxx)
81
+add_executable (rpr220-example rpr220.cxx)
82
+add_executable (rpr220-intr-example rpr220-intr.cxx)
81 83
 
82 84
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
83 85
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -142,6 +144,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/biss0001)
142 144
 include_directories (${PROJECT_SOURCE_DIR}/src/my9221)
143 145
 include_directories (${PROJECT_SOURCE_DIR}/src/rotaryencoder)
144 146
 include_directories (${PROJECT_SOURCE_DIR}/src/adxl345)
147
+include_directories (${PROJECT_SOURCE_DIR}/src/rpr220)
145 148
 
146 149
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
147 150
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -223,3 +226,5 @@ target_link_libraries (my9221-example my9221 ${CMAKE_THREAD_LIBS_INIT})
223 226
 target_link_libraries (grove_mcfled-example grove ${CMAKE_THREAD_LIBS_INIT})
224 227
 target_link_libraries (rotaryencoder-example rotaryencoder ${CMAKE_THREAD_LIBS_INIT})
225 228
 target_link_libraries (adxl345-example adxl345 ${CMAKE_THREAD_LIBS_INIT})
229
+target_link_libraries (rpr220-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
230
+target_link_libraries (rpr220-intr-example rpr220 ${CMAKE_THREAD_LIBS_INIT})

+ 49
- 0
examples/javascript/rpr220.js 查看文件

@@ -0,0 +1,49 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*
4
+* Author: Zion Orent <zorent@ics.com>
5
+* Copyright (c) 2015 Intel Corporation.
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining
8
+* a copy of this software and associated documentation files (the
9
+* "Software"), to deal in the Software without restriction, including
10
+* without limitation the rights to use, copy, modify, merge, publish,
11
+* distribute, sublicense, and/or sell copies of the Software, and to
12
+* permit persons to whom the Software is furnished to do so, subject to
13
+* the following conditions:
14
+*
15
+* The above copyright notice and this permission notice shall be
16
+* included in all copies or substantial portions of the Software.
17
+*
18
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+*/
26
+
27
+var reflectiveSensor = require("jsupm_rpr220");
28
+
29
+// This example uses a simple method to determine current status
30
+
31
+// Instantiate an RPR220 digital pin D2
32
+// This was tested on the Grove IR Reflective Sensor
33
+var myReflectiveSensor = new reflectiveSensor.RPR220(2);
34
+
35
+var myInterval = setInterval(function()
36
+{
37
+	if (myReflectiveSensor.blackDetected())
38
+		console.log("Black detected");
39
+	else
40
+		console.log("Black NOT detected");
41
+}, 100);
42
+
43
+// When exiting: clear interval and print message
44
+process.on('SIGINT', function()
45
+{
46
+	clearInterval(myInterval);
47
+	console.log("Exiting...");
48
+	process.exit(0);
49
+});

+ 78
- 0
examples/rpr220-intr.cxx 查看文件

@@ -0,0 +1,78 @@
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 "rpr220.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+volatile unsigned int counter = 0;
41
+
42
+// Our interrupt handler
43
+void rprISR(void *arg)
44
+{
45
+  counter++;
46
+}
47
+
48
+
49
+int main()
50
+{
51
+  signal(SIGINT, sig_handler);
52
+
53
+//! [Interesting]
54
+  // This example uses an interrupt handler to increment a counter
55
+  
56
+  // Instantiate an RPR220 digital pin D2
57
+  // This was tested on the Grove IR Reflective Sensor
58
+
59
+  upm::RPR220* rpr220 = new upm::RPR220(2);
60
+  
61
+  // Here, we setup our Interupt Service Routine (ISR) to count
62
+  // 'black' pulses detected.
63
+
64
+  rpr220->installISR(rprISR, NULL);
65
+
66
+  while (shouldRun)
67
+    {
68
+      cout << "Counter: " << counter << endl;
69
+
70
+      sleep(1);
71
+    }
72
+//! [Interesting]
73
+
74
+  cout << "Exiting..." << endl;
75
+
76
+  delete rpr220;
77
+  return 0;
78
+}

+ 68
- 0
examples/rpr220.cxx 查看文件

@@ -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 "rpr220.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+
41
+int main()
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+//! [Interesting]
46
+  // This example uses a simple method to determine current status
47
+
48
+  // Instantiate an RPR220 digital pin D2
49
+  // This was tested on the Grove IR Reflective Sensor
50
+
51
+  upm::RPR220* rpr220 = new upm::RPR220(2);
52
+  
53
+  while (shouldRun)
54
+    {
55
+      if (rpr220->blackDetected())
56
+        cout << "Black detected" << endl;
57
+      else
58
+        cout << "Black NOT detected" << endl;
59
+
60
+      usleep(100000);           // 100ms
61
+    }
62
+//! [Interesting]
63
+
64
+  cout << "Exiting..." << endl;
65
+
66
+  delete rpr220;
67
+  return 0;
68
+}

+ 5
- 0
src/rpr220/CMakeLists.txt 查看文件

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

+ 8
- 0
src/rpr220/jsupm_rpr220.i 查看文件

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

+ 9
- 0
src/rpr220/pyupm_rpr220.i 查看文件

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

+ 74
- 0
src/rpr220/rpr220.cxx 查看文件

@@ -0,0 +1,74 @@
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 "rpr220.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+RPR220::RPR220(int pin)
33
+{
34
+  m_isrInstalled = false;
35
+
36
+  if ( !(m_gpio = mraa_gpio_init(pin)) )
37
+   {
38
+      cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
39
+      return;
40
+    }
41
+
42
+  mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
43
+}
44
+
45
+RPR220::~RPR220()
46
+{
47
+  if (m_isrInstalled)
48
+    uninstallISR();
49
+
50
+  mraa_gpio_close(m_gpio);
51
+}
52
+
53
+bool RPR220::blackDetected()
54
+{
55
+  return (mraa_gpio_read(m_gpio) ? true : false);
56
+}
57
+
58
+void RPR220::installISR(void (*isr)(void *), void *arg)
59
+{
60
+  if (m_isrInstalled)
61
+    uninstallISR();
62
+
63
+  // install our interrupt handler
64
+  mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING, 
65
+                isr, arg);
66
+  m_isrInstalled = true;
67
+}
68
+
69
+void RPR220::uninstallISR()
70
+{
71
+  mraa_gpio_isr_exit(m_gpio);
72
+  m_isrInstalled = false;
73
+}
74
+

+ 93
- 0
src/rpr220/rpr220.h 查看文件

@@ -0,0 +1,93 @@
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 <string>
27
+#include <mraa/gpio.h>
28
+
29
+namespace upm {
30
+
31
+  /**
32
+   * @brief C++ API for the RPR220 based Grove IR Reflective Sensor. 
33
+   *
34
+   * UPM module for the Grove IR Reflective Sensor.  The sensitivity
35
+   * can be adjusted with the potentiometer on the sensor module.  It
36
+   * has a range of approximately 15mm, and a fast response time.
37
+   *
38
+   * It detects high contrast dark areas on a light background.
39
+   *
40
+   * This module allows the user to determine the current status
41
+   * (black detected or not).  Additionally, if desired, an Interrupt
42
+   * Service Routine (ISR) can be installed that will be called when
43
+   * black is detected.  Either method can be used, depending on your
44
+   * use case.
45
+
46
+   * @ingroup grove gpio
47
+   * @snippet rpr220.cxx Interesting
48
+   * @snippet rpr220-intr.cxx Interesting
49
+   */
50
+  class RPR220 {
51
+  public:
52
+    /**
53
+     * Grove IR Reflective Sensor constructor
54
+     *
55
+     * @param pin digital pin to use
56
+     */
57
+    RPR220(int pin);
58
+
59
+    /**
60
+     * RPR220 Destructor
61
+     */
62
+    ~RPR220();
63
+
64
+    /**
65
+     * Get the status of the pin, true means black detected
66
+     *
67
+     * @return true if the sensor is detecting black
68
+     */
69
+    bool blackDetected();
70
+
71
+    /**
72
+     * Install an Interrupt Service Routine (ISR) to be called when
73
+     * black is detected
74
+     *
75
+     * @param fptr function pointer to function to be called on interrupt
76
+     * @param arg pointer to an object that will be supplied as an
77
+     * arguement to the ISR.
78
+     */
79
+    void installISR(void (*isr)(void *), void *arg);
80
+
81
+    /**
82
+     * Uninstall the previously installed Interrupt Service Routine (ISR)
83
+     *
84
+     */
85
+    void uninstallISR();
86
+
87
+  private:
88
+    bool m_isrInstalled;
89
+    mraa_gpio_context m_gpio;
90
+  };
91
+}
92
+
93
+