Esempio n. 1
0
xl_ekf_context *xl_ekf_context_create() {
    xl_ekf_context *ctx = (xl_ekf_context *) malloc(sizeof(xl_ekf_context));
    memset(ctx, 0, sizeof(xl_ekf_context));
//    ctx->sensor_manager = ASensorManager_getInstanceForPackage("com.cls.xl.xl");
    ctx->sensor_manager = ASensorManager_getInstance();
    ctx->acc = ASensorManager_getDefaultSensor(ctx->sensor_manager, ASENSOR_TYPE_ACCELEROMETER);
    ctx->gyro = ASensorManager_getDefaultSensor(ctx->sensor_manager, ASENSOR_TYPE_GYROSCOPE);
    ctx->acc_min_delay = ASensor_getMinDelay(ctx->acc);
    ctx->gyro_min_delay = ASensor_getMinDelay(ctx->gyro);
    if (ctx->acc == NULL || ctx->gyro == NULL) {
        free(ctx);
        return NULL;
    }
    ctx->looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
    ctx->event_queue = ASensorManager_createEventQueue(
            ctx->sensor_manager,
            ctx->looper,
            LOOPER_ID_USER,
            NULL,
            NULL
    );
    ASensorEventQueue_enableSensor(ctx->event_queue, ctx->acc);
    ASensorEventQueue_enableSensor(ctx->event_queue, ctx->gyro);
    int acc_delay = 20000;
    int gyro_delay = 20000;
    acc_delay = acc_delay > ctx->acc_min_delay ? acc_delay : ctx->acc_min_delay;
    gyro_delay = gyro_delay > ctx->gyro_min_delay ? gyro_delay : ctx->gyro_min_delay;
    ASensorEventQueue_setEventRate(ctx->event_queue, ctx->acc, acc_delay);
    ASensorEventQueue_setEventRate(ctx->event_queue, ctx->gyro, gyro_delay);
    ctx->lock = malloc(sizeof(pthread_mutex_t));
    pthread_mutex_init(ctx->lock, NULL);
    return ctx;
}
void SensorCollector::EnableSensors(bool enableAccel, bool enableGyro, bool enableMagneto)
{
	if (!(enableAccel || enableGyro || enableMagneto))
	{
		LOGW(LOGTAG_SENSOR,"All sensor enable selections are false...");
		return;
	}

	int defaultRate = 15;
	bool success = true;
	if (enableAccel)
	{		
		LOGI(LOGTAG_SENSOR,"Enabling accelerometer");
		int resultEnable =  ASensorEventQueue_enableSensor(sensorQueue,accelSensor);
		int resultQueue = ASensorEventQueue_setEventRate(sensorQueue,accelSensor, (1000L/ defaultRate)*1000);

		if (resultEnable < 0)
			LOGE("Error enabling accelerometer sensor");
		if (resultQueue < 0)
			LOGE("Error setting event rate for accelerometer");
		accelEnabled = (resultEnable >= 0 && resultQueue >= 0);
		success = success && accelEnabled;
	}

	if (enableGyro)
	{
		LOGI(LOGTAG_SENSOR,"Enabling gyroscope");
		int resultEnable = ASensorEventQueue_enableSensor(sensorQueue,gyroSensor);
		int resultQueue =ASensorEventQueue_setEventRate(sensorQueue,gyroSensor, (1000L/ defaultRate)*1000);

		if (resultEnable < 0)
			LOGE("Error enabling gyroscope sensor");
		if (resultQueue < 0)
			LOGE("Error setting event rate for gyroscope");

		gyroEnabled = (resultEnable >= 0 && resultQueue >= 0);
		success = success && gyroEnabled;
	}

	if (enableMagneto)
	{
		LOGI(LOGTAG_SENSOR,"Enabling magnetic field sensor");
		int resultEnable = ASensorEventQueue_enableSensor(sensorQueue,magnetoSensor);
		int resultQueue =ASensorEventQueue_setEventRate(sensorQueue,magnetoSensor, (1000L/ defaultRate)*1000);

		if (resultEnable < 0)
			LOGE("Error enabling magnetic sensor");
		if (resultQueue < 0)
			LOGE("Error setting event rate for magnetic sensor");
		magEnabled = (resultEnable >= 0 && resultQueue >= 0);
		success = success && magEnabled;
	}

	if (success)
		LOGI(LOGTAG_SENSOR,"All sensors enabled successfully!");
}
Esempio n. 3
0
void setup_sensors() {

	ASensorEvent event;
	int events, ident;
	ASensorManager * sensorManager;
	const ASensor* accSensor;
	const ASensor* gyroSensor;
	const ASensor* magSensor;
	void* sensor_data = malloc(10000);

	LOGI("sensorValue() - ALooper_forThread()");
	SENSORS_ENABLED = 1;
	ALooper* looper = ALooper_forThread();

	if (looper == NULL) {
		looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
	}

	sensorManager = ASensorManager_getInstance();

	accSensor = ASensorManager_getDefaultSensor(sensorManager,
			ASENSOR_TYPE_ACCELEROMETER);
	gyroSensor = ASensorManager_getDefaultSensor(sensorManager,
			ASENSOR_TYPE_GYROSCOPE);
	magSensor = ASensorManager_getDefaultSensor(sensorManager,
			ASENSOR_TYPE_MAGNETIC_FIELD);

	sensorEventQueue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID,
			(ALooper_callbackFunc)get_sensorevents, sensor_data);

	ASensorEventQueue_enableSensor(sensorEventQueue, accSensor);
	ASensorEventQueue_enableSensor(sensorEventQueue, gyroSensor);
	ASensorEventQueue_enableSensor(sensorEventQueue, magSensor);

