Friday, 16 April 2010

Using native libraries

Here's an solution you don't need a extension library for, it uses the native libraries to processing. Unfortunately I am not getting my Arduino to operate at the moment so that's thrown a spanner in the works.
www.ardduino.cc/cgi-bin/yabb2/YaBB.pl?action=print,num=1205334952
import processing.video.*;
import processing.serial.*;
Capture video;
Serial arduinoSerPort;

color black = color(0);
color white = color(255);
int threshold = 127; // Set the threshold value
float pixelBrightness; // Declare variable to store a pixel's color
//single matrix=8,8,80 3x3Matrix=24,24,24 8x6Matrix=64,48,10
int numPixels;
int matrixWidth = 8;
int matrixHeight = 8;
int matrixMult = 80;
int screenWidth = matrixWidth * matrixMult;
int screenHeight = matrixHeight * matrixMult;
int cycles;
int bitValue;
int[] matrixBitTable = new int[matrixWidth*matrixHeight];
byte [] serialBytes = new byte[8];
String tempSerialString = "";
boolean dataDump = false;


void setup() {
 size(screenWidth, screenHeight);
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, matrixWidth, matrixHeight, 12);
 //frameRate(6);
 numPixels = matrixWidth * matrixHeight;
 noCursor();
 // Open the port that the board is connected to and use the same speed (9600 bps)
 println(Serial.list());
 arduinoSerPort = new Serial(this, Serial.list()[0], 9600);
 //arduinoSerPort = new Serial(this, "/dev/tty.usbserial-A4004BWB", 9600);

}

//
void draw() {
 if (video.available()) {
   // 1) get input from video camera:
   video.read();
   video.loadPixels();
   loadPixels();
   int matrix_x = 0;
   int matrix_y = 0;

   // 2) Draw video pixels to a scaled grid
   // a single pixel from the video feed gets drawn as a set of pixels
   for (int i = 0; i < numPixels; i++) {
     pixelBrightness = brightness(video.pixels[i]);
     // test to see if the thresholded value is White:
     if (pixelBrightness > threshold) {
       for (int offset_x = 0; offset_x < matrixMult; offset_x++) {
         for (int offset_y = 0; offset_y < matrixMult; offset_y++) {
           int tempPixel = (matrix_x * matrixMult) + ((matrix_y * matrixMult) * screenWidth);
           pixels[tempPixel+offset_x+(screenWidth * offset_y)] = white;
         }
       }
       bitValue = 1;
     }
     // the thresholded value is black:
     else {
       for (int offset_x = 0; offset_x < matrixMult; offset_x++) {
         for (int offset_y = 0; offset_y < matrixMult; offset_y++) {
           int tempPixel = (matrix_x * matrixMult) + ((matrix_y * matrixMult) * screenWidth);
           pixels[tempPixel+offset_x+(screenWidth * offset_y)] = black;
         }
       }
       bitValue = 0;
     }
     // create an array to hold bitvalues for Arduino LED - this is using an 8x8 displayonly
     int boolLoc = matrix_x + (matrix_y * 8);
     matrixBitTable[boolLoc] = bitValue;
     // update a model of the video grid as we scan through the pixels
     matrix_x++;
     if(matrix_x == matrixWidth){
       matrix_x = 0;
       matrix_y++;
     }
   }

   // 4) update the image on the screen:
   updatePixels();
   cycles++;

   // 5) send bytes to Arduino
   // send some strangely symetrical bytes as a header packet:
   for (int x = 0; x < 8; x++) {
     arduinoSerPort.write(85);
   }
   // put bitvalues from the camera into serial bytes  - this is using an 8x8 display only
   for (int x = 0; x < 8; x++) {
     tempSerialString = "";
     // build a bit sequence as a string for conversion
     for (int y = 0; y < 8; y++) {
       int tablePointer = y+(8*x);
       tempSerialString += str(matrixBitTable[tablePointer]);
     }
     // convert that binary string into a byte value, and send it!:
     int tempInt = unbinary(tempSerialString);
     print(tempSerialString+", ");
     byte tempByte = byte(tempInt);
     serialBytes[x] = tempByte;
     arduinoSerPort.write(serialBytes[x]);
     //print(serialBytes[x]+", ");
   }
   println("");
 
   //delay(10000);
 }
}

No comments:

Post a Comment