Skip to content

Commit 654c5ce

Browse files
authored
Merge pull request #2125 from adafruit/updating_pi_vid_synth
updating pi video synth code
2 parents 3b82052 + 2a29c1d commit 654c5ce

File tree

2 files changed

+119
-39
lines changed

2 files changed

+119
-39
lines changed

Raspberry_Pi_Video_Synth/BlinkaRaspberryPiVideoSynth.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
print('got connected', addr)
7373

7474
while True:
75+
# reset the VL53L4CD
76+
vl53.clear_interrupt()
77+
7578
# rotary encoder position read
7679
position = -rot_encoder.position
7780

@@ -88,6 +91,11 @@
8891
g_pot = int(g_mapped)
8992
b_pot = int(b_mapped)
9093

94+
# set neopixels on neosliders to match background color of Processing animations
95+
r_pix.fill((r_pot, g_pot, b_pot))
96+
g_pix.fill((r_pot, g_pot, b_pot))
97+
b_pix.fill((r_pot, g_pot, b_pot))
98+
9199
# rotary encoder position check
92100
if position != last_position:
93101
# rotary encoder is ranged to 0-3
@@ -102,40 +110,37 @@
102110
last_position = position
103111
# sliders only update data for changes >15 to avoid flooding socket
104112
# red neoslider position check
105-
if abs(r_pot - last_r) > 15:
113+
if abs(r_pot - last_r) > 2:
106114
# send red neoslider data over socket
107115
# identifying string is "red"
108116
c.send(str.encode(' '.join(["red", str(r_pot)])))
109117
# reset last_r
110118
last_r = r_pot
111119
# green neoslider position check
112-
if abs(g_pot - last_g) > 15:
120+
if abs(g_pot - last_g) > 2:
113121
# send green neoslider data over socket
114122
# identifying string is "green"
115123
c.send(str.encode(' '.join(["green", str(g_pot)])))
116124
# reset last_g
117125
last_g = g_pot
118126
# blue neoslider position check
119-
if abs(b_pot - last_b) > 15:
127+
if abs(b_pot - last_b) > 2:
120128
# send blue neoslider data over socket
121129
# identifying string is "blue"
122130
c.send(str.encode(' '.join(["blue", str(b_pot)])))
123131
# reset last_b
124132
last_b = b_pot
125133
# VL53L4CD value check
134+
135+
# setting max value of 45
136+
if flight > 45:
137+
flight = 45
138+
last_flight = flight
126139
if abs(flight - last_flight) > 2:
127-
# setting max value of 45
128-
if flight > 45:
129-
flight = 45
140+
print(flight)
130141
# send VL53L4CD data over socket
131142
# identifying string is "flight"
132143
c.send(str.encode(' '.join(["flight", str(flight)])))
133-
# reset last_flight
144+
# reset last_flight
134145
last_flight = flight
135-
# set neopixels on neosliders to match background color of Processing animations
136-
r_pix.fill((r_pot, g_pot, b_pot))
137-
g_pix.fill((r_pot, g_pot, b_pot))
138-
b_pix.fill((r_pot, g_pot, b_pot))
139-
# reset the VL53L4CD
140-
vl53.clear_interrupt()
141-
146+

Raspberry_Pi_Video_Synth/Raspberry_Pi_Video_Synth.pde

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,22 @@ int xdirection = 1;
5454
int ydirection = 1;
5555

5656
// variables for pizzaCat animation
57-
float pizza_speed = 20;
58-
float cat_speed = 30;
57+
int pizzaCount = 32;
58+
int catCount = 32;
59+
int emojiCount = 32;
5960

60-
float pizza_pos, cat_pos;
61+
PImage[] cats = new PImage[catCount];
62+
PImage[] pizzas = new PImage[pizzaCount];
6163

62-
int img_w = 10;
64+
float[] moveX = new float[emojiCount];
65+
float[] moveY = new float[emojiCount];
6366

64-
int pizza_dir = 1;
65-
int cat_dir = 1;
66-
int num_cats;
67+
float last_speed;
68+
69+
float[] x_dir = new float[emojiCount];
70+
float[] y_dir = new float[emojiCount];
71+
float[] x_speeds = new float[emojiCount];
72+
float[] y_speeds = new float[emojiCount];
6773

6874
// variables for dancingTriangles animation
6975
int x1;
@@ -98,6 +104,23 @@ void setup()
98104
cat_img = loadImage("cat.png");
99105
pizza_img = loadImage("pizza.png");
100106

107+
// adding pizza and cat emojis to their arrays
108+
for (int slice = 0; slice < 15; slice ++) {
109+
pizzas[slice] = pizza_img;
110+
}
111+
for (int claw = 16; claw< catCount; claw++) {
112+
cats[claw] = cat_img;
113+
}
114+
// creating arrays of coordinates and speed for pizzaCat
115+
for (int z = 0; z < emojiCount; z ++) {
116+
x_dir[z] = random(width);
117+
y_dir[z] = random(height);
118+
x_speeds[z] = random(5, 20);
119+
y_speeds[z] = random(5, 20);
120+
moveX[z] = x_speeds[z];
121+
moveY[z] = y_speeds[z];
122+
}
123+
101124
// connecting to socket to communicate with Blinka script
102125
myClient = new Client(this, "127.0.0.1", 12345);
103126