//Sampling rate: 100Hz
	int a = ASensor_getMinDelay(accSensor);
	int b = ASensor_getMinDelay(gyroSensor);
	int c = ASensor_getMinDelay(magSensor);
	LOGI("min-delay: %d, %d, %d", a, b, c);
	ASensorEventQueue_setEventRate(sensorEventQueue, accSensor, (1000L/SAMP_PER_SEC)*1000);
	ASensorEventQueue_setEventRate(sensorEventQueue, gyroSensor, (1000L/SAMP_PER_SEC)*1000);
	ASensorEventQueue_setEventRate(sensorEventQueue, magSensor, (1000L/SAMP_PER_SEC)*1000);

	LOGI("sensorValue() - START");

	while ((ident = ALooper_pollAll(-1, NULL, &events, NULL) >= 0)) {
		// If a sensor has data, process it now.
		if (ident == LOOPER_ID) {
			ASensorEvent event;
			while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) {
				LOGI("accelerometer X = %f y = %f z=%f ", event.acceleration.x, event.acceleration.y, event.acceleration.z);
			}
		}
	}
}
Esempio n. 4
0
/**
 * 繝。繧、繝ウ繧ウ繝槭Φ繝峨�蜃ヲ逅� */
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*) app->userData;
    switch (cmd) {
    case APP_CMD_SAVE_STATE:
        engine->app->savedState = malloc(sizeof(struct saved_state));
        *((struct saved_state*) engine->app->savedState) = engine->state;
        engine->app->savedStateSize = sizeof(struct saved_state);
        break;
    case APP_CMD_INIT_WINDOW:
        if (engine->app->window != NULL) {
            engine_init_display(engine);
            engine_draw_frame(engine);
        }
        break;
    case APP_CMD_TERM_WINDOW:
        engine_term_display(engine);
        break;
    case APP_CMD_GAINED_FOCUS:
        if (engine->accelerometerSensor != NULL) {
            ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                           engine->accelerometerSensor);
            ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                           engine->accelerometerSensor, (1000L / 60) * 1000);
        }
        break;
    case APP_CMD_LOST_FOCUS:
        if (engine->accelerometerSensor != NULL) {
            ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                            engine->accelerometerSensor);
        }
        engine->animating = 0;
        engine_draw_frame(engine);
        break;
    }
}
Esempio n. 5
0
 void resume() {
     ASensorEventQueue_enableSensor(accelerometerEventQueue, accelerometer);
     auto status = ASensorEventQueue_setEventRate(accelerometerEventQueue,
                                                  accelerometer,
                                                  SENSOR_REFRESH_PERIOD_US);
     assert(status >= 0);
 }
Esempio n. 6
0
	status Sensor::enable()
	{
		if (mEventLoop.mEnabled)
		{
			mSensor = ASensorManager_getDefaultSensor(mEventLoop.mSensorManager, mSensorType);
			if (mSensor != NULL)
			{
				if (ASensorEventQueue_enableSensor(mEventLoop.mSensorEventQueue, mSensor) < 0)
				{
					goto ERROR;
				}

				int32_t lMinDelay = ASensor_getMinDelay(mSensor);
				if (ASensorEventQueue_setEventRate(mEventLoop.mSensorEventQueue, mSensor, lMinDelay) < 0)
				{
					goto ERROR;
				}

			} else
			{
				Pegas_log_error("No sensor type %d", mSensorType);
			}
		}

		return STATUS_OK;

		ERROR:

		Pegas_log_error("Error while activating sensor.");
		disable();

		return STATUS_KO;
	}
/*
 * Class:     io_quadroid_ndk_QuadroidLib
 * Method:    init
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_io_quadroid_UDPSensor_ndk_QuadroidLib_init(JNIEnv *env, jclass clazz) {

    sensorManager = ASensorManager_getInstance();
    looper = ALooper_forThread();

    if(looper == NULL) {
        looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
    }

    queue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID, get_sensor_events, NULL);

    if(USE_PRESSURE==1) {
        pressureSensor = ASensorManager_getDefaultSensor(sensorManager, 6);
        ASensorEventQueue_enableSensor(queue, pressureSensor);
        ASensorEventQueue_setEventRate(queue, pressureSensor, SAMP_PER_SEC);
    }

    if(USE_ACCELEROMETER==1) {
        acceSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
        ASensorEventQueue_enableSensor(queue, acceSensor);
        ASensorEventQueue_setEventRate(queue, acceSensor, SAMP_PER_SEC);
        log_acce = ASensor_getMinDelay(acceSensor);
    }

    if(USE_GYROSCOPE==1) {
        gyroSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_GYROSCOPE);
        ASensorEventQueue_enableSensor(queue, gyroSensor);
        ASensorEventQueue_setEventRate(queue, gyroSensor, SAMP_PER_SEC);
        log_gyro = ASensor_getMinDelay(gyroSensor);
    }

    if(USE_MAGNETIC==1) {
        magnSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_MAGNETIC_FIELD);
        ASensorEventQueue_enableSensor(queue, magnSensor);
        ASensorEventQueue_setEventRate(queue, magnSensor, SAMP_PER_SEC);
        log_magn = ASensor_getMinDelay(magnSensor);
    }

    if(USE_DISTANCE==1) {
        proxSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_PROXIMITY);
        ASensorEventQueue_enableSensor(queue, proxSensor);
        ASensorEventQueue_setEventRate(queue, proxSensor, SAMP_PER_SEC);
    }

    currentTime = time(0);
    ALooper_pollAll(-1, NULL, &events, NULL);
}
static TCGvoid EngineHandleCmd(struct android_app* app, TCGint cmd)
{
    struct Engine* engine = (struct Engine*)app->userData;
    switch (cmd)
    {
    case APP_CMD_SAVE_STATE:
        // The system has asked us to save our current state.  Do so.
        engine->app->savedState = TCG_MALLOC(sizeof(struct SavedState), eTCG_MAIN);
        *((struct SavedState*)engine->app->savedState) = engine->state;
        engine->app->savedStateSize = sizeof(struct SavedState);
        break;

    case APP_CMD_INIT_WINDOW:
        //窗口初始化
        _Lock.Lock();
        g_bWindowMax = TCG_TRUE;
        _Lock.UnLock();
        
        if (engine->app->window != NULL)
        {
            g_WindowType = engine->app->window;
        }
        break;

    case APP_CMD_TERM_WINDOW:
        //窗口销毁
        _Lock.Lock();
        g_bWindowMax = TCG_FALSE;
        _Lock.UnLock();
        break;

    case APP_CMD_GAINED_FOCUS:
        // When our app gains focus, we start monitoring the accelerometer.
        if (engine->accelerometerSensor != NULL)
        {
            ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                    engine->accelerometerSensor);
            // We'd like to get 60 events per second (in us).
            ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                    engine->accelerometerSensor, (1000L/60)*1000);
        }
        break;

    case APP_CMD_LOST_FOCUS:
        // When our app loses focus, we stop monitoring the accelerometer.
        // This is to avoid consuming battery while not being used.
        if (engine->accelerometerSensor != NULL)
        {
            ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                    engine->accelerometerSensor);
        }
        // Also stop animating.
        engine->animating = 0;
        break;

    default:
        break;
    }
}
Esempio n. 9
0
static void enableSensorManager()
{
  if(sstJoystick.u32Frequency > 0 && !sstJoystick.bAccelerometerEnabled)
  {
    ASensorEventQueue_enableSensor(sstJoystick.sensorEventQueue, sstJoystick.accelerometerSensor);
    ASensorEventQueue_setEventRate(sstJoystick.sensorEventQueue, sstJoystick.accelerometerSensor, (1000L/sstJoystick.u32Frequency)*1000);
    sstJoystick.bAccelerometerEnabled = orxTRUE;
  }
}
Esempio n. 10
0
JNIEXPORT void JNICALL Java_org_nzdis_sensorspeed_CMagneticField_magneticFieldStartup (JNIEnv *e, jclass c, jobject handler) {
	updateHandler = handler;
	env = e;

	jclass handlerClass = env->FindClass("org/nzdis/sensorspeed/CMagneticField");
	if (handlerClass == NULL) {
		LOGI("big error 1");
	}
	mid = env->GetMethodID(handlerClass, "onSensorChanged", "()V");
	if (mid == NULL) {
		LOGI("big error 2");
	}

	ASensorEvent event;
	int events, ident;
	ASensorManager * sensorManager;
	const ASensor* magSensor;
	void* sensor_data = malloc(10000);

	SENSORS_ENABLED = 1;
	ALooper* looper = ALooper_forThread();

	if (looper == NULL) {
		looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
	}

	sensorManager = ASensorManager_getInstance();

	magSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_MAGNETIC_FIELD);

	sensorEventQueue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID,
			(ALooper_callbackFunc)get_sensorevents, sensor_data);

	ASensorEventQueue_enableSensor(sensorEventQueue, magSensor);

	int minDelay = ASensor_getMinDelay(magSensor);
	//LOGI("min-delay: %d", minDelay);
	ASensorEventQueue_setEventRate(sensorEventQueue, magSensor, (1000L/SAMP_PER_SEC)*1000);

	while ((ident = ALooper_pollAll(-1, NULL, &events, NULL) >= 0)) {
		// If a sensor has data, process it now.
		if (ident == LOOPER_ID) {
			LOGI("magneticFieldStartup() - LOOPER!!!!!!!!");
			ASensorEvent event;
			while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) {
				if (event.type == ASENSOR_TYPE_MAGNETIC_FIELD) {
					env->CallVoidMethod(updateHandler, mid);
					magneticfield_x = event.magnetic.x;
					magneticfield_y = event.magnetic.y;
					magneticfield_z = event.magnetic.z;
				}
			}
		} else {
			LOGI("magneticFieldStartup() - else!!!!!!!!!!!!!");
		}
	}
}
void Engine::ResumeSensors() {
  // When our app gains focus, we start monitoring the accelerometer.
  if (accelerometer_sensor_ != NULL) {
    ASensorEventQueue_enableSensor(sensor_event_queue_, accelerometer_sensor_);
    // We'd like to get 60 events per second (in us).
    ASensorEventQueue_setEventRate(sensor_event_queue_, accelerometer_sensor_,
                                   (1000L / 60) * 1000);
  }
}
Esempio n. 12
0
void SensorManager::Resume() {
  // When the app gains focus, start monitoring the accelerometer.
  if (accelerometerSensor_ != NULL) {
    ASensorEventQueue_enableSensor(sensorEventQueue_, accelerometerSensor_);
    // We'd like to get 60 events per second (in us).
    ASensorEventQueue_setEventRate(sensorEventQueue_, accelerometerSensor_,
                                   (1000L / 60) * 1000);
  }
}
Esempio n. 13
0
void Engine::resumeSensors()
{
    // When our app gains focus, we start monitoring the accelerometer.
    if( mAccelerometerSensor != NULL )
    {
        ASensorEventQueue_enableSensor( mSensorEventQueue, mAccelerometerSensor );
        ASensorEventQueue_setEventRate( mSensorEventQueue, mAccelerometerSensor, (1000L / 30) * 1000 );
    }
}
Esempio n. 14
0
static void setEnableAccelerometer(int32_t framerate){
	if(sensors.accelerometerSensor){
		ASensorEventQueue_enableSensor(sensors.sensorEventQueue,
									   sensors.accelerometerSensor);
		ASensorEventQueue_setEventRate(
							sensors.sensorEventQueue,
							sensors.accelerometerSensor, 
							framerate);
	}
}
Esempio n. 15
0
int GoAndroid_enableSensor(int s, int32_t usec) {
  ASensorManager* manager = ASensorManager_getInstance();
  const ASensor* sensor = ASensorManager_getDefaultSensor(manager, s);
  if (sensor == NULL) {
    return 1;
  }
  ASensorEventQueue_enableSensor(queue, sensor);
  ASensorEventQueue_setEventRate(queue, sensor, usec);
  return 0;
}
Esempio n. 16
0
/**
  * Process the next main command.
  */
