Freenect.java
Go to the documentation of this file.
1 
25 package org.openkinect.freenect;
26 
27 import com.sun.jna.*;
28 import com.sun.jna.ptr.PointerByReference;
29 
30 import java.io.IOException;
31 import java.nio.ByteOrder;
32 import java.nio.ByteBuffer;
33 import java.nio.DoubleBuffer;
34 
35 public class Freenect implements Library {
36  static {
37  try {
38  NativeLibrary.addSearchPath("freenect", "/usr/local/lib");
39  NativeLibrary instance = NativeLibrary.getInstance("freenect");
40  System.err.println("Loaded " + instance.getName() + " from " + instance.getFile().getCanonicalPath());
41  Native.register(instance);
42  } catch (IOException e) {
43  throw new AssertionError(e);
44  }
45  }
46 
47  protected Freenect () {
48  }
49 
50  public static Context createContext () {
51  PointerByReference ctxPtr = new PointerByReference();
52  int rval = freenect_init(ctxPtr, Pointer.NULL);
53  if (rval == 0) {
54  NativeContext ctx = new NativeContext(ctxPtr.getValue());
55  ctx.startEventThread();
56  return ctx;
57  }
58  throw new IllegalStateException("init() returned " + rval);
59  }
60 
61  protected static class NativeContext extends PointerType implements Context {
63 
64  public NativeContext () {
65  }
66 
69  @Override
70  public void callback (NativeDevice dev, int level, String msg) {
71  logHandler.onMessage(dev, LogLevel.fromInt(level), msg);
72  }
73  };
74 
75  protected NativeContext (Pointer ptr) {
76  super(ptr);
77  }
78 
79  @Override
81  this.logHandler = handler;
82  if (logHandler == null) {
83  freenect_set_log_callback(this, null);
84  } else {
85  freenect_set_log_callback(this, logCallback);
86  }
87  }
88 
89  @Override
90  public void setLogLevel (LogLevel level) {
91  freenect_set_log_level(this, level.intValue());
92  }
93 
94  @Override
95  public int numDevices () {
96  return freenect_num_devices(this);
97  }
98 
99  @Override
100  public Device openDevice (int index) {
101  PointerByReference devicePtr = new PointerByReference();
102  int rval = freenect_open_device(this, devicePtr, index);
103  if (rval != 0) {
104  throw new IllegalStateException("freenect_open_device() returned " + rval);
105  }
106  return new NativeDevice(devicePtr.getValue(), index);
107  }
108 
109  protected void processEvents () {
111  }
112 
113  protected void startEventThread () {
114  if (eventThread == null || !eventThread.isAlive()) {
115  eventThread = new EventThread(this);
116  eventThread.start();
117  }
118  }
119 
120  protected void stopEventThread () {
121  if (eventThread != null) {
122  eventThread.kill();
123  eventThread = null;
124  }
125  }
126 
127  @Override
128  public void shutdown () {
129  stopEventThread();
130  freenect_shutdown(this);
131  }
132  }
133 
134  protected static class NativeDevice extends PointerType implements Device {
136  private ByteBuffer videoBuffer;
138 
140  private ByteBuffer depthBuffer;
142 
143  private final DoubleBuffer accelX = DoubleBuffer.allocate(1);
144  private final DoubleBuffer accelY = DoubleBuffer.allocate(1);
145  private final DoubleBuffer accelZ = DoubleBuffer.allocate(1);
146 
147  private int index;
148 
151  private double tiltAngle;
152  private double[] accel;
153 
154  private final NativeVideoCallback videoCallback = new NativeVideoCallback() {
155  @Override
156  public void callback (Pointer dev, Pointer depth, int timestamp) {
157  videoHandler.onFrameReceived(videoMode, videoBuffer, timestamp);
158  }
159  };
160 
161  private final NativeDepthCallback depthCallback = new NativeDepthCallback() {
162  @Override
163  public void callback (Pointer dev, Pointer depth, int timestamp) {
164  depthHandler.onFrameReceived(depthMode, depthBuffer, timestamp);
165  }
166  };
167 
168  public NativeDevice () {
169  }
170 
171  protected NativeDevice (Pointer ptr, int index) {
172  super(ptr);
173  this.index = index;
174  this.rawTiltState = freenect_get_tilt_state(this);
175  this.accel = new double[3];
176  refreshTiltState();
177  //setVideoFormat(VideoFormat.RGB);
178  //setDepthFormat(DepthFormat.D10BIT);
179  }
180 
181  protected void setDeviceIndex (int index) {
182  this.index = index;
183  }
184 
185  @Override
186  public int getDeviceIndex () {
187  return index;
188  }
189 
190  public void close () {
191  freenect_close_device(this);
192  }
193 
194  @Override
195  public void setDepthFormat (DepthFormat fmt) {
196  setDepthFormat(fmt, Resolution.MEDIUM);
197  }
198 
199  @Override
200  public void setVideoFormat (VideoFormat fmt) {
201  setVideoFormat(fmt, Resolution.MEDIUM);
202  }
203 
204  @Override
205  public void setDepthFormat (DepthFormat fmt, Resolution res) {
207  if (mode.isValid()) {
208  freenect_set_depth_mode(this, mode);
209  depthBuffer = ByteBuffer.allocateDirect(mode.getFrameSize());
210  depthBuffer.order(ByteOrder.nativeOrder());
211  freenect_set_depth_buffer(this, depthBuffer);
212  this.depthMode = mode;
213  }
214  }
215 
216  @Override
217  public void setVideoFormat (VideoFormat fmt, Resolution res) {
219  if (mode.isValid()) {
220  freenect_set_video_mode(this, mode);
221  videoBuffer = ByteBuffer.allocateDirect(mode.getFrameSize());
222  videoBuffer.order(ByteOrder.nativeOrder());
223  freenect_set_video_buffer(this, videoBuffer);
224  this.videoMode = mode;
225  }
226  }
227 
228  @Override
230  return depthMode;
231  }
232 
233  @Override
235  return videoMode;
236  }
237 
238  @Override
239  public int setLed (LedStatus status) {
240  return freenect_set_led(this, status.intValue());
241  }
242 
243  @Override
244  public void refreshTiltState () {
246  this.rawTiltState = freenect_get_tilt_state(this);
247  this.tiltAngle = freenect_get_tilt_degs(rawTiltState);
248  this.tiltStatus = TiltStatus.fromInt(freenect_get_tilt_status(rawTiltState));
249  freenect_get_mks_accel(rawTiltState, accelX, accelY, accelZ);
250  this.accel[0] = accelX.get(0);
251  this.accel[1] = accelY.get(0);
252  this.accel[2] = accelZ.get(0);
253  }
254 
255  @Override
256  public double getTiltAngle () {
257  return tiltAngle;
258  }
259 
260  @Override
261  public int setTiltAngle (double angle) {
262  return freenect_set_tilt_degs(this, angle);
263  }
264 
265  @Override
267  return tiltStatus;
268  }
269 
270  @Override
271  public double[] getAccel () {
272  return new double[]{accelX.get(0), accelY.get(0), accelZ.get(0)};
273  }
274 
275  @Override
277  this.videoHandler = handler;
278  freenect_set_video_callback(this, videoCallback);
279  return freenect_start_video(this);
280  }
281 
282  @Override
283  public int stopVideo () {
284  int rval = freenect_stop_video(this);
285  freenect_set_video_callback(this, null);
286  this.videoHandler = null;
287  return rval;
288  }
289 
290  @Override
292  this.depthHandler = handler;
293  freenect_set_depth_callback(this, depthCallback);
294  return freenect_start_depth(this);
295  }
296 
297  @Override
298  public int stopDepth () {
299  int rval = freenect_stop_depth(this);
300  freenect_set_depth_callback(this, null);
301  this.depthHandler = null;
302  return rval;
303  }
304  }
305 
306  private static class EventThread extends Thread {
307  private final NativeContext ctx;
308  private volatile boolean alive = true;
309 
310  public EventThread (NativeContext ctx) {
311  this.ctx = ctx;
312  setDaemon(true);
313  setName("FreenectEventThread");
314  }
315 
316  public void kill () {
317  this.alive = false;
318  }
319 
320  @Override
321  public void run () {
322  while (alive) {
324  }
325  }
326  }
327 
328  ;
329 
330  protected static class TiltState extends PointerType {
331  public TiltState () {
332  }
333  }
334 
335  // function prototypes from libfreenect.h
336  // These must match the names used in the library!
337 
338  public interface NativeLogCallback extends Callback {
339  void callback (NativeDevice dev, int logLevel, String msg);
340  }
341 
342  private interface NativeDepthCallback extends Callback {
343  void callback (Pointer dev, Pointer depth, int timestamp);
344  }
345 
346  ;
347 
348  private interface NativeVideoCallback extends Callback {
349  void callback (Pointer dev, Pointer frame, int timestamp);
350  }
351 
352  ;
353 
354  private static native int freenect_init (PointerByReference ctx, Pointer usb_ctx);
355 
356  private static native int freenect_shutdown (NativeContext ctx);
357 
358  private static native void freenect_set_log_level (NativeContext ctx, int level);
359 
360  private static native void freenect_set_log_callback (NativeContext ctx, NativeLogCallback cb);
361 
362  private static native int freenect_process_events (NativeContext ctx);
363 
364  private static native int freenect_num_devices (NativeContext ctx);
365 
366  private static native int freenect_open_device (NativeContext ctx, PointerByReference dev, int index);
367 
368  private static native int freenect_close_device (NativeDevice dev);
369 
370  private static native void freenect_set_user (NativeDevice dev, Pointer user);
371 
372  private static native Pointer freenect_get_user (NativeDevice dev);
373 
374  private static native void freenect_set_depth_callback (NativeDevice dev, NativeDepthCallback cb);
375 
376  private static native void freenect_set_video_callback (NativeDevice dev, NativeVideoCallback cb);
377 
378  private static native int freenect_set_depth_buffer (NativeDevice dev, ByteBuffer buf);
379 
380  private static native int freenect_set_video_buffer (NativeDevice dev, ByteBuffer buf);
381 
382  private static native int freenect_start_depth (NativeDevice dev);
383 
384  private static native int freenect_start_video (NativeDevice dev);
385 
386  private static native int freenect_stop_depth (NativeDevice dev);
387 
388  private static native int freenect_stop_video (NativeDevice dev);
389 
390  private static native int freenect_update_tilt_state (NativeDevice dev);
391 
392  private static native TiltState freenect_get_tilt_state (NativeDevice dev);
393 
394  private static native byte freenect_get_tilt_status (TiltState tiltState);
395 
396  private static native double freenect_get_tilt_degs (TiltState tiltState);
397 
398  private static native int freenect_set_tilt_degs (NativeDevice dev, double angle);
399 
400  private static native int freenect_set_led (NativeDevice dev, int option);
401 
402  private static native void freenect_get_mks_accel (TiltState tiltState, DoubleBuffer x, DoubleBuffer y,
403  DoubleBuffer z);
404 
405  private static native int freenect_select_subdevices (NativeContext ctx, int flag);
406 
407  private static native int freenect_get_video_mode_count ();
408 
409  private static native FrameMode.ByValue freenect_get_video_mode (int mode_num);
410 
412 
413  private static native FrameMode.ByValue freenect_find_video_mode (int res, int fmt);
414 
415  private static native int freenect_set_video_mode (NativeDevice dev, FrameMode.ByValue mode);
416 
417  private static native int freenect_get_depth_mode_count ();
418 
419  private static native FrameMode.ByValue freenect_get_depth_mode (int mode_num);
420 
422 
423  private static native FrameMode.ByValue freenect_find_depth_mode (int res, int fmt);
424 
425  private static native int freenect_set_depth_mode (NativeDevice dev, FrameMode.ByValue mode);
426 
427 }
void onMessage(Device dev, LogLevel level, String msg)
static native FrameMode.ByValue freenect_find_video_mode(int res, int fmt)
static native TiltState freenect_get_tilt_state(NativeDevice dev)
static native void freenect_set_log_callback(NativeContext ctx, NativeLogCallback cb)
static native Pointer freenect_get_user(NativeDevice dev)
static native FrameMode.ByValue freenect_find_depth_mode(int res, int fmt)
static TiltStatus fromInt(int value)
Definition: TiltStatus.java:51
static native FrameMode.ByValue freenect_get_video_mode(int mode_num)
static native int freenect_shutdown(NativeContext ctx)
static native int freenect_set_video_buffer(NativeDevice dev, ByteBuffer buf)
void setVideoFormat(VideoFormat fmt, Resolution res)
Definition: Freenect.java:217
static native int freenect_set_depth_mode(NativeDevice dev, FrameMode.ByValue mode)
int frame
Definition: regview.c:72
static native FrameMode.ByValue freenect_get_current_video_mode(NativeDevice dev)
static native int freenect_num_devices(NativeContext ctx)
static native int freenect_process_events(NativeContext ctx)
static native int freenect_stop_video(NativeDevice dev)
static native int freenect_init(PointerByReference ctx, Pointer usb_ctx)
static native void freenect_set_log_level(NativeContext ctx, int level)
static native int freenect_update_tilt_state(NativeDevice dev)
static native void freenect_set_user(NativeDevice dev, Pointer user)
static native int freenect_start_depth(NativeDevice dev)
void onFrameReceived(FrameMode mode, ByteBuffer frame, int timestamp)
static native int freenect_open_device(NativeContext ctx, PointerByReference dev, int index)
static native double freenect_get_tilt_degs(TiltState tiltState)
static native byte freenect_get_tilt_status(TiltState tiltState)
static native int freenect_set_video_mode(NativeDevice dev, FrameMode.ByValue mode)
static native int freenect_set_tilt_degs(NativeDevice dev, double angle)
static native int freenect_stop_depth(NativeDevice dev)
void(* callback)(void)
static native void freenect_set_video_callback(NativeDevice dev, NativeVideoCallback cb)
static native int freenect_set_led(NativeDevice dev, int option)
static native void freenect_get_mks_accel(TiltState tiltState, DoubleBuffer x, DoubleBuffer y, DoubleBuffer z)
static native int freenect_get_video_mode_count()
static freenect_context * ctx
static native int freenect_close_device(NativeDevice dev)
static native int freenect_select_subdevices(NativeContext ctx, int flag)
static native int freenect_get_depth_mode_count()
static Context createContext()
Definition: Freenect.java:50
static native int freenect_start_video(NativeDevice dev)
static native FrameMode.ByValue freenect_get_current_depth_mode(NativeDevice dev)
static LogLevel fromInt(int value)
Definition: LogLevel.java:56
static native FrameMode.ByValue freenect_get_depth_mode(int mode_num)
def handler(signum, frame)
void onFrameReceived(FrameMode mode, ByteBuffer frame, int timestamp)
void setDepthFormat(DepthFormat fmt, Resolution res)
Definition: Freenect.java:205
static native void freenect_set_depth_callback(NativeDevice dev, NativeDepthCallback cb)
static native int freenect_set_depth_buffer(NativeDevice dev, ByteBuffer buf)


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Mon Jun 10 2019 13:46:42