Notice: MediaWiki has been updated. Report any rough edges to marcan@marcan.st

Difference between revisions of "Getting Started With Common Lisp"

From OpenKinect
Jump to: navigation, search
(Created page with "== Dependencies == == libfreenect == Install libfreenect as described on the Installation page. == cffi, verrazano == The next step is to create [http://c...")
 
Line 18: Line 18:
 
  (verrazano:generate-binding (list :cffi :package-name :libfreenect :input-files (list "libfreenect/libfreenect.h")))
 
  (verrazano:generate-binding (list :cffi :package-name :libfreenect :input-files (list "libfreenect/libfreenect.h")))
 
  (load (compile-file #p"libfreenect.lisp"))
 
  (load (compile-file #p"libfreenect.lisp"))
 +
(use-package :libfreenect)
  
 
== Load libfreenect in your Lisp image ==
 
== Load libfreenect in your Lisp image ==
Line 24: Line 25:
  
 
  (cffi:load-foreign-library #p"libfreenect.so")
 
  (cffi:load-foreign-library #p"libfreenect.so")
 +
 +
== Example Usage ==
 +
 +
Define a couple of globals which you would need to pass around for most calls to libfreenect:
 +
 +
(defvar *ctx*) ; freenect_context *ctx;
 +
(defvar *dev*) ; freenect_device *dev;
 +
 +
Initialize the library with a call to freenect_init:
 +
 +
(let ((ctx-ptr (cffi:foreign-alloc :pointer)))
 +
  (freenect-init ctx-ptr (cffi:null-pointer))
 +
  (setf *ctx* (cffi:mem-ref ctx-ptr :pointer)))
 +
 +
If a Kinect is connected, you can confirm it with a call to freenect_num_devices (which should return 1)
 +
 +
(freenect-num-devices *ctx*) ; this should return 1
 +
 +
Now "open" the device with a call to freenect_open_device (the last arg to freenect_open_device refers to the device number index, which is 0 in case a single device is connected):
 +
 +
(let ((dev-ptr (cffi:foreign-alloc :pointer)))
 +
  (freenect-open-device *ctx* dev-ptr 0)
 +
  (setf *dev* (cffi:mem-ref dev-ptr :pointer)))
 +
 +
Now you can confirm that you are able to work with the device with a call to freenect_set_led (see the LED colour change accordingly):
 +
 +
(freenect-set-led dev (cffi:foreign-enum-value 'freenect-led-options :led-blink-red-yellow))

Revision as of 06:59, 13 December 2010

Dependencies

libfreenect

Install libfreenect as described on the Installation page.

cffi, verrazano

The next step is to create CFFI bindings for libfreenect. We can use Verrazano to generate these bindings for us. Installing these is simple via quicklisp:

(ql:quickload :cffi)
(ql:quickload :verrazano)

Generate CFFI bindings

Generate the bindings with verrazano and load them

(verrazano:generate-binding (list :cffi :package-name :libfreenect :input-files (list "libfreenect/libfreenect.h")))
(load (compile-file #p"libfreenect.lisp"))
(use-package :libfreenect)

Load libfreenect in your Lisp image

On Linux, you would load libfreenect.so. Change the name accordingly for Mac and Windows.

(cffi:load-foreign-library #p"libfreenect.so")

Example Usage

Define a couple of globals which you would need to pass around for most calls to libfreenect:

(defvar *ctx*) ; freenect_context *ctx;
(defvar *dev*) ; freenect_device *dev;

Initialize the library with a call to freenect_init:

(let ((ctx-ptr (cffi:foreign-alloc :pointer)))
  (freenect-init ctx-ptr (cffi:null-pointer))
  (setf *ctx* (cffi:mem-ref ctx-ptr :pointer)))

If a Kinect is connected, you can confirm it with a call to freenect_num_devices (which should return 1)

(freenect-num-devices *ctx*) ; this should return 1

Now "open" the device with a call to freenect_open_device (the last arg to freenect_open_device refers to the device number index, which is 0 in case a single device is connected):

(let ((dev-ptr (cffi:foreign-alloc :pointer)))
  (freenect-open-device *ctx* dev-ptr 0)
  (setf *dev* (cffi:mem-ref dev-ptr :pointer)))

Now you can confirm that you are able to work with the device with a call to freenect_set_led (see the LED colour change accordingly):

(freenect-set-led dev (cffi:foreign-enum-value 'freenect-led-options :led-blink-red-yellow))