static void engine_handle_cmd(struct android_app* app, int32_t cmd) 
{
  struct engine* engine = (struct engine*)app->userData;
  switch (cmd) {
    case APP_CMD_SAVE_STATE:
      // The system has asked us to save our current state.  Do so.
      engine->app->savedState = malloc(sizeof(struct saved_state));
      *((struct saved_state*)engine->app->savedState) = engine->state;
      engine->app->savedStateSize = sizeof(struct saved_state);
      break;
    case APP_CMD_INIT_WINDOW:
      // The window is being shown, get it ready.
      if (engine->app->window != NULL) 
      {
        engine_init_display(engine);

        nuiAndroidBridge::androidResize(ANativeWindow_getWidth(app->window), ANativeWindow_getHeight(app->window));
        nuiButton* pButton = new nuiButton("Prout!");
//        pButton->SetPosition(nuiCenter);
        gpBridge->AddChild(pButton);

        engine_draw_frame(engine);
        engine->animating = 1;
      }
      break;
    case APP_CMD_TERM_WINDOW:
      // The window is being hidden or closed, clean it up.
      engine_term_display(engine);
      break;
    case APP_CMD_GAINED_FOCUS:
      // When our app gains focus, we start monitoring the accelerometer.
      if (engine->accelerometerSensor != NULL) 
      {
        ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                       engine->accelerometerSensor);
        // We'd like to get 60 events per second (in us).
        ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                       engine->accelerometerSensor, (1000L/60)*1000);
      }
      break;
    case APP_CMD_LOST_FOCUS:
      // When our app loses focus, we stop monitoring the accelerometer.
      // This is to avoid consuming battery while not being used.
      if (engine->accelerometerSensor != NULL) 
      {
        ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                        engine->accelerometerSensor);
      }
      // Also stop animating.
      engine->animating = 1;
      engine_draw_frame(engine);
      break;
  }
}
Esempio n. 17
0
bool CIrrDeviceAndroid::activateAccelerometer(float updateInterval)
{
	if (!isAccelerometerAvailable())
		return false;

	ASensorEventQueue_enableSensor(SensorEventQueue, Accelerometer);
	ASensorEventQueue_setEventRate(SensorEventQueue, Accelerometer, (int32_t)(updateInterval*1000.f*1000.f)); // in microseconds

	os::Printer::log("Activated accelerometer", ELL_DEBUG);
	return true;
}
Esempio n. 18
0
bool CIrrDeviceAndroid::activateGyroscope(float updateInterval)
{
	if (!isGyroscopeAvailable())
		return false;

	ASensorEventQueue_enableSensor(SensorEventQueue, Gyroscope);
	ASensorEventQueue_setEventRate(SensorEventQueue, Gyroscope, (int32_t)(updateInterval*1000.f*1000.f)); // in microseconds

	os::Printer::log("Activated gyroscope", ELL_DEBUG);
	return true;
}
/***********************************************************************************
 * Process the next main command.
 **********************************************************************************/
