소스 검색

grove: sliding potentiometer

Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Mihai Tudor Panu 10 년 전
부모
커밋
cebc339d91
7개의 변경된 파일215개의 추가작업 그리고 0개의 파일을 삭제
  1. BIN
      docs/images/groveslide.jpeg
  2. 4
    0
      examples/CMakeLists.txt
  3. 50
    0
      examples/groveslide.cxx
  4. 42
    0
      examples/javascript/groveslide.js
  5. 39
    0
      examples/python/groveslide.py
  6. 33
    0
      src/grove/grove.cxx
  7. 47
    0
      src/grove/grove.h

BIN
docs/images/groveslide.jpeg 파일 보기


+ 4
- 0
examples/CMakeLists.txt 파일 보기

@@ -1,6 +1,8 @@
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 (groverotary-example groverotary.cxx)
5
+add_executable (groveslide-example groveslide.cxx)
4 6
 add_executable (lcm-lcd-example lcm-lcd.cxx)
5 7
 add_executable (rgb-lcd-example rgb-lcd.cxx)
6 8
 add_executable (buzzer-sound-example buzzer-sound.cxx)
@@ -77,6 +79,8 @@ include_directories (${PROJECT_SOURCE_DIR}/src/tsl2561)
77 79
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
78 80
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
79 81
 target_link_libraries (grovetemp-example grove ${CMAKE_THREAD_LIBS_INIT})
82
+target_link_libraries (groverotary-example grove ${CMAKE_THREAD_LIBS_INIT})
83
+target_link_libraries (groveslide-example grove ${CMAKE_THREAD_LIBS_INIT})
80 84
 target_link_libraries (lcm-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
81 85
 target_link_libraries (rgb-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
82 86
 target_link_libraries (buzzer-sound-example buzzer ${CMAKE_THREAD_LIBS_INIT})

+ 50
- 0
examples/groveslide.cxx 파일 보기

@@ -0,0 +1,50 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@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
+
26
+#include <unistd.h>
27
+#include <iostream>
28
+#include <iomanip>
29
+#include "grove.h"
30
+
31
+using namespace std;
32
+
33
+int main ()
34
+{
35
+//! [Interesting]
36
+    upm::GroveSlide* slide = new upm::GroveSlide(0);    // Instantiate new grove slide potentiometer on analog pin A0
37
+
38
+    cout << slide->name() << endl;
39
+
40
+    while(true) {
41
+        float adc_value = slide->raw_value();       // Read raw value
42
+        float volts = slide->voltage_value();    // Read voltage, board reference set at 5.0V
43
+        fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts);
44
+
45
+        usleep(2500000);    // Sleep for 2.5s
46
+    }
47
+//! [Interesting]
48
+    delete slide;
49
+    return 0;
50
+}

+ 42
- 0
examples/javascript/groveslide.js 파일 보기

@@ -0,0 +1,42 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@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
+var upm_grove = require('jsupm_grove');
26
+
27
+//setup access analog input Analog pin #0 (A0)
28
+var groveSlide = new upm_grove.GroveSlide(0);
29
+
30
+loop();
31
+
32
+function loop()
33
+{
34
+    var raw = groveSlide.raw_value();
35
+    var volts = groveSlide.voltage_value();
36
+
37
+    //write the slider values to the console
38
+    console.log("Slider Value: " + raw + " = " + volts.toFixed(2) + " V");
39
+
40
+    //wait 2 s then call function again
41
+    setTimeout(loop, 2000);
42
+}

+ 39
- 0
examples/python/groveslide.py 파일 보기

@@ -0,0 +1,39 @@
1
+# Author: Mihai Tudor Panu <mihai.t.panu@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
+from time import sleep
24
+import pyupm_grove as grove
25
+
26
+# New Grove Slider on AIO pin 0
27
+slider = grove.GroveSlide(0)
28
+
29
+# Loop indefinitely
30
+while True:
31
+
32
+    # Read values
33
+    raw = slider.raw_value()
34
+    volts = slider.voltage_value()
35
+
36
+    print "Slider value: ", raw , " = %.2f" % volts , " V"
37
+
38
+    # Sleep for 2.5 s
39
+    sleep(2.5)

+ 33
- 0
src/grove/grove.cxx 파일 보기

@@ -160,3 +160,36 @@ float GroveRotary::rel_rad()
160 160
 {
161 161
     return GroveRotary::rel_deg() * M_PI / 180.0;
162 162
 }
163
+
164
+//// GroveSlide ////
165
+
166
+GroveSlide::GroveSlide(unsigned int pin, float ref_voltage)
167
+{
168
+    mraa_init();
169
+    m_aio = mraa_aio_init(pin);
170
+    m_ref_voltage = ref_voltage;
171
+    m_name = "Slide Potentiometer";
172
+}
173
+
174
+GroveSlide::~GroveSlide()
175
+{
176
+    mraa_aio_close(m_aio);
177
+}
178
+
179
+float GroveSlide::raw_value()
180
+{
181
+    return (float) mraa_aio_read(m_aio);
182
+}
183
+
184
+float GroveSlide::voltage_value()
185
+{
186
+    // conversion to Volts
187
+    float a = GroveSlide::raw_value();
188
+    a = m_ref_voltage * a / 1023.0 ;
189
+    return a;
190
+}
191
+
192
+float GroveSlide::ref_voltage()
193
+{
194
+    return m_ref_voltage;
195
+}

+ 47
- 0
src/grove/grove.h 파일 보기

@@ -200,4 +200,51 @@ class GroveRotary: public Grove {
200 200
         static const int m_max_angle = 300;
201 201
 };
202 202
 
203
+/**
204
+ * @brief C++ API for Grove Slide Potentiometer
205
+ *
206
+ * Very basic UPM module for Grove slide potentiometer on analog,
207
+ * returns either raw value or scaled voltage value.
208
+ *
209
+ * @ingroup grove analog
210
+ * @snippet groveslide.cxx Interesting
211
+ * @image html groveslide.jpeg
212
+ */
213
+class GroveSlide: public Grove {
214
+    public:
215
+        /**
216
+         * Grove analog slide potentiometer constructor
217
+         *
218
+         * @param pin number of analog pin to use
219
+         *
220
+         * @param ref_voltage the reference voltage the board is set to as float, e.g. 3.3 or 5.0 (default)
221
+         */
222
+        GroveSlide(unsigned int pin, float ref_voltage = 5.0);
223
+        /**
224
+         * GroveSlide Destructor
225
+         */
226
+        ~GroveSlide();
227
+        /**
228
+         * Get raw value from AIO pin
229
+         *
230
+         * @return the raw value from the ADC
231
+         */
232
+        float raw_value();
233
+        /**
234
+         * Get the voltage value from the pin
235
+         *
236
+         * @return the voltage reading based on the reference voltage
237
+         */
238
+        float voltage_value();
239
+        /**
240
+         * Get the board's reference voltage passed on object initialization
241
+         *
242
+         * @return the reference voltage the class was set for
243
+         */
244
+        float ref_voltage();
245
+    private:
246
+        mraa_aio_context m_aio;
247
+        float m_ref_voltage;
248
+};
249
+
203 250
 }