ソースを参照

rotaryencoder: Initial implementation

This module implements supoort for the Grove Rotary Encoder, though it
should function with any Rotary Encoder utilizing two GPIOs.

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 年 前
コミット
d307376abc

+ 3
- 0
examples/CMakeLists.txt ファイルの表示

@@ -76,6 +76,7 @@ add_executable (rfr359f-example rfr359f.cxx)
76 76
 add_executable (biss0001-example biss0001.cxx)
77 77
 add_executable (my9221-example my9221.cxx)
78 78
 add_executable (grove_mcfled-example grove_mcfled.cxx)
79
+add_executable (rotaryencoder-example rotaryencoder.cxx)
79 80
 
80 81
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
81 82
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -138,6 +139,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/grovespeaker)
138 139
 include_directories (${PROJECT_SOURCE_DIR}/src/rfr359f)
139 140
 include_directories (${PROJECT_SOURCE_DIR}/src/biss0001)
140 141
 include_directories (${PROJECT_SOURCE_DIR}/src/my9221)
142
+include_directories (${PROJECT_SOURCE_DIR}/src/rotaryencoder)
141 143
 
142 144
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
143 145
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -217,3 +219,4 @@ target_link_libraries (rfr359f-example rfr359f ${CMAKE_THREAD_LIBS_INIT})
217 219
 target_link_libraries (biss0001-example biss0001 ${CMAKE_THREAD_LIBS_INIT})
218 220
 target_link_libraries (my9221-example my9221 ${CMAKE_THREAD_LIBS_INIT})
219 221
 target_link_libraries (grove_mcfled-example grove ${CMAKE_THREAD_LIBS_INIT})
222
+target_link_libraries (rotaryencoder-example rotaryencoder ${CMAKE_THREAD_LIBS_INIT})

+ 43
- 0
examples/javascript/rotaryencoder.js ファイルの表示

@@ -0,0 +1,43 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*
4
+* Author: Zion Orent <zorent@ics.com>
5
+* Copyright (c) 2015 Intel Corporation.
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining
8
+* a copy of this software and associated documentation files (the
9
+* "Software"), to deal in the Software without restriction, including
10
+* without limitation the rights to use, copy, modify, merge, publish,
11
+* distribute, sublicense, and/or sell copies of the Software, and to
12
+* permit persons to whom the Software is furnished to do so, subject to
13
+* the following conditions:
14
+*
15
+* The above copyright notice and this permission notice shall be
16
+* included in all copies or substantial portions of the Software.
17
+*
18
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+*/
26
+
27
+var rotaryEncoder = require("jsupm_rotaryencoder");
28
+
29
+// Instantiate a Grove Rotary Encoder, using signal pins D2 and D3
30
+var myRotaryEncoder = new rotaryEncoder.RotaryEncoder(2, 3);
31
+
32
+var myInterval = setInterval(function()
33
+{
34
+	console.log("Position: " + myRotaryEncoder.position());
35
+}, 100);
36
+
37
+// When exiting: clear interval and print message
38
+process.on('SIGINT', function()
39
+{
40
+	clearInterval(myInterval);
41
+	console.log("Exiting...");
42
+	process.exit(0);
43
+});

+ 61
- 0
examples/rotaryencoder.cxx ファイルの表示

@@ -0,0 +1,61 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <signal.h>
28
+#include "rotaryencoder.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 Rotary Encoder, using signal pins D2 and D3
47
+  upm::RotaryEncoder* rotaryencoder = new upm::RotaryEncoder(2, 3);
48
+  
49
+  while (shouldRun)
50
+    {
51
+      cout << "Position: " << rotaryencoder->position() << endl;
52
+      usleep(100000);
53
+    }
54
+
55
+//! [Interesting]
56
+
57
+  cout << "Exiting..." << endl;
58
+
59
+  delete rotaryencoder;
60
+  return 0;
61
+}

+ 5
- 0
src/rotaryencoder/CMakeLists.txt ファイルの表示

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

+ 8
- 0
src/rotaryencoder/jsupm_rotaryencoder.i ファイルの表示

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

+ 9
- 0
src/rotaryencoder/pyupm_rotaryencoder.i ファイルの表示

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

+ 91
- 0
src/rotaryencoder/rotaryencoder.cxx ファイルの表示

