void main() { static SensorListener sensors; sensors.install(); // We're entirely event-driven. Everything is // updated by SensorListener's event callbacks. while (1) System::paint(); }
void main() { static SensorListener sensors; sensors.install(); while (1) { System::paint(); } }
static int sensor_events_listener(int fd, int events, void* data) { SensorListener* listener = (SensorListener*) data; ssize_t num_sensors; ASensorEvent sen_events[8]; while ((num_sensors = listener->mSensorEventQueue->read(sen_events, 8)) > 0) { for (int i = 0; i < num_sensors; i++) { if (sen_events[i].type == Sensor::TYPE_ACCELEROMETER) { float x = sen_events[i].vector.azimuth; float y = sen_events[i].vector.pitch; float z = sen_events[i].vector.roll; float radius = 0; int tilt = 0, orient = 0; CAMHAL_LOGVA("ACCELEROMETER EVENT"); CAMHAL_LOGVB(" azimuth = %f pitch = %f roll = %f", sen_events[i].vector.azimuth, sen_events[i].vector.pitch, sen_events[i].vector.roll); // see http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates // about conversion from cartesian to spherical for orientation calculations radius = (float) sqrt(x * x + y * y + z * z); tilt = (int) asinf(z / radius) * RADIANS_2_DEG; orient = (int) atan2f(-x, y) * RADIANS_2_DEG; if (orient < 0) { orient += 360; } if (orient >= DEGREES_270_THRESH) { orient = 270; } else if (orient >= DEGREES_180_THRESH) { orient = 180; } else if (orient >= DEGREES_90_THRESH) { orient = 90; } else { orient = 0; } listener->handleOrientation(orient, tilt); CAMHAL_LOGVB(" tilt = %d orientation = %d", tilt, orient); } else if (sen_events[i].type == Sensor::TYPE_GYROSCOPE) { CAMHAL_LOGVA("GYROSCOPE EVENT"); } } } if (num_sensors < 0 && num_sensors != -EAGAIN) { CAMHAL_LOGEB("reading events failed: %s", strerror(-num_sensors)); } return 1; }
static int sensor_events_listener(int fd, int events, void* data) { SensorListener* listener = (SensorListener*) data; ASensorEvent sen_events; status_t res; res = listener->mSensorEventQueue->read(&sen_events, 1); if (res == 0) { res = listener->mSensorEventQueue->waitForEvent(); if (res != NO_ERROR) { ALOGE("%s; waitForEvent error",__FUNCTION__); return -1; } } if (res <= 0) { ALOGE("%s: error",__FUNCTION__); return -1; } if (sen_events.sensor == listener->getRegisterHandle()) { float x = -sen_events.vector.v[0]; float y = -sen_events.vector.v[1]; float z = -sen_events.vector.v[2]; float magnitude = x*x + y*y; int orientation = 0; // Don't trust the angle if the magnitude is small compared to the y value if (magnitude * 4 >= z*z) { float OneEightyOverPi = 57.29577957855f; float angle = (float)atan2f(-y, x) * OneEightyOverPi; orientation = 90 - (int)round(angle); while (orientation >= 360) { orientation -= 360; } while (orientation < 0) { orientation += 360; } } listener->setOrientation(orientation); } return 1; }