Bläddra i källkod

grove: added GroveRelay class for Grove Relay

Signed-off-by: Alexandru Radovici <alexandru.radovici@wyliodrin.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Alexandru Radovici 10 år sedan
förälder
incheckning
eca9ed95c8

+ 2
- 0
examples/CMakeLists.txt Visa fil

@@ -1,5 +1,6 @@
1 1
 add_executable (hmc5883l-example hmc5883l.cxx)
2 2
 add_executable (groveled-example groveled.cxx)
3
+add_executable (groverelay-example groverelay.cxx)
3 4
 add_executable (grovelight-example grovelight.cxx)
4 5
 add_executable (grovetemp-example grovetemp.cxx)
5 6
 add_executable (grovebutton-example grovebutton.cxx)
@@ -95,6 +96,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/enc03r)
95 96
 
96 97
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
97 98
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
99
+target_link_libraries (groverelay-example grove ${CMAKE_THREAD_LIBS_INIT})
98 100
 target_link_libraries (grovelight-example grove ${CMAKE_THREAD_LIBS_INIT})
99 101
 target_link_libraries (grovetemp-example grove ${CMAKE_THREAD_LIBS_INIT})
100 102
 target_link_libraries (grovebutton-example grove ${CMAKE_THREAD_LIBS_INIT})

+ 58
- 0
examples/groverelay.cxx Visa fil

@@ -0,0 +1,58 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.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 "grove.h"
28
+
29
+int
30
+main(int argc, char **argv)
31
+{
32
+    // This example uses GPIO 0
33
+//! [Interesting]
34
+
35
+    // Create the relay switch object using GPIO pin 0
36
+    upm::GroveRelay* relay = new upm::GroveRelay(0);
37
+
38
+    // Close and then open the relay switch 3 times,
39
+    // waiting one second each time.  The LED on the relay switch
40
+    // will light up when the switch is on (closed).
41
+    // The switch will also make a noise between transitions.
42
+    for ( int i = 0; i < 3; i++ ) {
43
+        relay->on();
44
+        if ( relay->isOn() ) 
45
+            std::cout << relay->name() << " is on" << std::endl;
46
+        sleep(1);
47
+        relay->off();
48
+        if ( relay->isOff() ) 
49
+            std::cout << relay->name() << " is off" << std::endl;
50
+        sleep(1);
51
+    }
52
+
53
+    // Delete the relay switch object
54
+    delete relay;
55
+//! [Interesting]
56
+
57
+    return 0;
58
+}

+ 49
- 0
examples/javascript/groverelay.js Visa fil

