"Blinker!" Megabrite/Shifty VU LED Wall Project

#include "Functions.h"
#include "Color.h"
#include "Controls.h"
#include "MegaBrite.h"
#include "Screen.h"
#include "Programs.h"
#include "ShiftyVU.h"

MegaBrite megaBrite;
ShiftyVU shifty;
Programs progs;
Screen screen;
Controls controls(progs);

int mode = 0; //0 - off, 1 - on, 2 - pulse

double beatSegments[9] = {1.00/16.00,1.00/8.00,1.00/4.00,1.00/2.00,1.00,2.00,4.00,8.00,16.00};

int currentProgram = 0;
int effectTime = 0;
int millisecondsPerBeat = 60000 / 128;
int effectDuration = millisecondsPerBeat;
int pulseDuration = millisecondsPerBeat * beatSegments[2];
int currentLED = 0;
int color;
int fadeArray[NUMLEDS];
bool effecting = false;
bool pulsing = false;
long refreshDif = 0;
long refreshTimer = millis();
long timer;
long ms;
long palette[PALETTEARRAY];
long plasma[SCREENHEIGHT][SCREENWIDTH];
ColorRGB colorFrom;
ColorRGB colorOff = {0, 0, 0};

void setup() {
   Serial.begin(19200);
   randomSeed(analogRead(0));

   controls.setup();

   for(int x = 0; x < NUMLEDS; x++)
       fadeArray[x] = -1;
}

void loop() {

   refreshDif = millis() - refreshTimer;
   refreshTimer = millis();

   //check if param positions have changed and update LCD if so
   controls.checkKnobs();

   //check if bpm has changed
   if(controls.bpmChanged())
       millisecondsPerBeat = (int)(60000.00 / controls.bpm);

   if(controls.programChanged()) {
       
       progs.currentProgram(controls.program);
       currentProgram = progs.currentProgram();

       //generate the palette
       ColorRGB rgb;
       ColorHSV hsv;
       for(int x = 0; x < 256; x++)
       {
           ColorHSV hsv = {x, 255, 255};
           rgb = HSVtoRGB(hsv);
           palette[x] = RGBtoInt(rgb);
       }

       //setup for next effect
       switch (currentProgram) {
           //Beat
           case PROGRAM_VU_CYCLE:
               {
                   color = 0;
                   effectDuration = progs.param1();
                   break;
               }
           //Beat
           case PROGRAM_VU_FIXED:
               {
                   color = progs.param1();
                   break;
               }
           //Sine Plasma
           case PROGRAM_PLASMA_FIXED:
           case PROGRAM_PLASMA_CYCLE:
               {
                   color = -1;
                   effectDuration = progs.param2();

                   //generate the plasma once
                   for(int x = 0; x < SCREENHEIGHT; x++)
                   {
                       for(int y = 0; y < SCREENWIDTH; y++)
                       {
                           long color;
                           //straight line / b & w
                           //color = long(128.0 + (128.0 * sin(y / 8.0)));
                           //plasma like
                           color = long(
                                 128.0 + (128.0 * sin(x / 8.0))
                               + 128.0 + (128.0 * sin(y / -16.0))
                               + 128.0 + (128.0 * sin(sqrt(double((x - 128 / 2.0)* (x - 128 / 2.0) + (y - 6 / 2.0) * (y - 6 / 2.0))) / 8.0))
                               + 128.0 + (128.0 * sin(sqrt(double(x * x + y * y)) / -16.0))
                           ) / 4;
                           plasma[x][y] = color;
                       }
                   }
                   break;
               }
           //Chaser
           case PROGRAM_CHASER_FIXED:
           case PROGRAM_CHASER_CYCLE:
               {
                   effectDuration = millisecondsPerBeat * beatSegments[progs.param2()];
                   pulseDuration = millisecondsPerBeat * beatSegments[progs.param3()];
                   break;
               }
           //Random Colour
           case PROGRAM_RANDOM_FIXED:
           case PROGRAM_RANDOM_CYCLE:
           case PROGRAM_RANDOM_RAND:
               {
                   effectDuration = millisecondsPerBeat * beatSegments[progs.param2()];
                   pulseDuration = millisecondsPerBeat * beatSegments[progs.param3()];
                   break;
               }
           //Blink
           case PROGRAM_BLINK:
               {
                   color = progs.param1();
                   effectDuration = millisecondsPerBeat * beatSegments[progs.param2()];
                   pulseDuration = millisecondsPerBeat * beatSegments[progs.param3()];
                   break;
               }
           //Test
           case PROGRAM_TEST_FIXED:
           case PROGRAM_TEST_CYCLE:
               {
                   effectDuration = millisecondsPerBeat * beatSegments[progs.param2()];
                   pulseDuration = millisecondsPerBeat * beatSegments[progs.param3()];
                   break;
               }
           //Nothing - Program Not Found - This shouldn't happen
           default:
               {
                   break;
               }
       }

       pulsing = true;

       ms = millis();
   }

   //check if mode has been switched to 0 (OFF)
   if(controls.modeChanged()) {
       mode = controls.mode();
       if(mode==0)
       {
           effecting = true;
           effectDuration = (millisecondsPerBeat) * beatSegments[4];
           for (int i = 0; i < NUMLEDS; i++) {
               megaBrite.ledChannels(i, COLORFROM, megaBrite.ledChannels(i, COLORCURRENT));
           }
       }
   }