Browse Source

rgbringcoder: Initial implementation.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 9 years ago
parent
commit
231a1f1b43

+ 3
- 1
examples/c++/CMakeLists.txt View File

@@ -124,6 +124,7 @@ add_executable (ad8232-example ad8232.cxx)
124 124
 add_executable (grovescam-example grovescam.cxx)
125 125
 add_executable (m24lr64e-example m24lr64e.cxx)
126 126
 add_executable (grovecircularled-example grovecircularled.cxx)
127
+add_executable (rgbringcoder-example rgbringcoder.cxx)
127 128
 
128 129
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
129 130
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -226,6 +227,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ad8232)
226 227
 include_directories (${PROJECT_SOURCE_DIR}/src/grovescam)
227 228
 include_directories (${PROJECT_SOURCE_DIR}/src/m24lr64e)
228 229
 include_directories (${PROJECT_SOURCE_DIR}/src/grovecircularled)
230
+include_directories (${PROJECT_SOURCE_DIR}/src/rgbringcoder)
229 231
 
230 232
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
231 233
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -351,4 +353,4 @@ target_link_libraries (ad8232-example ad8232 ${CMAKE_THREAD_LIBS_INIT})
351 353
 target_link_libraries (grovescam-example grovescam ${CMAKE_THREAD_LIBS_INIT})
352 354
 target_link_libraries (m24lr64e-example m24lr64e ${CMAKE_THREAD_LIBS_INIT})
353 355
 target_link_libraries (grovecircularled-example grovecircularled ${CMAKE_THREAD_LIBS_INIT})
354
-
356
+target_link_libraries (rgbringcoder-example rgbringcoder ${CMAKE_THREAD_LIBS_INIT})

+ 110
- 0
examples/c++/rgbringcoder.cxx View File

@@ -0,0 +1,110 @@
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 "rgbringcoder.h"
28
+#include <signal.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(int argc, char **argv)
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+  //! [Interesting]
46
+  
47
+  // There are a lot of pins to hook up.  These pins are valid for the
48
+  // Edison board, but may need to be adjusted for other platforms.
49
+
50
+  // In order:
51
+  // enable      - 4
52
+  // latch       - 10
53
+  // clear       - 11
54
+  // clock       - 2
55
+  // data        - 9
56
+  // switch      - 7
57
+
58
+  // red pwm     - 3
59
+  // green pwm   - 5
60
+  // blue pwm    - 6
61
+
62
+  // encA        - 12
63
+  // encB        - 13
64
+
65
+  upm::RGBRingCoder *ringCoder = 
66
+    new upm::RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 5, 6);
67
+
68
+  uint16_t spin = 0x0001;
69
+  bool oldState = false;
70
+  int oldPos = 0;
71
+
72
+  // Lets go green
73
+  ringCoder->setRGBLED(0.99, 0.01, 0.99);
74
+
75
+  while (shouldRun)
76
+    {
77
+      // you spin me round...
78
+      if (spin == 0)
79
+        spin = 0x0001;
80
+
81
+      ringCoder->setRingLEDS(spin);
82
+      spin <<= 1;
83
+
84
+      // check button state
85
+      bool bstate = ringCoder->getButtonState();
86
+      if (bstate != oldState)
87
+        {
88
+          cout << "Button state changed from " << oldState << " to "
89
+               << bstate << endl;
90
+          oldState = bstate;
91
+        }
92
+
93
+      // check encoder position
94
+      int epos = ringCoder->getEncoderPosition();
95
+      if (epos != oldPos)
96
+        {
97
+          cout << "Encoder position changed from " << oldPos << " to "
98
+               << epos << endl;
99
+          oldPos = epos;
100
+        }
101
+
102
+      usleep(100000);
103
+    }
104
+      
105
+  //! [Interesting]
106
+  
107
+  delete ringCoder;
108
+  
109
+  return 0;
110
+}

+ 98
- 0
examples/javascript/rgbringcoder.js View File

