Kaynağa Gözat

grovewater: Initial implementation

jrvandr: removing unecessary mraa_init()

This module implements support for the Grove Water sensor.

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 yıl önce
ebeveyn
işleme
9784f6e6ab

+ 3
- 0
examples/CMakeLists.txt Dosyayı Görüntüle

@@ -62,6 +62,7 @@ add_executable (groveehr-example groveehr.cxx)
62 62
 add_executable (ta12200-example ta12200.cxx)
63 63
 add_executable (grovelinefinder-example grovelinefinder.cxx)
64 64
 add_executable (grovevdiv-example grovevdiv.cxx)
65
+add_executable (grovewater-example grovewater.cxx)
65 66
 
66 67
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
67 68
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -111,6 +112,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/groveehr)
111 112
 include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
112 113
 include_directories (${PROJECT_SOURCE_DIR}/src/grovelinefinder)
113 114
 include_directories (${PROJECT_SOURCE_DIR}/src/grovevdiv)
115
+include_directories (${PROJECT_SOURCE_DIR}/src/grovewater)
114 116
 
115 117
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
116 118
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -176,3 +178,4 @@ target_link_libraries (groveehr-example groveehr ${CMAKE_THREAD_LIBS_INIT})
176 178
 target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})
177 179
 target_link_libraries (grovelinefinder-example grovelinefinder ${CMAKE_THREAD_LIBS_INIT})
178 180
 target_link_libraries (grovevdiv-example grovevdiv ${CMAKE_THREAD_LIBS_INIT})
181
+target_link_libraries (grovewater-example grovewater ${CMAKE_THREAD_LIBS_INIT})

+ 65
- 0
examples/grovewater.cxx Dosyayı Görüntüle

@@ -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
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include <signal.h>
28
+#include "grovewater.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
+  // Instantiate a Grove Water sensor on digital pin D2
47
+  upm::GroveWater* water = new upm::GroveWater(2);
48
+  
49
+  while (shouldRun)
50
+    {
51
+      bool val = water->isWet();
52
+      if (val)
53
+        cout << "Sensor is wet." << endl;
54
+      else
55
+        cout << "Sensor is dry." << endl;
56
+
57
+      sleep(1);
58
+    }
59
+//! [Interesting]
60
+
61
+  cout << "Exiting..." << endl;
62
+
63
+  delete water;
64
+  return 0;
65
+}

+ 49
- 0
examples/javascript/grovewater.js Dosyayı Görüntüle

@@ -0,0 +1,49 @@
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
+// Load Grove module
29
+var waterSensor = require('jsupm_grovewater');
30
+
31
+// Instantiate a Grove Water sensor on digital pin D2
32
+var water = new waterSensor.GroveWater(2);
33
+
34
+// Read whether the sensor is wet/dry, waiting one second between readings
35
+function readWaterState()
36
+{
37
+	if (water.isWet())
38
+		console.log("Sensor is wet");
39
+	else
40
+		console.log("Sensor is dry");
41
+}
42
+setInterval(readWaterState, 1000);
43
+
44
+// Print message when exiting
45
+process.on('SIGINT', function()
46
+{
47
+	console.log("Exiting...");
48
+	process.exit(0);
49
+});

+ 5
- 0
src/grovewater/CMakeLists.txt Dosyayı Görüntüle

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

+ 51
- 0
src/grovewater/grovewater.cxx Dosyayı Görüntüle

@@ -0,0 +1,51 @@
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 "grovewater.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+GroveWater::GroveWater(int pin)
33
+{
34
+  if ( !(m_gpio = mraa_gpio_init(pin)) )
35
+    {
36
+      cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
37
+      return;
38
+    }
39
+
40
+  mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
41
+}
42
+
43
+GroveWater::~GroveWater()
44
+{
45
+  mraa_gpio_close(m_gpio);
46
+}
47
+
48
+bool GroveWater::isWet()
49
+{
50
+  return (!mraa_gpio_read(m_gpio) ? true : false);
51
+}

+ 63
- 0
src/grovewater/grovewater.h Dosyayı Görüntüle

@@ -0,0 +1,63 @@
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/gpio.h>
28
+
29
+namespace upm {
30
+
31
+  /**
32
+   * @brief C++ API for the Grove Water Sensor
33
+   *
34
+   * UPM module for the Grove Water Sensor
35
+   *
36
+   * @ingroup grove gpio
37
+   * @snippet grovewater.cxx Interesting
38
+   */
39
+  class GroveWater {
40
+  public:
41
+    /**
42
+     * Grove digital water sensor constructor
43
+     *
44
+     * @param pin digital pin to use
45
+     */
46
+    GroveWater(int pin);
47
+    /**
48
+     * GroveWater Destructor
49
+     */
50
+    ~GroveWater();
51
+    /**
52
+     * Get the water (wet/not wet) value from the sensor
53
+     *
54
+     * @return True if the sensor is wet, false otherwise
55
+     */
56
+    bool isWet();
57
+
58
+  private:
59
+    mraa_gpio_context m_gpio;
60
+  };
61
+}
62
+
63
+

+ 8
- 0
src/grovewater/jsupm_grovewater.i Dosyayı Görüntüle

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

+ 9
- 0
src/grovewater/pyupm_grovewater.i Dosyayı Görüntüle

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