int read_sms(int type, int *x, int *y, int *z) { int _x, _y, _z; int xoff, yoff, zoff; Boolean ok; int ret; ret = read_sms_raw(type, &_x, &_y, &_z); if ( !ret ) return 0; static CFStringRef app = CFSTR("com.ramsayl.UniMotion"); static CFStringRef xoffstr = CFSTR("x_offset"); static CFStringRef yoffstr = CFSTR("y_offset"); static CFStringRef zoffstr = CFSTR("z_offset"); xoff = CFPreferencesGetAppIntegerValue(xoffstr, app, &ok); if ( ok ) _x += xoff; yoff = CFPreferencesGetAppIntegerValue(yoffstr, app, &ok); if ( ok ) _y += yoff; zoff = CFPreferencesGetAppIntegerValue(zoffstr, app, &ok); if ( ok ) _z += zoff; *x = _x; *y = _y; *z = _z; return ret; }
int read_sms_real(int type, double *x, double *y, double *z) { int _x, _y, _z; int xscale, yscale, zscale; int ret; Boolean ok; ret = read_sms_raw(type, &_x, &_y, &_z); if ( !ret ) return 0; static CFStringRef app = CFSTR("com.ramsayl.UniMotion"); static CFStringRef xscalestr = CFSTR("x_scale"); static CFStringRef yscalestr = CFSTR("y_scale"); static CFStringRef zscalestr = CFSTR("z_scale"); xscale = CFPreferencesGetAppIntegerValue(xscalestr, app, &ok); if ( !ok ) return 0; yscale = CFPreferencesGetAppIntegerValue(yscalestr, app, &ok); if ( !ok ) return 0; zscale = CFPreferencesGetAppIntegerValue(zscalestr, app, &ok); if ( !ok ) return 0; *x = _x / (double)xscale; *y = _y / (double)yscale; *z = _z / (double)zscale; return 1; }
// public module function, return a tuple with (x,y,z) coordinates static PyObject* coords(PyObject *self, PyObject *args) { int x, y, z; int type; // discover hardware type (powerbook, ibook, hirespb, macbookpro) type = detect_sms(); // launch exception if hardware is not supported if( type == unknown ){ PyErr_SetString(AppleSMSError, "No motion sensor available"); return 0; } // grab sensor raw coordinates int result = read_sms_raw(type, &x, &y, &z); // create exception if result is lower than 1 if( result < 1 ){ switch(result){ case 0: PyErr_SetString(AppleSMSError, "Hardware type not supported"); break; case -1: PyErr_SetString(AppleSMSError, "IOServiceGetMatchingServices returned error"); break; case -2: PyErr_SetString(AppleSMSError, "No motion sensor available"); break; case -3: PyErr_SetString(AppleSMSError, "Could not open motion sensor device"); break; case -4: PyErr_SetString(AppleSMSError, "No coords"); break; } return 0; } // build tuple to return return Py_BuildValue("(i,i,i)", x, y, z); }
static PyObject* pymotion_read_raw(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; int x, y, z; if (!read_sms_raw(g_accelerometerType, &x, &y, &z)) { PyErr_SetString(PyExc_RuntimeError, "Failed to read accelerometer data."); return NULL; } return Py_BuildValue("(iii)", x, y, z); }
void akabookmotion_bang(t_akabookmotion *x) { int sms_x, sms_y, sms_z; int result; if (x->type != unknown) { result = read_sms_raw(x->type, &sms_x, &sms_y, &sms_z); if (result) { outlet_int(x->m_zout, sms_z); outlet_int(x->m_yout, sms_y); outlet_int(x->m_xout, sms_x); } } }
/***************************************************************************** * 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 default: assert( 0 ); } }
//-------------------------------- bool ofxSuddenMotion::readMotion(){ if(!bHasHardware){ if(bVerbose)printf("ofxSuddenMotion - no hardware found / make sure to call setupHardware() first \n"); return false; } bool bRead = false; if( valueMode == OFX_SM_RAW ){ int rawX, rawY, rawZ; bRead = read_sms_raw(hardwareType, &rawX, &rawY, &rawZ); if(bRead){ actualX = (double)rawX; actualY = (double)rawY; actualZ = (double)rawZ; } }else if(valueMode == OFX_SM_REAL){ double realX, realY, realZ; bRead = read_sms_real(hardwareType, &realX, &realY, &realZ); if(bRead){ actualX = realX; actualY = realY; actualZ = realZ; } }else if(valueMode == OFX_SM_SCALED){ int scaledX, scaledY, scaledZ; bRead = read_sms_scaled(hardwareType, &scaledX, &scaledY, &scaledZ); if(bRead){ actualX = (double)scaledX; actualY = (double)scaledY; actualZ = (double)scaledZ; } }else{ if(bVerbose)printf("ofxSuddenMotion - incorrect value mode\n"); return false; } if( bRead ){ x = actualX - offsetX; y = actualY - offsetY; z = actualZ - offsetZ; //smooth values smoothX *= smoothPct; smoothY *= smoothPct; smoothZ *= smoothPct; smoothX += x * (1.0- smoothPct); smoothY += y * (1.0- smoothPct); smoothZ += z * (1.0- smoothPct); }else { if(bVerbose)printf("ofxSuddenMotion - problem reading sms data\n"); } return bRead; }