@@ -0,0 +1,98 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+ * Author: Jon Trulson <jtrulson@ics.com>
6
+ * Copyright (c) 2015 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
+var ringcoderObj = require('jsupm_rgbringcoder');
29
+
30
+// There are a lot of pins to hook up.  These pins are valid for the
31
+// Edison board, but may need to be adjusted for other platforms.
32
+
33
+// In order:
34
+// enable      - 4
35
+// latch       - 10
36
+// clear       - 11
37
+// clock       - 2
38
+// data        - 9
39
+// switch      - 7
40
+
41
+// red pwm     - 3
42
+// green pwm   - 5
43
+// blue pwm    - 6
44
+
45
+// encA        - 12
46
+// encB        - 13
47
+var ringCoder = new ringcoderObj.RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 
48
+                                              5, 6);
49
+
50
+var spin = 0x0001;
51
+var oldState = false;
52
+var oldPos = 0;
53
+
54
+// Lets go green
55
+ringCoder.setRGBLED(0.99, 0.01, 0.99);
56
+
57
+setInterval(function()
58
+{
59
+    // you spin me round...
60
+    if ((spin & 0xffff) == 0)
61
+        spin = 0x0001;
62
+    
63
+    ringCoder.setRingLEDS(spin);
64
+    spin <<= 1;
65
+    
66
+    // check button state
67
+    var bstate = ringCoder.getButtonState();
68
+    if (bstate != oldState)
69
+    {
70
+        console.log("Button state changed from " + oldState + " to "
71
+            + bstate);
72
+        oldState = bstate;
73
+    }
74
+    
75
+    // check encoder position
76
+    var epos = ringCoder.getEncoderPosition();
77
+    if (epos != oldPos)
78
+    {
79
+        console.log("Encoder position changed from " + oldPos + " to "
80
+            + epos);
81
+        oldPos = epos;
82
+    }
83
+}, 100);
84
+
85
+
86
+// exit on ^C
87
+process.on('SIGINT', function()
88
+{
89
+    ringCoder = null;
90
+    ringcoderObj.cleanUp();
91
+    ringcoderObj = null;
92
+    console.log("Exiting.");
93
+    process.exit(0);
94
+});
95
+
96
+
97
+
98
+

+ 90
- 0
examples/python/rgbringcoder.py View File

@@ -0,0 +1,90 @@
1
+#!/usr/bin/python
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
+import time, sys, signal, atexit
25
+import pyupm_rgbringcoder as upmRGBRingCoder
26
+
27
+# There are a lot of pins to hook up.  These pins are valid for the
28
+# Edison board, but may need to be adjusted for other platforms.
29
+
30
+# In order:
31
+# enable      - 4
32
+# latch       - 10
33
+# clear       - 11
34
+# clock       - 2
35
+# data        - 9
36
+# switch      - 7
37
+
38
+# red pwm     - 3
39
+# green pwm   - 5
40
+# blue pwm    - 6
41
+
42
+# encA        - 12
43
+# encB        - 13
44
+ringCoder = upmRGBRingCoder.RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 
45
+                                         5, 6)
46
+
47
+## Exit handlers ##
48
+# This stops python from printing a stacktrace when you hit control-C
49
+def SIGINTHandler(signum, frame):
50
+	raise SystemExit
51
+
52
+# This function lets you run code on exit,
53
+# including functions from ringCoder
54
+def exitHandler():
55
+	print "Exiting"
56
+	sys.exit(0)
57
+
58
+# Register exit handlers
59
+atexit.register(exitHandler)
60
+signal.signal(signal.SIGINT, SIGINTHandler)
61
+
62
+
63
+spin = 0x0001;
64
+oldState = False;
65
+oldPos = 0;
66
+
67
+# Lets go green
68
+ringCoder.setRGBLED(0.99, 0.01, 0.99);
69
+
70
+while(1):
71
+        # you spin me round...
72
+        if ((spin & 0xffff) == 0):
73
+                spin = 0x0001
74
+    
75
+        ringCoder.setRingLEDS(spin)
76
+        spin <<= 1
77
+
78
+        # check button state
79
+        bstate = ringCoder.getButtonState()
80
+        if (bstate != oldState):
81
+                print "Button state changed from", oldState, " to ", bstate
82
+                oldState = bstate
83
+    
84
+        # check encoder position
85
+        epos = ringCoder.getEncoderPosition()
86
+        if (epos != oldPos):
87
+                print "Encoder position changed from", oldPos, "to", epos
88
+                oldPos = epos
89
+
90
+        time.sleep(0.1)

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