@@ -0,0 +1,91 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <iostream>
26
+
27
+#include "rotaryencoder.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+RotaryEncoder::RotaryEncoder(int pinA, int pinB)
33
+{
34
+  if ( !(m_gpioA = mraa_gpio_init(pinA)) )
35
+    {
36
+      cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
37
+      return;
38
+    }
39
+
40
+  mraa_gpio_dir(m_gpioA, MRAA_GPIO_IN);
41
+
42
+  if ( !(m_gpioB = mraa_gpio_init(pinB)) )
43
+    {
44
+      cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
45
+      return;
46
+    }
47
+
48
+  mraa_gpio_dir(m_gpioB, MRAA_GPIO_IN);
49
+
50
+  m_position = 0;
51
+
52
+  // setup the ISR
53
+
54
+  // We would prefer to use MRAA_GPIO_EDGE_BOTH for better resolution,
55
+  // but that does not appear to be supported
56
+  mraa_gpio_isr(m_gpioA, MRAA_GPIO_EDGE_RISING, 
57
+                &signalAISR, this);
58
+}
59
+
60
+RotaryEncoder::~RotaryEncoder()
61
+{
62
+  mraa_gpio_isr_exit(m_gpioA);
63
+
64
+  mraa_gpio_close(m_gpioA);
65
+  mraa_gpio_close(m_gpioB);
66
+}
67
+
68
+void RotaryEncoder::initPosition(int count)
69
+{
70
+  m_position = count;
71
+}
72
+
73
+int RotaryEncoder::position()
74
+{
75
+  return m_position;
76
+}
77
+
78
+void RotaryEncoder::signalAISR(void *ctx)
79
+{
80
+  upm::RotaryEncoder *This = (upm::RotaryEncoder *)ctx;
81
+
82
+  if (mraa_gpio_read(This->m_gpioA))
83
+    {
84
+      if (mraa_gpio_read(This->m_gpioB))
85
+        This->m_position++;      // CW
86
+      else
87
+        This->m_position--;      // CCW
88
+    }
89
+}
90
+
91
+

+ 91
- 0
src/rotaryencoder/rotaryencoder.h ファイルの表示

@@ -0,0 +1,91 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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
+#pragma once
25
+
26
+#include <string>
27
+#include <stdint.h>
28
+#include <sys/time.h>
29
+#include <mraa/gpio.h>
30
+
31
+namespace upm {
32
+
33
+  /**
34
+   * @brief C++ API for the Grove Rotary Encoder
35
+   *
36
+   * UPM module for the Grove Rotary Encoder.  A Rotary Encoder
37
+   * encodes a rotation signal into electronic pulses that can be used
38
+   * to measure rotation and direction.  It is useful in cases where a
39
+   * rotary knob is required, but using a potentiometer is not
40
+   * desireable.  A rotary encoder can turn a full 360 degrees+
41
+   * without a stop and does not place a resistive load on the
42
+   * circuit, as is the case with a potentiometer.
43
+   *
44
+   * This module maintains a position that is incremented or
45
+   * decremented according to the rotation on the encoder.
46
+   *
47
+   * @ingroup gpio 
48
+   * @snippet rotaryencoder.cxx Interesting
49
+   */
50
+  class RotaryEncoder {
51
+  public:
52
+    /**
53
+     * RotaryEncoder constructor
54
+     *
55
+     * @param pinA digital pin to use for signal A
56
+     * @param pinB digital pin to use for signal B
57
+     */
58
+    RotaryEncoder(int pinA, int pinB);
59
+    /**
60
+     * RotaryEncoder Destructor
61
+     */
62
+    ~RotaryEncoder();
63
+
64
+    /**
65
+     * Reset the position to a given number, default is 0. 
66
+     *
67
+     * @param count integer to initialize the position to
68
+     */
69
+    void initPosition(int count=0);
70
+
71
+    /**
72
+     * Get the position value
73
+     *
74
+     */
75
+    int position();
76
+
77
+    /**
78
+     * ISR for signal A
79
+     *
80
+     * @param ctx user context for the ISR (*this pointer)
81
+     */
82
+    static void signalAISR(void *ctx);
83
+
84
+  private:
85
+    volatile int m_position;
86
+    mraa_gpio_context m_gpioA;
87
+    mraa_gpio_context m_gpioB;
88
+  };
89
+}
90
+
91
+