Index: content/public/browser/sensor_manager.cc |
diff --git a/content/public/browser/sensor_manager.cc b/content/public/browser/sensor_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ff29ce3092ee40761a0578c606cac16bd7d2569e |
--- /dev/null |
+++ b/content/public/browser/sensor_manager.cc |
@@ -0,0 +1,87 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
timvolodine
2014/11/06 02:53:47
can you call this file sensor_manager_chromeos.cc?
timvolodine
2014/11/06 02:53:47
also I don't understand why this needs to be in co
jonross
2014/11/06 18:18:52
This file is also called from the ash library. So
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/public/browser/sensor_manager.h" |
+ |
+#include "base/logging.h" |
+#include "base/memory/singleton.h" |
+ |
+namespace { |
+// Conversion ratio from radians to degrees. |
+const double kRad2deg = 180.0 / M_PI; |
+} |
+ |
+namespace content { |
+ |
+SensorManager* SensorManager::GetInstance() { |
+ return Singleton<SensorManager, LeakySingletonTraits<SensorManager>>::get(); |
+} |
+ |
+void SensorManager::OnAccelerationIncludingGravity(double x, |
+ double y, |
+ double z) { |
timvolodine
2014/11/06 02:53:47
on which thread is this method called?
do you need
jonross
2014/11/06 18:18:52
This will be called from the UI thread. I'm adding
|
+ if (!orientation_buffer_) |
+ return; |
+ |
+ // Create a unit vector to remove gravity. |
+ // TODO(jonross): Stop reversing signs for vector components once |
+ // accelerometer values have been reversed. |
+ gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
+ data.Scale(1.0f / data.Length()); |
+ |
+ // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
+ // x = sin(gamma) |
+ // y = -cos(gamma) * sin(beta) |
+ // z = cos(beta) * cos(gamma) |
+ // With only accelerometer alpha cannot be provided. |
+ double beta = kRad2deg * atan2(data.y(), data.z()); |
+ double gamma = kRad2deg * asin(data.x()); |
+ |
+ // Have the interval boundaries comply with the specification. Beta is |
+ // [-180,180) and gamma is [-90, 90). |
+ if (beta >= 180.0f) |
+ beta = -180.0f; |
+ if (gamma >= 90.0f) |
+ gamma = -90.0f; |
+ |
+ orientation_buffer_->seqlock.WriteBegin(); |
+ orientation_buffer_->data.beta = beta; |
+ orientation_buffer_->data.hasBeta = true; |
+ orientation_buffer_->data.gamma = gamma; |
+ orientation_buffer_->data.hasGamma = true; |
+ orientation_buffer_->data.allAvailableSensorsAreActive = true; |
+ orientation_buffer_->seqlock.WriteEnd(); |
+} |
+ |
+void SensorManager::StartFetchingDeviceOrientationData( |
+ DeviceOrientationHardwareBuffer* buffer) { |
+ DCHECK(buffer); |
+ base::AutoLock autolock(orientation_buffer_lock_); |
+ orientation_buffer_ = buffer; |
+ |
+ // No compass information, so we cannot provide absolute orientation. |
+ orientation_buffer_->seqlock.WriteBegin(); |
+ orientation_buffer_->data.absolute = false; |
+ orientation_buffer_->data.hasAbsolute = true; |
+ orientation_buffer_->seqlock.WriteEnd(); |
+} |
+ |
+void SensorManager::StopFetchingDeviceOrientationData() { |
+ base::AutoLock autolock(orientation_buffer_lock_); |
+ if (!orientation_buffer_) |
+ return; |
+ // Null message to signify that sensora are no longer available. |
timvolodine
2014/11/06 02:53:47
sensora -> sensors
jonross
2014/11/06 18:18:52
Done.
|
+ orientation_buffer_->seqlock.WriteBegin(); |
+ orientation_buffer_->data.allAvailableSensorsAreActive = false; |
+ orientation_buffer_->seqlock.WriteEnd(); |
+ orientation_buffer_ = nullptr; |
+} |
+ |
+SensorManager::SensorManager() : orientation_buffer_(nullptr) { |
+} |
+ |
+SensorManager::~SensorManager() { |
+} |
+ |
+} // namespace content |