@@ -0,0 +1,5 @@
1
+set (libname "rgbringcoder")
2
+set (libdescription "upm Sparkfun RGB RingCoder")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 9
- 0
src/rgbringcoder/jsupm_rgbringcoder.i View File

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

+ 9
- 0
src/rgbringcoder/pyupm_rgbringcoder.i View File

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

+ 187
- 0
src/rgbringcoder/rgbringcoder.cxx View File

@@ -0,0 +1,187 @@
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 "rgbringcoder.h"
28
+
29
+using namespace std;
30
+using namespace upm;
31
+
32
+RGBRingCoder::RGBRingCoder(int en, int latch, int clear, int clk, int dat, 
33
+                           int sw, int encA, int encB, int red, 
34
+                           int green, int blue) :
35
+  m_gpioEn(en), m_gpioLatch(latch), m_gpioClear(clear), m_gpioClock(clk), 
36
+  m_gpioData(dat), m_gpioSwitch(sw), m_gpioEncA(encA), m_gpioEncB(encB),
37
+  m_pwmRed(red), m_pwmGreen(green), m_pwmBlue(blue)
38
+{
39
+  m_counter = 0;
40
+
41
+  // enable, set LOW
42
+  m_gpioEn.dir(mraa::DIR_OUT);
43
+  m_gpioEn.write(0);
44
+
45
+  // latch
46
+  m_gpioLatch.dir(mraa::DIR_OUT);
47
+  m_gpioLatch.write(0);
48
+  
49
+  // clear, HIGH
50
+  m_gpioClear.dir(mraa::DIR_OUT);
51
+  m_gpioLatch.write(1);
52
+  
53
+  // clock
54
+  m_gpioClock.dir(mraa::DIR_OUT);
55
+  m_gpioClock.write(0);
56
+  
57
+  // data
58
+  m_gpioData.dir(mraa::DIR_OUT);
59
+  m_gpioData.write(0);
60
+
61
+  // switch
62
+  m_gpioSwitch.dir(mraa::DIR_IN);
63
+  m_gpioSwitch.mode(mraa::MODE_HIZ);  // no pullup
64
+  m_gpioSwitch.write(0);
65
+  
66
+  // ecoder A interrupt
67
+  m_gpioEncA.dir(mraa::DIR_IN);
68
+  m_gpioEncA.mode(mraa::MODE_PULLUP);
69
+  // EDGE_BOTH would be nice...
70
+  m_gpioEncA.isr(mraa::EDGE_RISING, &interruptHandler, this);
71
+
72
+  // ecoder B interrupt
73
+  m_gpioEncB.dir(mraa::DIR_IN);
74
+  m_gpioEncB.mode(mraa::MODE_PULLUP);
75
+  // EDGE_BOTH would be nice...
76
+  m_gpioEncB.isr(mraa::EDGE_RISING, &interruptHandler, this);
77
+
78
+  // RGB LED pwms, set to off
79
+
80
+  // Red led
81
+  m_pwmRed.period_ms(1);
82
+  m_pwmRed.write(0.99);
83
+  m_pwmRed.enable(true);
84
+
85
+  // Green led
86
+  m_pwmGreen.period_ms(1);
87
+  m_pwmGreen.write(0.99);
88
+  m_pwmGreen.enable(true);
89
+
90
+  // Blue led
91
+  m_pwmBlue.period_ms(1);
92
+  m_pwmBlue.write(0.99);
93
+  m_pwmBlue.enable(true);
94
+
95
+  // whew.
96
+}
97
+
98
+RGBRingCoder::~RGBRingCoder()
99
+{
100
+  m_gpioEncA.isrExit();
101
+  m_gpioEncB.isrExit();
102
+
103
+  // turn off the ring
104
+  setRingLEDS(0x0000);
105
+
106
+  // Turn of RGB LEDS
107
+  setRGBLED(0.99, 0.99, 0.99);
108
+  usleep(100000);
109
+
110
+  // turn off PWM's
111
+  m_pwmRed.enable(false);
112
+  m_pwmGreen.enable(false);
113
+  m_pwmBlue.enable(false);
114
+}
115
+
116
+void RGBRingCoder::interruptHandler(void *ctx)
117
+{
118
+   upm::RGBRingCoder *This = (upm::RGBRingCoder *)ctx;
119
+
120
+   // From the Sparkfun guys:
121
+
122
+   // enc_states[] is a fancy way to keep track of which direction
123
+   // the encoder is turning. 2-bits of oldEncoderState are paired
124
+   // with 2-bits of newEncoderState to create 16 possible values.
125
+   // Each of the 16 values will produce either a CW turn (1),
126
+   // CCW turn (-1) or no movement (0).
127
+
128
+   static int8_t enc_states[] = {0, -1, 1, 0, 1, 0, 0, -1,
129
+                                 -1, 0, 0, 1, 0, 1, -1, 0};
130
+   static uint8_t oldEncoderState = 0;
131
+   static uint8_t newEncoderState = 0;
132
+
133
+   // First, find the newEncoderState. This'll be a 2-bit value
134
+   // the msb is the state of the B pin. The lsb is the state
135
+   // of the A pin on the encoder.
136
+   newEncoderState = (This->m_gpioEncB.read()<<1) | 
137
+     (This->m_gpioEncA.read());
138
+
139
+   // Now we pair oldEncoderState with new encoder state
140
+   // First we need to shift oldEncoder state left two bits.
141
+   // This'll put the last state in bits 2 and 3.
142
+
143
+   oldEncoderState <<= 2;
144
+
145
+   // Mask out everything in oldEncoderState except for the previous state
146
+   oldEncoderState &= 0x0c;
147
+
148
+   // Now add the newEncoderState. oldEncoderState will now be of
149
+   // the form: 0b0000(old B)(old A)(new B)(new A)
150
+   oldEncoderState |= newEncoderState;
151
+
152
+   // update our counter
153
+   This->m_counter += enc_states[oldEncoderState & 0x0f];
154
+}
155
+
156
+void RGBRingCoder::setRingLEDS(uint16_t bits)
157
+{
158
+  // First we need to set latch LOW
159
+  m_gpioLatch.write(0);
160
+
161
+  // Now shift out the bits, msb first
162
+  for (int i=0; i<16; i++)
163
+    {
164
+      m_gpioData.write( ((bits & 0x8000) ? 1 : 0) );
165
+
166
+      // pulse the clock pin
167
+      m_gpioClock.write(1);
168
+      m_gpioClock.write(0);
169
+
170
+      bits <<= 1;
171
+    }
172
+
173
+  // latch it
174
+  m_gpioLatch.write(1);
175
+}
176
+
177
+bool RGBRingCoder::getButtonState()
178
+{
179
+  return (m_gpioSwitch.read() ? true : false);
180
+}
181
+
182
+void RGBRingCoder::setRGBLED(float r, float g, float b)
183
+{
184
+  m_pwmRed.write(r);
185
+  m_pwmGreen.write(g);
186
+  m_pwmBlue.write(b);
187
+}

