Advertisement
Guest User

Processing code

a guest
Dec 7th, 2014
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 38.50 KB
  1. //Author: fBlah
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5.  
  6. import processing.serial.*;
  7.  
  8. import ddf.minim.*;
  9. import ddf.minim.analysis.*;
  10.  
  11. import controlP5.*;
  12.  
  13. DisposeHandler dh;
  14.  
  15. Minim minim;
  16. AudioInput in;
  17. FFT fft;
  18.  
  19. AudioPlayer song;
  20. float[] peaks;
  21. int peak_hold_time = 10;  // how long before peak decays
  22. int[] peak_age;  // tracks how long peak has been stable, before decaying
  23. // how wide each 'peak' band is, in fft bins
  24. int binsperband = 10;
  25. int peak = 25;
  26. int peaksize; // how many individual peak bands we have (dep. binsperband)
  27. float gain = 40; // in dB
  28. float dB_scale = 2.0;  // pixels per dB
  29. int buffer_size = 1024;  // also sets FFT size (frequency resolution)
  30. float sample_rate = 44100;
  31. int spectrum_height = 200; // determines range of dB shown
  32. int spectrum_width = 128; // determines how much of spectrum we see
  33. int sampleRate = 44100;
  34. int timeSize = 1024;
  35.  
  36. //-
  37. ControlP5 cp5;
  38. boolean AmbiLight = true, Case = true, AvgCase = false, Random = false, MusicC = false, MusicA = false, Rythm = false, Once = true, Auto = false, Glow = false, bAlt = false, gAlt = false;
  39. BufferedImage screenshot;
  40. int fps = 7;
  41. int R,G,B;
  42. int  rp, gp, bp; //mode 14 , 15
  43. short aR = 250,aG = 0,aB = 0; //mode 14 , 15
  44. int cR = 0,cG = 0,cB = 0; // Average of ambilight
  45. float inc = 0.1;
  46. float angle = 0;
  47.  
  48. int aAge = 3;
  49. int type = 0;
  50. int maxtype = 2;
  51. int gtn = 1000, gtf;//GlowTime on/off
  52. int mode = 5;
  53. int maxmode = 20;
  54. int timeCur, timeLast, lastGlow;
  55. int peakA;
  56.  
  57. int portNo = 1;
  58. int noFails = 0;
  59. /*
  60. // using 12 RGB LEDs
  61. static final int led_num_x = 4;
  62. static final int led_num_y = 4;
  63. static final int leds[][] = new int[][] {
  64.   {1,3}, {0,3}, // Bottom edge, left half
  65.   {0,2}, {0,1}, // Left edge
  66.   {0,0}, {1,0}, {2,0}, {3,0}, // Top edge
  67.   {3,1}, {3,2}, // Right edge
  68.   {3,3}, {2,3}, // Bottom edge, right half
  69. };
  70.  
  71. */
  72.  
  73. // using 25 RGB LEDs
  74. static final int led_num_x = 28;
  75. static final int led_num_y = 14;
  76. int leds[][] = new int[][] {
  77.   {6+1,11+1}, {5+1,11+1}, {4+1,11+1}, {3+1,11+1}, {2+1,11+1}, {1+1,11+1}, {0+1,11+1}, {0+1,10+1}, {0+1,9+1}, {0+1,8+1}, {0+1,7+1}, // Bottom edge, left half
  78.  
  79.   {0+1,6+1}, {0+1,5+1}, {0+1,4+1}, {0+1,3+1}, {0+1,2+1}, {0+1,1+1}, {0+1,0+1}, {1+1,0+1}, {2+1,0+1}, {3+1,0+1},
  80.  
  81.   {4+1,0+1}, {5+1,0+1}, {6+1,0+1}, {7+1,0+1}, {8+1,0+1}, {9+1,0+1}, {10+1,0+1}, {11+1,0+1}, {12+1,0+1}, {13+1,0+1},
  82.  
  83.   {14+1,0+1}, {15+1,0+1}, {16+1,0+1}, {17+1,0+1}, {18+1,0+1}, {19+1,0+1}, {20+1,0+1}, {21+1,0+1}, {22+1,0+1}, {23+1,0+1},
  84.  
  85.   {24+1,0+1}, {25+1,0+1}, {25+1,1+1}, {25+1,2+1}, {25+1,3+1}, {25+1,4+1}, {25+1,5+1}, {25+1,6+1}, {25+1,7+1}, {25+1,8+1},
  86.  
  87.   {25+1,9+1}, {25+1,10+1}, {25+1,11+1}, {24+1,11+1}, {23+1,11+1}, {22+1,11+1}, {21+1,11+1}, {20+1,11+1}, {19+1,11+1}
  88.  
  89. };
  90.  
  91. static final short fade = 70;
  92.  
  93. static final int minBrightness = 120;
  94.  
  95. // Preview windows
  96. int window_width;
  97. int window_height;
  98. int preview_pixel_width;
  99. int preview_pixel_height;
  100. int[][] pixelOffset = new int[leds.length][256];
  101.  
  102. // RGB values for each LED
  103. short[][]  ledColor    = new short[leds.length][3],
  104.       prevColor   = new short[leds.length][3];  
  105.  
  106. short chassis[] = { 255, 0, 0 } ;
  107. int ABright = 0, CBright = 0, CBrightt = 0, ABrightt = 0;
  108.  
  109.  
  110. byte[][]  gamma       = new byte[256][3];
  111. byte[]    serialData  = new byte[ leds.length * 3 + 2 + 3 + 2];
  112. int data_index = 0;
  113.  
  114.  
  115. //creates object from java library that lets us take screenshots
  116. Robot bot;
  117.  
  118. // bounds area for screen capture        
  119. Rectangle dispBounds;
  120.  
  121. // Monitor Screen information    
  122. GraphicsEnvironment     ge;
  123. GraphicsConfiguration[] gc;
  124. GraphicsDevice[]        gd;
  125.  
  126. Serial           port;
  127.  
  128. void setup(){
  129.  
  130.   timeCur = timeLast = millis();
  131.  
  132.   dh = new DisposeHandler(this);
  133.  
  134.   frameRate(fps);
  135.  
  136.   int[] x = new int[16];
  137.   int[] y = new int[16];
  138.  
  139.   minim = new Minim(this);
  140.  
  141.   // use the getLineIn method of the Minim object to get an AudioInput
  142.   in = minim.getLineIn(Minim.MONO,1024,44100);
  143.   fft = new FFT(in.bufferSize(), in.sampleRate());
  144.   fft.window(FFT.HAMMING);
  145.  
  146.   // initialize peak-hold structures
  147.   peaksize = 1+Math.round(fft.specSize()/binsperband);
  148.   peaks = new float[peaksize];
  149.   peak_age = new int[peaksize];
  150.  
  151.   // calculate averages based on a miminum octave width of 11 Hz
  152.   // split each octave into 1 bands - this should result in 12 averages
  153.   fft.logAverages(11, 1); // results in 12 averages, each corresponding to an octave, the first spanning 0 to 11 Hz.
  154.   //-
  155.   cp5 = new ControlP5(this);
  156.   cp5.addButton("AmbiLight",10,20,60,50,20);//ambilight mode
  157.   cp5.addButton("Case",10,80,60,30,20);// chassis light on off
  158.   cp5.addButton("AvgCase",10,80,40,40,20);// chassis light based on ambilight
  159.   cp5.addButton("Random",10,120,60,40,20);//chassis light random
  160.   cp5.addButton("MusicC",10,170,60,40,20);//chassis light based on music frequency
  161.   cp5.addButton("MusicA",10,220,60,40,20);//chassis light based on music beat
  162.   cp5.addButton("Glow",10,220,40,30,20);// glow
  163.   cp5.addButton("T",10,250,40,15,20);// glow type breathe, strobe 1, strobe 2
  164.   cp5.addButton("Rythm",10,270,60,50,20);// music mode for ambilight
  165.   cp5.addButton("M",10,320,60,15,20);// Mode of rythm 20 modes/visualizations
  166.   cp5.addButton("A",10,335,60,15,20);// auto Mode
  167.   cp5.addSlider("ABright",0,255,120,20,100,10,100);//ambilight brightness
  168.   cp5.addSlider("CBright",0,255,120,50,100,10,100);//chassis light brightness
  169.   cp5.addSlider("R",0,255,120,80,100,10,100);//chassis light red component
  170.   cp5.controller("R").setColorForeground(#FC0000);
  171.   cp5.addSlider("G",0,255,120,110,100,10,100);//chassis light green
  172.   cp5.controller("G").setColorForeground(#0BFC00);
  173.   cp5.addSlider("B",0,255,120,140,100,10,100);// chassis light blue
  174.   cp5.controller("B").setColorForeground(#002CFC);
  175.   cp5.addSlider("gtn",0,10000,120,170,100,10,100);//Strobe light on and off times is ms n -on f - off
  176.   cp5.addSlider("gtf",0,10000,120,200,100,10,100);//Strobe light on and off times is ms
  177.  
  178.   ABright = CBright = 120;  
  179.   R = 185; //For the scrolly
  180.   G = 75; //
  181.   B = 10; //
  182.   //-
  183.  
  184.   // ge - Grasphics Environment
  185.   ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  186.   // gd - Grasphics Device
  187.   gd = ge.getScreenDevices();
  188.   DisplayMode mode = gd[0].getDisplayMode();
  189.   dispBounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
  190.  
  191.   // Preview windows
  192.   window_width      = mode.getWidth()/5;
  193.   window_height      = mode.getHeight()/5;
  194.   preview_pixel_width     = window_width/led_num_x;
  195.   preview_pixel_height   = window_height/led_num_y;
  196.  
  197.   // Preview window size
  198.   size(window_width, window_height);
  199.  
  200.   //standard Robot class error check
  201.   try   {
  202.     bot = new Robot(gd[0]);
  203.   }
  204.   catch (AWTException e)  {
  205.     println("Robot class not supported by your system!");
  206.     exit();
  207.   }
  208.  
  209.   float range, step, start;
  210.  
  211.   for(int i=0; i<leds.length; i++) { // For each LED...
  212.  
  213.     // Precompute columns, rows of each sampled point for this LED
  214.  
  215.     // --- for columns -----
  216.     range = (float)dispBounds.width / led_num_x;
  217.     // we only want 256 samples, and 16*16 = 256
  218.     step  = range / 16.0;
  219.     start = range * (float)leds[i][0] + step * 0.5;
  220.  
  221.     for(int col=0; col<16; col++) {
  222.       x[col] = (int)(start + step * (float)col);
  223.     }
  224.  
  225.     // ----- for rows -----
  226.     range = (float)dispBounds.height / led_num_y;
  227.     step  = range / 16.0;
  228.     start = range * (float)leds[i][1] + step * 0.5;
  229.  
  230.     for(int row=0; row<16; row++) {
  231.       y[row] = (int)(start + step * (float)row);
  232.     }
  233.  
  234.     // ---- Store sample locations -----
  235.  
  236.     // Get offset to each pixel within full screen capture
  237.     for(int row=0; row<16; row++) {
  238.       for(int col=0; col<16; col++) {
  239.         pixelOffset[i][row * 16 + col] = y[row] * dispBounds.width + x[col];
  240.       }
  241.     }
  242.  
  243.   }
  244.  
  245.   // Open serial port. this assumes the Arduino is the
  246.   // first/only serial device on the system.  If that's not the case,
  247.   // change "Serial.list()[0]" to the name of the port to be used:
  248.   // you can comment it out if you only want to test it without the Arduino
  249.  
  250.  port = new Serial(this, Serial.list()[portNo], 115200);
  251.  
  252.   // A special header expected by the Arduino, to identify the beginning of a new bunch data.  
  253.   serialData[0] = 'a';
  254.   serialData[1] = 'd';
  255.   //song = minim.loadFile("beat.mp3");
  256.   //song.play();
  257. }
  258.  
  259. void draw(){
  260.  
  261.   clear();
  262.   timeCur = millis();
  263.   // perform a forward FFT on the samples in input buffer
  264.   fft.forward(in.mix);
  265.  
  266.   float avg = 0.0;
  267.   cR = cG = cB = 0;
  268.   if(Auto&&Rythm)
  269.   {
  270.    
  271.     if(timeCur - timeLast > random(random(1000000)))
  272.     {
  273.       mode = (int)random(maxmode+1);
  274.       aR = (short)random(256);
  275.       aG = (short)random(256);
  276.       aB = (short)random(256);
  277.       timeCur = timeLast = millis();
  278.     }
  279.   }
  280.  
  281.   if(MusicC||MusicA||Rythm)
  282.   {
  283.   for(int i = 0; i < peaksize; ++i) {
  284.    
  285.    
  286.     // update decays
  287.     if (peak_age[i] < peak_hold_time) {
  288.       ++peak_age[i];
  289.     } else {
  290.       peaks[i] -= 50.0;//aging rate
  291.       if (peaks[i] < 0) { peaks[i] = 0; }
  292.     }
  293.   }
  294.  
  295.   for(int i = 0; i < spectrum_width; i++)  {
  296.    
  297.     float val = dB_scale*(20*((float)Math.log10(fft.getBand(i))) + gain);
  298.     if (fft.getBand(i) == 0) {   val = -200;   }  // avoid log(0)
  299.     int y = spectrum_height - Math.round(val);
  300.     if (y > spectrum_height) { y = spectrum_height; }
  301.    
  302.     // update the peak record
  303.     // which peak bin are we in?
  304.     int peaksi = i/binsperband;
  305.     if (val > peaks[peaksi]) {
  306.       peaks[peaksi] = val;
  307.       // reset peak age counter
  308.       peak_age[peaksi] = 0;
  309.     }
  310.   }
  311.   if(MusicC)
  312.     {
  313.     chassis[0] = chassis[1] =  chassis[2] = (byte)0;
  314.     for(int i = 0; i < 3; i++)  {
  315.     chassis[0] += peaks[i]/2.2;
  316.     }
  317.     for(int i = 3; i < 11; i++)  {
  318.     chassis[1] += peaks[i]/4;
  319.     }
  320.     for(int i = 12; i < 52; i++)  {
  321.     chassis[2] += peaks[i]*2.2;
  322.     }
  323.     for(int i = 0; i < 3; i++){
  324.       if(chassis[i] < 170)
  325.         chassis[i] = 0;
  326.     }
  327.    
  328.    
  329.     //delay(100);
  330.   }
  331.   else
  332.   {
  333.     chassis[0] = (byte)R;
  334.     chassis[1] = (byte)G;
  335.     chassis[2] = (byte)B;
  336.   }
  337.     for (int i = 0; i < 12; i++) {  // 12 is the number of bands
  338.    
  339.       int lowFreq;
  340.  
  341.       if ( i == 0 ) {
  342.         lowFreq = 0;
  343.       }
  344.       else {
  345.         lowFreq = (int)((sampleRate/2) / (float)Math.pow(2, 12 - i));
  346.       }
  347.  
  348.       int hiFreq = (int)((sampleRate/2) / (float)Math.pow(2, 11 - i));
  349.  
  350.       // we're asking for the index of lowFreq & hiFreq
  351.       int lowBound = fft.freqToIndex(lowFreq); // freqToIndex returns the index of the frequency band that contains the requested frequency
  352.       int hiBound = fft.freqToIndex(hiFreq);
  353.  
  354.       //println("range " + i + " = " + "Freq: " + lowFreq + " Hz - " + hiFreq + " Hz " + "indexes: " + lowBound + "-" + hiBound);
  355.  
  356.       // calculate the average amplitude of the frequency band
  357.       float avg1 = fft.calcAvg(lowBound, hiBound);
  358.        //println(avg1);
  359.       avg = max(avg,avg1);
  360.      
  361.     }
  362.   }
  363.   else
  364.   {
  365.     chassis[0] = (byte)R;
  366.     chassis[1] = (byte)G;
  367.     chassis[2] = (byte)B;
  368.   }
  369.  
  370.  if(Random){
  371.   chassis[0]=(short)(random(245)+10);
  372.   chassis[1]=(short)(random(245)+10);
  373.   chassis[2]=(short)(random(245)+10);
  374.   }
  375.  
  376.  
  377.   if(AmbiLight)
  378.   {
  379.   //get screenshot into object "screenshot" of class BufferedImage
  380.   screenshot = bot.createScreenCapture(dispBounds);
  381.   }
  382.   // Pass all the ARGB values of every pixel into an array
  383.   int[] screenData = ((DataBufferInt)screenshot.getRaster().getDataBuffer()).getData();
  384.  
  385.   data_index = 2; // 0, 1 are predefined header
  386.  
  387.   for(int i=0; i<leds.length; i++) {  // For each LED...
  388.  
  389.     int r = 0;
  390.     int g = 0;
  391.     int b = 0;
  392.  
  393.   if(AmbiLight)
  394.   {
  395.    
  396.     for(int o=0; o<256; o++)    
  397.     {       //ARGB variable with 32 int bytes where              
  398.         int pixel = screenData[ pixelOffset[i][o] ];            
  399.         r += pixel & 0x00ff0000;            
  400.         g += pixel & 0x0000ff00;            
  401.         b += pixel & 0x000000ff;        
  402.     }          
  403.     // Blend new pixel value with the value from the prior frame  
  404.  
  405.     cR += ledColor[i][0]  = (short)(((( r >> 24) & 0xff) * (255 - fade) + prevColor[i][0] * fade) >> 8);
  406.     cG += ledColor[i][1]  = (short)(((( g >> 16) & 0xff) * (255 - fade) + prevColor[i][1] * fade) >> 8);
  407.     cB += ledColor[i][2]  = (short)(((( b >>  8) & 0xff) * (255 - fade) + prevColor[i][2] * fade) >> 8);
  408.   }
  409.  
  410.   if(Rythm)
  411.   {
  412.     if(mode < 2)
  413.     {
  414.       if(Once)
  415.       {
  416.        
  417.         r = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50 + in.right.get(1) * 50 + in.right.get(2) * 50 - random(avg)));
  418.         g = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100 + in.right.get(5) * 50 + in.right.get(6) * 50 - random(avg)));
  419.         b = (int)(avg + abs(in.left.get(9) * 50 + in.left.get(10) * 50 + in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)));
  420.        
  421.         if(mode == 0)
  422.         {
  423.           prevColor[2][0] = prevColor[1][0] = prevColor[0][0] = (short)(r);
  424.           prevColor[2][1] = prevColor[1][1] = prevColor[0][1] = (short)(g);
  425.           prevColor[2][2] = prevColor[1][2] = prevColor[0][2] = (short)(b);
  426.           for(int k = 0 ; k < 3; k++)
  427.             for(int j = leds.length - 1; j > 0; j--)
  428.             {
  429.               prevColor[j][0]=prevColor[j - 1][0];
  430.               prevColor[j][1]=prevColor[j - 1][1];
  431.               prevColor[j][2]=prevColor[j - 1][2];
  432.             }
  433.        
  434.         }
  435.         if(mode == 1)
  436.         {
  437.           prevColor[leds.length - 1][0] = (short)(r);
  438.           prevColor[leds.length - 1][1] = (short)(g);
  439.           prevColor[leds.length - 1][2] = (short)(b);
  440.           for(int k = 0 ; k < 3; k++)
  441.             for(int j = 1; j < leds.length ; j++)
  442.             {
  443.               prevColor[j - 1][0]=prevColor[j][0];
  444.               prevColor[j - 1][1]=prevColor[j][1];
  445.               prevColor[j - 1][2]=prevColor[j][2];
  446.             }
  447.         }
  448.          
  449.         Once = false;
  450.       }
  451.    
  452.       ledColor[i][0]  = (short)(prevColor[i][0]);
  453.       ledColor[i][1]  = (short)(prevColor[i][1]);
  454.       ledColor[i][2]  = (short)(prevColor[i][2]);
  455.     }
  456.    
  457.     if(mode == 2)
  458.     {
  459.       if(i < leds.length/2)
  460.       {
  461.        
  462.         if(abs(in.left.get(1) * 100) > i)
  463.           r = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 100));
  464.         else
  465.           r = (int)(avg);
  466.        
  467.         if(abs(in.left.get(5) * 100) > i)
  468.           g = (int)(avg + abs(in.left.get(8) * 100 + in.left.get(9) * 100));
  469.         else
  470.           g = (int)(avg);
  471.        
  472.         if(abs(in.left.get(8) * 100) > i)
  473.           b = (int)(avg + abs(in.left.get(15) * 100 + in.left.get(16) * 100));
  474.         else
  475.           b = (int)(avg);
  476.       }else{
  477.        
  478.         if(abs(in.right.get(1) * 100) > (leds.length - i))
  479.           r = (int)(avg + abs(in.right.get(1) * 100 + in.right.get(2) * 100));
  480.         else
  481.           r = (int)(avg);
  482.        
  483.         if(abs(in.right.get(5) * 100) > (leds.length - i))
  484.           g = (int)(avg + abs(in.right.get(8) * 100 + in.right.get(9) * 100));
  485.         else
  486.           g = (int)(avg);
  487.        
  488.         if(abs(in.right.get(8) * 100) > (leds.length - i))
  489.           b = (int)(avg + abs(in.right.get(15) * 100 + in.right.get(16) * 100));
  490.         else
  491.           b = (int)(avg);
  492.       }
  493.            
  494.       if(AmbiLight)
  495.       {
  496.         ledColor[i][0]  += (short)(((r) * (255 - fade) + prevColor[i][0] * fade) >> 8);
  497.         ledColor[i][1]  += (short)(((g) * (255 - fade) + prevColor[i][1] * fade) >> 8);
  498.         ledColor[i][2]  += (short)(((b) * (255 - fade) + prevColor[i][2] * fade) >> 8);
  499.       }else{
  500.         ledColor[i][0]  = (short)(((r) * (255 - fade) + prevColor[i][0] * fade) >> 8);
  501.         ledColor[i][1]  = (short)(((g) * (255 - fade) + prevColor[i][1] * fade) >> 8);
  502.         ledColor[i][2]  = (short)(((b) * (255 - fade) + prevColor[i][2] * fade) >> 8);
  503.       }
  504.     }
  505.     //Volume style
  506.     if(mode == 3)
  507.     {
  508.       if(Once)
  509.       {
  510.         Once = false;
  511.         int tavg = (int)avg;
  512.         tavg = tavg % (leds.length/2);
  513.         if(tavg > peakA)
  514.           peakA = tavg;
  515.         else
  516.           peakA -= (int)(peakA - tavg)/2;
  517.         if(peakA < 0)
  518.           peakA = 0;
  519.         for(int j = 0; j < peakA; j++)
  520.         {
  521.           prevColor[j][0] = aR;
  522.           prevColor[j][1] = aG;
  523.           prevColor[j][2] = aB;
  524.         }
  525.         for(int j = leds.length - 1; j > leds.length - 1 - peakA; j--)
  526.         {
  527.           prevColor[j][0] = aR;
  528.           prevColor[j][1] = aG;
  529.           prevColor[j][2] = aB;
  530.         }
  531.         for(int j = peakA; j <= leds.length - 1 - peakA; j++)
  532.         {
  533.           prevColor[j][0] = 0;
  534.           prevColor[j][1] = 0;
  535.           prevColor[j][2] = 0;
  536.         }
  537.       }
  538.       ledColor[i][0]  = (short)(prevColor[i][0]);
  539.       ledColor[i][1]  = (short)(prevColor[i][1]);
  540.       ledColor[i][2]  = (short)(prevColor[i][2]);
  541.     }
  542.     //Glowy
  543.     if(mode == 4)
  544.     {
  545.       if(Once)
  546.       {
  547.         Once = false;
  548.         int lr, lg, lb, rr, rg ,rb;
  549.         int tavg = (int)avg/2;
  550.          for(int j = 0; j < leds.length; j++)
  551.         {
  552.           prevColor[j][0] = (short)(prevColor[j][0]*.70);
  553.           prevColor[j][1] = (short)(prevColor[j][1]*.70);
  554.           prevColor[j][2] = (short)(prevColor[j][2]*.70);
  555.         }
  556.         for(int j = 0; j < leds.length; j++)
  557.         {
  558.           prevColor[j][0] += tavg;
  559.           prevColor[j][1] += tavg;
  560.           prevColor[j][2] += tavg;
  561.         }
  562.         lr = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50  - random(avg)) + 50);
  563.         lg = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100  - random(avg)) + 50);
  564.         lb = (int)(avg + abs(in.left.get(9) * 100 + in.left.get(10) * 50  - random(avg)) + 50);
  565.         rr = (int)(avg + abs(in.right.get(1) * 100 + in.right.get(2) * 50 - random(avg)) + 50);
  566.         rg = (int)(avg + abs(in.right.get(5) * 50 + in.right.get(6) * 100 - random(avg)) + 50);
  567.         rb = (int)(avg + abs(in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)) + 50);
  568.         peakA = 1;// += random(100);
  569.         if(true)//(peakA%2) == 1)
  570.         {
  571.           prevColor[0][0] = (short)(lr);
  572.           prevColor[0][1] = (short)(lg);
  573.           prevColor[0][2] = (short)(lb);
  574.           for(int j = leds.length/2; j > 0; j--)
  575.           {
  576.             prevColor[j][0]=prevColor[j - 1][0];
  577.             prevColor[j][1]=prevColor[j - 1][1];
  578.             prevColor[j][2]=prevColor[j - 1][2];
  579.           }
  580.        
  581.         }
  582.         if(true)//(peakA % 2) == 0)
  583.         {
  584.           prevColor[leds.length - 1][0] = (short)(rr);
  585.           prevColor[leds.length - 1][1] = (short)(rg);
  586.           prevColor[leds.length - 1][2] = (short)(rb);
  587.           for(int j = leds.length/2; j < leds.length; j++)
  588.           {
  589.             prevColor[j - 1][0]=prevColor[j][0];
  590.             prevColor[j - 1][1]=prevColor[j][1];
  591.             prevColor[j - 1][2]=prevColor[j][2];
  592.           }
  593.         }
  594.       }
  595.       ledColor[i][0]  = (short)(prevColor[i][0]);
  596.       ledColor[i][1]  = (short)(prevColor[i][1]);
  597.       ledColor[i][2]  = (short)(prevColor[i][2]);
  598.     }
  599.     if(mode == 5||mode == 16||mode == 17||mode == 18||mode == 19||mode == 20)
  600.     {
  601.       if(Once)
  602.       {
  603.         Once = false;
  604.         int lr = 0, lg = 0, lb = 0, rr = 0, rg = 0 ,rb = 0;
  605.         int tavg = (int)avg/2;
  606.          for(int j = 0; j < leds.length; j++)
  607.         {
  608.           prevColor[j][0] = (short)(prevColor[j][0]*.70);
  609.           prevColor[j][1] = (short)(prevColor[j][1]*.70);
  610.           prevColor[j][2] = (short)(prevColor[j][2]*.70);
  611.         }
  612.         for(int j = 0; j < leds.length; j++)
  613.         {
  614.           prevColor[j][0] += tavg;
  615.           prevColor[j][1] += tavg;
  616.           prevColor[j][2] += tavg;
  617.         }
  618.         if(mode == 5)
  619.         {  
  620.           lr = (int)(peaks[0]);
  621.           rr = (int)(peaks[0]);
  622.         }
  623.         if(mode == 16)
  624.         {
  625.           lg = (int)(peaks[2] + peaks[3] + peaks[4]);
  626.           rg = (int)(peaks[2] + peaks[3] + peaks[4]);
  627.         }
  628.         if(mode == 17)
  629.         {
  630.           lb = (int)(peaks[5] + peaks[6] + peaks[7] + peaks[8] + peaks[9] + peaks[10]);  
  631.           rb = (int)(peaks[5] + peaks[6] + peaks[7] + peaks[8] + peaks[9] + peaks[10]);
  632.         }
  633.         if(mode == 18)
  634.         {
  635.           lg = (int)(peaks[2] + peaks[3] + peaks[4]);
  636.           rg = (int)(peaks[2] + peaks[3] + peaks[4]);
  637.           lb = (int)(peaks[5] + peaks[6] + peaks[7] + peaks[8] + peaks[9] + peaks[10]);  
  638.           rb = (int)(peaks[5] + peaks[6] + peaks[7] + peaks[8] + peaks[9] + peaks[10]);
  639.         }
  640.         if(mode == 19)
  641.         {
  642.           lg = (int)(peaks[2] + peaks[3] + peaks[4]);
  643.           rg = (int)(peaks[2] + peaks[3] + peaks[4]);
  644.           lr = (int)(peaks[0]);
  645.           rr = (int)(peaks[0]);
  646.         }
  647.         if(mode == 20)
  648.         {
  649.           lr = (int)(peaks[0]);
  650.           rr = (int)(peaks[0]);
  651.           lb = (int)(peaks[5] + peaks[6] + peaks[7] + peaks[8] + peaks[9] + peaks[10]);  
  652.           rb = (int)(peaks[5] + peaks[6] + peaks[7] + peaks[8] + peaks[9] + peaks[10]);
  653.         }
  654.         peakA = 1;// += random(100);
  655.         if(true)//(peakA%2) == 1)
  656.         {
  657.           prevColor[0][0] = (short)(lr);
  658.           prevColor[0][1] = (short)(lg);
  659.           prevColor[0][2] = (short)(lb);
  660.           for(int j = leds.length/2; j > 0; j--)
  661.           {
  662.             prevColor[j][0]=prevColor[j - 1][0];
  663.             prevColor[j][1]=prevColor[j - 1][1];
  664.             prevColor[j][2]=prevColor[j - 1][2];
  665.           }
  666.        
  667.         }
  668.         if(true)//(peakA % 2) == 0)
  669.         {
  670.           prevColor[leds.length - 1][0] = (short)(rr);
  671.           prevColor[leds.length - 1][1] = (short)(rg);
  672.           prevColor[leds.length - 1][2] = (short)(rb);
  673.           for(int j = leds.length/2; j < leds.length; j++)
  674.           {
  675.             prevColor[j - 1][0]=prevColor[j][0];
  676.             prevColor[j - 1][1]=prevColor[j][1];
  677.             prevColor[j - 1][2]=prevColor[j][2];
  678.           }
  679.         }
  680.       }
  681.       ledColor[i][0]  = (short)(prevColor[i][0]);
  682.       ledColor[i][1]  = (short)(prevColor[i][1]);
  683.       ledColor[i][2]  = (short)(prevColor[i][2]);
  684.     }
  685.     if(mode == 6||mode == 7)
  686.     {
  687.       if(avg>peak)
  688.       {
  689.         Once = true;
  690.         peak++;
  691.       }else{
  692.         Once = false;
  693.       peak-=2;
  694.       if(peak<10)
  695.         peak = 10;
  696.       }
  697.       if(Once)
  698.       {
  699.        
  700.         r = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50 + in.right.get(1) * 50 + in.right.get(2) * 50 - random(avg)));
  701.         g = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100 + in.right.get(5) * 50 + in.right.get(6) * 50 - random(avg)));
  702.         b = (int)(avg + abs(in.left.get(9) * 50 + in.left.get(10) * 50 + in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)));
  703.        
  704.         if(mode == 6)
  705.         {
  706.           //prevColor[2][0] = prevColor[1][0] =
  707.           prevColor[0][0] = (short)(r);
  708.           //prevColor[2][1] = prevColor[1][1] =
  709.           prevColor[0][1] = (short)(g);
  710.           //prevColor[2][2] = prevColor[1][2] =
  711.           prevColor[0][2] = (short)(b);
  712.           for(int k = 0 ; k < 3; k++)
  713.             for(int j = leds.length - 1; j > 0; j--)
  714.             {
  715.               prevColor[j][0]=prevColor[j - 1][0];
  716.               prevColor[j][1]=prevColor[j - 1][1];
  717.               prevColor[j][2]=prevColor[j - 1][2];
  718.             }
  719.        
  720.         }
  721.         if(mode == 7)
  722.         {
  723.           prevColor[leds.length - 1][0] = (short)(r);
  724.           prevColor[leds.length - 1][1] = (short)(g);
  725.           prevColor[leds.length - 1][2] = (short)(b);
  726.           for(int k = 0 ; k < 3; k++)
  727.             for(int j = 1; j < leds.length ; j++)
  728.             {
  729.               prevColor[j - 1][0]=prevColor[j][0];
  730.               prevColor[j - 1][1]=prevColor[j][1];
  731.               prevColor[j - 1][2]=prevColor[j][2];
  732.             }
  733.         }
  734.          
  735.         Once = false;
  736.       }
  737.    
  738.       ledColor[i][0]  = (short)(prevColor[i][0]);
  739.       ledColor[i][1]  = (short)(prevColor[i][1]);
  740.       ledColor[i][2]  = (short)(prevColor[i][2]);
  741.     }
  742.     if(mode == 8||mode == 9)
  743.     {
  744.       if(avg>peak)
  745.       {
  746.         peak++;
  747.         Once = true;
  748.       }else{
  749.         peak-=2;
  750.         Once = false;
  751.       if(peak<10)
  752.         peak = 10;
  753.       }
  754.       if(Once)
  755.       {
  756.        
  757.         r = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50 + in.right.get(1) * 50 + in.right.get(2) * 50 - random(avg)));
  758.         g = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100 + in.right.get(5) * 50 + in.right.get(6) * 50 - random(avg)));
  759.         b = (int)(avg + abs(in.left.get(9) * 50 + in.left.get(10) * 50 + in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)));
  760.       }else{
  761.         r = g = b = 0;
  762.       }
  763.        
  764.       if(mode == 8)
  765.       {
  766.         //prevColor[2][0] = prevColor[1][0] =
  767.         prevColor[0][0] = (short)(r);
  768.         //prevColor[2][1] = prevColor[1][1] =
  769.         prevColor[0][1] = (short)(g);
  770.         //prevColor[2][2] = prevColor[1][2] =
  771.         prevColor[0][2] = (short)(b);
  772.         for(int k = 0 ; k < 1; k++)
  773.           for(int j = leds.length - 1; j > 0; j--)
  774.           {
  775.             prevColor[j][0]=prevColor[j - 1][0];
  776.             prevColor[j][1]=prevColor[j - 1][1];
  777.             prevColor[j][2]=prevColor[j - 1][2];
  778.           }
  779.      
  780.       }
  781.       if(mode == 9)
  782.       {
  783.         prevColor[leds.length - 1][0] = (short)(r);
  784.         prevColor[leds.length - 1][1] = (short)(g);
  785.         prevColor[leds.length - 1][2] = (short)(b);
  786.         for(int k = 0 ; k < 1; k++)
  787.           for(int j = 1; j < leds.length ; j++)
  788.           {
  789.             prevColor[j - 1][0]=prevColor[j][0];
  790.             prevColor[j - 1][1]=prevColor[j][1];
  791.             prevColor[j - 1][2]=prevColor[j][2];
  792.           }
  793.       }
  794.        
  795.       Once = false;
  796.      
  797.    
  798.       ledColor[i][0]  = (short)(prevColor[i][0]);
  799.       ledColor[i][1]  = (short)(prevColor[i][1]);
  800.       ledColor[i][2]  = (short)(prevColor[i][2]);
  801.     }
  802.    
  803.     if(mode == 10||mode == 11)
  804.     {
  805.       if(avg>(peak))
  806.       {
  807.         peak+=50;
  808.         Once = true;
  809.         r = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50 + in.right.get(1) * 50 + in.right.get(2) * 50 - random(avg)));
  810.         g = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100 + in.right.get(5) * 50 + in.right.get(6) * 50 - random(avg)));
  811.         b = (int)(avg + abs(in.left.get(9) * 50 + in.left.get(10) * 50 + in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)));
  812.       }else{
  813.         peak-=2;
  814.         Once = false;
  815.         r = g = b = 1;
  816.       if(peak<10)
  817.         peak = 10;
  818.       }
  819.      
  820.       if(Once)
  821.       {      
  822.       if(mode == 10)
  823.       {
  824.         //prevColor[2][0] = prevColor[1][0] =
  825.         prevColor[0][0] = (short)(r);
  826.         //prevColor[2][1] = prevColor[1][1] =
  827.         prevColor[0][1] = (short)(g);
  828.         //prevColor[2][2] = prevColor[1][2] =
  829.         prevColor[0][2] = (short)(b);
  830.         for(int k = 0 ; k < 2; k++)
  831.           for(int j = leds.length - 1; j > 0; j--)
  832.           {
  833.             prevColor[j][0]=prevColor[j - 1][0];
  834.             prevColor[j][1]=prevColor[j - 1][1];
  835.             prevColor[j][2]=prevColor[j - 1][2];
  836.           }
  837.      
  838.       }
  839.       if(mode == 11)
  840.       {
  841.         prevColor[leds.length - 1][0] = (short)(r);
  842.         prevColor[leds.length - 1][1] = (short)(g);
  843.         prevColor[leds.length - 1][2] = (short)(b);
  844.         for(int k = 0 ; k < 2; k++)
  845.           for(int j = 1; j < leds.length ; j++)
  846.           {
  847.             prevColor[j - 1][0]=prevColor[j][0];
  848.             prevColor[j - 1][1]=prevColor[j][1];
  849.             prevColor[j - 1][2]=prevColor[j][2];
  850.           }
  851.       }
  852.       }
  853.       Once = false;
  854.      
  855.    
  856.       ledColor[i][0]  = (short)(prevColor[i][0]);
  857.       ledColor[i][1]  = (short)(prevColor[i][1]);
  858.       ledColor[i][2]  = (short)(prevColor[i][2]);
  859.     }
  860.     if(mode == 12||mode == 13)
  861.     {
  862.       if(avg>(peak))
  863.       {
  864.         peak+=100;
  865.         Once = true;
  866.         r = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50 + in.right.get(1) * 50 + in.right.get(2) * 50 - random(avg)));
  867.         g = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100 + in.right.get(5) * 50 + in.right.get(6) * 50 - random(avg)));
  868.         b = (int)(avg + abs(in.left.get(9) * 50 + in.left.get(10) * 50 + in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)));
  869.       }else{
  870.         peak-=2;
  871.         //Once = false;
  872.         r = g = b = 1;
  873.       if(peak<10)
  874.         peak = 10;
  875.       }
  876.      
  877.       if(Once)
  878.       {      
  879.       if(mode == 12)
  880.       {
  881.         //prevColor[2][0] = prevColor[1][0] =
  882.         prevColor[0][0] = (short)(r);
  883.         //prevColor[2][1] = prevColor[1][1] =
  884.         prevColor[0][1] = (short)(g);
  885.         //prevColor[2][2] = prevColor[1][2] =
  886.         prevColor[0][2] = (short)(b);
  887.         for(int k = 0 ; k < 1; k++)
  888.           for(int j = leds.length - 1; j > 0; j--)
  889.           {
  890.             prevColor[j][0]=prevColor[j - 1][0];
  891.             prevColor[j][1]=prevColor[j - 1][1];
  892.             prevColor[j][2]=prevColor[j - 1][2];
  893.           }
  894.      
  895.       }
  896.       if(mode == 13)
  897.       {
  898.         prevColor[leds.length - 1][0] = (short)(r);
  899.         prevColor[leds.length - 1][1] = (short)(g);
  900.         prevColor[leds.length - 1][2] = (short)(b);
  901.         for(int k = 0 ; k < 1; k++)
  902.           for(int j = 1; j < leds.length ; j++)
  903.           {
  904.             prevColor[j - 1][0]=prevColor[j][0];
  905.             prevColor[j - 1][1]=prevColor[j][1];
  906.             prevColor[j - 1][2]=prevColor[j][2];
  907.           }
  908.       }
  909.       }
  910.       Once = false;
  911.      
  912.    
  913.       ledColor[i][0]  = (short)(prevColor[i][0]);
  914.       ledColor[i][1]  = (short)(prevColor[i][1]);
  915.       ledColor[i][2]  = (short)(prevColor[i][2]);
  916.     }
  917.    
  918.     if(mode == 14||mode == 15)
  919.     {
  920.       if(avg>(peak))
  921.       {
  922.         peak+=100;
  923.         Once = true;
  924.         if(abs((aR + aG +aB) - (rp + gp + bp)) < 5)
  925.         {
  926.           rp = (int)(avg + abs(in.left.get(1) * 100 + in.left.get(2) * 50 + in.right.get(1) * 50 + in.right.get(2) * 50 - random(avg)));
  927.           gp = (int)(avg + abs(in.left.get(5) * 50 + in.left.get(6) * 100 + in.right.get(5) * 50 + in.right.get(6) * 50 - random(avg)));
  928.           bp = (int)(avg + abs(in.left.get(9) * 50 + in.left.get(10) * 50 + in.right.get(9) * 100 + in.right.get(10) * 50 - random(avg)));
  929.         }
  930.       }else{
  931.         peak-=2;
  932.         //Once = false;
  933.  
  934.       }
  935.       if(aR != rp)
  936.         aR += (rp-aR)/2;
  937.       if(aG != gp)
  938.         aG += (gp-aG)/2;
  939.       if(aB != bp)
  940.         aB += (bp-aB)/2;
  941.       if(peak<10)
  942.         peak = 10;
  943.       if(Once)
  944.       {      
  945.       if(mode == 14)
  946.       {
  947.         //prevColor[2][0] = prevColor[1][0] =
  948.         prevColor[0][0] = (short)(aR);
  949.         //prevColor[2][1] = prevColor[1][1] =
  950.         prevColor[0][1] = (short)(aG);
  951.         //prevColor[2][2] = prevColor[1][2] =
  952.         prevColor[0][2] = (short)(aB);
  953.         for(int k = 0 ; k < 1; k++)
  954.           for(int j = leds.length - 1; j > 0; j--)
  955.           {
  956.             prevColor[j][0]=prevColor[j - 1][0];
  957.             prevColor[j][1]=prevColor[j - 1][1];
  958.             prevColor[j][2]=prevColor[j - 1][2];
  959.           }
  960.      
  961.       }
  962.       if(mode == 15)
  963.       {
  964.         prevColor[leds.length - 1][0] = (short)(aR);
  965.         prevColor[leds.length - 1][1] = (short)(aG);
  966.         prevColor[leds.length - 1][2] = (short)(aB);
  967.         for(int k = 0 ; k < 1; k++)
  968.           for(int j = 1; j < leds.length ; j++)
  969.           {
  970.             prevColor[j - 1][0]=prevColor[j][0];
  971.             prevColor[j - 1][1]=prevColor[j][1];
  972.             prevColor[j - 1][2]=prevColor[j][2];
  973.           }
  974.       }
  975.       }
  976.       Once = false;
  977.      
  978.    
  979.       ledColor[i][0]  = (short)(prevColor[i][0]);
  980.       ledColor[i][1]  = (short)(prevColor[i][1]);
  981.       ledColor[i][2]  = (short)(prevColor[i][2]);
  982.     }
  983.    
  984.   }
  985.  
  986.     serialData[data_index++] = (byte)(ledColor[i][0]*0.76);
  987.     serialData[data_index++] = (byte)ledColor[i][1];
  988.     serialData[data_index++] = (byte)ledColor[i][2];
  989.  
  990.  
  991.  
  992.     float preview_pixel_left  = (float)dispBounds.width  /5 / led_num_x * leds[i][0] ;
  993.     float preview_pixel_top    = (float)dispBounds.height /5 / led_num_y * leds[i][1] ;
  994.  
  995.     color rgb = color(ledColor[i][0], ledColor[i][1], ledColor[i][2]);
  996.     fill(rgb);  
  997.     rect(preview_pixel_left, preview_pixel_top, preview_pixel_width, preview_pixel_height);
  998.  
  999.   }
  1000.  
  1001.  
  1002.   if(Case)
  1003.   {
  1004.     if(AvgCase)
  1005.     {
  1006.       chassis[0] = (byte)(cR/leds.length);
  1007.       chassis[1] = (byte)(cG/leds.length);
  1008.       chassis[2] = (byte)(cB/leds.length);
  1009.       //println("R "+ cR/leds.length);
  1010.     }
  1011.     serialData[data_index++] = (byte)chassis[0];
  1012.     serialData[data_index++] = (byte)chassis[1];
  1013.     serialData[data_index++] = (byte)chassis[2];
  1014.    
  1015.   }else{
  1016.     serialData[data_index++] = serialData[data_index++] = serialData[data_index++] = 0;
  1017.   }
  1018.  
  1019.   if(!Once)
  1020.     Once = true;
  1021.  
  1022.   if(MusicA)
  1023.   {
  1024.    
  1025.     CBrightt -= 160;
  1026.     if(avg>peak)
  1027.     {
  1028.       CBrightt = 255;
  1029.       peak++;
  1030.     }else{
  1031.       peak-=2;
  1032.       if(peak<10)
  1033.         peak = 10;
  1034.       if(CBrightt < 0)
  1035.         CBrightt = 0;
  1036.     }
  1037.     if(MusicA)  
  1038.       serialData[data_index++] = (byte)ABright;
  1039.     else
  1040.       serialData[data_index++] = (byte)ABright;
  1041.     serialData[data_index] = (byte)CBrightt;
  1042.      
  1043.   }else{
  1044.     serialData[data_index++] = (byte)ABright;
  1045.     serialData[data_index] = (byte)CBright;
  1046.   }
  1047.   if(Glow)
  1048.   {
  1049.      if(type == 0)
  1050.      {
  1051.        serialData[185] *= abs(sin(angle));
  1052.        serialData[186] *= abs(sin(angle));
  1053.        angle += inc;
  1054.      }
  1055.      if(type == 1)
  1056.      {
  1057.        if(bAlt)
  1058.        {
  1059.          serialData[185] = 0;
  1060.          serialData[186] = 0;
  1061.        }
  1062.          
  1063.        if(abs(timeCur - lastGlow) > gtn)
  1064.        {
  1065.          lastGlow = timeCur;
  1066.          bAlt = !bAlt;
  1067.          
  1068.        }
  1069.      }
  1070.      if(type == 2)
  1071.      {
  1072.        if(bAlt)
  1073.        {
  1074.          serialData[185] = 0;
  1075.          serialData[186] = 0;
  1076.        }
  1077.        if(gAlt)
  1078.        {  
  1079.          if(abs(timeCur - lastGlow) > gtn)
  1080.          {
  1081.            lastGlow = timeCur;
  1082.            bAlt = !bAlt;
  1083.            gAlt = !gAlt;
  1084.          }
  1085.        }else{
  1086.          if(abs(timeCur - lastGlow) > gtf)
  1087.          {
  1088.            lastGlow = timeCur;
  1089.            bAlt = !bAlt;
  1090.            gAlt = !gAlt;
  1091.          }
  1092.        }
  1093.        println(serialData[185]);
  1094.      }
  1095.   }
  1096.  
  1097.   if(port != null) {
  1098.  
  1099.     // wait for Arduino to send data
  1100.     for(;;){
  1101.  
  1102.       if(port.available() > 0){
  1103.         int inByte = port.read();
  1104.         if (inByte == 121)
  1105.           {noFails = 0;break;}
  1106.       }
  1107.  
  1108. //println(++noFails);
  1109.  
  1110.       if(noFails > 1000)
  1111.       {
  1112.        
  1113.         //port.stop();
  1114.         //port = new Serial(this, Serial.list()[portNo], 115200);
  1115.         noFails = 0;
  1116.       }
  1117.     }
  1118.     port.write(serialData); // Issue data to Arduino
  1119.  
  1120.   }
  1121.   if(noFails > 1000)
  1122.   {
  1123.     //port.stop();
  1124.     //port = new Serial(this, Serial.list()[portNo], 115200);
  1125.     noFails = 0;
  1126.   }
  1127.   // Benchmark, how are we doing?
  1128.   println(frameRate);
  1129.   arraycopy(ledColor, 0, prevColor, 0, ledColor.length);
  1130.  
  1131. }
  1132.  
  1133. void stop()
  1134. {
  1135.   // always close Minim audio classes when you finish with them
  1136.   in.close();
  1137.   minim.stop();
  1138.   super.stop();  
  1139. }
  1140.  
  1141. public void AmbiLight()
  1142. {
  1143.   AmbiLight=!AmbiLight;
  1144.   for(int i=0; i<leds.length; i++) {
  1145.     data_index = 2; // 0, 1 are predefined header
  1146.     ledColor[i][0] = 0;
  1147.     ledColor[i][1] = 0;
  1148.     ledColor[i][2] = 0;
  1149.   }
  1150. }
  1151.  
  1152. public void Rythm()
  1153. {
  1154.   //AmbiLight = false;
  1155.   Rythm = !Rythm;
  1156. }
  1157.  
  1158. public void Case()
  1159. {
  1160.   Case = !Case;
  1161. }
  1162.  
  1163. public void AvgCase()
  1164. {
  1165.   AvgCase = !AvgCase;
  1166. }
  1167.  
  1168.  
  1169. public void Random()
  1170. {
  1171.   Random = !Random;
  1172. }
  1173.  
  1174. public void MusicC()
  1175. {
  1176.   MusicC = !MusicC;
  1177. }
  1178.  
  1179. public void MusicA()
  1180. {
  1181.   MusicA = !MusicA;
  1182. }
  1183. public void M()
  1184. {
  1185.   mode++;
  1186.   if(mode > maxmode)
  1187.     mode = 0;
  1188. }
  1189. public void A()
  1190. {
  1191.   Auto=!Auto;
  1192. }
  1193. public void Glow()
  1194. {
  1195.   Glow=!Glow;
  1196. }
  1197. public void T()
  1198. {
  1199.   type++;
  1200.   if(type > maxtype)
  1201.     type = 0;
  1202. }
  1203.  
  1204. /*
  1205. // Fill the dots one after the other with a color
  1206. public void colorWipe(uint32_t c, uint8_t wait) {
  1207.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  1208.       strip.setPixelColor(i, c);
  1209.       strip.show();
  1210.       //chassisIdle();
  1211.       delay(wait);
  1212.   }
  1213. }
  1214.  
  1215. void rainbow(uint8_t wait) {
  1216.   uint16_t i, j;
  1217.  
  1218.   for(j=0; j<256; j++) {
  1219.     for(i=0; i<strip.numPixels(); i++) {
  1220.       strip.setPixelColor(i, Wheel((i+j) & 255));
  1221.     }
  1222.     //chassisIdle();
  1223.     strip.show();
  1224.     delay(wait);
  1225.   }
  1226. }
  1227.  
  1228. // Slightly different, this makes the rainbow equally distributed throughout
  1229. void rainbowCycle(uint8_t wait) {
  1230.   uint16_t i, j;
  1231.  
  1232.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  1233.     for(i=0; i< strip.numPixels(); i++) {
  1234.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  1235.     }
  1236.     //chassisIdle();
  1237.     strip.show();
  1238.     delay(wait);
  1239.   }
  1240. }
  1241.  
  1242. //Theatre-style crawling lights.
  1243. void theaterChase(uint32_t c, uint8_t wait) {
  1244.   for (int j=0; j<10; j++) {  //do 10 cycles of chasing
  1245.     for (int q=0; q < 3; q++) {
  1246.       for (int i=0; i < strip.numPixels(); i=i+3) {
  1247.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  1248.       }
  1249.       strip.show();
  1250.       //chassisIdle();    
  1251.       delay(wait);
  1252.      
  1253.       for (int i=0; i < strip.numPixels(); i=i+3) {
  1254.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  1255.       }
  1256.     }
  1257.   }
  1258. }
  1259.  
  1260. //Theatre-style crawling lights with rainbow effect
  1261. void theaterChaseRainbow(uint8_t wait) {
  1262.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  1263.     for (int q=0; q < 3; q++) {
  1264.         for (int i=0; i < strip.numPixels(); i=i+3) {
  1265.           strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  1266.         }
  1267.         strip.show();
  1268.         //chassisIdle();      
  1269.         delay(wait);
  1270.        
  1271.         for (int i=0; i < strip.numPixels(); i=i+3) {
  1272.           strip.setPixelColor(i+q, 0);        //turn every third pixel off
  1273.         }
  1274.     }
  1275.   }
  1276. }
  1277.  
  1278. // Input a value 0 to 255 to get a color value.
  1279. // The colours are a transition r - g - b - back to r.
  1280. uint32_t Wheel(byte WheelPos) {
  1281.   if(WheelPos < 85) {
  1282.    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  1283.   } else if(WheelPos < 170) {
  1284.    WheelPos -= 85;
  1285.    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  1286.   } else {
  1287.    WheelPos -= 170;
  1288.    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  1289.   }
  1290. }
  1291. */
  1292. public class DisposeHandler {
  1293.    
  1294.   DisposeHandler(PApplet pa)
  1295.   {
  1296.     pa.registerMethod("dispose", this);
  1297.   }
  1298.    
  1299.   public void dispose()
  1300.   {      
  1301.     AmbiLight = false;
  1302.     Case = false;
  1303.     ABright = CBright = 0;
  1304.     for(int i=0;i < 5; i++)
  1305.       draw();
  1306.     // Place here the code you want to execute on exit
  1307.   }
  1308. }
Advertisement
RAW Paste Data Copied
Advertisement