-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED_projector.pde
214 lines (186 loc) · 5.07 KB
/
LED_projector.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
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
//LED Projector v1
//Made at Jason Bruges Studio
//by Dhruv Kumar
//with Love
import controlP5.*;
import processing.serial.*;
import processing.video.*;
Serial arduino;
String[] ports;
int numRows;
int numColumns;
LED[][] led;
byte pat = 0;
int size = 10;
int cPicker = color(100);
long sendInterval = 60;
long lastTrigger = 0;
PImage img;
Movie movie;
boolean playVideo = false;
boolean bw = false;
boolean drag = false;
float globalB = .5;
int gridDistance = 50;
float gridRotation = 0;
int ledBox = 10;
int fading = 0;
PVector m;
color c;
int uiWidth = 170;
int baudr = 500000;
void setup() {
//BASIC SETUP
size(1200, 600);
surface.setResizable(true);
noStroke();
smooth();
rectMode(CENTER);
strokeWeight(2);
noFill();
stroke(255);
//GET LIST OF PORTS, SETUP THE FIRST ONE FOR NOW
ports = Serial.list();
arduino = new Serial(this, ports[0], baudr);
//INITIALISE LED OBJECTS
ledSetup();
m = new PVector(width/2, height/2);
//LOAD AN IMAGE AND VIDEO
img = loadImage("gradient.jpg");
movie = new Movie(this, "transit.mov");
movie.loop();
numRows = 8;
numColumns = 1;
createGUI();
}
void draw() {
background(255);
//PLAYVIDEO VARIABLE IS SET FROM THE UI, IMAGE OR VIDEO
if (playVideo == false) {
img.resize(width, height);
image(img, 0, 0);
} else {
image(movie, 0, 0, width, height);
}
//DRAW LEDS ON SCREEN AND GET PIXEL COLOR VALUES
drawLEDs();
//SEND DATA TO ARDUINO AFTER EVERY INTERVAL FOR SANITY
if (millis()-lastTrigger > sendInterval) {
arduino.write(getValues());
lastTrigger = millis();
}
}
//INITIALISING LEDS
void ledSetup() {
led = new LED[numRows][numColumns];
for (int i=0; i<numRows; i++) {
for (int j=0; j<numColumns; j++) {
led[i][j] = new LED();
}
}
}
//DRAWING LEDS ON SCREEN AND GETTING PIXEL VALUES
void drawLEDs() {
if (drag)
m = new PVector(mouseX, mouseY);
switch(pat) {
//GET COLOURS FROM A SINGLE POINT
case 0:
c = get(int(m.x), int(m.y)); //Pick colours
rect(m.x, m.y, ledBox, ledBox); //Draw rectangle at position
for (int i=0; i<numRows; i++) { //assign same color value to all LEDs
for (int j=0; j<numColumns; j++) {
led[i][j].red = byte(red(c)*globalB); //enforce global brightness value
led[i][j].blue = byte(blue(c)*globalB);
led[i][j].green = byte(green(c)*globalB);
}
}
break;
//GET COLOURS FROM A STRIP PATTERN (COLUMNS IGNORED)
case 1:
for (int i=0; i<numRows; i++) {
//Get each co-ordinate based on grid distance and rotation set by user
int x = int(m.x + gridDistance * (i-numRows/2) * cos(gridRotation));
int y = int(m.y + gridDistance * (i-numRows/2) * sin(gridRotation));
c = get(x, y); //Pick colour
rect(x, y, ledBox, ledBox); //Draw Rectangle
led[i][0].red = byte(red(c)*globalB);
led[i][0].blue = byte(blue(c)*globalB);
led[i][0].green = byte(green(c)*globalB);
}
break;
//GET COLOURS FROM A 2D MATRIX PATTERN
case 2:
for (int i=0; i<numColumns; i++) {
for (int j=0; j<numRows; j++) {
//Get each co-ordinate based on grid distance
int x = int(m.x + gridDistance * (j-numRows/2));
int y = int(m.y + gridDistance * (i-numColumns/2));
//Apply rotation using PVector
PVector cord = new PVector (x, y);
cord.sub(m);
cord.rotate(gridRotation);
cord.add(m);
x = int(cord.x);
y = int(cord.y);
//Get colour and draw rectangles
c = get(x, y);
rect(x, y, ledBox, ledBox);
led[j][i].red = byte(red(c)*globalB);
led[j][i].blue = byte(blue(c)*globalB);
led[j][i].green = byte(green(c)*globalB);
}
}
break;
//GET COLOURS FROM A RING PATTERN (COLUMNS IGNORED)
case 3:
for (int i=0; i<numRows; i++) {
//Get position based on angles
PVector pos = PVector.fromAngle(gridRotation+TWO_PI/numRows*i);
pos.setMag(gridDistance*5);
pos.add(m);
//Get colours and draw rectangles
c = get(int(pos.x), int(pos.y));
rect(pos.x, pos.y, ledBox, ledBox);
led[i][0].red = byte(red(c)*globalB);
led[i][0].blue = byte(blue(c)*globalB);
led[i][0].green = byte(green(c)*globalB);
}
break;
}
}
//A SIMPLE LED CLASS WITH RGB VALUES AND BW FUNCTION
class LED {
byte red;
byte green;
byte blue;
LED() {
red = 0;
green = 0;
blue = 0;
}
void makeBW() {
byte average = byte((red + blue + green)/3);
red = average;
blue = average;
green = average;
}
}
//FUNCTION TO PACK ALL VALUES IN A BYTE ARRAY, READY TO SEND TO ARDUINO
byte[] getValues() {
byte[] values = new byte[numRows*numColumns*3];
int k = 0;
for (int i=0; i<numColumns; i++) {
for (int j=0; j<numRows; j++) {
if (bw == true)
led[j][i].makeBW();
values[k++] = led[j][i].green;
values[k++] = led[j][i].red;
values[k++] = led[j][i].blue;
}
}
return values;
}
void movieEvent(Movie m) {
m.read();
}