Permalink
Browse files

Add SWIG Bindings for PSMoveFusion (closes #48)

Added bindings for the PSMoveFusion class and an
example in Python how to use it with pyglet/OpenGL.
  • Loading branch information...
thp committed Jan 26, 2013
1 parent b983f37 commit b3fc820fc25e627b9658a367e105a8a160032ddf
Showing with 158 additions and 0 deletions.
  1. +62 −0 bindings/swig/psmove.i
  2. +91 −0 examples/python/test_fusion.py
  3. +5 −0 include/psmove_fusion.h
View
@@ -83,6 +83,7 @@
#include "psmove.h"
#include "psmove_tracker.h"
+#include "psmove_fusion.h"
%}
@@ -93,7 +94,10 @@ typedef struct {} PSMove;
#ifdef PSMOVE_BUILD_TRACKER
%include "psmove_tracker.h"
+%include "psmove_fusion.h"
typedef struct {} PSMoveTracker;
+typedef struct {} PSMoveFusion;
+
#endif /* PSMOVE_BUILD_TRACKER */
@@ -138,6 +142,20 @@ void reinit();
void get_magnetometer_vector(float *OUTPUT, float *OUTPUT, float *OUTPUT);
+ void enable_orientation(int enabled) {
+ psmove_enable_orientation($self, enabled);
+ }
+
+ int has_orientation() {
+ return psmove_has_orientation($self);
+ }
+
+ void get_orientation(float *OUTPUT, float *OUTPUT, float *OUTPUT, float *OUTPUT);
+
+ void reset_orientation() {
+ psmove_reset_orientation($self);
+ }
+
void set_leds(int r, int g, int b) {
psmove_set_leds($self, r, g, b);
}
@@ -321,6 +339,38 @@ void reinit();
}
+%extend PSMoveFusion {
+ PSMoveFusion(PSMoveTracker *tracker, float z_near, float z_far) {
+ return psmove_fusion_new(tracker, z_near, z_far);
+ }
+
+ ~PSMoveFusion() {
+ psmove_fusion_free($self);
+ }
+
+ void get_position(PSMove *move, float *OUTPUT, float *OUTPUT, float *OUTPUT);
+
+ PSMoveMatrix4x4 get_projection_matrix() {
+ PSMoveMatrix4x4 tmp;
+ float *matrix = psmove_fusion_get_projection_matrix($self);
+ memcpy(&tmp, matrix, sizeof(tmp));
+ return tmp;
+ }
+
+ PSMoveMatrix4x4 get_modelview_matrix(PSMove *move) {
+ PSMoveMatrix4x4 tmp;
+ float *matrix = psmove_fusion_get_modelview_matrix($self, move);
+ memcpy(&tmp, matrix, sizeof(tmp));
+ return tmp;
+ }
+}
+
+%extend PSMoveMatrix4x4 {
+ float at(int i) {
+ return $self->m[i];
+ }
+}
+
#endif /* PSMOVE_BUILD_TRACKER */
@@ -344,6 +394,12 @@ PSMove_get_magnetometer_vector(PSMove *move, float *x, float *y, float *z)
psmove_get_magnetometer_vector(move, x, y, z);
}
+void
+PSMove_get_orientation(PSMove *move, float *w, float *x, float *y, float *z)
+{
+ psmove_get_orientation(move, w, x, y, z);
+}
+
void
PSMove_get_button_events(PSMove *move,
unsigned int *pressed, unsigned int *released)
@@ -495,6 +551,12 @@ PSMoveTracker_get_size(PSMoveTracker *tracker, int *width, int *height)
return psmove_tracker_get_size(tracker, width, height);
}
+void
+PSMoveFusion_get_position(PSMoveFusion *fusion, PSMove *move,
+ float *x, float *y, float *z)
+{
+ psmove_fusion_get_position(fusion, move, x, y, z);
+}
#endif /* PSMOVE_BUILD_TRACKER */
@@ -0,0 +1,91 @@
+
+#
+# PS Move API - An interface for the PS Move Motion Controller
+# Copyright (c) 2013 Thomas Perl <m@thp.io>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+# This example requires both pyglet and the Python OpenGL bindings to work
+
+import sys
+import os
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'build'))
+
+import pyglet
+from pyglet.gl import *
+from OpenGL.GLUT import *
+import psmove
+
+glutInit(sys.argv)
+
+window = pyglet.window.Window(width=640, height=480)
+
+tracker = psmove.PSMoveTracker()
+tracker.set_mirror(True)
+
+def load_matrix(mode, matrix):
+ glMatrixMode(mode)
+ glLoadMatrixf((GLfloat*16)(*[matrix.at(i) for i in range(16)]))
+
+fusion = psmove.PSMoveFusion(tracker, 1, 1000)
+projection_matrix = fusion.get_projection_matrix()
+
+move = psmove.PSMove()
+move.enable_orientation(True)
+move.reset_orientation()
+
+while tracker.enable(move) != psmove.Tracker_CALIBRATED:
+ pass
+
+glEnable(GL_DEPTH_TEST)
+glEnable(GL_BLEND)
+glEnable(GL_LIGHTING)
+glEnable(GL_LIGHT0)
+glEnable(GL_COLOR_MATERIAL)
+
+@window.event
+def on_draw():
+ while move.poll():
+ pressed, released = move.get_button_events()
+ if pressed & psmove.Btn_MOVE:
+ move.reset_orientation()
+
+ tracker.update_image()
+ tracker.update()
+
+ window.clear()
+
+ load_matrix(GL_PROJECTION, projection_matrix)
+ load_matrix(GL_MODELVIEW, fusion.get_modelview_matrix(move))
+
+ status = tracker.get_status(move)
+ if status == psmove.Tracker_TRACKING:
+ glColor4f(1., 1., 1., 1.)
+ else:
+ glColor4f(1., 0., 0., .5)
+ glutSolidCube(1)
+
+pyglet.app.run()
+
View
@@ -44,6 +44,11 @@ extern "C" {
#define PSEYE_FOV_RED_DOT 56
+/* This struct is used by the SWIG bindings as result value */
+typedef struct {
+ float m[16];
+} PSMoveMatrix4x4;
+
#ifndef SWIG
struct _PSMoveFusion;
typedef struct _PSMoveFusion PSMoveFusion; /*!< Handle to a PS Move Fusion object.

0 comments on commit b3fc820

Please sign in to comment.