summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/rgbringcoder/rgbringcoder.cxx
blob: 4a48314f0bd4a5328eac23aacaf077f2e03dd83f (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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.
 */

#include <iostream>

#ifdef JAVACALLBACK
#undef JAVACALLBACK
#endif
#include "rgbringcoder.hpp"

using namespace std;
using namespace upm;

RGBRingCoder::RGBRingCoder(int en, int latch, int clear, int clk, int dat, 
                           int sw, int encA, int encB, int red, 
                           int green, int blue) :
  m_gpioEn(en), m_gpioLatch(latch), m_gpioClear(clear), m_gpioClock(clk), 
  m_gpioData(dat), m_gpioSwitch(sw), m_gpioEncA(encA), m_gpioEncB(encB),
  m_pwmRed(red), m_pwmGreen(green), m_pwmBlue(blue)
{
  m_counter = 0;

  // enable, set LOW
  m_gpioEn.dir(mraa::DIR_OUT);
  m_gpioEn.write(0);

  // latch
  m_gpioLatch.dir(mraa::DIR_OUT);
  m_gpioLatch.write(0);
  
  // clear, HIGH
  m_gpioClear.dir(mraa::DIR_OUT);
  m_gpioLatch.write(1);
  
  // clock
  m_gpioClock.dir(mraa::DIR_OUT);
  m_gpioClock.write(0);
  
  // data
  m_gpioData.dir(mraa::DIR_OUT);
  m_gpioData.write(0);

  // switch
  m_gpioSwitch.dir(mraa::DIR_IN);
  m_gpioSwitch.mode(mraa::MODE_HIZ);  // no pullup
  m_gpioSwitch.write(0);
  
  // ecoder A interrupt
  m_gpioEncA.dir(mraa::DIR_IN);
  m_gpioEncA.mode(mraa::MODE_PULLUP);
  // EDGE_BOTH would be nice...
  m_gpioEncA.isr(mraa::EDGE_RISING, &interruptHandler, this);

  // ecoder B interrupt
  m_gpioEncB.dir(mraa::DIR_IN);
  m_gpioEncB.mode(mraa::MODE_PULLUP);
  // EDGE_BOTH would be nice...
  m_gpioEncB.isr(mraa::EDGE_RISING, &interruptHandler, this);

  // RGB LED pwms, set to off

  // Red led
  m_pwmRed.period_ms(1);
  m_pwmRed.write(0.99);
  m_pwmRed.enable(true);

  // Green led
  m_pwmGreen.period_ms(1);
  m_pwmGreen.write(0.99);
  m_pwmGreen.enable(true);

  // Blue led
  m_pwmBlue.period_ms(1);
  m_pwmBlue.write(0.99);
  m_pwmBlue.enable(true);

  // whew.
}

RGBRingCoder::~RGBRingCoder()
{
  m_gpioEncA.isrExit();
  m_gpioEncB.isrExit();

  // turn off the ring
  setRingLEDS(0x0000);

  // Turn of RGB LEDS
  setRGBLED(0.99, 0.99, 0.99);
  usleep(100000);

  // turn off PWM's
  m_pwmRed.enable(false);
  m_pwmGreen.enable(false);
  m_pwmBlue.enable(false);
}

void RGBRingCoder::interruptHandler(void *ctx)
{
   upm::RGBRingCoder *This = (upm::RGBRingCoder *)ctx;

   // From the Sparkfun guys:

   // enc_states[] is a fancy way to keep track of which direction
   // the encoder is turning. 2-bits of oldEncoderState are paired
   // with 2-bits of newEncoderState to create 16 possible values.
   // Each of the 16 values will produce either a CW turn (1),
   // CCW turn (-1) or no movement (0).

   static int8_t enc_states[] = {0, -1, 1, 0, 1, 0, 0, -1,
                                 -1, 0, 0, 1, 0, 1, -1, 0};
   static uint8_t oldEncoderState = 0;
   static uint8_t newEncoderState = 0;

   // First, find the newEncoderState. This'll be a 2-bit value
   // the msb is the state of the B pin. The lsb is the state
   // of the A pin on the encoder.
   newEncoderState = (This->m_gpioEncB.read()<<1) | 
     (This->m_gpioEncA.read());

   // Now we pair oldEncoderState with new encoder state
   // First we need to shift oldEncoder state left two bits.
   // This'll put the last state in bits 2 and 3.

   oldEncoderState <<= 2;

   // Mask out everything in oldEncoderState except for the previous state
   oldEncoderState &= 0x0c;

   // Now add the newEncoderState. oldEncoderState will now be of
   // the form: 0b0000(old B)(old A)(new B)(new A)
   oldEncoderState |= newEncoderState;

   // update our counter
   This->m_counter += enc_states[oldEncoderState & 0x0f];
}

void RGBRingCoder::setRingLEDS(uint16_t bits)
{
  // First we need to set latch LOW
  m_gpioLatch.write(0);

  // Now shift out the bits, msb first
  for (int i=0; i<16; i++)
    {
      m_gpioData.write( ((bits & 0x8000) ? 1 : 0) );

      // pulse the clock pin
      m_gpioClock.write(1);
      m_gpioClock.write(0);

      bits <<= 1;
    }

  // latch it
  m_gpioLatch.write(1);
}

bool RGBRingCoder::getButtonState()
{
  return (m_gpioSwitch.read() ? true : false);
}

void RGBRingCoder::setRGBLED(float r, float g, float b)
{
  m_pwmRed.write(r);
  m_pwmGreen.write(g);
  m_pwmBlue.write(b);
}