Browse Source

grovecollision: initial implementation

This implements support for the Grove Collision Sensor.

Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
Zion Orent 9 years ago
parent
commit
ae10518a0f

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

@@ -88,6 +88,7 @@ add_executable (adxl335-example adxl335.cxx)
88 88
 add_executable (hmtrp-example hmtrp.cxx)
89 89
 add_executable (nunchuck-example nunchuck.cxx)
90 90
 add_executable (otp538u-example otp538u.cxx)
91
+add_executable (grovecollision-example grovecollision.cxx)
91 92
 
92 93
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
93 94
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -158,6 +159,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/adxl335)
158 159
 include_directories (${PROJECT_SOURCE_DIR}/src/hmtrp)
159 160
 include_directories (${PROJECT_SOURCE_DIR}/src/nunchuck)
160 161
 include_directories (${PROJECT_SOURCE_DIR}/src/otp538u)
162
+include_directories (${PROJECT_SOURCE_DIR}/src/grovecollision)
161 163
 
162 164
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
163 165
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -247,3 +249,4 @@ target_link_libraries (adxl335-example adxl335 ${CMAKE_THREAD_LIBS_INIT})
247 249
 target_link_libraries (hmtrp-example hmtrp ${CMAKE_THREAD_LIBS_INIT})
248 250
 target_link_libraries (nunchuck-example nunchuck ${CMAKE_THREAD_LIBS_INIT})
249 251
 target_link_libraries (otp538u-example otp538u ${CMAKE_THREAD_LIBS_INIT})
252
+target_link_libraries (grovecollision-example grovecollision ${CMAKE_THREAD_LIBS_INIT})

+ 69
- 0
examples/c++/grovecollision.cxx View File

@@ -0,0 +1,69 @@
1
+/*
2
+* Author: Zion Orent <zorent@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
+#include <iostream>
25
+#include <unistd.h>
26
+#include <signal.h>
27
+#include "grovecollision.h"
28
+
29
+using namespace std;
30
+
31
+int shouldRun = true;
32
+
33
+void sig_handler(int signo)
34
+{
35
+  if (signo == SIGINT)
36
+    shouldRun = false;
37
+}
38
+
39
+int main(int argc, char **argv)
40
+{
41
+  signal(SIGINT, sig_handler);
42
+
43
+//! [Interesting]
44
+  // The was tested with the Grove Collision Sensor
45
+  // Instantiate a Grove Collision on digital pin D2
46
+  upm::GroveCollision* collision = new upm::GroveCollision(2);
47
+
48
+  bool collisionState = false;
49
+  cout << "No collision" << endl;
50
+  while (shouldRun)
51
+  {
52
+	if (collision->isColliding() && !collisionState)
53
+	{
54
+		cout << "Collision!" << endl;
55
+		collisionState = true;
56
+	}
57
+	else if (collisionState)
58
+	{
59
+		cout << "No collision" << endl;
60
+		collisionState = false;
61
+	}
62
+  }
63
+
64
+//! [Interesting]
65
+  cout << "Exiting" << endl;
66
+
67
+  delete collision;
68
+  return 0;
69
+}

+ 62
- 0
examples/javascript/grovecollision.js View File

@@ -0,0 +1,62 @@
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 collision_lib = require("jsupm_grovecollision");
28
+
29
+// The was tested with the Grove Collision Sensor
30
+// Instantiate a Grove Collision on digital pin D2
31
+var collision_obj = new collision_lib.GroveCollision(2);
32
+
33
+var collisionState = false;
34
+console.log("No collision");
35
+
36
+// Having an infinate loop prevents nodeJS from catching Cntl-C
37
+// We need to catch Cntl-C to clean up memory
38
+// Instead, we check the collision sensor every millisecond
39
+var myInterval = setInterval(function()
40
+{
41
+	if (collision_obj.isColliding() && !collisionState)
42
+	{
43
+		console.log("Collision!");
44
+		collisionState = true;
45
+	}
46
+	else if (collisionState)
47
+	{
48
+		console.log("No collision");
49
+		collisionState = false;
50
+	}
51
+}, 1);
52
+
53
+// When exiting: clear interval, clean up memory, and print message
54
+process.on('SIGINT', function()
55
+{
56
+	clearInterval(myInterval);
57
+	collision_obj = null;
58
+	collision_lib.cleanUp();
59
+	collision_lib = null;
60
+	console.log("Exiting...");
61
+	process.exit(0);
62
+});

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

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

+ 44
- 0
src/grovecollision/grovecollision.cxx View File

@@ -0,0 +1,44 @@
1
+/*
2
+ * Author: Zion Orent <sorent@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 "grovecollision.h"
26
+
27
+using namespace upm;
28
+
29
+GroveCollision::GroveCollision(int pin)
30
+{
31
+    m_gpio = mraa_gpio_init(pin);
32
+    mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
33
+}
34
+
35
+GroveCollision::~GroveCollision()
36
+{
37
+    mraa_gpio_close(m_gpio);
38
+}
39
+
40
+bool GroveCollision::isColliding()
41
+{
42
+	// Collisions cause 0; no collision is 1
43
+	return (!(bool)mraa_gpio_read(m_gpio));
44
+}

+ 63
- 0
src/grovecollision/grovecollision.h View File

@@ -0,0 +1,63 @@
1
+/*
2
+ * Author: Zion Orent <sorent@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 <mraa/gpio.h>
27
+
28
+namespace upm {
29
+  /**
30
+   * @brief UPM module for the Grove Collision
31
+   * @defgroup grovecollision libupm-grovecollision
32
+   */
33
+  /**
34
+   * @brief C++ API for the Grove Collision
35
+   * 
36
+   * The Grove Collision Sensor can detect whether any
37
+   * collision movement or vibration happens.
38
+   * It will output a low pulse signal when vibration is detected. 
39
+   *
40
+   * @ingroup gpio grovecollision
41
+   * @snippet grovecollision.cxx Interesting
42
+   */
43
+  class GroveCollision {
44
+  public:
45
+    /**
46
+     * Grove Collision Constructor
47
+     *
48
+     * @param pin digital pin to use
49
+     */
50
+    GroveCollision(int pin);
51
+    /**
52
+     * Grove Collision Destructor
53
+     */
54
+    ~GroveCollision();
55
+    /**
56
+     * @return bool  returns whether something is colliding with sensor
57
+     */
58
+     bool isColliding();
59
+
60
+  private:
61
+        mraa_gpio_context m_gpio;
62
+	};
63
+}

+ 8
- 0
src/grovecollision/jsupm_grovecollision.i View File

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

+ 9
- 0
src/grovecollision/pyupm_grovecollision.i View File

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