/**
 * (./) OpenKinect.java, v0.1 - 20/05/2011
 * (by) Douglas Edric Stanley & Cousot Stéphane
 * (cc) some right reserved
 *
 * Part of the Processing/Java OpenCV Libraries project, for the Atelier Hypermedia, art 
 * school of Aix-en-Provence, and for the Processing and Java community of course.
 *
 *
 * THIS LIBRARY (AND ALSO THIS FILE) IS RELEASED UNDER A CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
 * ‹ http://creativecommons.org/licenses/by/3.0/ ›
 */
 


// package name
package hypermedia.video;


// external librairies
import processing.core.*;
import java.awt.*;




/**
 * Kinect wrapper for OpenCV
 */
public class OpenKinect extends OpenCV
{



	/**
	 * Processing library version
	 */
	public final static String VERSION	= "1.0b";
	
	/**
	 * C++ libraries version
	 */
	public static String FREENECT_VERSION;
	
	
	

	
	public final static int FPS			= 20; // milliseconds
	public final static int WIDTH		= 640;
	public final static int HEIGHT		= 480;
	
	
	/**
	 * LED state
	 */
	public final static int LED_OFF					= 0; // turn LED off
	public final static int LED_GREEN				= 1; // turn LED to Green
	public final static int LED_RED					= 2; // turn LED to Red
	public final static int LED_YELLOW				= 3; // turn LED to Yellow
	public final static int LED_BLINK_GREEN			= 4; // make LED blink Green
	public final static int LED_BLINK_RED_YELLOW	= 6; // make LED blink Red/Yellow
	


	/**
	 * Type of depth image format
	 */
	public final static int DEPTH_RGB			 = 0x1907;//32992;
	public final static int DEPTH_GRAY			 = 0x1909;//6409; 
	
	
	

	
	
	
	static
	{
		FREENECT_VERSION = version();
	}
	
	
	

	/**
	 * Create a new OpenKinect object.
	 *
	 * @see #OpenCV( PApplet )
	 * @invisible
	 */
	public OpenKinect() { this(null); }
	
	
	
	/**
	 * Create a new OpenKinect object.
	 * @param parent	the Processing PApplet object (typically use "this")
	 */
	public OpenKinect( PApplet parent ) {
		super(parent,false);
		
		width  = WIDTH;
		height = HEIGHT;
		
		// create related C++ object
		instantiate();
	}
	
	
	
	
	/**
	 * Returns C++ Freenect library version.
	 *
	 * @return String
	 */
	private static native String version();
	
	/** 
	 * Forget all OpenKinect resources and free memory.
	 * This method is automatically called by Processing when the PApplet shuts down.
	 *
	 * @see #stop()
	 * @invisible
	 */
	public native void dispose();
	
	/**
	 * Stop OpenKinect process.
	 *
	 * @usage	Application
	 */
	public void close() { dispose(); }
	
	/**
	 * Ceate an initilize a new C++ object
	 */
	private native void instantiate();
	
	
	
	
	public void setup() { // <----- RGB source and RGB/GRAY buffer
		setup( true );
	}
	public void setup( boolean rgb ) { //<----  RGB or IR source and RGB/GRAY buffer
		setup( rgb, false );
	}
	public native void setup( boolean rgb, boolean bitmap ); // <--- source RGB or IR, buffer RGB,GRAY or bitmap mode
	
	
	
	public boolean capture() {
		return open( 0 );
	}
	public boolean capture( int index ) {
		return open( index );
	}
	public boolean capture( int index, int framerate ) {
		return open( index, framerate );
	}
	
	public boolean open() {
		return open( 0 );
	}
	public boolean open( int index ) {
		return open( index, FPS );
	}
	
	
	
	public static native int list();
	public native boolean open( int index, int framerate );
	public native boolean connected();
	
	public native void update();
	public native void threshold( int near, int far );
	public native void led( int state );
	public native void tiltto( float degree );
	public native void tiltby( int by );
	public native void level();
	
	
	
	public native void start();
	public native void stop();
	
	
	public native float angle();
	public native float[] data();
	
	
	public native short[] depth();
	
	
	public static float depthToMeters( int raw ) {
		return depthToMeters( (short)raw );
	}
	public static native float depthToMeters( short raw );
	
	/**
	 * Retrieve current (or specified) image data.
	 *
	 * @description Retrieve current (or specified) image data.
	 * @param type	the type of the image: SOURCE, BUFFER, MEMORY
	 * @return int[]
	 */
	public native int[] pixels( int type );
	
}