static void engine_handle_cmd(struct android_app* app, int32_t cmd)
{

    struct engine* engine = (struct engine*)app->userData;
    switch (cmd)
    {
    case APP_CMD_SAVE_STATE:
        // The system has asked us to save our current state.  Do so.
        engine->app->savedState = malloc(sizeof(struct saved_state));
        *((struct saved_state*)engine->app->savedState) = engine->state;
        engine->app->savedStateSize = sizeof(struct saved_state);
        break;
    case APP_CMD_INIT_WINDOW:
        // The window is being shown, get it ready.


        LOGI("APP_CMD_INIT_WINDOW 11");
        if (engine->app->window != NULL)
        {

            engine_init_display(engine);

            //engine_draw_frame(engine);
        }
        break;
    case APP_CMD_TERM_WINDOW:
        LOGI("APP_CMD_TERM_WINDOW 11");
        // The window is being hidden or closed, clean it up.
        engine_term_display(engine);
        break;
    case APP_CMD_GAINED_FOCUS:
        // When our app gains focus, we start monitoring the accelerometer.
        if (engine->accelerometerSensor != NULL)
        {
            ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                           engine->accelerometerSensor);
            // We'd like to get 60 events per second (in us).
            ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                           engine->accelerometerSensor, (1000L/60)*1000);
        }
        break;
    case APP_CMD_LOST_FOCUS:
        // When our app loses focus, we stop monitoring the accelerometer.
        // This is to avoid consuming battery while not being used.
        if (engine->accelerometerSensor != NULL)
        {
            ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                            engine->accelerometerSensor);
        }
        //engine_draw_frame(engine);
        break;
    }
}
Esempio n. 20
0
void SensorHandler::InitializeHandler() {
    // Set and prepare looper
    SensorHandler::tsInstance().looper = ALooper_forThread();
    if (SensorHandler::tsInstance().looper == NULL) {
        SensorHandler::tsInstance().looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
    }

    SensorHandler::tsInstance().sensorManager = ASensorManager_getInstance();

    SensorHandler::tsInstance().accelerometerSensor = ASensorManager_getDefaultSensor(
        SensorHandler::tsInstance().sensorManager, ASENSOR_TYPE_ACCELEROMETER);
    SensorHandler::tsInstance().magneticSensor = ASensorManager_getDefaultSensor(
        SensorHandler::tsInstance().sensorManager, ASENSOR_TYPE_MAGNETIC_FIELD);
    SensorHandler::tsInstance().gyroscopeSensor = ASensorManager_getDefaultSensor(
        SensorHandler::tsInstance().sensorManager, ASENSOR_TYPE_GYROSCOPE);

    // Create event queue for sensor events
    SensorHandler::tsInstance().sensorEventQueue = ASensorManager_createEventQueue(
        SensorHandler::tsInstance().sensorManager, SensorHandler::tsInstance().looper,
        3, SensorCallback, sensor_data);

    ASensorEventQueue_enableSensor(SensorHandler::tsInstance().sensorEventQueue,
        SensorHandler::tsInstance().accelerometerSensor);
    ASensorEventQueue_enableSensor(SensorHandler::tsInstance().sensorEventQueue,
        SensorHandler::tsInstance().magneticSensor);
    ASensorEventQueue_enableSensor(SensorHandler::tsInstance().sensorEventQueue,
        SensorHandler::tsInstance().gyroscopeSensor);

    // Target sampling rate 100Hz
    int a = ASensor_getMinDelay(accelerometerSensor);
    int b = ASensor_getMinDelay(magneticSensor);
    int c = ASensor_getMinDelay(gyroscopeSensor);
    LOGI("Minimum delay; acceleration = %d  magnetic = %d  gyroscope = %d",a,b,c);
    ASensorEventQueue_setEventRate(SensorHandler::tsInstance().sensorEventQueue,
        SensorHandler::tsInstance().accelerometerSensor, 100000);
    ASensorEventQueue_setEventRate(SensorHandler::tsInstance().sensorEventQueue,
        SensorHandler::tsInstance().magneticSensor, 100000);
    ASensorEventQueue_setEventRate(SensorHandler::tsInstance().sensorEventQueue,
        SensorHandler::tsInstance().gyroscopeSensor, 100000);
}
Esempio n. 21
0
void enableAccelerometer(void) {
    LOGI("enableAccelerometer()");

    if (engine.accelerometerSensor != NULL) {
        ASensorEventQueue_enableSensor(engine.sensorEventQueue,
                                       engine.accelerometerSensor);

        // Set a default sample rate
        // We'd like to get 60 events per second (in us).
        ASensorEventQueue_setEventRate(engine.sensorEventQueue,
                                       engine.accelerometerSensor, (1000L/60)*1000);
    }
}
Esempio n. 22
0
// センサーからの値取得を許可する
void enable_sensors(struct engine* engine) {
    int i;

    for (i = 0; i < SENSOR_MAX; i++) {
        sensor_type* st = &engine->sensors[i];
        if (st->sensor != NULL) {
            ASensorEventQueue_enableSensor(engine->sensorEventQueue, /////-----(1)
                                           st->sensor);
            // 値を取得する周期を設定する
            ASensorEventQueue_setEventRate(engine->sensorEventQueue, /////-----(2)
                                           st->sensor, (1000L / 60) * 1000);
        }
    }
}
Esempio n. 23
0
/**
 * Process the next main command.
 */
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*)app->userData;
    switch (cmd) {
        case APP_CMD_SAVE_STATE:
            LOGI("from APP_CMD_SAVE_STATE\n");
            saveState(app);
            break;
        case APP_CMD_INIT_WINDOW:
            LOGI("from APP_CMD_INIT_WINDOW\n");
            // The window is being shown, get it ready.
            if (engine->app->window != NULL) {
                engine_init_display(engine);
                engine_init_shaders(engine);
                engine_draw_frame(engine);
            }
            break;
        case APP_CMD_TERM_WINDOW:
            LOGI("from APP_CMD_TERM_WINDOW\n");
            // The window is being hidden or closed, clean it up.
            cleanup(engine);
            break;
        case APP_CMD_GAINED_FOCUS:
            saveState(app);
#if 0
            // When our app gains focus, we start monitoring the accelerometer.
            if (engine->accelerometerSensor != NULL) {
                ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                        engine->accelerometerSensor);
                // We'd like to get 60 events per second (in us).
                ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                        engine->accelerometerSensor, (1000L/60)*1000);
            }