@@ -0,0 +1,49 @@
1
+/*
2
+ * Author: Sarah Knepper <sarah.knepper@intel.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
+// Load Grove module
26
+var groveSensor = require('jsupm_grove');
27
+
28
+// Create the relay switch object using GPIO pin 0
29
+var relay = new groveSensor.GroveRelay(0);
30
+
31
+// Close and then open the relay switch 3 times,
32
+// waiting one second each time.  The LED on the relay switch
33
+// will light up when the switch is on (closed).
34
+// The switch will also make a noise between transitions.
35
+var i = 0;
36
+var waiting = setInterval(function() {
37
+        if ( i % 2 == 0 ) {
38
+            relay.on();
39
+            if ( relay.isOn() )
40
+                console.log(relay.name() + " is on");
41
+        } else {
42
+            relay.off();
43
+            if ( relay.isOff() )
44
+                console.log(relay.name() + " is off");
45
+        }
46
+        i++;
47
+        if ( i == 6) clearInterval(waiting);
48
+        }, 1000);
49
+

+ 44
- 0
examples/python/groverelay.py Visa fil

@@ -0,0 +1,44 @@
1
+# Author: Sarah Knepper <sarah.knepper@intel.com>
2
+# Copyright (c) 2015 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 relay switch object using GPIO pin 0
27
+relay = grove.GroveRelay(0)
28
+
29
+# Close and then open the relay switch 3 times,
30
+# waiting one second each time.  The LED on the relay switch
31
+# will light up when the switch is on (closed).
32
+# The switch will also make a noise between transitions.
33
+for i in range (0,3):
34
+    relay.on()
35
+    if relay.isOn():
36
+        print relay.name(), 'is on'
37
+    time.sleep(1)
38
+    relay.off()
39
+    if relay.isOff():
40
+        print relay.name(), 'is off'
41
+    time.sleep(1)
42
+
43
+# Delete the relay switch object
44
+del relay

+ 35
- 0
src/grove/grove.cxx Visa fil

@@ -64,6 +64,41 @@ mraa_result_t GroveLed::off()
64 64
     return write(0);
65 65
 }
66 66
 
67
+//// GroveRelay ////
68
+
69
+GroveRelay::GroveRelay(unsigned int pin)
70
+{
71
+    mraa_init();
72
+    m_gpio = mraa_gpio_init(pin);
73
+    mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);
74
+    m_name = "Relay Switch";
75
+}
76
+
77
+GroveRelay::~GroveRelay()
78
+{
79
+    mraa_gpio_close(m_gpio);
80
+}
81
+
82
+mraa_result_t GroveRelay::on()
83
+{
84
+    return mraa_gpio_write(m_gpio, 1);
85
+}
86
+
87
+mraa_result_t GroveRelay::off()
88
+{
89
+    return mraa_gpio_write(m_gpio, 0);
90
+}
91
+
92
+bool GroveRelay::isOn()
93
+{
94
+    return mraa_gpio_read(m_gpio) == 1;
95
+}
96
+
97
+bool GroveRelay::isOff()
98
+{
99
+    return mraa_gpio_read(m_gpio) == 0;
100
+}
101
+
67 102
 //// GroveTemp ////
68 103
 
69 104
 GroveTemp::GroveTemp(unsigned int pin)

+ 53
- 0
src/grove/grove.h Visa fil

@@ -99,6 +99,59 @@ class GroveLed: public Grove {
99 99
         mraa_gpio_context m_gpio;
100 100
 };
101 101
 
102
+/**
103
+ * @brief C++ API for Grove Relay
104
+ *
105
+ * UPM module for Grove relay switch.  The Grove relay is a
106
+ * digital normally-open switch that uses low voltage or current to 
107
+ * control a higher voltage and/or higher current.  When closed, 
108
+ * the indicator LED will light up and current is allowed to flow.
109
+ *
110
+ * @ingroup grove gpio
111
+ * @snippet groverelay.cxx Interesting
112
+ */
113
+class GroveRelay: public Grove {
114
+    public:
115
+        /**
116
+         * Grove relay constructor
117
+         *
118
+         * @param gpio pin to use
119
+         */
120
+        GroveRelay(unsigned int pin);
121
+        /**
122
+         * Grove relay destructor
123
+         */
124
+        ~GroveRelay();
125
+        /**
126
+         * Set the relay switch to on (close).  This allows current
127
+         * to flow and lights up the indicator LED.
128
+         *
129
+         * @return 0 on success; non-zero otherwise
130
+         */
131
+        mraa_result_t on();
132
+        /**
133
+         * Set the relay switch to off (open).  This stops current
134
+         * from flowing and the indicator LED will not be lit.
135
+         *
136
+         * @return 0 on success; non-zero otherwise
137
+         */
138
+        mraa_result_t off();
139
+        /**
140
+         * Returns whether or not the relay switch is closed.
141
+         *
142
+         * @return true if the switch is on (closed); false otherwise
143
+         */
144
+        bool isOn();
145
+        /**
146
+         * Returns whether or not the relay switch is open.
147
+         *
148
+         * @return true if the switch is off (open); false otherwise
149
+         */
150
+        bool isOff();
151
+    private:
152
+        mraa_gpio_context m_gpio;
153
+};
154
+
102 155
 /**
103 156
  * @brief C++ API for Grove temperature sensor
104 157
  *