Laser Projector Processing code

Code for showing images loaded from TIF files as an animated sequence.

import processing.serial.*;
Serial myPort;
int frametime=2;
int[] linebyte = new int[17];
int pixelholder;
int linepixels;
int readit;
int lineparts;
int pixelposition;
PImage[] movieframe =  new PImage[2500];
int startpos=0;
int saveslot;
int lastsave=1314;
int filestepsize=4;
int pixelvalue = -16777216; //black pixel
String path = "/hands/";
String filename = "screen";

void setup()
{
    for (saveslot=startpos; saveslot<=(lastsave-filestepsize); saveslot=saveslot+filestepsize)
    {
       movieframe[saveslot] = loadImage(path +filename + saveslot + ".TIF"); 
    }
  
  //background(0);
  frameRate(1500);
  size(800, 400, P2D);
  myPort = new Serial(this, "COM4", 57600);   
  myPort.write(42);
  delay(1100);
  myPort.clear(); 
  saveslot=startpos;
}

void draw()
{
  println(saveslot);
  movieframe[saveslot].loadPixels();
 image(movieframe[saveslot], 0, 0, 320, 160);

 for (int lineparts=0; lineparts<16; lineparts++)
  {
linebyte[lineparts]=0;
  }
  
  for (int lineparts=0; lineparts<16; lineparts=lineparts+2)
  {
      for (int linepixels=0; linepixels<16; linepixels++)
      {
          pixelposition=(linepixels+((lineparts/2)*16));
          pixelholder = movieframe[saveslot].pixels[pixelposition];
          //println(pixelholder);
        if (linepixels<8 & pixelholder!=pixelvalue) //first half of a line, a pixel is counted as long as it is NOT black.
        {
            linebyte[lineparts] = linebyte[lineparts] + (1 << linepixels);
        }
        if (linepixels>=8 & pixelholder!=pixelvalue)//second half of a line
        {         
            linebyte[lineparts+1] = linebyte[lineparts+1] + (1 << (linepixels-8));          
        }
      }
  }
  if (myPort.available()>0)
  { 
    delay(3);
    myPort.clear();
    
    myPort.write(41);
    for (int lineparts=0; lineparts<16; lineparts++)
    {
      myPort.write(linebyte[lineparts]);
     linebyte[lineparts]=0;
    }
    myPort.write(42);
    
    saveslot=saveslot+filestepsize;      
  }
  
   if (saveslot>=(lastsave-filestepsize))
    {
      saveslot=startpos;
    }
}

The code below is used to directly display frames from a webcam on the laser projector

import processing.video.*;
import processing.serial.*;
Serial myPort;
int ltrline;
int[] templtrpixels = new int[8];
int[] ltrpixels = new int[16];
char ltrchar;
color black = color(0);
color white = color(255);
int numPixels;
Capture video;
int filenumber=0;

void setup() {
  frameRate(20);
  size(16, 8);
  video = new Capture(this, 16, 8, 24);
  numPixels=128;
  myPort = new Serial(this, "COM4", 57600);
  myPort.write(42);
  delay(1100);
  myPort.clear();
}

void draw() {
  save("screen" + filenumber + ".TIF");
  filenumber++;
  if (video.available()) {
    video.read();
    video.loadPixels();
    int threshold = 160;
    float pixelBrightness;  
    loadPixels();
    for (int i = 0; i < numPixels; i++) {
      pixelBrightness = brightness(video.pixels[i]);
      if (pixelBrightness > threshold) { 
        pixels[i] = white; 
      } 
      else {
        pixels[i] = black; 
      }
    }
    updatePixels();    
    for (int linecounter=0; linecounter<7; linecounter++)
    {      
      for (int pixelcounter=0; pixelcounter<16; pixelcounter++)

      {
        int pixelnumber = pixelcounter + (16*(linecounter));

        if (pixels[pixelnumber] == white)
        {
          templtrpixels[linecounter] = templtrpixels[linecounter] + (1 << (15-pixelcounter));
        }
      }
    }
    for (int x=0; x<16; x=x+2)
    {
      ltrpixels[x+1] = templtrpixels[x/2] >> 8;
      ltrpixels[x] = templtrpixels[x/2] & 0xFF;
    }

    if (myPort.available() >0) 
    {
      delay(3);
      myPort.clear();
      myPort.write(41);
      for (ltrline=0; ltrline<16; ltrline++)
      {
        myPort.write(ltrpixels[ltrline]);
      }
       myPort.write(42);
    }
    for (ltrline=0; ltrline<8; ltrline++)
    {
      templtrpixels[ltrline] = 0;
    }
  }
}