Skip to content

Instantly share code, notes, and snippets.

@iamsurya
Created July 7, 2019 16:35
Show Gist options
  • Save iamsurya/120f47083ab10b4c713ee34d5fad6d51 to your computer and use it in GitHub Desktop.
Save iamsurya/120f47083ab10b4c713ee34d5fad6d51 to your computer and use it in GitHub Desktop.
// From https://github.com/gameplay3d/GamePlay/blob/master/gameplay/src/PlatformAndroid.cpp
// Line 1373
{
// Prepare to monitor accelerometer.
__sensorManager = ASensorManager_getInstance();
__accelerometerSensor = ASensorManager_getDefaultSensor(__sensorManager, ASENSOR_TYPE_ACCELEROMETER);
__gyroscopeSensor = ASensorManager_getDefaultSensor(__sensorManager, ASENSOR_TYPE_GYROSCOPE);
__sensorEventQueue = ASensorManager_createEventQueue(__sensorManager, __state->looper, LOOPER_ID_USER, NULL, NULL);
// Get the initial time.
clock_gettime(CLOCK_REALTIME, &__timespec);
__timeStart = timespec2millis(&__timespec);
__timeAbsolute = 0L;
while (true)
{
// Read all pending events.
int ident;
int events;
struct android_poll_source* source;
while ((ident=ALooper_pollAll(!__suspended ? 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 && __accelerometerSensor != NULL)
{
ASensorEventQueue_getEvents(__sensorEventQueue, &__sensorEvent, 1);
if (__sensorEvent.type == ASENSOR_TYPE_ACCELEROMETER)
{
__accelRawX = __sensorEvent.acceleration.x;
__accelRawY = __sensorEvent.acceleration.y;
__accelRawZ = __sensorEvent.acceleration.z;
}
else if (__sensorEvent.type == ASENSOR_TYPE_GYROSCOPE)
{
__gyroRawX = __sensorEvent.vector.x;
__gyroRawY = __sensorEvent.vector.y;
__gyroRawZ = __sensorEvent.vector.z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment