Thursday, December 30, 2010

Fun with Processing + Future project (IR thermal scanner)

I anticipation for my next project I've been looking at different ways to process and represent feedback from a digital IR sensor. I found this very nice tool called Processing. The tool allows me to write a piece of code that ca take multiple inputs (such as serial) and display it in anyway I want. As a sample test I decided to make a binary clock. A lot of this will be translated into my project such as the live updates and ellipse drawings. It's too bad I can't attach a java app to this blog so here's the code:

Processing page (http://www.processing.org/)

void setup() {
size(300,160);
frameRate(1);
}

void draw() {
//hh:mm:ss
//24:69:69
//23:34:34
background(50,50,50);
String s = ""+second();  // Values from 0 - 59
String m = ""+minute();  // Values from 0 - 59
String h = ""+hour();    // Values from 0 - 23
String time = ""+h+m+s;

println (time);

boolean [][] hs = digit(h);
boolean [][] ms = digit(m);
boolean [][] ss = digit(s);
digit_draw(2,3,33,0,hs);
digit_draw(3,4,33,3,ms);
digit_draw(3,4,33,6,ss);
}

void digit_draw(int d0, int d1, int sz, int xoffset, boolean [][]vs) {  
for(int i=0; i < d0; i++) {
        if(vs[0][i]) fill(255,0,0);
        else fill(0,0,0);
        ellipse(sz*xoffset+sz, sz*i+sz, sz, sz);
    }
    for(int i=0; i < d1; i++) {
        if(vs[1][i]) fill(255,0,0);
        else fill(0,0,0);
        ellipse(sz*(xoffset+1)+sz, sz*i+sz, sz, sz);
    }
}

boolean [][] digit(String v) {
  int h0,h1;
  h1=-1;
  h0=v.charAt(0)-48;
  if(v.length() > 1) h1=v.charAt(1)-48;
int ht=h1;
if(h1==-1) { h1=h0; h0=0; };
int sz=4;
boolean [][] d = new boolean[sz][sz];
String b0 = binary(h0);
String b1 = binary(h1);
//println (h0 + "" + h1 + " " + b0 + " " + b1);
for(int i=0; i < sz; i++) {
d[0][i] = d[1][i] = false;
}

for(int i=0; i < b0.length(); i++) {
if(b0.charAt(b0.length()-1-i) == '1') d[0][i]=true;
else d[0][i]=false;
}
for(int i=0; i < b1.length(); i++) {
if(b1.charAt(b1.length()-1-i) == '1') d[1][i]=true;
else d[1][i]=false;
}
return d;
}

No comments:

Post a Comment