Browse Source

grovemoisture: Initial UPM implementation

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Jon Trulson 10 years ago
parent
commit
0ef81a3b1c

+ 3
- 0
examples/CMakeLists.txt View File

@@ -57,6 +57,7 @@ add_executable (adc121c021-example adc121c021.cxx)
57 57
 add_executable (ds1307-example ds1307.cxx)
58 58
 add_executable (a110x-example a110x.cxx)
59 59
 add_executable (gp2y0a21yk-example gp2y0a21yk.cxx)
60
+add_executable (grovemoisture-example grovemoisture.cxx)
60 61
 
61 62
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
62 63
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -101,6 +102,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/adc121c021)
101 102
 include_directories (${PROJECT_SOURCE_DIR}/src/ds1307)
102 103
 include_directories (${PROJECT_SOURCE_DIR}/src/a110x)
103 104
 include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a21yk)
105
+include_directories (${PROJECT_SOURCE_DIR}/src/grovemoisture)
104 106
 
105 107
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
106 108
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -161,3 +163,4 @@ target_link_libraries (adc121c021-example adc121c021 ${CMAKE_THREAD_LIBS_INIT})
161 163
 target_link_libraries (ds1307-example ds1307 ${CMAKE_THREAD_LIBS_INIT})
162 164
 target_link_libraries (a110x-example a110x ${CMAKE_THREAD_LIBS_INIT})
163 165
 target_link_libraries (gp2y0a21yk-example gp2y0a21yk ${CMAKE_THREAD_LIBS_INIT})
166
+target_link_libraries (grovemoisture-example grovemoisture ${CMAKE_THREAD_LIBS_INIT})

+ 75
- 0
examples/grovemoisture.cxx View File

@@ -0,0 +1,75 @@
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 "grovemoisture.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 Moisture sensor on analog pin A0
47
+  upm::GroveMoisture* moisture = new upm::GroveMoisture(0);
48
+  
49
+  // Values (approximate): 
50
+  // 0-300, sensor in air or dry soil
51
+  // 300-600, sensor in humid soil
52
+  // 600+, sensor in wet soil or submerged in water.
53
+  // Read the value every second and print the corresponding moisture level
54
+  while (shouldRun)
55
+    {
56
+      int val = moisture->value();
57
+      cout << "Moisture value: " << val << ", ";
58
+      if (val >= 0 && val < 300)
59
+        cout << "dry";
60
+      else if (val >= 300 && val < 600)
61
+        cout << "moist";
62
+      else
63
+        cout << "wet";
64
+
65
+      cout << endl;
66
+
67
+      sleep(1);
68
+    }
69
+//! [Interesting]
70
+
71
+  cout << "Exiting" << endl;
72
+
73
+  delete moisture;
74
+  return 0;
75
+}

+ 57
- 0
examples/javascript/grovemoisture.js View File

@@ -0,0 +1,57 @@
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 Moisture module
29
+var grove_moisture = require('jsupm_grovemoisture');
30
+
31
+// Instantiate a Grove Moisture sensor on analog pin A0
32
+var myMoistureObj = new grove_moisture.GroveMoisture(0);
33
+
34
+// Values (approximate):
35
+// 0-300,   sensor in air or dry soil
36
+// 300-600, sensor in humid soil
37
+// 600+,    sensor in wet soil or submerged in water
38
+// Read the value every second and print the corresponding moisture level
39
+setInterval(function()
40
+{
41
+	var result;
42
+	var moisture_val = parseInt(myMoistureObj.value());
43
+	if (moisture_val >= 0 && moisture_val < 300)
44
+		result = "Dry";
45
+	else if (moisture_val >= 300 && moisture_val < 600)
46
+		result = "Moist";
47
+	else
48
+		result = "Wet";
49
+	console.log("Moisture value: " + moisture_val + ", " + result);
50
+}, 1000);
51
+
52
+// Print message when exiting
53
+process.on('SIGINT', function()
54
+{
55
+	console.log("Exiting...");
56
+	process.exit(0);
57
+});

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

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

+ 46
- 0
src/grovemoisture/grovemoisture.cxx View File

@@ -0,0 +1,46 @@
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 "grovemoisture.h"
28
+
29
+using namespace upm;
30
+
31
+GroveMoisture::GroveMoisture(int pin)
32
+{
33
+  mraa_init();
34
+
35
+  m_aio = mraa_aio_init(pin);
36
+}
37
+
38
+GroveMoisture::~GroveMoisture()
39
+{
40
+  mraa_aio_close(m_aio);
41
+}
42
+
43
+int GroveMoisture::value()
44
+{
45
+  return mraa_aio_read(m_aio);
46
+}

+ 68
- 0
src/grovemoisture/grovemoisture.h View File

@@ -0,0 +1,68 @@
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 Grove Moisture Sensor
33
+   *
34
+   * UPM module for the Grove Moisture Sensor.
35
+   * This sensor can be used to detect the moisture content
36
+   * of soil or whether there is water around the sensor.
37
+   * As the moisture content increases, so does the value that is read.
38
+   * Note that this sensor is not designed to be left in soil
39
+   * nor to be used outdoors.
40
+   *
41
+   * @ingroup grove analog
42
+   * @snippet grovemoisture.cxx Interesting
43
+   */
44
+  class GroveMoisture {
45
+  public:
46
+    /**
47
+     * Grove analog moisture sensor constructor
48
+     *
49
+     * @param pin analog pin to use
50
+     */
51
+    GroveMoisture(int pin);
52
+    /**
53
+     * GroveMoisture Destructor
54
+     */
55
+    ~GroveMoisture();
56
+    /**
57
+     * Get the moisture value from the sensor
58
+     *
59
+     * @return the moisture reading
60
+     */
61
+    int value();
62
+
63
+  private:
64
+    mraa_aio_context m_aio;
65
+  };
66
+}
67
+
68
+

+ 8
- 0
src/grovemoisture/jsupm_grovemoisture.i View File

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

+ 9
- 0
src/grovemoisture/pyupm_grovemoisture.i View File

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