Home | History | Annotate | Download | only in rgbringcoder
      1 /*
      2  * Author: Jon Trulson <jtrulson (at) 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 }
    188