Commit 96a19d6c authored by Pierre Ynard's avatar Pierre Ynard

motion: split out motion sensor code

parent ec636160
...@@ -12,6 +12,8 @@ motion_extra = $(NULL) ...@@ -12,6 +12,8 @@ motion_extra = $(NULL)
endif endif
SOURCES_motion = \ SOURCES_motion = \
motion.c \ motion.c \
motionlib.c \
motionlib.h \
$(motion_extra) \ $(motion_extra) \
$(NULL) $(NULL)
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
# include "config.h" # include "config.h"
#endif #endif
#include <math.h>
#include <unistd.h> #include <unistd.h>
#include <vlc_common.h> #include <vlc_common.h>
...@@ -39,29 +38,14 @@ ...@@ -39,29 +38,14 @@
#include <vlc_playlist.h> #include <vlc_playlist.h>
#include <vlc_vout.h> #include <vlc_vout.h>
#ifdef __APPLE__ #include "motionlib.h"
# include "TargetConditionals.h"
# if !TARGET_OS_IPHONE
# define HAVE_MACOS_UNIMOTION
# endif
#endif
#ifdef HAVE_MACOS_UNIMOTION
# include "unimotion.h"
#endif
/***************************************************************************** /*****************************************************************************
* intf_sys_t: description and status of interface * intf_sys_t: description and status of interface
*****************************************************************************/ *****************************************************************************/
struct intf_sys_t struct intf_sys_t
{ {
enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR, motion_sensors_t *p_motion;
UNIMOTION_SENSOR } sensor;
#ifdef HAVE_MACOS_UNIMOTION
enum sms_hardware unimotion_hw;
#endif
int i_calibrate;
bool b_use_rotate; bool b_use_rotate;
}; };
...@@ -72,7 +56,6 @@ static int Open ( vlc_object_t * ); ...@@ -72,7 +56,6 @@ static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * ); static void Close ( vlc_object_t * );
static void RunIntf( intf_thread_t *p_intf ); static void RunIntf( intf_thread_t *p_intf );
static int GetOrientation( intf_thread_t *p_intf );
#define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform") #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
...@@ -100,8 +83,6 @@ vlc_module_end () ...@@ -100,8 +83,6 @@ vlc_module_end ()
int Open ( vlc_object_t *p_this ) int Open ( vlc_object_t *p_this )
{ {
intf_thread_t *p_intf = (intf_thread_t *)p_this; intf_thread_t *p_intf = (intf_thread_t *)p_this;
FILE *f;
int i_x = 0, i_y = 0;
p_intf->p_sys = malloc( sizeof( intf_sys_t ) ); p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
if( p_intf->p_sys == NULL ) if( p_intf->p_sys == NULL )
...@@ -109,57 +90,17 @@ int Open ( vlc_object_t *p_this ) ...@@ -109,57 +90,17 @@ int Open ( vlc_object_t *p_this )
return VLC_ENOMEM; return VLC_ENOMEM;
} }
if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 ) p_intf->p_sys->p_motion = motion_create( VLC_OBJECT( p_intf ) );
{ if( p_intf->p_sys->p_motion == NULL )
/* IBM HDAPS support */
f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
if( f )
{
p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
fclose( f );
p_intf->p_sys->sensor = HDAPS_SENSOR;
}
else
{
p_intf->p_sys->sensor = NO_SENSOR;
}
}
else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
{ {
/* Apple Motion Sensor support */ free( p_intf->p_sys );
p_intf->p_sys->sensor = AMS_SENSOR; return VLC_EGENERIC;
}
else if( access( "/sys/devices/platform/applesmc.768/position", R_OK ) == 0 )
{
/* Apple SMC (newer macbooks) */
/* Should be factorised with HDAPS */
f = fopen( "/sys/devices/platform/applesmc.768/calibrate", "r" );
if( f )
{
p_intf->p_sys->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
fclose( f );
p_intf->p_sys->sensor = APPLESMC_SENSOR;
}
else
{
p_intf->p_sys->sensor = NO_SENSOR;
}
}
#ifdef HAVE_MACOS_UNIMOTION
else if( (p_intf->p_sys->unimotion_hw = detect_sms()) )
p_intf->p_sys->sensor = UNIMOTION_SENSOR;
#endif
else
{
/* No motion sensor support */
p_intf->p_sys->sensor = NO_SENSOR;
} }
p_intf->pf_run = RunIntf; p_intf->pf_run = RunIntf;
p_intf->p_sys->b_use_rotate = var_InheritBool( p_intf, "motion-use-rotate" ); p_intf->p_sys->b_use_rotate = var_InheritBool( p_intf, "motion-use-rotate" );
msg_Dbg( p_intf, "Motion detection correctly loaded" );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -170,20 +111,18 @@ void Close ( vlc_object_t *p_this ) ...@@ -170,20 +111,18 @@ void Close ( vlc_object_t *p_this )
{ {
intf_thread_t *p_intf = (intf_thread_t *)p_this; intf_thread_t *p_intf = (intf_thread_t *)p_this;
free( p_intf->p_sys->p_motion );
free( p_intf->p_sys ); free( p_intf->p_sys );
} }
/***************************************************************************** /*****************************************************************************
* RunIntf: main loop * RunIntf: main loop
*****************************************************************************/ *****************************************************************************/
#define FILTER_LENGTH 16
#define LOW_THRESHOLD 800 #define LOW_THRESHOLD 800
#define HIGH_THRESHOLD 1000 #define HIGH_THRESHOLD 1000
static void RunIntf( intf_thread_t *p_intf ) static void RunIntf( intf_thread_t *p_intf )
{ {
int i_x, i_oldx = 0, i_sum = 0, i = 0; int i_x, i_oldx = 0;
int p_oldx[FILTER_LENGTH];
memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
for( ;; ) for( ;; )
{ {
...@@ -195,11 +134,7 @@ static void RunIntf( intf_thread_t *p_intf ) ...@@ -195,11 +134,7 @@ static void RunIntf( intf_thread_t *p_intf )
msleep( INTF_IDLE_SLEEP ); msleep( INTF_IDLE_SLEEP );
int canc = vlc_savecancel(); int canc = vlc_savecancel();
i_x = GetOrientation( p_intf ); i_x = motion_get_angle( p_intf->p_sys->p_motion );
i_sum += i_x - p_oldx[i];
p_oldx[i++] = i_x;
if( i == FILTER_LENGTH ) i = 0;
i_x = i_sum / FILTER_LENGTH;
if( p_intf->p_sys->b_use_rotate ) if( p_intf->p_sys->b_use_rotate )
{ {
...@@ -266,85 +201,6 @@ loop: ...@@ -266,85 +201,6 @@ loop:
vlc_restorecancel( canc ); vlc_restorecancel( canc );
} }
} }
#undef FILTER_LENGTH
#undef LOW_THRESHOLD #undef LOW_THRESHOLD
#undef HIGH_THRESHOLD #undef HIGH_THRESHOLD
/*****************************************************************************
* GetOrientation: get laptop orientation, range -1800 / +1800
*****************************************************************************/
static int GetOrientation( intf_thread_t *p_intf )
{
FILE *f;
int i_x = 0, i_y = 0, i_z = 0;
int i_ret;
switch( p_intf->p_sys->sensor )
{
case HDAPS_SENSOR:
f = fopen( "/sys/devices/platform/hdaps/position", "r" );
if( !f )
{
return 0;
}
i_ret = fscanf( f, "(%d,%d)", &i_x, &i_y );
fclose( f );
if( i_ret < 2 )
return 0;
else
return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
case AMS_SENSOR:
f = fopen( "/sys/devices/ams/x", "r" );
if( !f )
{
return 0;
}
i_ret = fscanf( f, "%d", &i_x);
fclose( f );
if( i_ret < 1 )
return 0;
else
return - i_x * 30; /* FIXME: arbitrary */
case APPLESMC_SENSOR:
f = fopen( "/sys/devices/platform/applesmc.768/position", "r" );
if( !f )
{
return 0;
}
i_ret = fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
fclose( f );
if( i_ret < 3 )
return 0;
else
return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
#ifdef HAVE_MACOS_UNIMOTION
case UNIMOTION_SENSOR:
if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
{
double d_norm = sqrt( i_x*i_x+i_z*i_z );
if( d_norm < 100 )
return 0;
double d_x = i_x / d_norm;
if( i_z > 0 )
return -asin(d_x)*3600/3.141;
else
return 3600 + asin(d_x)*3600/3.141;
}
else
return 0;
#endif
case NO_SENSOR:
default:
return 0;
}
}
/*****************************************************************************
* motion.c: laptop built-in motion sensors
*****************************************************************************
* Copyright (C) 2006 - 2012 the VideoLAN team
* $Id$
*
* Author: Sam Hocevar <sam@zoy.org>
* Jérôme Decoodt <djc@videolan.org> (unimotion integration)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <math.h>
#include <unistd.h>
#include <vlc_common.h>
#ifdef __APPLE__
# include "TargetConditionals.h"
# if !TARGET_OS_IPHONE
# define HAVE_MACOS_UNIMOTION
# endif
#endif
#ifdef HAVE_MACOS_UNIMOTION
# include "unimotion.h"
#endif
#include "motionlib.h"
struct motion_sensors_t
{
enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
UNIMOTION_SENSOR } sensor;
#ifdef HAVE_MACOS_UNIMOTION
enum sms_hardware unimotion_hw;
#endif
int i_calibrate;
int p_oldx[16];
int i;
int i_sum;
};
motion_sensors_t *motion_create( vlc_object_t *obj )
{
FILE *f;
int i_x = 0, i_y = 0;
motion_sensors_t *motion = malloc( sizeof( motion_sensors_t ) );
if( unlikely( motion == NULL ) )
{
return NULL;
}
if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
{
/* IBM HDAPS support */
f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
if( f )
{
motion->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
fclose( f );
motion->sensor = HDAPS_SENSOR;
}
else
{
motion->sensor = NO_SENSOR;
}
}
else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
{
/* Apple Motion Sensor support */
motion->sensor = AMS_SENSOR;
}
else if( access( "/sys/devices/platform/applesmc.768/position", R_OK ) == 0 )
{
/* Apple SMC (newer macbooks) */
/* Should be factorised with HDAPS */
f = fopen( "/sys/devices/platform/applesmc.768/calibrate", "r" );
if( f )
{
motion->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
fclose( f );
motion->sensor = APPLESMC_SENSOR;
}
else
{
motion->sensor = NO_SENSOR;
}
}
#ifdef HAVE_MACOS_UNIMOTION
else if( (motion->unimotion_hw = detect_sms()) )
motion->sensor = UNIMOTION_SENSOR;
#endif
else
{
/* No motion sensor support */
motion->sensor = NO_SENSOR;
}
memset( motion->p_oldx, 0, sizeof( motion->p_oldx ) );
motion->i = 0;
motion->i_sum = 0;
msg_Dbg( obj, "Motion detection correctly loaded" );
return motion;
}
void motion_destroy( motion_sensors_t *motion )
{
free( motion );
}
/*****************************************************************************
* GetOrientation: get laptop orientation, range -1800 / +1800
*****************************************************************************/
static int GetOrientation( motion_sensors_t *motion )
{
FILE *f;
int i_x = 0, i_y = 0, i_z = 0;
int i_ret;
switch( motion->sensor )
{
case HDAPS_SENSOR:
f = fopen( "/sys/devices/platform/hdaps/position", "r" );
if( !f )
{
return 0;
}
i_ret = fscanf( f, "(%d,%d)", &i_x, &i_y );
fclose( f );
if( i_ret < 2 )
return 0;
else
return ( i_x - motion->i_calibrate ) * 10;
case AMS_SENSOR:
f = fopen( "/sys/devices/ams/x", "r" );
if( !f )
{
return 0;
}
i_ret = fscanf( f, "%d", &i_x);
fclose( f );
if( i_ret < 1 )
return 0;
else
return - i_x * 30; /* FIXME: arbitrary */
case APPLESMC_SENSOR:
f = fopen( "/sys/devices/platform/applesmc.768/position", "r" );
if( !f )
{
return 0;
}
i_ret = fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
fclose( f );
if( i_ret < 3 )
return 0;
else
return ( i_x - motion->i_calibrate ) * 10;
#ifdef HAVE_MACOS_UNIMOTION
case UNIMOTION_SENSOR:
if( read_sms_raw( motion->unimotion_hw, &i_x, &i_y, &i_z ) )
{
double d_norm = sqrt( i_x*i_x+i_z*i_z );
if( d_norm < 100 )
return 0;
double d_x = i_x / d_norm;
if( i_z > 0 )
return -asin(d_x)*3600/3.141;
else
return 3600 + asin(d_x)*3600/3.141;
}
else
return 0;
#endif
case NO_SENSOR:
default:
return 0;
}
}
/*****************************************************************************
* motion_get_angle: get averaged laptop orientation, range -1800 / +1800
*****************************************************************************/
int motion_get_angle( motion_sensors_t *motion )
{
const int filter_length = ARRAY_SIZE( motion->p_oldx );
int i_x = GetOrientation( motion );
motion->i_sum += i_x - motion->p_oldx[motion->i];
motion->p_oldx[motion->i] = i_x;
motion->i = ( motion->i + 1 ) % filter_length;
i_x = motion->i_sum / filter_length;
return i_x;
}
/*****************************************************************************
* motion.h: laptop built-in motion sensors
*****************************************************************************
* Copyright (C) 2012 the VideoLAN team
* $Id$
*
* Author: Pierre Ynard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef MOTION_H
#define MOTION_H
typedef struct motion_sensors_t motion_sensors_t;
motion_sensors_t *motion_create( vlc_object_t *obj );
void motion_destroy( motion_sensors_t *motion );
int motion_get_angle( motion_sensors_t *motion );
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment