Browse Source

grove: add grove button support and examples

Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
sknepper 10 years ago
parent
commit
801209678b

+ 2
- 0
examples/CMakeLists.txt View File

@@ -1,6 +1,7 @@
1 1
 add_executable (hmc5883l-example hmc5883l.cxx)
2 2
 add_executable (groveled-example groveled.cxx)
3 3
 add_executable (grovetemp-example grovetemp.cxx)
4
+add_executable (grovebutton-example grovebutton.cxx)
4 5
 add_executable (groverotary-example groverotary.cxx)
5 6
 add_executable (groveslide-example groveslide.cxx)
6 7
 add_executable (lcm-lcd-example lcm-lcd.cxx)
@@ -85,6 +86,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ldt0028)
85 86
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
86 87
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
87 88
 target_link_libraries (grovetemp-example grove ${CMAKE_THREAD_LIBS_INIT})
89
+target_link_libraries (grovebutton-example grove ${CMAKE_THREAD_LIBS_INIT})
88 90
 target_link_libraries (groverotary-example grove ${CMAKE_THREAD_LIBS_INIT})
89 91
 target_link_libraries (groveslide-example grove ${CMAKE_THREAD_LIBS_INIT})
90 92
 target_link_libraries (lcm-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT})

+ 49
- 0
examples/grovebutton.cxx View File

@@ -0,0 +1,49 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.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 "grove.h"
28
+
29
+int
30
+main(int argc, char **argv)
31
+{
32
+    // This example uses GPIO 0
33
+//! [Interesting]
34
+
35
+    // Create the button object using GPIO pin 0
36
+    upm::GroveButton* button = new upm::GroveButton(0);
37
+
38
+    // Read the input and print, waiting one second between readings
39
+    while( 1 ) {
40
+        std::cout << button->name() << " value is " << button->value() << std::endl;
41
+        sleep(1);
42
+    }
43
+
44
+    // Delete the button object
45
+    delete button;
46
+//! [Interesting]
47
+
48
+    return 0;
49
+}

+ 35
- 0
examples/javascript/grovebutton.js View File

@@ -0,0 +1,35 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.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
+// Load Grove module
26
+var groveSensor = require('jsupm_grove');
27
+
28
+// Create the button object using GPIO pin 0
29
+var button = new groveSensor.GroveButton(0);
30
+
31
+// Read the input and print, waiting one second between readings
32
+function readButtonValue() {
33
+    console.log(button.name() + " value is " + button.value());
34
+}
35
+setInterval(readButtonValue, 1000);

+ 35
- 0
examples/python/grovebutton.py View File

@@ -0,0 +1,35 @@
1
+# Author: Sarah Knepper <sarah.knepper@intel.com>
2
+# Copyright (c) 2014 Intel Corporation.
3
+#
4
+# Permission is hereby granted, free of charge, to any person obtaining
5
+# a copy of this software and associated documentation files (the
6
+# "Software"), to deal in the Software without restriction, including
7
+# without limitation the rights to use, copy, modify, merge, publish,
8
+# distribute, sublicense, and/or sell copies of the Software, and to
9
+# permit persons to whom the Software is furnished to do so, subject to
10
+# the following conditions:
11
+#
12
+# The above copyright notice and this permission notice shall be
13
+# included in all copies or substantial portions of the Software.
14
+#
15
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+import time
24
+import pyupm_grove as grove
25
+
26
+# Create the button object using GPIO pin 0
27
+button = grove.GroveButton(0)
28
+
29
+# Read the input and print, waiting one second between readings
30
+while 1:
31
+    print button.name(), ' value is ', button.value()
32
+    time.sleep(1)
33
+
34
+# Delete the button object
35
+del button

+ 25
- 0
src/grove/grove.cxx View File

@@ -193,3 +193,28 @@ float GroveSlide::ref_voltage()
193 193
 {
194 194
     return m_ref_voltage;
195 195
 }
196
+
197
+//// GroveButton ////
198
+
199
+GroveButton::GroveButton(unsigned int pin)
200
+{
201
+    mraa_init();
202
+    m_gpio = mraa_gpio_init(pin);
203
+    mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
204
+    m_name = "Button Sensor";
205
+}
206
+
207
+GroveButton::~GroveButton()
208
+{
209
+    mraa_gpio_close(m_gpio);
210
+}
211
+
212
+std::string GroveButton::name()
213
+{
214
+    return m_name;
215
+}
216
+
217
+int GroveButton::value()
218
+{
219
+    return mraa_gpio_read(m_gpio);
220
+}

+ 37
- 0
src/grove/grove.h View File

@@ -247,4 +247,41 @@ class GroveSlide: public Grove {
247 247
         float m_ref_voltage;
248 248
 };
249 249
 
250
+/**
251
+ * @brief C++ API for Grove button
252
+ *
253
+ * Very basic UPM module for Grove button
254
+ *
255
+ * @ingroup grove gpio
256
+ * @snippet grovebutton.cxx Interesting
257
+ */
258
+class GroveButton: public Grove {
259
+    public:
260
+        /**
261
+         * Grove button constructor
262
+         *
263
+         * @param gpio pin to use
264
+         */
265
+        GroveButton(unsigned int pin);
266
+        /**
267
+         * Grove button destructor
268
+         */
269
+        ~GroveButton();
270
+        /**
271
+         * Get name of sensor
272
+         *
273
+         * @return the name of this sensor
274
+         */
275
+        std::string name();
276
+        /**
277
+         * Get value from GPIO pin
278
+         *
279
+         * @return the value from the GPIO pin
280
+         */
281
+        int value();
282
+    private:
283
+        std::string m_name;
284
+        mraa_gpio_context m_gpio;
285
+};
286
+
250 287
 }