implement simulator in processing
This commit is contained in:
parent
21733b6c6c
commit
61233ae6c9
|
@ -0,0 +1,109 @@
|
|||
import hypermedia.net.*;
|
||||
|
||||
int pixelSize = 4;
|
||||
int bitPerPixel = 2;
|
||||
int width=160;
|
||||
int height=144;
|
||||
int windowWidth=width*pixelSize;
|
||||
int windowHeight=height*pixelSize;
|
||||
|
||||
UDP udp;
|
||||
|
||||
byte[] emptyByteArray = new byte[0];
|
||||
byte receivedData[]=emptyByteArray;
|
||||
byte imagebuffer[][]=new byte[width][height];
|
||||
|
||||
PImage led[]=new PImage[(int)pow(2,bitPerPixel)];
|
||||
|
||||
void settings() {
|
||||
size(width*pixelSize, height*pixelSize,P2D);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
//Setup Graphics
|
||||
|
||||
frameRate(30);
|
||||
noSmooth();
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
//UDP
|
||||
udp = new UDP( this, 2323 );
|
||||
//udp.log( true ); // <-- printout the connection activit
|
||||
udp.listen( true );
|
||||
|
||||
//Precompute LED Images
|
||||
for (byte b=0;b<led.length;b++){
|
||||
led[b] = createImage(pixelSize, pixelSize, RGB);
|
||||
led[b].loadPixels();
|
||||
for (int px = 0; px < pixelSize; px++) {
|
||||
for (int py = 0; py < pixelSize; py++) {
|
||||
float distanceFromCenter=sqrt( pow(px-pixelSize/2,2) + pow(py-pixelSize/2,2) ) / sqrt(pow(pixelSize/2,2) + pow(pixelSize/2,2));
|
||||
int colorR=255/3*b;
|
||||
int colorG=127/3*b;
|
||||
int colorB=0;
|
||||
float brightnessMultiplier=map(distanceFromCenter, 0.4, 1/sqrt(2), 1.0, 0.0);
|
||||
if(brightnessMultiplier<0){
|
||||
brightnessMultiplier=0;
|
||||
}else if(brightnessMultiplier>1){
|
||||
brightnessMultiplier=1;
|
||||
}
|
||||
|
||||
colorR*=brightnessMultiplier;
|
||||
colorG*=brightnessMultiplier;
|
||||
colorB*=brightnessMultiplier;
|
||||
|
||||
led[b].pixels[py*pixelSize+px] = color(colorR,colorG,colorB);
|
||||
}
|
||||
}
|
||||
led[b].updatePixels();
|
||||
}
|
||||
}
|
||||
|
||||
//draw event handler
|
||||
void draw() {
|
||||
//map byte data to imagebuffer
|
||||
if (receivedData.length > 0){ //new data?
|
||||
byte[] cachedData=receivedData; //cache data to free array for new data
|
||||
receivedData = emptyByteArray;
|
||||
|
||||
for (int i=0;i<cachedData.length;i++){
|
||||
for (int shift=0;shift<(8/bitPerPixel);shift++){ //bitshifts because a byte can contain multiple pixels
|
||||
int pixelnumber=i*(8/bitPerPixel)+shift; //absolute pixelnumber
|
||||
int x=pixelnumber%width;
|
||||
int y=pixelnumber/width;
|
||||
int value= (cachedData[i]&( 3 <<(shift*bitPerPixel))) >> (shift*bitPerPixel); //mask relevant bits for current pixel
|
||||
|
||||
imagebuffer[x][y]=(byte)value; //write pixel value to image buffer
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
clear(); //clear screen
|
||||
|
||||
//int timeA=millis();
|
||||
|
||||
//Draw leds
|
||||
for (int x = 0; x < imagebuffer.length; x++){
|
||||
for (int y = 0; y < imagebuffer[x].length; y++){
|
||||
byte pixel=imagebuffer[x][y];
|
||||
|
||||
//fill(255/3*pixel,127/3*pixel,0); //set pixel color
|
||||
//ellipse(x*pixelSize+pixelSize/2, y*pixelSize+pixelSize/2, pixelSize, pixelSize); //Very slow
|
||||
//rect(x*pixelSize, y*pixelSize, pixelSize, pixelSize); //works fast enough
|
||||
|
||||
image(led[pixel], x*pixelSize, y*pixelSize); //draw precomputed led image, as fast as rect but looks nicer
|
||||
}
|
||||
}
|
||||
|
||||
//int timeB=millis()-timeA;
|
||||
//println("Time="+(timeB));
|
||||
|
||||
|
||||
}
|
||||
|
||||
//udp receive event handler
|
||||
void receive( byte[] data, String ip, int port ) {
|
||||
receivedData=data; //received data to buffer
|
||||
}
|
Loading…
Reference in New Issue