Pasted as C++ [Remove this paste ]
Description: No description
URL: http://bcas.tv/paste/results/NIjXph40.html
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// 8 Led Control script
// Coded by Interitus
// With much help from fp|Blaze from Fragapalooza, Phylter in #programming on EFnet and P1lotCX7 from Fragapalooza
// Honourable mentions to timecop, psychocanuk and Steve2
// Also much code copied verbatim from octobrite example documentation
 
 
// data definitions
 
// Defines for use with Arduino functions
#define clockpin   13 // CL
#define enablepin  10 // BL
#define latchpin    9 // XL
#define datapin    11 // SI
 
// Defines for direct port access
#define CLKPORT PORTB
#define ENAPORT PORTB
#define LATPORT PORTB
#define DATPORT PORTB
#define CLKPIN  5
#define ENAPIN  2
#define LATPIN  1
#define DATPIN  3
 
// Number of OctoBrites / TLC5947 devices
#define NumOctoBrites 1
 
// Array storing color values
//  BLUE: LEDChannels[x][0]   Range: {0 to 4095}
// GREEN: LEDChannels[x][1]   Range: {0 to 4095}
//   RED: LEDChannels[x][2]   Range: {0 to 4095}
uint16_t LEDChannels[NumOctoBrites*8][3] = {0};
 
struct patterns // defines a pattern state
{ 
  int red;
  int green; 
  int blue; 
  int time; 
  byte steps; 
};
 
enum state  // defines the current LED run state 
{
  RUN,
  DONE,
  PTRN
};
 
//  Order in which the led will run through the patterns before looping
const byte led1pattern[] = {0,1,2,3,1,1,4,4,5,6,4,5,6,5,4,4,4,1,1,8,254};
//const byte led1pattern[] = {1,1,4,4,4,4,4,4,5,254};
//const byte led1pattern[] = {4,6,5,4,4,4,254};
// pattern data
 
// All the available patterns to run
// ,{0,0,0,0,40}  is a termination value, indicating end of pattern
const patterns patternMain[][7] = 
{
  { // red-green-blue x2 0
    {4095,0,0,2000,8}, //{ Red val, green val, blue val, duration, # of increments }
    {0,4095,0,2000,8},
    {0,0,4095,2000,8},
    {4095,0,0,2000,15},
    {0,4095,0,2000,15},
    {0,0,4095,2000,15},
    {0,0,0,0,40}
  },
  {   //flash red slowly twice 1
    {4050,0,0,4000,0},
    {0,0,0,2000,0},
    {4050,0,0,4000,0},
    {0,0,0,2000,0},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  }, 
  {//flash green slowly twice 2 
    {0,4090,0,1000,8},
    {0,0,0,1000,8},
    {0,4090,0,1000,8},
    {0,0,0,1000,8},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  }, 
  {//flash blue slowly twice 3
    {0,0,4090,1000,8},
    {0,0,0,1000,8},
    {0,0,4090,1000,8},
    {0,0,0,1000,8},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  },  
  {//flash red once quick 4
    {4095,0,0,100,1},
    {0,0,0,100,1},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  }, 
  {//flash green once quick 5
    {0,4090,0,100,1},
    {0,0,0,100,1},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  }, 
  { //flash blue once quick 6
    {0,0,4090,100,1},
    {0,0,0,100,1},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  },
  {// flash white once quick 7
    {255,250,250,100,1},
    {0,0,0,100,1},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40},
    {0,0,0,0,40}
  },
  { //Fade each color smoothly once 8
    {4095,0,0,2000,0},
    {0,4090,0,2000,0},
    {0,0,4090,2000,0},
    {4095,4090,4090,2000,0},
    {0,0,0,1000,0},
    {0,0,0,0,40},
    {0,0,0,0,40}
  } 
};
 
class Led // led class definition
{
  public:
    Led(int, int, int, int);
    void transition_to(int, int, int, int, int);
    void update();
    void lerp();
    void transition_complete();
    void patternGroupSelect();
 
    state ledState;    // status of the led
 
  private:
    int redPin;      // should be in the led constructor
    int greenPin;    // same as above
    int bluePin;     // same as above
 
    int startRed;    // colors at the start of the transition
    int startGreen;
    int startBlue;
 
    int redVal;      // current color during transition
    int greenVal;
    int blueVal;
 
    int dstRed;      // color at the end of the transition
    int dstGreen;
    int dstBlue;
 
    unsigned int transition_start;    // ticks at start of transition
    //unsigned long transition_end;      // ticks at end       
    int length; 
    //unsigned long previousMillis;
    unsigned int currentTime;
    unsigned int increment;
 
    int pattern;
    int patternGroup;
    int ledNum;
    int patternIndex;
    float progress;
    int stepAmt;
 
};    // end of LED Class 
 
Led::Led(int red, int green, int blue, int num)
{
  ledNum = num;
  ledState = RUN;
 
  redPin   = red;
  greenPin = green;
  bluePin  = blue; 
 
  pattern = 0;
  patternIndex = 0;
 
 
  patternGroup = led1pattern[patternIndex];
}
 
