static void updateSensorsAccelerometer(void* data, int ident, int events){ if (ident == LOOPER_ID_USER) { if (sensors.accelerometerSensor) { ASensorEvent event; while (ASensorEventQueue_getEvents(sensors.sensorEventQueue,&event, 1) > 0) { if(accelerometerEvent) accelerometerEvent(data, event.acceleration.x, event.acceleration.y, event.acceleration.z, event.acceleration.azimuth, event.acceleration.pitch, event.acceleration.roll); } } } }
static gboolean gdk_android_event_check(GSource *source) { gboolean retval; struct android_app *app = _gdk_display->app; int astate = app->activityState; gdk_threads_enter(); retval = _gdk_event_queue_find_first(GDK_DISPLAY(_gdk_display)) != NULL; if (!retval && astate != APP_CMD_PAUSE && astate != APP_CMD_STOP) { int ident, events; struct android_poll_source *asource; // Read all pending events. while ((ident = ALooper_pollAll(0, NULL, &events, (void**) &asource)) >= 0) { // Process this event. if (asource != NULL) asource->process(app, asource); if (ident == LOOPER_ID_USER) { if (sensorEventQueue != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) { g_debug("sensor event received"); } } } if (app->destroyRequested != 0 && gdk_android_stop) { gdk_android_stop(); break; } } } gdk_threads_leave(); return retval; }
int accelerometerCallback(int fd, int events, void* data) { struct engine_t* userData = (struct engine_t*) data; ASensorEvent event[NB_SENSOR_EVENTS]; int i, n; while ((n = ASensorEventQueue_getEvents(userData->accelerometerEventQueue, event, NB_SENSOR_EVENTS)) > 0) { for (i = 0; i < n; ++i) { ASensorVector* vector = &event[i].vector; if (event[i].type == ASENSOR_TYPE_ACCELEROMETER) { std::stringstream ss; // ss << std::setprecision(16) // << (double) event[i].timestamp / 1000000; ss << vector->x << "\t" << vector->y << "\t" << vector->z; accelerometerLogger.save(ss.str()); } } } return 1; }
int gyroscopeCallback(int fd, int events, void* data) { struct engine_t* userData = (struct engine_t*) data; ASensorEvent event[NB_SENSOR_EVENTS]; int i, n; while ((n = ASensorEventQueue_getEvents(userData->gyroscopeEventQueue, event, NB_SENSOR_EVENTS)) > 0) { for (i = 0; i < n; ++i) { ASensorVector* vector = &event[i].vector; if (event[i].type == ASENSOR_TYPE_GYROSCOPE) { std::stringstream ss; ss << std::setprecision(32) << (double) event[i].timestamp / 1000000 << "\t"; ss << vector->x << "\t" << vector->y << "\t" << vector->z; gyroscopeLogger.save(ss.str()); } } } return 1; }
static int get_sensorevents(int fd, int events, void* data) { //LOGI(" ********** inside get_sensor_events"); ASensorEvent event; //ASensorEventQueue* sensorEventQueue; while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) { if (event.type == ASENSOR_TYPE_ACCELEROMETER) { //LOGI("accl(x,y,z,t): %f %f %f %lld", event.acceleration.x, event.acceleration.y, event.acceleration.z, event.timestamp); acceleration_x = event.acceleration.x; acceleration_y = event.acceleration.y; acceleration_z = event.acceleration.z; if (accCounter == 0 || accCounter == 1000) { LOGI("Acc-Time: %lld (%f)", event.timestamp, ((double)(event.timestamp-lastAccTime))/1000000000.0); lastAccTime = event.timestamp; accCounter = 0; } accCounter++; } else if (event.type == ASENSOR_TYPE_GYROSCOPE) { //LOGI("gyro(x,y,z,t): %f %f %f %lld", event.acceleration.x, event.acceleration.y, event.acceleration.z, event.timestamp); if (gyroCounter == 0 || gyroCounter == 1000) { LOGI("Gyro-Time: %lld (%f)", event.timestamp, ((double)(event.timestamp-lastGyroTime))/1000000000.0); lastGyroTime = event.timestamp; gyroCounter = 0; } gyroCounter++; } else if (event.type == ASENSOR_TYPE_MAGNETIC_FIELD) { //LOGI("accl(x,y,z,t): %f %f %f %lld", event.acceleration.x, event.acceleration.y, event.acceleration.z, event.timestamp); if (magCounter == 0 || magCounter == 1000) { LOGI("Mag-Time: %lld (%f)", event.timestamp, ((double)(event.timestamp-lastMagTime))/1000000000.0); lastMagTime = event.timestamp; magCounter = 0; } magCounter++; } } //should return 1 to continue receiving callbacks, or 0 to unregister return SENSORS_ENABLED; }
int Window::mainloop() { if( m_silent ) { finalize(); return 0; } while( true ) { int ident; int events; android_poll_source* source; while( ( ident = ALooper_pollAll( 0, 0, &events, ( void** )&source ) ) >= 0 ) { if( source ) { source->process( m_app, source ); } if( ident == LOOPER_ID_USER ) { if( m_accelerometerSensor ) { ASensorEvent event; while( ASensorEventQueue_getEvents( m_sensorEventQueue, &event, 1 ) > 0 ) { handle_sensor( &event ); } } } if( m_finished.load( std::memory_order_relaxed ) || m_app->destroyRequested ) { finalize(); return 0; } } paint(); } }
int GoAndroid_readQueue(int n, int32_t* types, int64_t* timestamps, float* vectors) { int id; int events; ASensorEvent event; int i = 0; // Try n times read from the event queue. // If anytime timeout occurs, don't retry to read and immediately return. // Consume the event queue entirely between polls. while (i < n && (id = ALooper_pollAll(-1, NULL, &events, NULL)) >= 0) { if (id != GO_ANDROID_SENSOR_LOOPER_ID) { continue; } while (i < n && ASensorEventQueue_getEvents(queue, &event, 1)) { types[i] = event.type; timestamps[i] = event.timestamp; vectors[i*3] = event.vector.x; vectors[i*3+1] = event.vector.y; vectors[i*3+2] = event.vector.z; i++; } } return i; }
// SensorCallback for accessing sensor data static int SensorCallback(int fd, int events, void *data) { // Write callback confirmation to logcat for debugging LOGI("SensorCallback"); ASensorEvent event; while (ASensorEventQueue_getEvents(SensorHandler::tsInstance().sensorEventQueue, &event, 1) > 0) { if(event.type == ASENSOR_TYPE_ACCELEROMETER) { /*LOGI("Accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);*/ //data_out::DataOut::doInstance().WriteAcceleration(event); server_out::ServerIO::csIO().PostAcceleration(event); } else if (event.type == ASENSOR_TYPE_MAGNETIC_FIELD) { /*LOGI("Magnetic field: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);*/ //data_out::DataOut::doInstance().WriteMagnetic(event); } else if (event.type ==ASENSOR_TYPE_GYROSCOPE) { /*LOGI("Gyroscope: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);*/ //data_out::DataOut::doInstance().WriteGyroscope(event); } } return 1; }
void SensorCollector::ProcessSensorEvents() { int result = -1; ASensorEvent sensorEvent; //Assume this is all gyro for now while (ASensorEventQueue_getEvents(sensorQueue,&sensorEvent, 1) > 0 ) { LOGV(LOGTAG_SENSOR,"Processing sensor event"); //First time reading an event, store the timestamp and go to the next one if (lastTimestamp == 0) { lastTimestamp = sensorEvent.timestamp; rotationVector = cv::Mat::eye(4,4,CV_32F); continue; } if (resetNextTime) { rotationVector = cv::Mat::eye(4,4,CV_32F); resetNextTime = false; } double deltaT = (sensorEvent.timestamp - lastTimestamp) / 1000000000.0; float axisX = -sensorEvent.data[1]; float axisY = -sensorEvent.data[0]; float axisZ = -sensorEvent.data[2]; // Calculate the angular speed of the sample float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); LOGD(LOGTAG_SENSOR,"OmegaMagnitue=%f",omegaMagnitude); // Normalize the rotation vector if it's big enough to get the axis if (omegaMagnitude > .001f) { axisX /= omegaMagnitude; axisY /= omegaMagnitude; axisZ /= omegaMagnitude; } // Integrate around this axis with the angular speed by the timestep // in order to get a delta rotation from this sample over the timestep // We will convert this axis-angle representation of the delta rotation // into a quaternion before turning it into the rotation matrix. float thetaOverTwo = omegaMagnitude * deltaT / 2.0f; float sinThetaOverTwo = sin(thetaOverTwo); float cosThetaOverTwo = cos(thetaOverTwo); float deltaRotationVector[] = {0,0,0,0}; deltaRotationVector[0] = sinThetaOverTwo * axisX; deltaRotationVector[1] = sinThetaOverTwo * axisY; deltaRotationVector[2] = sinThetaOverTwo * axisZ; deltaRotationVector[3] = cosThetaOverTwo; /* double deltaT = (sensorEvent.timestamp - lastTimestamp) / 1000000000.0; double data[] = {sensorEvent.data[0],sensorEvent.data[1],sensorEvent.data[2]}; cv::Mat deltaVector = cv::Mat(3,1,CV_64F,data); deltaVector *= deltaT; rotationVector += deltaVector;*/ lastTimestamp = sensorEvent.timestamp; LOGD(LOGTAG_SENSOR,"Creating rotation matrix from vector (%f,%f,%f,%f)",deltaRotationVector[0],deltaRotationVector[1],deltaRotationVector[2],deltaRotationVector[3]); cv::Mat deltaMat = getRotationMatrixFromVector(deltaRotationVector); rotationVector *= deltaMat; } }
void android_main(struct android_app* state) { app_dummy(); //sleep(5); // Sleep a little so GDB can attach itself pthread_key_create(&s_thread_key, detach_current_thread); JNIEnv* env; state->activity->vm->AttachCurrentThread(&env, nullptr); pthread_setspecific(s_thread_key, state->activity->vm); AInstance ainstance; state->userData = &ainstance; state->onAppCmd = android_handle_event; state->onInputEvent = android_handle_input; ainstance.app = state; g_native_activity = state->activity; Path::set_current(); // Prepare to monitor accelerometer ainstance.sensorManager = ASensorManager_getInstance(); ainstance.accelerometerSensor = ASensorManager_getDefaultSensor( ainstance.sensorManager, ASENSOR_TYPE_ACCELEROMETER); if (ainstance.accelerometerSensor) { ainstance.sensorEventQueue = ASensorManager_createEventQueue( ainstance.sensorManager, state->looper, LOOPER_ID_USER, nullptr, nullptr); } Chrono chrono; while (!ainstance.done) { int ident; int events; struct android_poll_source* source; while ((ident = ALooper_pollAll( (!ainstance.active ? -1 : 0), nullptr, &events, reinterpret_cast<void**>(&source))) >= 0) { if (source) source->process(state, source); if (ainstance.done) break; // If a sensor has data, process it now. if (ainstance.active && ident == LOOPER_ID_USER && ainstance.accelerometerSensor) { ASensorEvent event; while (ASensorEventQueue_getEvents( ainstance.sensorEventQueue, &event, 1) > 0) { ainstance.director->input().accelerated( event.acceleration.x, event.acceleration.y, event.acceleration.z, event.timestamp); } } } chrono.update(); if (!(ainstance.initialised & ainstance.active)) continue; ainstance.director->update(chrono.delta()); ainstance.director->draw(); eglSwapBuffers(ainstance.display, ainstance.surface); } android_destroy_display(&ainstance); }
int defaultExecuteApplication() { struct android_app *vid = main_view_id; if (is_valid(vid) == false) return Failed; while (true) { int ident; int events; struct android_poll_source *source; // dispatch events from sensors while ((ident = ALooper_pollAll(!suspended ? 0 : -1, NULL, &events, (void **)&source)) >= 0) { if (source != NULL) { source->process(vid, source); } // process, exist data from sensors if (ident == LOOPER_ID_USER) { if (sensor_accelerometer != NULL) { ASensorEvent sensorEvent; while (ASensorEventQueue_getEvents(sensor_event_queue, &sensorEvent, 1) > 0) { //LOGI("accelerometer: x = %f, y = %f, z = %f", sensorEvent.acceleration.x, sensorEvent.acceleration.y, sensorEvent.acceleration.z); } } } // check of requesting destroy if (vid->destroyRequested != 0) { return Success; } } // application update and renderer update here if (!suspended) { type::default_callback_t applicationUpdate = gy::getCallback(gy::type::__cb::A_APPLICATION_UPDATER); if (applicationUpdate != NULL) { if (applicationUpdate() == Failed) return Failed; } else { assert(false); return Failed; } type::default_callback_t rendererUpdate = gy::getCallback(gy::type::__cb::A_RENDERER_UPDATER); if (rendererUpdate != NULL) rendererUpdate(); } } // while (true) return Failed; }
/** * 这是使用 android_native_app_glue * 的本地应用程序的主要入口点。它在其自己的线程中运行,具有自己的 * 事件循环用于接收输入事件并执行其他操作。 */ void android_main(struct android_app* state) { struct engine engine; memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; //准备监控加速器 engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); if (state->savedState != NULL) { //我们从之前保存的状态开始;从它还原。 engine.state = *(struct saved_state*)state->savedState; } engine.animating = 1; //循环等待事情以进行处理。 while (1) { //读取所有挂起的事件。 int ident; int events; struct android_poll_source* source; //如果没有动态效果,我们将一直阻止等待事件。 //如果有动态效果,我们进行循环,直到读取所有事件,然后继续 //绘制动画的下一帧。 while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { //处理此事件。 if (source != NULL) { source->process(state, source); } //如果传感器有数据,立即处理。 if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { // LOGI("accelerometer: x=%f y=%f z=%f", // event.acceleration.x, event.acceleration.y, // event.acceleration.z); } } } //检查,我们是否存在。 if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { //事件完成;绘制下一动画帧。 engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } //绘图被降低到屏幕更新速率, //因此,没有必要在此处计时。 engine_draw_frame(&engine); } } }
// Main関数 void android_main(struct android_app* state) { struct engine engine; // glueが削除されないように app_dummy(); // アプリ情報保存エリアの確保 memset(&engine, 0, sizeof(engine)); // ユーザーデータの配置 state->userData = &engine; // アプリケーションコマンド処理関数の設定 state->onAppCmd = engine_handle_cmd; // 入力イベント処理関数の設定 state->onInputEvent = engine_handle_input; engine.app = state; // センサーからのデータ取得に必要な初期化 engine.sensorManager = ASensorManager_getInstance(); // 加速度センサーのデータ取得準備 engine.accelerometerSensor = ASensorManager_getDefaultSensor( engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); // ジャイロスコープのデータ取得準備 engine.gyroscopeSensor = ASensorManager_getDefaultSensor( engine.sensorManager, ASENSOR_TYPE_GYROSCOPE ); // センサー情報取得キューの新規作成 engine.sensorEventQueue = ASensorManager_createEventQueue( engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); // AssetManagerの取得 engine.assetManager = state->activity->assetManager; if (state->savedState != NULL) { // 以前の状態に戻す engine.state = *(struct saved_state*) state->savedState; } while (1) { int ident; int events; struct android_poll_source* source; // アプリケーションの状態にあわせてセンサー情報の処理を行う while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**) &source)) >= 0) { // 内部イベントを処理する if (source != NULL) { source->process(state, source); } // センサー情報取得キューのデータを処理する if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL && engine.gyroscopeSensor != NULL) { ASensorEvent event[2]; int count; int i; while ((count = ASensorEventQueue_getEvents( engine.sensorEventQueue, event, 2)) > 0) { for (i = 0; i < count; i++){ switch(event[i].type){ case ASENSOR_TYPE_ACCELEROMETER: // 加速度センサーの値を出力する LOGI("accelerometer: x=%f y=%f z=%f", event[i].acceleration.x, event[i].acceleration.y, event[i].acceleration.z); break; case ASENSOR_TYPE_GYROSCOPE: // ジャイロスコープの値を出力する LOGI("GYROSCOPE: x=%f y=%f z=%f",event[i].vector.azimuth,event[i].vector.pitch,event[i].vector.roll ); break; } } } } } // EGL情報を破棄する if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { // 次のフレームを描画するのに必要な処理を行う int i = 0,j; engine.angle[0] += 3; engine.angle[1] += 1; for (j = 0; j < 3; j++){ if (engine.angle[j] > 360) engine.angle[j] -= 360; if (engine.angle[j] < 0) engine.angle[j] += 360; } // 画面描画 engine_draw_frame(&engine); } } }
// Main関数 void android_main(struct android_app* state) { struct engine engine; // glueが削除されないように app_dummy(); memset(&engine, 0, sizeof(engine)); // ユーザーデータの配置 state->userData = &engine; // アプリケーションコマンド処理関数の設定 state->onAppCmd = engine_handle_cmd; // 入力イベント処理関数の設定 state->onInputEvent = engine_handle_input; engine.app = state; // センサーの初期化を行う init_sensors(&engine, state); if (state->savedState != NULL) { // 以前の状態に戻す engine.state = *(struct saved_state*) state->savedState; } // Configurationを表示 displayConfiguration(&engine); engine.animating = 1; while (1) { int ident; int events; struct android_poll_source* source; // アプリケーションの状態にあわせてセンサー情報の処理を行う while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**) &source)) >= 0) { /////-----(1) // 内部イベントを処理する if (source != NULL) { source->process(state, source); } // センサー情報取得キューのデータを処理する if (ident == LOOPER_ID_USER) {/////-----(2) ASensorEvent event[1]; int count, i, j; while ((count = ASensorEventQueue_getEvents(engine.sensorEventQueue, event, 1)) > 0) { for (i = 0; i < count; i++) { for (j = 0; j < SENSOR_MAX; j++) { if ((engine.sensors[j].sensor != NULL) && (engine.sensors[j].type == event[i].type)) { engine.sensors[j].value = event[i].vector; /////-----(3) } } } } } // EGL情報を破棄する if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } // 画面の描画 engine_draw_frame(&engine); } }
/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ void android_main(struct android_app* state) { la_window_t* window = la_memory_allocate(sizeof(la_window_t)); int ident; int events; struct android_poll_source* source; // Make sure glue isn't stripped. app_dummy(); state->userData = window; state->onAppCmd = window_handle_cmd; state->onInputEvent = window_handle_input; window->app = state; // Prepare to monitor accelerometer window->sensorManager = ASensorManager_getInstance(); window->accelerometerSensor = ASensorManager_getDefaultSensor( window->sensorManager, ASENSOR_TYPE_ACCELEROMETER); window->sensorEventQueue = ASensorManager_createEventQueue( window->sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); // if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. // window->state = *(struct saved_state*)state->savedState; // } // Run main(): la_window = window; // TODO: is needed? SDL_AtomicSet(&la_rmcexit, 1); // Window thread ( Drawing + Events ). while (SDL_AtomicGet(&la_rmcexit)) { // Poll Events ident = ALooper_pollAll(0, NULL, &events, (void**)&source); // Process this event. if (source != NULL) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (window->accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents( window->sensorEventQueue, &event, 1) > 0) { window->input.accel.x = event.acceleration.x; window->input.accel.y = event.acceleration.y; window->input.accel.z = event.acceleration.z; } } } // Run the cross-platform window loop. if(window->context) la_window_loop__(window); // Update the screen. la_port_swap_buffers(window); } la_print("port-android quitting...."); // The cross-platform window kill. la_window_kill__(window); // The window is being hidden or closed, clean it up. window_term_display(window); la_print("port-android quitted...."); exit(0); return; }
/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ void android_main(struct android_app* state) { // Make sure glue isn't stripped. app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; // Prepare to monitor accelerometer engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. engine.state = *(struct saved_state*)state->savedState; } // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { LOG_EVENTS_DEBUG("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); AConfiguration* _currentconf = AConfiguration_new(); AConfiguration_fromAssetManager(_currentconf, state->activity->assetManager); static int32_t _orientation = AConfiguration_getOrientation(_currentconf); if (ACONFIGURATION_ORIENTATION_LAND != _orientation) { // ACONFIGURATION_ORIENTATION_ANY // ACONFIGURATION_ORIENTATION_PORT // ACONFIGURATION_ORIENTATION_SQUARE cocos2d::Acceleration acc; acc.x = -event.acceleration.x/10; acc.y = -event.acceleration.y/10; acc.z = event.acceleration.z/10; acc.timestamp = 0; cocos2d::EventAcceleration accEvent(acc); cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); } else { // ACONFIGURATION_ORIENTATION_LAND // swap x and y parameters cocos2d::Acceleration acc; acc.x = event.acceleration.y/10; acc.y = -event.acceleration.x/10; acc.z = event.acceleration.z/10; acc.timestamp = 0; cocos2d::EventAcceleration accEvent(acc); cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); } } } } // Check if we are exiting. if (state->destroyRequested != 0) { engine_term_display(&engine); memset(&engine, 0, sizeof(engine)); s_methodInitialized = false; return; } } if (engine.animating) { // Done with events; draw next animation frame. engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } // Drawing is throttled to the screen update rate, so there // is no need to do timing here. LOG_RENDER_DEBUG("android_main : engine.animating"); engine_draw_frame(&engine); } else { LOG_RENDER_DEBUG("android_main : !engine.animating"); } } }
/** * アプリケーション開始 */ void android_main(struct android_app* state) { struct engine engine; // glueが削除されないように app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; // センサーからのデータ取得に必要な初期化 engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor( engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue( engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); if (state->savedState != NULL) { // 以前の状態に戻す engine.state = *(struct saved_state*) state->savedState; } while (1) { int ident; int events; struct android_poll_source* source; // アプリケーションが動作することになれば、これらセンサーの制御を行う while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**) &source)) >= 0) { // イベントを処理する if (source != NULL) { source->process(state, source); } // センサーに何かしらのデータが存在したら処理する if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents( engine.sensorEventQueue, &event, 1) > 0) { LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); } } } // 破棄要求があったか if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { // 次のフレームを描画するのに必要な処理を行う engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } // 画面描画 engine_draw_frame(&engine); } } }
/* * Class: io_quadroid_ndk_QuadroidLib * Method: get_sensor_events * Description: callback function for the sensor manager */ static int get_sensor_events(int fd, int events, void* data) { /* typedef struct ASensorVector { union { float v[3]; struct { float x; float y; float z; } struct { float azimuth; float pitch; float roll; }; }; int8_t status; uint8_t reserved[3]; } ASensorVector; typedef struct ASensorEvent { int32_t version; sizeof(struct ASensorEvent) int32_t sensor; int32_t type; int32_t reserved0; int64_t timestamp; union { float data[16]; ASensorVector vector; ASensorVector acceleration; ASensorVector magnetic; float temperature; float distance; float light; float pressure; }; int32_t reserved1[4]; } ASensorEvent; */ ASensorEvent event; while (ASensorEventQueue_getEvents(queue, &event, 1) > 0) { switch (event.type) { case 6: // PRESSURE pressure = event.pressure; if(DEBUG==1) { LOGI("pressure: %f", pressure); } break; case ASENSOR_TYPE_ACCELEROMETER: acce[0] = event.acceleration.roll; acce[1] = event.acceleration.pitch; acce[2] = event.acceleration.azimuth; acceRaw[0] = event.acceleration.x; acceRaw[1] = event.acceleration.y; acceRaw[2] = event.acceleration.z; acceRaw[1] *= -1.0f; if(DEBUG==1) { LOGI( "accl(roll,pitch,azimuth,x,y,z,t): %f %f %f %f %f %f %lld", acce[0], acce[1], acce[2], acceRaw[0], acceRaw[1], acceRaw[2], event.timestamp ); } break; case ASENSOR_TYPE_GYROSCOPE: gyro[0] = event.vector.roll; gyro[1] = event.vector.pitch; gyro[2] = event.vector.azimuth; gyroRaw[0] = event.vector.x; gyroRaw[1] = event.vector.y; gyroRaw[2] = event.vector.z; if(DEBUG==1) { LOGI( "gyro(roll,pitch,azimuth,x,y,z,t): %f %f %f %f %f %f %lld", gyro[0], gyro[1], gyro[2], gyroRaw[0], gyroRaw[1], gyroRaw[2], event.timestamp ); } break; case ASENSOR_TYPE_MAGNETIC_FIELD: magn[0] = event.magnetic.roll; magn[1] = event.magnetic.pitch; magn[2] = event.magnetic.azimuth; magnRaw[0] = event.magnetic.x; magnRaw[1] = event.magnetic.y; magnRaw[2] = event.magnetic.z; if(DEBUG==1) { LOGI( "magn(roll,pitch,azimuth,x,y,z,t): %f %f %f %f %f %f %lld", magn[0], magn[1], magn[2], magnRaw[0], magnRaw[1], magnRaw[2], event.timestamp ); } break; case ASENSOR_TYPE_PROXIMITY: altitude = event.distance; break; } // Logging if(LOG==1) { // ASENSOR_TYPE_ACCELEROMETER if(event.type==ASENSOR_TYPE_ACCELEROMETER && USE_ACCELEROMETER==1) { if(log_i<(LOG_SIZE-1)) { loog[log_i] = event.timestamp; log_i++; } else { if(log_output==0) { for(log_j=1; log_j<LOG_SIZE; log_j++) { LOGI("#id: %d: delta acce(t): %f", log_j, (loog[log_j]-loog[log_j-1])/1000000.0f); } LOGI("SOLL: acce: %d, acce: %d, magn: %d", log_acce, log_gyro, log_magn); log_output = 1; } break; } } // ASENSOR_TYPE_GYROSCOPE if(event.type==ASENSOR_TYPE_GYROSCOPE && USE_GYROSCOPE==1) { if(log_i<(LOG_SIZE-1)) { loog[log_i] = event.timestamp; log_i++; } else { if(log_output==0) { for(log_j=1; log_j<LOG_SIZE; log_j++) { LOGI("#id: %d: delta gyro(t): %f", log_j, (loog[log_j]-loog[log_j-1])/1000000.0f); } LOGI("SOLL: acce: %d, gyro: %d, magn: %d", log_acce, log_gyro, log_magn); log_output = 1; } break; } } // ASENSOR_TYPE_MAGNETIC_FIELD if(event.type==ASENSOR_TYPE_MAGNETIC_FIELD && USE_MAGNETIC==1) { if(log_i<(LOG_SIZE-1)) { loog[log_i] = event.timestamp; log_i++; } else { if(log_output==0) { for(log_j=1; log_j<LOG_SIZE; log_j++) { LOGI("#id: %d: delta magn(t): %f", log_j, (loog[log_j]-loog[log_j-1])/1000000.0f); } LOGI("SOLL: acce: %d, gyro: %d, magn: %d", log_acce, log_gyro, log_magn); log_output = 1; } break; } } } } timestamp = event.timestamp; sendValues(); return 1; }
static orxSTATUS orxFASTCALL orxJoystick_Android_AccelerometerEventHandler(const orxEVENT *_pstEvent) { orxSTATUS eResult = orxSTATUS_SUCCESS; if(_pstEvent->eType == orxANDROID_EVENT_TYPE_SURFACE && _pstEvent->eID == orxANDROID_EVENT_SURFACE_CHANGED) { /* reset rotation */ sstJoystick.s32ScreenRotation = -1; } if(_pstEvent->eType == orxANDROID_EVENT_TYPE_ACCELERATE) { static float in[3]; static float out[3]; ASensorEvent event; if(sstJoystick.s32ScreenRotation == -1) { sstJoystick.s32ScreenRotation = orxAndroid_JNI_GetRotation(); } while (ASensorEventQueue_getEvents(sstJoystick.sensorEventQueue, &event, 1) > 0) { in[0] = event.acceleration.x; in[1] = event.acceleration.y; in[2] = event.acceleration.z; canonicalToScreen(sstJoystick.s32ScreenRotation, in, out); /* Gets new acceleration */ orxVector_Set(&(sstJoystick.vAcceleration), out[0], out[1], out[2]); } } if(_pstEvent->eType == orxEVENT_TYPE_SYSTEM) { switch(_pstEvent->eID) { case orxSYSTEM_EVENT_FOCUS_GAINED: { enableSensorManager(); break; } case orxSYSTEM_EVENT_FOCUS_LOST: { disableSensorManager(); break; } default: { break; } } } /* Done! */ return eResult; }
void android_main(struct android_app* app) { // Make sure glue isn't stripped app_dummy(); // Create application manager AppManager manager; app->userData = &manager; app->onAppCmd = engine_handle_cmd; app->onInputEvent = engine_handle_input; // Create x2d engine X2DEngine *engine = CreateEngine(0); engine->app = new AndroidApp(&manager); engine->gfx = new AndroidRender(&manager); engine->sfx = new AndroidSound; engine->debug = new AndroidDebug; engine->assetLoader = new AndroidAssetLoader(&manager); // NOTE: engine->init() can be found in engine_handle_cmd manager.android = app; manager.engine = engine; manager.display = NULL; // Prepare to monitor accelerometer manager.sensorManager = ASensorManager_getInstance(); manager.accelerometerSensor = ASensorManager_getDefaultSensor(manager.sensorManager, ASENSOR_TYPE_ACCELEROMETER); manager.sensorEventQueue = ASensorManager_createEventQueue(manager.sensorManager, app->looper, LOOPER_ID_USER, NULL, NULL); if(app->savedState != NULL) { // We are starting from a previous saved state; restore from it. manager.state = *(SaveState*)app->savedState; } // Loop waiting for stuff to do. while(true) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. while((ident = ALooper_pollAll(manager.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) source->process(app, source); // If a sensor has data, process it now. if(ident == LOOPER_ID_USER) { if(manager.accelerometerSensor != NULL) { ASensorEvent event; while(ASensorEventQueue_getEvents(manager.sensorEventQueue, &event, 1) > 0) { iosystem::print("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); } } } // Check if we are exiting. if(app->destroyRequested != 0) { engine_term_display(&manager); return; } } if(manager.display != NULL && manager.animating) { // Drawing is throttled to the screen update rate, so there // is no need to do timing here. //engine_draw_frame(&manager); engine->draw(); } } }
/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ void android_main(struct android_app* state) { struct engine engine; // Make sure glue isn't stripped. app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; // Prepare to monitor accelerometer engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. engine.state = *(struct saved_state*)state->savedState; } else { JNIEnv *jni = state->activity->env; state->activity->vm->AttachCurrentThread(&jni, NULL); jclass activityClass = jni->FindClass("android/app/NativeActivity"); jmethodID getClassLoader = jni->GetMethodID(activityClass,"getClassLoader", "()Ljava/lang/ClassLoader;"); jobject cls = jni->CallObjectMethod(state->activity->clazz, getClassLoader); jclass classLoader = jni->FindClass("java/lang/ClassLoader"); jmethodID findClass = jni->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); jstring strClassName = jni->NewStringUTF("com/oriku/Bridge"); jclass j_bridge = (jclass)jni->CallObjectMethod(cls, findClass, strClassName); jmethodID j_initOuya = jni->GetStaticMethodID(j_bridge, "initOuya","(Landroid/app/Activity;)V"); jni->CallStaticVoidMethod(j_bridge, j_initOuya, state->activity->clazz); // Finished with the JVM. state->activity->vm->DetachCurrentThread(); } // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); } } } // Check if we are exiting. if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { // Done with events; draw next animation frame. engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } // Drawing is throttled to the screen update rate, so there // is no need to do timing here. engine_draw_frame(&engine); } } }
/******************************************************************** * This is the main entry point of a native application *that is using android_native_app_glue. It runs in *its own thread, with its own event loop for receiving *input events and doing other things. *******************************************************************/ void android_main(struct android_app* state) { // mgr = state->activity->assetManager; struct engine engine; engine.display = NULL; // Make sure glue isn't stripped. app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; // Prepare to monitor accelerometer engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. engine.state = *(struct saved_state*)state->savedState; } // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { // LOGI("accelerometer: x=%f y=%f z=%f", // event.acceleration.x, event.acceleration.y, // event.acceleration.z); } } } // Check if we are exiting. if (state->destroyRequested != 0) { LOGI("engine_term_display(&engine); state->destroyRequested"); engine_term_display(&engine); return; } } // if(mgr == NULL)break; engine_draw_frame(&engine); } }
/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ void android_main(struct android_app* state) { struct engine engine; // Make sure glue isn't stripped. app_dummy(); GAppOnStartup = &UCppLab::OnStartup; GAppOnTouched = &UCppLab::OnTouched; memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; // Prepare to monitor accelerometer engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. engine.state = *(struct saved_state*)state->savedState; } // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { // LOGI("accelerometer: x=%f y=%f z=%f", // event.acceleration.x, event.acceleration.y, // event.acceleration.z); } } } // Check if we are exiting. if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { // Done with events; draw next animation frame. engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } // Drawing is throttled to the screen update rate, so there // is no need to do timing here. engine_draw_frame(&engine); } } }
int SensorImpl::processSensorEvents(int fd, int events, void* data) { ASensorEvent event; while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) { unsigned int type = Sensor::Count; Vector3f data; switch (event.type) { case ASENSOR_TYPE_ACCELEROMETER: type = Sensor::Accelerometer; data.x = event.acceleration.x; data.y = event.acceleration.y; data.z = event.acceleration.z; break; case ASENSOR_TYPE_GYROSCOPE: type = Sensor::Gyroscope; data.x = event.vector.x; data.y = event.vector.y; data.z = event.vector.z; break; case ASENSOR_TYPE_MAGNETIC_FIELD: type = Sensor::Magnetometer; data.x = event.magnetic.x; data.y = event.magnetic.y; data.z = event.magnetic.z; break; case ASENSOR_TYPE_GRAVITY: type = Sensor::Gravity; data.x = event.vector.x; data.y = event.vector.y; data.z = event.vector.z; break; case ASENSOR_TYPE_LINEAR_ACCELERATION: type = Sensor::UserAcceleration; data.x = event.acceleration.x; data.y = event.acceleration.y; data.z = event.acceleration.z; break; case ASENSOR_TYPE_ORIENTATION: type = Sensor::Orientation; data.x = event.vector.x; data.y = event.vector.y; data.z = event.vector.z; break; } // An unknown sensor event has been detected, we don't know how to process it if (type == Sensor::Count) continue; sensorData[type] = data; } return 1; }
void android_main(struct android_app* state) { struct engine engine; // Make sure glue isn't stripped. app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; engine.requested_quit=false; engine.os=NULL; engine.display_active=false; FileAccessAndroid::asset_manager=state->activity->assetManager; // Prepare to monitor accelerometer engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); ANativeActivity_setWindowFlags(state->activity,AWINDOW_FLAG_FULLSCREEN|AWINDOW_FLAG_KEEP_SCREEN_ON,0); state->activity->vm->AttachCurrentThread(&engine.jni, NULL); // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. int nullmax=50; while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { // LOGI("process\n"); source->process(state, source); } else { nullmax--; if (nullmax<0) break; } // If a sensor has data, process it now. // LOGI("events\n"); if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { if (engine.os) { engine.os->process_accelerometer(Vector3(event.acceleration.x, event.acceleration.y, event.acceleration.z)); } } } } // Check if we are exiting. if (state->destroyRequested != 0) { if (engine.os) { engine.os->main_loop_request_quit(); } state->destroyRequested=0; } if (engine.requested_quit) { engine_term_display(&engine); exit(0); return; } // LOGI("end\n"); } // LOGI("engine animating? %i\n",engine.animating); if (engine.animating) { //do os render engine_draw_frame(&engine); //LOGI("TERM WINDOW"); } } }
/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ void android_main(struct android_app* state) { struct engine engine; // Make sure glue isn't stripped. app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; // Prepare to monitor accelerometer engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor( engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue( engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL); // gpg-cpp: Set platform intiialization gpg::AndroidInitialization::android_main(state); // gpg-cpp: Here we create the callback on auth operations auto callback = [&](gpg::AuthOperation op, gpg::AuthStatus status) { LOGI("OnAuthActionFinished"); if (IsSuccess(status)) { LOGI("You are logged in!"); } else { LOGI("You are not logged in!"); } engine.animating = 1; }; // gpg-cpp: We need to check to see if there's a previous state. // If there was, we'll just continue, but if not we'll set up // gpg-cpp for the first time. if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. engine.state = *(struct saved_state*)state->savedState; } else { LOGI("Setting up gpg-cpp"); // Get the platform configuration. gpg::AndroidPlatformConfiguration platform_configuration; platform_configuration.SetActivity(state->activity->clazz); // Now, create the game service (see StateManager.cpp) // and pass in our callback StateManager::InitServices(platform_configuration, NULL, callback); } // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != NULL) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { /* LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); */ } } } // Check if we are exiting. if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { // Done with events; draw next animation frame. engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } // Drawing is throttled to the screen update rate, so there // is no need to do timing here. engine_draw_frame(&engine); } } }