/ Raspberry_Pi_Video_Synth / Raspberry_Pi_Video_Synth.pde
Raspberry_Pi_Video_Synth.pde
  1  // SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  // Processing library for Networking
  6  // make sure that you have the library installed!
  7  import processing.net.*;
  8  
  9  // HTTP client
 10  Client myClient;
 11  
 12  // variables for receiving data from Blinka socket
 13  int index;
 14  String inString;
 15  
 16  // holding the red, green, blue data
 17  String[] r;
 18  String[] g;
 19  String[] b;
 20  int red;
 21  int green;
 22  int blue;
 23  
 24  // holding the VL53L4CD data
 25  String[] f;
 26  int flight;
 27  
 28  // cat and pizza images
 29  //emojis are from the OpenMoji emoji library (https://openmoji.org/)
 30  PImage cat_img; 
 31  PImage pizza_img;
 32  
 33  // colors for Circles animation
 34  color c_red = color(255, 0, 0);
 35  color c_green = color(0, 255, 0);
 36  color c_blue = color(0, 0, 255);
 37  color c_yellow = color(255, 125, 0);
 38  color c_aqua = color(0, 125, 255);
 39  color c_purple = color(255, 0, 255);
 40  
 41  IntList colors; 
 42  
 43  // float for Cube animation
 44  float i = 0.0;
 45  
 46  // variables for Circles animation
 47  int rad = 60;        
 48  float xpos, ypos;       
 49  
 50  float xspeed = 2.8;  
 51  float yspeed = 10;  
 52  
 53  int xdirection = 1;  
 54  int ydirection = 1;  
 55  
 56  // variables for pizzaCat animation
 57  int pizzaCount = 32;
 58  int catCount = 32;
 59  int emojiCount = 32;
 60  
 61  PImage[] cats = new PImage[catCount];
 62  PImage[] pizzas = new PImage[pizzaCount];
 63  
 64  float[] moveX = new float[emojiCount];
 65  float[] moveY = new float[emojiCount];
 66  
 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];
 73  
 74  // variables for dancingTriangles animation
 75  int x1;
 76  int y1;
 77  int x2; 
 78  int y2;
 79  int x3;
 80  int y3;
 81  
 82  void setup() 
 83  {
 84    // setting animations to run fullscreen with P3D
 85    fullScreen(P3D);
 86    // RGB color mode with value range of 0-255
 87    colorMode(RGB, 255);
 88    ellipseMode(RADIUS);
 89    // setting xpos and ypos in center
 90    xpos = width/2;
 91    ypos = height/2;
 92    
 93    // creating array of colors
 94    // this is used for the Circles animation
 95    colors = new IntList();
 96    colors.append(c_red);
 97    colors.append(c_yellow);
 98    colors.append(c_green);
 99    colors.append(c_aqua);
100    colors.append(c_blue);
101    colors.append(c_purple);
102    
103    // loading the cat and pizza images
104    cat_img = loadImage("cat.png");
105    pizza_img = loadImage("pizza.png");
106    
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    
124    // connecting to socket to communicate with Blinka script
125    myClient = new Client(this, "127.0.0.1", 12345);
126  
127  }
128  
129  // void draw() is the loop in Processing
130  void draw() 
131  {
132    //if data is coming in over the socket...
133    if (myClient.available() > 0) {
134      //string data is stored in inString
135      inString = myClient.readString();
136      //if the string begins with 'enc'
137      //aka is a msg from the rotary encoder...
138      if (inString.startsWith("enc")) {
139        // the encoder pos is stored in index
140        String[] q = splitTokens(inString);
141        index = int(q[1]);
142        println(index);
143      }
144      //if the string begins with 'red'
145      //aka is from the red neoslider
146      if (inString.startsWith("red")) {
147        //the red value is stored in red
148        String[] r = splitTokens(inString);
149        red = int(r[1]);
150        println(red);
151      }
152      //if the string begins with 'green'
153      //aka is from the green neoslider
154      if (inString.startsWith("green")) {
155        // the green value is stored in green
156        String[] g = splitTokens(inString);
157        green = int(g[1]);
158        println(green);
159      }
160      //if the string begins with 'blue'
161      //aka is from the blue neoslider
162      if (inString.startsWith("blue")) {
163        //the blue value is stored in blue
164        String[] b = splitTokens(inString);
165        blue = int(b[1]);
166        println(blue);
167      }
168      //if the string begins with flight
169      //aka is from the VL53L4CD
170      if (inString.startsWith("flight")) {
171        //the time of flight value is stored in flight
172        String[] f = splitTokens(inString);
173        flight = int(f[1]);
174      }
175    }
176    //the encoder's position corresponds with which animation plays
177    if (index == 3) {
178      circles();
179      }
180  
181    if (index == 1) {
182      cube();
183      }
184    if (index == 2) {
185      dancingTriangles();
186      }
187    if (index == 0) {
188      pizzaCat();
189      }
190    }
191      
192  //the Circles animation
193  //colorful circles randomly appear in the middle of the screen
194  //background color is affected by the sliders
195  //the circles' size is affected by the VL53L4CD
196  void circles() {
197    background(red, green, blue);
198    strokeWeight(1);
199    
200    ypos = ypos + ( yspeed * ydirection );
201    
202    if (ypos > height-rad || ypos < rad) {
203      ydirection *= +1;
204    }
205  
206    int size = int(map(flight, 0, 45, 300, 25));
207    
208    for (int i = 0; i < 10; i++) {
209      for (int z = 0; z < 6; z++) {
210        fill(colors.get(z));
211        circle(width/2, random(ypos), random(size));
212      }
213    }
214  }
215  
216  //the Cube animation
217  //a 3D cube spins in the center of the screen
218  //background color is affected by the sliders
219  //the speed of the spinning cube is affected by the VL53L4CD
220  void cube() {
221    strokeWeight(5);
222    
223    float speed = map(flight, 0, 45, 10, 0.1);
224    
225    background(red, green, blue);
226    translate(width/2, height/2, 0);
227    
228    i = i + speed;
229    if (i > 180) {
230      i = 0.0;
231    }
232      rotateY(radians(i));
233      noFill();
234      box(500);
235  }
236  
237  //the Pizza Cat animation
238  //pizza and cat face emojis bounce around the screen
239  //emojis are from the OpenMoji emoji library (https://openmoji.org/)
240  //the background color is affected by the sliders
241  //the speed of the emojis are affected by the V53L4CD
242  //green slider affects # of cats
243  //blue slider affects # of pizzas
244  void pizzaCat() { 
245    background(red, green, blue);
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        }
275  
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        }
302  
303      image(pizzas[p], x_dir[p], y_dir[p]);
304      }
305      last_speed = speed;
306  }
307  
308  
309  
310  // the dancingTriangles animation
311  // triangles are randomly generated in the center of the screen
312  //the background is affected by the sliders
313  // the speed of new triangles being added are affected by the V53L4CD
314  void dancingTriangles() {
315    int speed = int(map(flight, 0, 45, 25, 100));
316    
317    background(red, green, blue);
318    strokeWeight(30);
319    
320    for (int w = 800; w < 1000; w ++) {
321      for (int h = 1100; h < 1500; h++) {
322      
323        x1 = int(random(h));
324        y1 = int(random(w));
325      
326        x2 = int(random(h));
327        y2 = int(random(w));
328      
329        x3 = int(random(h));
330        y3 = int(random(w));
331       }
332     }
333    noFill();
334    triangle(x1, y1, x2, y2, x3, y3);
335    delay(speed);
336  }