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