Skip to content

Commit

Permalink
Clean up example 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas committed May 7, 2019
1 parent ef52a8a commit c4d695b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 43 deletions.
129 changes: 89 additions & 40 deletions p3_serial_rgb_preview/p3_serial_rgb_preview.pde
Original file line number Diff line number Diff line change
@@ -1,64 +1,113 @@
import processing.serial.*;
import java.lang.reflect.*;
/**
* Example 3.
* This sketch sends all the pixels rendered to a texture to the serial port.
* A preview is rendere also to the main canvas.
* A helper function to scan all the serial ports for a configured controller is provided.
*
* Note
* The serial object is disabled for preview purposes.
* Make sure to initialize it properly
*/

import processing.serial.*;

int MATRIX_WIDTH = 64;
int MATRIX_HEIGHT = 32;
final int NUM_TILES_X = 2; // The number of tiles, make sure that the NUM_TILES const
final int NUM_TILES_Y = 2; // has the correct value in the slave program
final int MATRIX_WIDTH = 64;
final int MATRIX_HEIGHT = 32;
final int NUM_CHANNELS = 3;

Serial serial;
byte[]buffer;
PGraphics tex;
byte[]buffer;

void setup() {
size(640, 480);
noSmooth();
size(810, 600, P3D);

buffer = new byte[MATRIX_WIDTH * MATRIX_HEIGHT * 3];
tex = createGraphics(MATRIX_WIDTH, MATRIX_HEIGHT);
int buffer_length = NUM_TILES_X * MATRIX_WIDTH * NUM_TILES_Y * MATRIX_HEIGHT * NUM_CHANNELS;
buffer = new byte[buffer_length];
tex = createGraphics(NUM_TILES_X * MATRIX_WIDTH, NUM_TILES_Y * MATRIX_HEIGHT, P3D);

// init serial:
// serial = new Serial(this, "COM3");
// serial = scanSerial();
// Init serial
// serial = scanSerial(); // Mac
// serial = new Serial(this, "COM3"); // Windows
}

void draw() {
background(160);

// Render something to a render target
tex.beginDraw();
tex.background(0);
tex.ortho();
tex.stroke(255);
for (int i=0; i<200; i++) {
tex.point(random(tex.width), random(tex.height));
}
tex.noStroke();
tex.fill(0);
tex.translate(tex.width/2, tex.height/2);
float b = min(tex.width, tex.height) * 0.5;
for (int i=-20; i<=20; i++) {
tex.fill((i+100) % 2 * 255);
tex.pushMatrix();
tex.translate(i * b * 0.2, 0);
float a = frameCount + i * 5;
tex.rotateX(a * 0.011);
tex.rotateY(a * 0.012);
tex.rotateZ(a * 0.013);
tex.box(b);
tex.popMatrix();
}
tex.endDraw();

// preview:
int s = 9;
int ox = (width - s * MATRIX_WIDTH) / 2;
int oy = 80;
image(tex, ox , 20);
tex.loadPixels();

// Write to the serial port (if open)
if (serial != null) {
int idx = 0;
for (int j=0; j<NUM_TILES_Y; j++) {
for (int i=0; i<NUM_TILES_X; i++) {
PImage tmp = tex.get(i * MATRIX_WIDTH, j * MATRIX_HEIGHT, MATRIX_WIDTH, MATRIX_HEIGHT);
for (color c : tmp.pixels) {
buffer[idx++] = (byte)(c >> 16 & 0xFF);
buffer[idx++] = (byte)(c >> 8 & 0xFF);
buffer[idx++] = (byte)(c & 0xFF);
}
}
}
serial.write('*'); // The 'data' command
serial.write(buffer); // ...and the pixel values
}

// Preview
background(80);

// Offset and size of the preview
int preview_size = 6;
int ox = 20;
int oy = 40;

// Grid background
fill(0);
noStroke();
rect(ox, oy, tex.width * preview_size, tex.height * preview_size);

// LEDs
for (int j=0; j<tex.height; j++) {
for (int i=0; i<tex.width; i++) {
color col = tex.pixels[i + j * tex.width];
fill(col);
rect(i * s + ox, j * s + oy, s-1, s-1);
for (int i=0; i<tex.width; i++) {
int idx = i + j * tex.width;
color c = tex.pixels[idx];
fill(c);
int x = ox + i * preview_size;
int y = oy + j * preview_size;
rect(x, y, preview_size-1, preview_size-1);
}
}

// serial write
if (serial != null) {
tex.loadPixels();

int b = 0; // brightness adjust
int idx = 0;
color c;
for (int i=0; i<tex.pixels.length; i++) {
c = tex.pixels[i];
buffer[idx++] = (byte) max((c >> 16 & 0xFF) - b, 0);
buffer[idx++] = (byte) max((c >> 8 & 0xFF) - b, 0);
buffer[idx++] = (byte) max((c & 0xFF) - b, 0);
// Matrix outline
noFill();
stroke(255);
for (int j=0; j<NUM_TILES_Y; j++) {
for (int i=0; i<NUM_TILES_X; i++) {
int x = i * MATRIX_WIDTH * preview_size + ox;
int y = j * MATRIX_HEIGHT * preview_size + oy;
rect(x, y, MATRIX_WIDTH * preview_size, MATRIX_HEIGHT * preview_size);
}
serial.write('*');
serial.write(buffer);
}
}
10 changes: 7 additions & 3 deletions p4_serial_rgb_multi_anims/p4_serial_rgb_multi_anims.pde
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PGraphics tex;
byte[]buffer;

Anim anim;
int current_anim_id = 0;
int current_anim_id;
ArrayList <Class> anim_classes;

void setup() {
Expand All @@ -48,10 +48,14 @@ void setup() {
anim_classes.add(c);
}
}

// Create the first instance
current_anim_id = 0;
anim = createInstance(current_anim_id);

// Init serial
// serial = scanSerial();
anim = createInstance(current_anim_id);
// serial = scanSerial(); // Mac
// serial = new Serial(this, "COM3"); // Windows
}

void draw() {
Expand Down

0 comments on commit c4d695b

Please sign in to comment.