LinearAccelerationSensor.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <math.h>
  18. #include <sys/types.h>
  19. #include <utils/Errors.h>
  20. #include <hardware/sensors.h>
  21. #include "LinearAccelerationSensor.h"
  22. #include "SensorDevice.h"
  23. #include "SensorFusion.h"
  24. namespace android {
  25. // ---------------------------------------------------------------------------
  26. LinearAccelerationSensor::LinearAccelerationSensor(sensor_t const* list, size_t count)
  27. : mSensorDevice(SensorDevice::getInstance()),
  28. mGravitySensor(list, count)
  29. {
  30. }
  31. bool LinearAccelerationSensor::process(sensors_event_t* outEvent,
  32. const sensors_event_t& event)
  33. {
  34. bool result = mGravitySensor.process(outEvent, event);
  35. if (result && event.type == SENSOR_TYPE_ACCELEROMETER) {
  36. outEvent->data[0] = event.acceleration.x - outEvent->data[0];
  37. outEvent->data[1] = event.acceleration.y - outEvent->data[1];
  38. outEvent->data[2] = event.acceleration.z - outEvent->data[2];
  39. outEvent->sensor = '_lin';
  40. outEvent->type = SENSOR_TYPE_LINEAR_ACCELERATION;
  41. return true;
  42. }
  43. return false;
  44. }
  45. status_t LinearAccelerationSensor::activate(void* ident, bool enabled) {
  46. return mGravitySensor.activate(ident, enabled);
  47. }
  48. status_t LinearAccelerationSensor::setDelay(void* ident, int handle, int64_t ns) {
  49. return mGravitySensor.setDelay(ident, handle, ns);
  50. }
  51. Sensor LinearAccelerationSensor::getSensor() const {
  52. Sensor gsensor(mGravitySensor.getSensor());
  53. sensor_t hwSensor;
  54. hwSensor.name = "Linear Acceleration Sensor";
  55. hwSensor.vendor = "AOSP";
  56. hwSensor.version = gsensor.getVersion();
  57. hwSensor.handle = '_lin';
  58. hwSensor.type = SENSOR_TYPE_LINEAR_ACCELERATION;
  59. hwSensor.maxRange = gsensor.getMaxValue();
  60. hwSensor.resolution = gsensor.getResolution();
  61. hwSensor.power = gsensor.getPowerUsage();
  62. hwSensor.minDelay = gsensor.getMinDelay();
  63. Sensor sensor(&hwSensor);
  64. return sensor;
  65. }
  66. // ---------------------------------------------------------------------------
  67. }; // namespace android