-
Notifications
You must be signed in to change notification settings - Fork 7
/
LEDStripController.pde
170 lines (141 loc) · 3.99 KB
/
LEDStripController.pde
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
// bring in library to talk with USB
import processing.serial.*;
Serial port;
/*libraries to talk to midi devices
to make this work go to the Sketch->Import Library->Add Library
and add the MidiBus library*/
import themidibus.*;
MidiBus myBus;
// set the size and configuration of your LEDs (never less that 1 on either)
int cols = 40;
int rows = 20;
int LEDCount = cols*rows;
// ArrayList to hold the current effects
ArrayList <Effect> effects;
// Image buffer to hold pixels in
PGraphics canvas;
// Interface to pick colors for effects
ColorPicker colorPicker;
color selectedColor;
void setup() {
// Size of the on-screen display
size(800, 600);
// makes the screen feedback accurate
noSmooth();
// set the frame refresh rate
frameRate(24);
// set serial (USB) port. Change the number in [brackets]
try {
port = new Serial(this, Serial.list()[2], 9600);
} catch (Exception e) {
println("Problem connecting to Serial port: " + e);
}
// initialize effect array
effects = new ArrayList();
// canvas for the leds to run on
canvas = createGraphics(cols, rows);
colorPicker = new ColorPicker(0,0,50,50);
selectedColor = color(255,255,255);
myBus = new MidiBus(this, "USB Keystation 49e", -1);
}
void draw() {
background(60);
// update any changes to effects
updateEffects();
// add all the images from effects to the canvas buffer
canvas.beginDraw();
canvas.background(0);
for(Effect e : effects){
canvas.image(e.getBuffer(),0,0);
}
canvas.endDraw();
// stretch the canvas image accross the screen
pushStyle();
imageMode(CENTER);
image(canvas, width/2,height/2,cols*15,rows*15);
popStyle();
colorPicker.display();
fill(selectedColor);
noStroke();
rect(50,0,50,50);
try {
sendToArduino();
} catch (Exception e) {
// println("Problem with sendToArduino(): " + e);
}
debug();
}
// run to show framerate and other debug info
void debug(){
fill(0,120);
noStroke();
rect(0, height-30, width/8, 10);
fill(255);
stroke(255);
text(frameRate, 10, height-20);
}
void updateEffects(){
// go backwards through the effect array. Update and remove dead effects
for (int i = effects.size() - 1; i >= 0; i--) {
Effect e = (Effect) effects.get(i);
if (e.finished()) {
effects.remove(i);
} else {
e.update();
}
}
}
void sendToArduino(){
/*This is where we will send the canvas through the serial
bus to the Arduino*/
// load the pixel array in the canvas object
canvas.loadPixels();
// send each pixel to the arduino
for(int i=0;i<canvas.pixels.length; i++){
color p = canvas.pixels[i];
int nR = (p >> 16) & 0xFF; // Faster way of getting red(p)
int nG = (p >> 8) & 0xFF; // Faster way of getting green(p)
int nB = p & 0xFF;
// send comma separated values: ledID red green blue
String output = i + "\t" + round(nR) + "\t" + round(nG) + "\t" + round(nB) + "\n";
port.write(output);
}
port.write("\n");
}
void keyPressed(){
println(keyCode);
if (key == 's') {
Snow snow = new Snow(9000);
effects.add(snow);
} else if (key == 't') {
color c = color(245, 255, 120);
Twinkle twinkle = new Twinkle(3000, c);
effects.add(twinkle);
} else if (key == 'f') {
Flashes flashes = new Flashes(15000);
effects.add(flashes);
} else if (key == 'c') {
CircleBurst circleBurst = new CircleBurst(2000, selectedColor);
effects.add(circleBurst);
} else if (key == 'w') {
Wipe wipe = new Wipe(2000, selectedColor);
effects.add(wipe);
} else if (key == 'p') {
// Modifying steps and zoom produces drastic variation.
Plasma plasma = new Plasma(12000, 35, 200, 200, 8);
effects.add(plasma);
} else if (key == 'l') {
GameOfLife gameOfLife = new GameOfLife(3000, selectedColor);
effects.add(gameOfLife);
} else if (key == 'v'){
Flock flock = new Flock(4000, selectedColor);
effects.add(flock);
}
}
void mousePressed(){
color c = colorPicker.getColor();
if(c != -1){
selectedColor = c;
}
println(selectedColor);
}