summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/rgbringcoder/rgbringcoder.h
blob: 5675c4fc29ff92c8b72633cdf06bfadc8ac77c72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <iostream>
#include <string>
#include <stdint.h>
#include <unistd.h>

#include <mraa/gpio.hpp>

#include <mraa/pwm.hpp>


namespace upm {
  /**
   * @brief SparkFun RGB RingCoder
   * @defgroup rgbringcoder libupm-rgbringcoder
   * @ingroup seeed gpio led
   */

  /**
   * @library rgbringcoder
   * @sensor rgbringcoder
   * @comname SparkFun RGB Ringcoder
   * @type led
   * @web https://www.sparkfun.com/products/11040
   * @man sparkfun
   * @con pwm gpio
   *
   * @brief API for the SparkFun* RGB RingCoder
   *
   * RGB RingCoder is a breakout board, a circular LED containing
   * 16 LEDs arranged in a ring, and a rotary encoder. The encoder
   * contains an RGB LED as well as a push button function.
   *
   * The device requires 11 pins, 3 of which must be PWM-capable
   * (for the RGB LEDs).
   * 
   * @image html rgbringcoder.jpg
   * @snippet rgbringcoder.cxx Interesting
   */

  class RGBRingCoder {
  public:

    /**
     * RGBRingCoder constructor
     *
     * @param en Enables GPIO
     * @param latch Latch GPIO
     * @param clear Clears GPIO
     * @param clk Clock GPIO
     * @param dat Data out GPIO
     * @param sw Push button switch GPIO
     * @param encA Encoder A GPIO
     * @param encB Encoder B GPIO
     * @param red RGB red LED PWM
     * @param green RGB green LED PWM
     * @param blue RGB blue LED PWM
     */
    RGBRingCoder(int en, int latch, int clear, int clk, int dat, int sw, 
                 int encA, int encB, int red, int green, int blue);

    /**
     * RGBRingCoder destructor
     */
    ~RGBRingCoder();

    /*
     * Sets the state of the ring LEDs. This is a 16-bit value, where
     * each bit corresponds to one of the ring LEDs. A 1 bit means
     * that a specific LED is on, and a 0 bit means that a specific LED is
     * off.
     *
     * @param bits Bits representing the state of each LED
     */
    void setRingLEDS(uint16_t bits);

    /* 
     * Returns the state of the button
     *
     * @return True if the button is pressed, false otherwise
     */
    bool getButtonState();

    /* 
     * Gets the current rotary encoder counter value
     *
     * @return Current counter value
     */
    int getEncoderPosition() { return m_counter; };

    /* 
     * Sets the encoder counter to 0
     */
    void clearEncoderPosition() { m_counter = 0; };

    /* 
     * Sets the intensity of the red, green, and blue LEDs. Values can
     * be between 0.0 and 1.0. Lower is brighter, higher is dimmer.
     *
     * @param r Red value; valid values are 0.0-1.0
     * @param g Green value; valid values are 0.0-1.0
     * @param b Blue value; valid values are 0.0-1.0
     */
    void setRGBLED(float r, float g, float b);

  private:
    
    mraa::Gpio m_gpioEn;

    mraa::Gpio m_gpioLatch;
    mraa::Gpio m_gpioClear;
    mraa::Gpio m_gpioClock;
    mraa::Gpio m_gpioData;

    mraa::Gpio m_gpioSwitch;

    mraa::Pwm m_pwmRed;
    mraa::Pwm m_pwmGreen;
    mraa::Pwm m_pwmBlue;

    mraa::Gpio m_gpioEncA;
    mraa::Gpio m_gpioEncB;

    static void interruptHandler(void *ctx);
    volatile int m_counter;

  };
}