From 36409dac6ce3c241d878d5997b6f7dca29321c6a Mon Sep 17 00:00:00 2001 From: Kyle Repinski Date: Sun, 6 Dec 2015 11:53:02 -0600 Subject: libsensors: Big cleanup. - Some formatting nitpicks. - Removed useless 'dev_name'/'dev_fd' stuff for SensorBase. - Merged sensor structs together. - Added handleToDriver for MPLSensor. --- libsensors/MPLSensor.cpp | 166 ++++++++--------------------------------------- 1 file changed, 28 insertions(+), 138 deletions(-) (limited to 'libsensors/MPLSensor.cpp') diff --git a/libsensors/MPLSensor.cpp b/libsensors/MPLSensor.cpp index bbdcf3a..d06a39a 100644 --- a/libsensors/MPLSensor.cpp +++ b/libsensors/MPLSensor.cpp @@ -56,7 +56,6 @@ extern "C" { } #include "mlcontrol.h" -#include "sensor_params.h" #define EXTRA_VERBOSE (0) //#define FUNC_LOG ALOGV("%s", __PRETTY_FUNCTION__) @@ -69,39 +68,6 @@ extern "C" { /******************************************/ -/* Base values for the sensor list, these need to be in the order defined in MPLSensor.h */ -static struct sensor_t sSensorList[] = -{ - {"MPL Gyroscope", "Invensense", 1, SENSORS_GYROSCOPE_HANDLE, - SENSOR_TYPE_GYROSCOPE, GYRO_MPU3050_RANGE, GYRO_MPU3050_RESOLUTION, - GYRO_MPU3050_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_GYROSCOPE, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, - {"MPL Accelerometer", "Invensense", 1, SENSORS_ACCELERATION_HANDLE, - SENSOR_TYPE_ACCELEROMETER, ACCEL_BMA250_RANGE, ACCEL_BMA250_RESOLUTION, - ACCEL_BMA250_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_ACCELEROMETER, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, - {"MPL Magnetic Field", "Invensense", 1, SENSORS_MAGNETIC_FIELD_HANDLE, - SENSOR_TYPE_MAGNETIC_FIELD, COMPASS_YAS530_RANGE, COMPASS_YAS530_RESOLUTION, - COMPASS_YAS530_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_MAGNETIC_FIELD, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, - {"MPL Orientation", "Invensense", 1, SENSORS_ORIENTATION_HANDLE, - SENSOR_TYPE_ORIENTATION, NINEAXIS_ORIENTATION_RANGE, NINEAXIS_ORIENTATION_RESOLUTION, - NINEAXIS_ORIENTATION_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_ORIENTATION, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, - {"MPL Rotation Vector", "Invensense", 1, SENSORS_ROTATION_VECTOR_HANDLE, - SENSOR_TYPE_ROTATION_VECTOR, NINEAXIS_ROTATION_VECTOR_RANGE, NINEAXIS_ROTATION_VECTOR_RESOLUTION, - NINEAXIS_ROTATION_VECTOR_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_ORIENTATION, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, - {"MPL Linear Acceleration", "Invensense", 1, SENSORS_LINEAR_ACCEL_HANDLE, - SENSOR_TYPE_LINEAR_ACCELERATION, NINEAXIS_LINEAR_ACCEL_RANGE, NINEAXIS_LINEAR_ACCEL_RESOLUTION, - NINEAXIS_LINEAR_ACCEL_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_LINEAR_ACCELERATION, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, - {"MPL Gravity", "Invensense", 1, SENSORS_GRAVITY_HANDLE, - SENSOR_TYPE_GRAVITY, NINEAXIS_GRAVITY_RANGE, NINEAXIS_GRAVITY_RESOLUTION, - NINEAXIS_GRAVITY_POWER, 10000, 0, 0, SENSOR_STRING_TYPE_GRAVITY, "", - 0, SENSOR_FLAG_CONTINUOUS_MODE, {}}, -}; - static unsigned long long irq_timestamp = 0; /* *************************************************************************** * MPL interface misc. @@ -149,7 +115,7 @@ void setCallbackObject(MPLSensor* gbpt) #define RV_ENABLED ((1<requested_sensors) & INV_THREE_AXIS_GYRO) { - //if gyros are on and we got a no motion, set a flag + // if gyros are on and we got a no motion, set a flag // indicating that the cal file can be written. mHaveGoodMpuCal = true; } @@ -567,8 +517,8 @@ void MPLSensor::cbProcData() mNewData = 1; } -//these handlers transform mpl data into one of the Android sensor types -// scaling and coordinate transforms should be done in the handlers +// these handlers transform mpl data into one of the Android sensor types. +// scaling and coordinate transforms should be done in the handlers void MPLSensor::gyroHandler(sensors_event_t* s, uint32_t* pending_mask, int index) @@ -603,7 +553,7 @@ int MPLSensor::estimateCompassAccuracy() int rv; res = inv_get_compass_accuracy(&rv); - if(rv >= SENSOR_STATUS_ACCURACY_MEDIUM) { + if (rv >= SENSOR_STATUS_ACCURACY_MEDIUM) { mHaveGoodCompassCal = true; } ALOGE_IF(res != INV_SUCCESS, "error returned from inv_get_compass_accuracy"); @@ -763,34 +713,7 @@ int MPLSensor::enable(int32_t handle, int en) { FUNC_LOG; - int what = -1; - - switch (handle) { - case ID_A: - what = Accelerometer; - break; - case ID_M: - what = MagneticField; - break; - case ID_O: - what = Orientation; - break; - case ID_GY: - what = Gyro; - break; - case ID_GR: - what = Gravity; - break; - case ID_RV: - what = RotationVector; - break; - case ID_LA: - what = LinearAccel; - break; - default: //this takes care of all the gestures - what = handle; - break; - } + int what = handleToDriver(handle); if (uint32_t(what) >= numSensors) return -EINVAL; @@ -819,33 +742,8 @@ int MPLSensor::setDelay(int32_t handle, int64_t ns) FUNC_LOG; ALOGV_IF(EXTRA_VERBOSE, " setDelay handle: %d rate %d", handle, (int) (ns / 1000000LL)); - int what = -1; - switch (handle) { - case ID_A: - what = Accelerometer; - break; - case ID_M: - what = MagneticField; - break; - case ID_O: - what = Orientation; - break; - case ID_GY: - what = Gyro; - break; - case ID_GR: - what = Gravity; - break; - case ID_RV: - what = RotationVector; - break; - case ID_LA: - what = LinearAccel; - break; - default: - what = handle; - break; - } + + int what = handleToDriver(handle); if (uint32_t(what) >= numSensors) return -EINVAL; @@ -895,7 +793,7 @@ int MPLSensor::update_delay() rv = (res == INV_SUCCESS); } - if (((inv_get_dl_config()->requested_sensors & INV_DMP_PROCESSOR) == 0)) { + if ((inv_get_dl_config()->requested_sensors & INV_DMP_PROCESSOR) == 0) { if (mUseTimerirq) { ioctl(mIrqFds.valueFor(TIMERIRQ_FD), TIMERIRQ_STOP, 0); clearIrqData(irq_set); @@ -934,9 +832,7 @@ int MPLSensor::readEvents(sensors_event_t* data, int count) if (mDmpStarted) { rv = inv_update_data(); ALOGE_IF(rv != INV_SUCCESS, "inv_update_data error (code %d)", (int) rv); - } - - else { + } else { //probably just one extra read after shutting down ALOGV_IF(EXTRA_VERBOSE, "MPLSensor::readEvents called, but there's nothing to do."); @@ -1032,8 +928,8 @@ void MPLSensor::wakeEvent() pthread_mutex_lock(&mMplMutex); if (mForceSleep) { setPowerStates((mOldEnabledMask | mEnabled)); + mForceSleep = false; } - mForceSleep = false; pthread_mutex_unlock(&mMplMutex); } @@ -1042,25 +938,19 @@ void MPLSensor::wakeEvent() * parameter list must point to a memory region of at least 7*sizeof(sensor_t) * parameter len gives the length of the buffer pointed to by list */ - int MPLSensor::populateSensorList(struct sensor_t *list, size_t len) { - int numsensors; + int numsensors = 7; - if(len < 7*sizeof(sensor_t)) { + if (len < numsensors * sizeof(sensor_t)) { ALOGE("sensor list too small, not populating."); return 0; } - /* fill in the base values */ - memcpy(list, sSensorList, sizeof (struct sensor_t) * 7); - - if (mNineAxisEnabled) { - numsensors = 7; - } else { + if (!mNineAxisEnabled) { /* no 9-axis sensors, zero fill that part of the list */ numsensors = 3; - memset(list+3, 0, 4*sizeof(struct sensor_t)); + memset(list + numsensors, 0, (7 - numsensors) * sizeof(struct sensor_t)); } return numsensors; -- cgit v1.1