void Led::transition_to(int R, int G, int B, int L, int I) 
{
    dstRed   = R;
    dstBlue  = B;
    dstGreen = G;
 
    startRed   = redVal;
    startBlue  = blueVal;
    startGreen = greenVal;
 
    increment = I;
 
    //transition_start = millis();
    currentTime = 0;
    length = L;
    ledState = RUN;
    //previousMillis = currentTime;
}
 
void Led::update() 
{
//  analogWrite(redPin, redVal);
//  analogWrite(greenPin, greenVal);
//  analogWrite(bluePin, blueVal);
    LEDChannels[7][0] = redVal;
    LEDChannels[7][1] = greenVal;
    LEDChannels[7][2] = blueVal;
 
}
 
void Led::lerp() 
{ 
  if (ledState == RUN) 
  {
      //currentTime = millis();  
 
      // This is the previous code that seemed to be causing problems. And other code using the progress variable
      //progress = 1.0 - (float)(transition_end - currentTime)/(transition_end - transition_start);
 
      //redVal = startRed + ((currentTime - startTime)/(length)) * (dstRed - startRed);
      redVal = startRed + ( (float)currentTime / (float)length ) * ((float)dstRed - (float)startRed);     
      blueVal = startBlue + ( (float)currentTime / (float)length ) * ((float)dstBlue - (float)startBlue);
      greenVal = startGreen + ( (float)currentTime / (float)length ) * ((float)dstGreen - (float)startGreen);
      // Gets the value for the color based on progress
      //redVal   = startRed   + (dstRed - startRed) * progress;
      //greenVal = startGreen + (dstGreen - startGreen) * progress;
      //blueVal  = startBlue  + (dstBlue - startBlue) * progress;
 
      currentTime++;
      if (increment < 1) // If the increment value is set to 0, it will just update and do a smooth
      {                   // fade
          update();
      } 
      else if(increment == 1) 
      {   // Updates only at the end of the transition length
          if(currentTime > (length - 2)) update();  
      }
      else if(increment == 2)
      {
//        if(progress > 0.40 && progress < 0.42) update();
          // updates once (or so) in the middle of the transition length, and at the end
          if(currentTime > ((length / 2) -2) && currentTime < ((length/2)+2)) update();
          if(progress > (length - 2)) update();
      }
      else if(increment >2) // Only updates the leds the specified amount of times
      {                     // allows for a low frame rate type transition (looks cool, ok?!)
          stepAmt = length / increment;
 
          for( int z = 1; z <= increment; z++) 
          {
             if (currentTime == stepAmt *z)
             {
                update();
             }
          }
      }
 
      if ( currentTime >= length ) 
      {
          redVal   = dstRed;  // Progress is finished, so set the color values to what they should be.
          greenVal = dstGreen;
          blueVal  = dstBlue;
          pattern++;  // advances the pattern
          ledState = DONE; 
      }
    }
} // end of lerp()
 
// Runs through a given pattern
void Led::transition_complete() 
{
  if ( patternMain[patternGroup][pattern].steps < 30) // 30+ is a EOL value
  { // Sets up the next transition
    transition_to(
                    patternMain[patternGroup][pattern].red, 
                    patternMain[patternGroup][pattern].green, 
                    patternMain[patternGroup][pattern].blue, 
                    patternMain[patternGroup][pattern].time,
                    patternMain[patternGroup][pattern].steps
                    );
  } 
  else 
  { // Pattern is done, so set state and reset pattern
    ledState = PTRN;
    pattern = 0;  
  }
 
}
 
void Led::patternGroupSelect() 
{
  pattern = 0;  //When selecting a new pattern, resets it to the beginning
  switch(ledNum) // Each LED number will have its own set pattern
  {
    case 1:
      //advances the pattern index
      patternIndex++;
      //Resets pattern to start
      if (led1pattern[patternIndex] <= 250) 
      {
        patternGroup = led1pattern[patternIndex];
 
      } 
        else 
      {   
           patternIndex = 0;
           patternGroup = led1pattern[patternIndex]; 
      }
 
 
    break;
 
  }
}
 
// global variables and constants
 
 
 
Led Led1(9, 10, 11, 1);
 
void setup() 
{
  pinMode(datapin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(enablepin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  digitalWrite(latchpin, LOW);
  digitalWrite(enablepin, LOW);
 
  Led1.transition_complete(); //Gets it going
 
}
 
 
void WriteLEDArray() {
 
  unsigned int tempOne = 0;
 
  for (int i = 0; i < (NumOctoBrites * 24); i++) {
 
    tempOne = *(&LEDChannels[0][0] + i);
 
    for (int j = 0; j < 12; j++) {
      if ((tempOne >> (11 - j)) & 1) {
        DATPORT |= (1 << DATPIN);
      } 
      else {
        DATPORT &= ~(1 << DATPIN);
      }
      CLKPORT |= (1 << CLKPIN);
      CLKPORT &= ~(1 << CLKPIN); 
    } 
 
  }
  LATPORT |= (1 << LATPIN);
  LATPORT &= ~(1 << LATPIN);
}
 
void loop() 
{
  WriteLEDArray();
  switch(Led1.ledState)
  {
    case RUN:
      Led1.lerp();
      break; 
    case DONE: // Finishes transition
      Led1.transition_complete();
      break;
    case PTRN: //Pattern finished
      Led1.patternGroupSelect();
      Led1.ledState = RUN;
      Led1.transition_complete();
      break;
  }
 
 
}