#endif
            LOGI("from APP_CMD_GAINED_FOCUS\n");
            break;
        case APP_CMD_LOST_FOCUS:
            LOGI("from APP_CMD_LOST_FOCUS\n");
            // When our app loses focus, we stop monitoring the accelerometer.
            // This is to avoid consuming battery while not being used.
            if (engine->accelerometerSensor != NULL) {
                ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                        engine->accelerometerSensor);
            }
            // Also stop animating.
            engine->animating = 0;
            engine_draw_frame(engine);
            break;
    }
}
Esempio n. 24
0
/**
 * Process the next main command.
 */
static void window_handle_cmd(struct android_app* app, int32_t cmd) {
	la_window_t* window = (la_window_t*)app->userData;
	switch (cmd) {
		case APP_CMD_SAVE_STATE:
			la_print("---------------------------- app save state");
			// The system has asked us to save our current state.  Do so.
//			window->app->savedState = malloc(sizeof(struct saved_state));
//			*((struct saved_state*)window->app->savedState) = NULL;//window->state;
//			window->app->savedStateSize = sizeof(struct saved_state);
			break;
		case APP_CMD_INIT_WINDOW:
			// The window is being shown, get it ready.
			if (window->app->window != NULL) {
				window_init_display(window);
				la_port_swap_buffers(window);
			}
			main(0, NULL);
			break;
		case APP_CMD_TERM_WINDOW:
			// The cross-platform window kill.
			la_window_kill__(window);
			// The window is being hidden or closed, clean it up.
			window_term_display(window);
			break;
		case APP_CMD_GAINED_FOCUS:
			// When our app gains focus, we start monitoring the accelerometer.
			if (window->accelerometerSensor != NULL) {
				ASensorEventQueue_enableSensor(window->sensorEventQueue,
						window->accelerometerSensor);
				// We'd like to get 60 events per second (in us).
				ASensorEventQueue_setEventRate(window->sensorEventQueue,
						window->accelerometerSensor, (1000L/60)*1000);
			}
			break;
		case APP_CMD_LOST_FOCUS:
			// When our app loses focus, we stop monitoring the accelerometer.
			// This is to avoid consuming battery while not being used.
			if (window->accelerometerSensor != NULL) {
				ASensorEventQueue_disableSensor(window->sensorEventQueue,
						window->accelerometerSensor);
			}
			la_port_swap_buffers(window);
			break;
	}
}
Esempio n. 25
0
void gdk_android_handle_glue_cmd(struct android_app* app, int32_t cmd)
{
    GError *err = NULL;
    //GTKAndroidInitPhase *engine = (GTKAndroidInitPhase *) app->userData;
    switch (cmd)
    {
        case APP_CMD_SAVE_STATE:
            // The system has asked us to save our current state.  Do so.
            app->savedState = NULL;
            app->savedStateSize = 0;
            break;
        case APP_CMD_INIT_WINDOW:
            // The window is being shown, get it ready.
            if (app->window != NULL && !gdk_android_init_display(app, &err))
            {
                g_error("%s", err->message);
                g_error_free(err);
            }
            break;
        case APP_CMD_TERM_WINDOW:
            if (gdk_android_stop)
                gdk_android_stop();
            break;
        case APP_CMD_PAUSE:
        case APP_CMD_STOP:
            if (gdk_android_stop)
                gdk_android_stop();
            break;
        case APP_CMD_GAINED_FOCUS:
            // When our app gains focus, we start monitoring the accelerometer.
            if (accelerometerSensor != NULL)
            {
                ASensorEventQueue_enableSensor(sensorEventQueue, accelerometerSensor);
                ASensorEventQueue_setEventRate(sensorEventQueue, accelerometerSensor, 0.5);
            }
            break;
        case APP_CMD_LOST_FOCUS:
            // When our app loses focus, we stop monitoring the accelerometer.
            // This is to avoid consuming battery while not being used.
            if (accelerometerSensor != NULL)
                ASensorEventQueue_disableSensor(sensorEventQueue, accelerometerSensor);
            break;

    }
}
Esempio n. 26
0
/**
*处理下一主命令。
*/
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*)app->userData;
    switch (cmd) {
        case APP_CMD_SAVE_STATE:
            //系统已经要求我们保存当前状态。就这样做。
            engine->app->savedState = malloc(sizeof(struct saved_state));
            *((struct saved_state*)engine->app->savedState) = engine->state;
            engine->app->savedStateSize = sizeof(struct saved_state);
            break;
        case APP_CMD_INIT_WINDOW:
            //正在显示窗口,让其准备就绪。
            if (engine->app->window != NULL) {
                engine_init_display(engine);
                engine_draw_frame(engine);
            }
            break;
        case APP_CMD_TERM_WINDOW:
            //正在隐藏或关闭窗口,请其进行清理。
            engine_term_display(engine);
            break;
        case APP_CMD_GAINED_FOCUS:
            //当我们的应用获得焦点时,我们开始监控加速计。
            if (engine->accelerometerSensor != NULL) {
                ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                               engine->accelerometerSensor);
                //我们想要每秒获得 60 个事件(在美国)。
                ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                               engine->accelerometerSensor, (1000L / 60) * 1000);
            }
            break;
        case APP_CMD_LOST_FOCUS:
            //当我们的应用程序失去焦点时,我们会停止监控加速计。
            //这可在不使用时避免使用电池。
            if (engine->accelerometerSensor != NULL) {
                ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                                engine->accelerometerSensor);
            }
            //另外,停止动画。
            engine->animating = 0;
            engine_draw_frame(engine);
            break;
    }
}
Esempio n. 27
0
bool SensorImpl::open(Sensor::Type sensor)
{
    // Get the default sensor matching the type
    m_sensor = getDefaultSensor(sensor);

    // Sensor not available, stop here
    if (!m_sensor)
        return false;

    // Get the minimum delay allowed between events
    Time minimumDelay = microseconds(ASensor_getMinDelay(m_sensor));

    // Set the event rate (not to consume too much battery)
    ASensorEventQueue_setEventRate(sensorEventQueue, m_sensor, minimumDelay.asMicroseconds());

    // Save the index of the sensor
    m_index = static_cast<unsigned int>(sensor);

    return true;
}
Esempio n. 28
0
static void engine_handle_command(struct android_app* app, int32_t cmd)
{
	struct engine* engine = (struct engine*)app->userData;
	switch (cmd)
	{
		case APP_CMD_SAVE_STATE:
			app_save_state_callback();
			break;

		case APP_CMD_INIT_WINDOW:
			if (engine->app->window != nullptr)
			{
				engine_init_display(engine);
				engine_draw_frame(engine);
			}
			break;

		case APP_CMD_TERM_WINDOW:
			engine_term_display(engine);
			break;

		case APP_CMD_GAINED_FOCUS:
			if (engine->accelerometerSensor != nullptr)
			{
				ASensorEventQueue_enableSensor(engine->sensorEventQueue, engine->accelerometerSensor);
				ASensorEventQueue_setEventRate(engine->sensorEventQueue, engine->accelerometerSensor, (1000L/60)*1000);

				accelerometer_enable_callback();
			}
			break;

		case APP_CMD_LOST_FOCUS:
			if (engine->accelerometerSensor != nullptr)
			{
				ASensorEventQueue_disableSensor(engine->sensorEventQueue, engine->accelerometerSensor);
				accelerometer_disable_callback();
			}
			engine_draw_frame(engine);
			break;
	}
}
Esempio n. 29
0
    void init(AAssetManager *assetManager) {
        AAsset *vertexShaderAsset = AAssetManager_open(assetManager, "shader.glslv",
                                                       AASSET_MODE_BUFFER);
        assert(vertexShaderAsset != NULL);
        const void *vertexShaderBuf = AAsset_getBuffer(vertexShaderAsset);
        assert(vertexShaderBuf != NULL);
        off_t vertexShaderLength = AAsset_getLength(vertexShaderAsset);
        vertexShaderSource = std::string((const char*)vertexShaderBuf,
                                         (size_t)vertexShaderLength);
        AAsset_close(vertexShaderAsset);

        AAsset *fragmentShaderAsset = AAssetManager_open(assetManager, "shader.glslf",
                                                         AASSET_MODE_BUFFER);
        assert(fragmentShaderAsset != NULL);
        const void *fragmentShaderBuf = AAsset_getBuffer(fragmentShaderAsset);
        assert(fragmentShaderBuf != NULL);
        off_t fragmentShaderLength = AAsset_getLength(fragmentShaderAsset);
        fragmentShaderSource = std::string((const char*)fragmentShaderBuf,
                                           (size_t)fragmentShaderLength);
        AAsset_close(fragmentShaderAsset);

        sensorManager = AcquireASensorManagerInstance();
        assert(sensorManager != NULL);
        accelerometer = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
        assert(accelerometer != NULL);
        looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
        assert(looper != NULL);
        accelerometerEventQueue = ASensorManager_createEventQueue(sensorManager, looper,
                                                                  LOOPER_ID_USER, NULL, NULL);
        assert(accelerometerEventQueue != NULL);
        auto status = ASensorEventQueue_enableSensor(accelerometerEventQueue,
                                                     accelerometer);
        assert(status >= 0);
        status = ASensorEventQueue_setEventRate(accelerometerEventQueue,
                                                accelerometer,
                                                SENSOR_REFRESH_PERIOD_US);
        assert(status >= 0);
        (void)status;   //to silent unused compiler warning

        generateXPos();
    }
Esempio n. 30
0
    void SensorImpl::setEnabled(const bool enabled)
    {
        if (enabled == isEnabled() || !isAvailable())
            return;

        if (enabled)
        {
            if (ASensorEventQueue_enableSensor(ns_sensorEventQueue, getDefault(m_type)) >= 0)
            {
                m_sensor = getDefault(m_type);

                if (auto rate = ASensor_getMinDelay(m_sensor))
                    ASensorEventQueue_setEventRate(ns_sensorEventQueue, m_sensor, rate);
            }
        }
        else
        {
            ASensorEventQueue_disableSensor(ns_sensorEventQueue, m_sensor);
            m_sensor = NULL;
        }
    }