+ 151
- 0
src/rgbringcoder/rgbringcoder.h View File

@@ -0,0 +1,151 @@
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 <iostream>
27
+#include <string>
28
+#include <stdint.h>
29
+#include <unistd.h>
30
+#include <mraa/gpio.hpp>
31
+#include <mraa/pwm.hpp>
32
+
33
+
34
+namespace upm {
35
+  /**
36
+   * @brief Sparkfun RGB RingCoder
37
+   * @defgroup rgbringcoder libupm-rgbringcoder
38
+   * @ingroup seeed gpio led
39
+   */
40
+
41
+  /**
42
+   * @library rgbringcoder
43
+   * @sensor rgbringcoder
44
+   * @comname Sparkfun RGB Ringcoder
45
+   * @type led
46
+   * @web https://www.sparkfun.com/products/11040
47
+   * @man sparkfun
48
+   * @con pwm gpio
49
+   *
50
+   * @brief API for the Sparkfun RGB Ringcoder
51
+   *
52
+   * The RGB RingCoder is a breakout board, a circular LED containing
53
+   * 16 LEDs arranged in a ring, and a rotary encoder.  The encoder
54
+   * contains an RGB LED as well as a push button function.
55
+   *
56
+   * The device will require 11 pins, 3 of which must be PWM capable
57
+   * (for the RGB LEDs).
58
+   *
59
+   * @snippet rgbringcoder.cxx Interesting
60
+   */
61
+
62
+  class RGBRingCoder {
63
+  public:
64
+
65
+    /**
66
+     * RGBRingCoder constructor
67
+     *
68
+     * @param en enable gpio
69
+     * @param latch latch gpio
70
+     * @param clear clear gpio
71
+     * @param clk clock gpio
72
+     * @param dat data out gpio
73
+     * @param sw push button switch gpio
74
+     * @param encA encoder A gpio
75
+     * @param encB encoder B gpio
76
+     * @param red RGB red led pwm
77
+     * @param green RGB green led pwm
78
+     * @param blue RGB blue led pwm
79
+     */
80
+    RGBRingCoder(int en, int latch, int clear, int clk, int dat, int sw, 
81
+                 int encA, int encB, int red, int green, int blue);
82
+
83
+    /**
84
+     * RGBRingCoder Destructor
85
+     */
86
+    ~RGBRingCoder();
87
+
88
+    /*
89
+     * set the state of the ring LEDs.  This is a 16 bit value, where
90
+     * each bit corresponds to one of the ring LEDs.  A 1 bit means
91
+     * that specific LED is on, and 0 bit means that specific LED is
92
+     * off.
93
+     *
94
+     * @param bits the bits representing the state of each LED
95
+     */
96
+    void setRingLEDS(uint16_t bits);
97
+
98
+    /* 
99
+     * return the state of the button
100
+     *
101
+     * @return true if the button is pressed, false otherwise
102
+     */
103
+    bool getButtonState();
104
+
105
+    /* 
106
+     * get the current rotary encoder counter value
107
+     *
108
+     * @return the current counter value
109
+     */
110
+    int getEncoderPosition() { return m_counter; };
111
+
112
+    /* 
113
+     * set the encoder counter to 0
114
+     */
115
+    void clearEncoderPosition() { m_counter = 0; };
116
+
117
+    /* 
118
+     * set the intensity of the red, grenn, and blue LEDs.  Values can
119
+     * be between 0.0 and 1.0.  Lower is brighter, higher is dimmer.
120
+     *
121
+     * @param r the red value, valid values are between 0.0 and 1.0
122
+     * @param g the green value, valid values are between 0.0 and 1.0
123
+     * @param b the blue value, valid values are between 0.0 and 1.0
124
+     */
125
+    void setRGBLED(float r, float g, float b);
126
+
127
+  private:
128
+    
129
+    mraa::Gpio m_gpioEn;
130
+
131
+    mraa::Gpio m_gpioLatch;
132
+    mraa::Gpio m_gpioClear;
133
+    mraa::Gpio m_gpioClock;
134
+    mraa::Gpio m_gpioData;
135
+
136
+    mraa::Gpio m_gpioSwitch;
137
+
138
+    mraa::Pwm m_pwmRed;
139
+    mraa::Pwm m_pwmGreen;
140
+    mraa::Pwm m_pwmBlue;
141
+
142
+    mraa::Gpio m_gpioEncA;
143
+    mraa::Gpio m_gpioEncB;
144
+
145
+    static void interruptHandler(void *ctx);
146
+    volatile int m_counter;
147
+
148
+  };
149
+}
150
+
151
+