1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*****************************************************************************
* 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 <assert.h>
#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 { 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
&& ( f = fopen( "/sys/devices/platform/hdaps/calibrate", "re" ) ) )
{
/* IBM HDAPS support */
motion->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
fclose( f );
motion->sensor = HDAPS_SENSOR;
msg_Dbg( obj, "HDAPS motion detection correctly loaded" );
}
else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
{
/* Apple Motion Sensor support */
motion->sensor = AMS_SENSOR;
msg_Dbg( obj, "AMS motion detection correctly loaded" );
}
else if( access( "/sys/devices/platform/applesmc.768/position", R_OK ) == 0
&& ( f = fopen( "/sys/devices/platform/applesmc.768/calibrate", "re" ) ) )
{
/* Apple SMC (newer macbooks) */
/* Should be factorised with HDAPS */
motion->i_calibrate = fscanf( f, "(%d,%d)", &i_x, &i_y ) == 2 ? i_x: 0;
fclose( f );
motion->sensor = APPLESMC_SENSOR;
msg_Dbg( obj, "Apple SMC motion detection correctly loaded" );
}
#ifdef HAVE_MACOS_UNIMOTION
else if( (motion->unimotion_hw = detect_sms()) )
{
motion->sensor = UNIMOTION_SENSOR;
msg_Dbg( obj, "UniMotion motion detection correctly loaded" );
}
#endif
else
{
/* No motion sensor support */
msg_Err( obj, "No motion sensor available" );
free( motion );
return NULL;
}
memset( motion->p_oldx, 0, sizeof( motion->p_oldx ) );
motion->i = 0;
motion->i_sum = 0;
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", "re" );
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", "re" );
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", "re" );
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
default:
vlc_assert_unreachable();
}
}
/*****************************************************************************
* 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;
}
|