@@ -116,27 +139,31 @@ void draw()
116139
// the encoder pos is stored in index
117140
String[] q = splitTokens(inString);
118141
index = int(q[1]);
142+
println(index);
119143
}
120144
//if the string begins with 'red'
121145
//aka is from the red neoslider
122146
if (inString.startsWith("red")) {
123147
//the red value is stored in red
124148
String[] r = splitTokens(inString);
125149
red = int(r[1]);
150+
println(red);
126151
}
127152
//if the string begins with 'green'
128153
//aka is from the green neoslider
129154
if (inString.startsWith("green")) {
130155
// the green value is stored in green
131156
String[] g = splitTokens(inString);
132157
green = int(g[1]);
158+
println(green);
133159
}
134160
//if the string begins with 'blue'
135161
//aka is from the blue neoslider
136162
if (inString.startsWith("blue")) {
137163
//the blue value is stored in blue
138164
String[] b = splitTokens(inString);
139165
blue = int(b[1]);
166+
println(blue);
140167
}
141168
//if the string begins with flight
142169
//aka is from the VL53L4CD
@@ -147,7 +174,7 @@ void draw()
147174
}
148175
}
149176
//the encoder's position corresponds with which animation plays
150-
if (index == 0) {
177+
if (index == 3) {
151178
circles();
152179
}
153180

@@ -157,7 +184,7 @@ void draw()
157184
if (index == 2) {
158185
dancingTriangles();
159186
}
160-
if (index == 3) {
187+
if (index == 0) {
161188
pizzaCat();
162189
}
163190
}
@@ -208,30 +235,78 @@ void cube() {
208235
}
209236

210237
//the Pizza Cat animation
211-
//pizza and cat face emojis go back and forth across the screen
238+
//pizza and cat face emojis bounce around the screen
212239
//emojis are from the OpenMoji emoji library (https://openmoji.org/)
213240
//the background color is affected by the sliders
214-
//the speed of the cat emojis are affected by the V53L4CD
241+
//the speed of the emojis are affected by the V53L4CD
242+
//green slider affects # of cats
243+
//blue slider affects # of pizzas
215244
void pizzaCat() {
216245
background(red, green, blue);
217-
int num_cats = int(map(flight, 0, 45, 65, 15));
246+
float meow = map(green, 0, 255, 32, 16);
247+
float pie = map(blue, 0, 255, 15, 0);
248+
float speed = map(flight, 0, 45, 0, 25);
249+
250+
for (int e = 16; e < meow; e++) {
251+
if (last_speed != speed) {
252+
moveX[e] = x_speeds[e] + speed;
253+
moveY[e] = y_speeds[e] + speed;
254+
}
255+
else {
256+
moveX[e] = moveX[e];
257+
moveY[e] = moveY[e];
258+
}
259+
x_dir[e] += moveX[e];
260+
if (x_dir[e] < 0 || x_dir[e] > width) {
261+
moveX[e] *= -1;
262+
263+
}
264+
if (x_dir[e] > width) {
265+
x_dir[e] = (width - 2);
266+
}
267+
y_dir[e] += moveY[e];
268+
if(y_dir[e] < 0 || y_dir[e] > height) {
269+
moveY[e] *= -1;
270+
271+
}
272+
if (y_dir[e] > height) {
273+
y_dir[e] = (height - 2);
274+
}
218275

219-
pizza_pos = pizza_pos + ( pizza_speed * pizza_dir );
220-
cat_pos = cat_pos + ( num_cats * cat_dir );
276+
image(cats[e], x_dir[e], y_dir[e]);
277+
278+
}
279+
for (int p = 1; p < pie; p++) {
280+
if (last_speed != speed) {
281+
moveX[p] = x_speeds[p] + speed;
282+
moveY[p] = y_speeds[p] + speed;
283+
}
284+
else {
285+
moveX[p] = moveX[p];
286+
moveY[p] = moveY[p];
287+
}
288+
x_dir[p] += moveX[p];
289+
if (x_dir[p] < 0 || x_dir[p] > width) {
290+
moveX[p] *= -1;
291+
}
292+
if (x_dir[p] > width) {
293+
x_dir[p] = (width - 2);
294+
}
295+
y_dir[p] += moveY[p];
296+
if(y_dir[p] < 0 || y_dir[p] > height) {
297+
moveY[p] *= -1;
298+
}
299+
if (y_dir[p] > height) {
300+
y_dir[p] = (height - 2);
301+
}
221302

222-
if (pizza_pos + img_w > width || pizza_pos < img_w) {
223-
pizza_dir *= -1;
224-
}
225-
if (cat_pos + img_w > height || cat_pos < img_w) {
226-
cat_dir *= -1;
227-
}
228-
229-
for (int p = 0; p < 25; p++) {
230-
image(cat_img, pizza_pos-10, cat_pos*p);
231-
image(pizza_img, pizza_pos*p, cat_pos+10);
232-
}
303+
image(pizzas[p], x_dir[p], y_dir[p]);
304+
}
305+
last_speed = speed;
233306
}
234307

308+
309+
235310
// the dancingTriangles animation
236311
// triangles are randomly generated in the center of the screen
237312
//the background is affected by the sliders

0 commit comments

Comments
 (0)