Created
July 5, 2013 21:43
-
-
Save thomo/5937441 to your computer and use it in GitHub Desktop.
Processing example to generate led matrix content and send data via serial line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
// 2D Array of objects | |
DrawCell[][] grid; | |
Cell[] buffer; | |
Serial myPort; | |
// Number of columns and rows in the grid | |
int cols = 16; | |
int rows = 16; | |
int cellsize = 20; | |
int lastgreen = 0; | |
int lastred = 0; | |
int offset = 0; | |
int lowbyte(int word) { | |
return word % 256; | |
} | |
int highbyte(int word) { | |
return word / 256; | |
} | |
void setup() { | |
size(cols*20,rows*20); | |
grid = new DrawCell[rows][cols]; | |
buffer = new Cell[cols]; | |
for (int j = 0; j < cols; j++) { | |
buffer[j] = new Cell(j); | |
for (int i = 0; i < rows; i++) { | |
grid[i][j] = new DrawCell(cellsize); | |
} | |
} | |
println(Serial.list()); | |
myPort = new Serial(this, Serial.list()[0], 14400); | |
frameRate(10); | |
} | |
void draw() { | |
int bitmask; | |
int reddata; | |
int greendata; | |
background(0); | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
grid[(offset + i) % rows][j].display(j*cellsize, i*cellsize); | |
} | |
} | |
calc(); | |
if (newline()) { | |
bitmask = 1; | |
reddata = 0; | |
greendata = 0; | |
for(int j = 0; j < cols; ++j) { | |
if (buffer[j].red() > 0) { | |
reddata |= bitmask; | |
} | |
if (buffer[j].green() > 0) { | |
greendata |= bitmask; | |
} | |
grid[offset % rows][j].setRedGreen(buffer[j].red(), buffer[j].green()); | |
bitmask *= 2; | |
} | |
myPort.write(lowbyte(greendata)); myPort.write(highbyte(greendata)); myPort.write(lowbyte(reddata)); myPort.write(highbyte(reddata)); | |
++offset; | |
} | |
} | |
void calc() { | |
for(int j = 0; j < cols; ++j) { | |
buffer[j].oscillate(); | |
} | |
} | |
boolean newline() { | |
return true; | |
} | |
class DrawCell { | |
float w,h; // width and height | |
int red,green; | |
DrawCell(float size) { | |
w = size; | |
h = size; | |
red = 0; | |
green = 0; | |
} | |
void setRedGreen(int r, int g) { | |
red = r; | |
green = g; | |
} | |
void display(float x, float y) { | |
stroke(255); | |
fill(red, green, 0); | |
rect(x,y,w,h); | |
} | |
} | |
// A Cell object | |
class Cell { | |
float angle; // angle for oscillating brightness | |
// Cell Constructor | |
Cell(float tempAngle) { | |
angle = tempAngle; | |
} | |
// Oscillation means increase angle | |
void oscillate() { | |
angle += 0.2; | |
} | |
// Red Color calculated using sine wave | |
int red() { | |
return (127*sin(angle) > 0) ? 255 : 0; | |
} | |
// Green Color calculated using cos wave | |
int green() { | |
return (127*cos(angle) > 0) ? 255 : 0; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment