We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Phidgets Java (Read 2215 times)
Phidgets Java
Dec 6th, 2007, 4:03pm
 
Hello all,
I've searched this forum, websites without succes to find information about Processing and Phidgets. People talk about that it's possible to connect these two. But nobody talks about the HowTo Smiley

So you can use the java Libs from phidgets inside Processing. But how do you do that? I already have the phidgets(IFK) connected to my computer, now I need it to connect it to Processing.

All the information in the forum lead to deadlinks...help!

Gr Jan
Re: Phidgets Java
Reply #1 - Mar 13th, 2009, 5:49am
 
I was looking for same thing - a helloworld for InterfaceKitPhidget. Didn't find it, but based on these examples..

Phidget RFID Reader + Processing: http://thegeekmovement.com/blog/?p=42#more-42

Phidgets accelerometer with processing: http://www.interactiondesign.se/blog/2008/11/phidgets-accelerometer-with-processing/

..I was was able to modify Phidgets' java example, OpenIFKitExample.java and got to work in Processing:  



import com.phidgets.*;
import com.phidgets.event.*;

InterfaceKitPhidget ik;


void setupIKP() {
 try {
   println( Phidget.getLibraryVersion() );
   ik = new InterfaceKitPhidget();

   ik.addAttachListener(new AttachListener() {
     public void attached(AttachEvent ae) {
       println("attachment of " + ae);
     }
   });
   ik.addDetachListener(new DetachListener() {
     public void detached(DetachEvent de) {
       println("detachment of " + de);
     }
   });
   ik.addErrorListener(new ErrorListener() {
     public void error(ErrorEvent ee) {
       println("error event for " + ee);
     }
   });
   ik.addInputChangeListener(new InputChangeListener() {
     public void inputChanged(InputChangeEvent ie) {
       println("input event for " + ie);
     }
   });
   ik.addOutputChangeListener(new OutputChangeListener() {
     public void outputChanged(OutputChangeEvent oe) {
       println("output event for " + oe);
     }
   });
   ik.addSensorChangeListener(new SensorChangeListener() {
     public void sensorChanged(SensorChangeEvent se) {
       println("sensor event for " + se);
     }
   });

   ik.openAny();
   println("waiting for InterfaceKit attachment...");
   ik.waitForAttachment();

   println(ik.getDeviceName());

   delay(500); //Thread.sleep(500);

   if (false) { //retry init
     print("closing...");
     System.out.flush();
     ik.close();
     println(" ok");
     print("reopening...");
     ik.openAny();
     println(" ok");
     ik.waitForAttachment();
   }

   if (ik.getInputCount() > 8) {
     println("input(7,8) = (" +
       ik.getInputState(7) + "," +
       ik.getInputState(8) + ")");
   }

   if (false) {
     print("turn on outputs (slowly)...");
     for (int i = 0; i < ik.getOutputCount() ; i++) {
       ik.setOutputState(i, true);
       try {
         delay(1000); //Thread.sleep(1000);
       } catch (Exception e) {
       }
     }
     println(" ok");
   }

   if (false) {
     for (;;) {
       try {
         delay(1000); //Thread.sleep(1000);
       } catch (Exception e) {
       }
     }
   }

   for (int j = 0; j < 1000 ; j++) {
     for (int i = 0; i < ik.getOutputCount(); i++) {
       ik.setOutputState(i, true);
     }
     for (int i = 0; i < ik.getOutputCount(); i++) {
       ik.setOutputState(i, false);
     }
   }

   if (false) {
     println("toggling outputs like crazy");
     boolean o[] = new boolean[ik.getOutputCount()];
     for (int i = 0; i < ik.getOutputCount(); i++) {
       o[i] = ik.getOutputState(i);
     }
     for (int i = 0; i < 100000; i++) {
       int n = (int)(Math.random() * ik.getOutputCount());
       ik.setOutputState(n, !o[n]);
       println("setOutputState " + n +
         ": " + !o[n]);
       o[n] = !o[n];
       try {
         delay(1); //Thread.sleep(1);
       } catch (Exception e) {
       }
     }
   }

   //println("Outputting events.  Input to stop.");
   //System.in.read();

 } catch (PhidgetException ex) {
   println(ex);
 }
}


void closeIKP(){
 try {
   print("closing...");
   ik.close();
   ik = null;
   println(" ok");
   if (false) {
     println("wait for finalization...");
     System.gc();
   }
 } catch (PhidgetException ex) {
   println(ex);
 }
}


void setup() {
 size(400,100);
 setupIKP();
}


void draw() {
 //background(200);
}


//Should add a button or some other trigger to call closeIKP();
Page Index Toggle Pages: 1