Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: content/public/browser/sensor_manager.cc

Issue 680383007: DeviceOrientation API on ChromeOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Isolate Chrome OS changes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/public/browser/sensor_manager.h"
6
7 #include "base/logging.h"
8 #include "base/memory/singleton.h"
9
10 namespace {
11 // Conversion ratio from radians to degrees.
12 const double kRad2deg = 180.0 / M_PI;
13 }
14
15 namespace content {
16
17 SensorManager* SensorManager::GetInstance() {
18 return Singleton<SensorManager, LeakySingletonTraits<SensorManager>>::get();
19 }
20
21 void SensorManager::OnAccelerationIncludingGravity(double x,
timvolodine 2014/11/07 03:27:39 you could probably also implement Device Motion in
jonross 2014/11/07 19:59:37 I was planning to use this patch to settle on how
22 double y,
23 double z) {
24 base::AutoLock autolock(orientation_buffer_lock_);
25 if (!orientation_buffer_)
26 return;
27
28 // Create a unit vector to remove gravity.
29 // TODO(jonross): Stop reversing signs for vector components once
30 // accelerometer values have been reversed.
flackr 2014/11/07 19:30:46 Can you file a bug for this?
jonross 2014/11/07 19:59:37 crbug.com/431391 filed
31 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f);
32 data.Scale(1.0f / data.Length());
33
34 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix.
35 // x = sin(gamma)
36 // y = -cos(gamma) * sin(beta)
37 // z = cos(beta) * cos(gamma)
38 // With only accelerometer alpha cannot be provided.
39 double beta = kRad2deg * atan2(data.y(), data.z());
40 double gamma = kRad2deg * asin(data.x());
41
42 // Have the interval boundaries comply with the specification. Beta is
43 // [-180,180) and gamma is [-90, 90).
44 if (beta >= 180.0f)
45 beta = -180.0f;
46 if (gamma >= 90.0f)
47 gamma = -90.0f;
48
49 orientation_buffer_->seqlock.WriteBegin();
50 orientation_buffer_->data.beta = beta;
51 orientation_buffer_->data.hasBeta = true;
52 orientation_buffer_->data.gamma = gamma;
53 orientation_buffer_->data.hasGamma = true;
54 orientation_buffer_->data.allAvailableSensorsAreActive = true;
55 orientation_buffer_->seqlock.WriteEnd();
56 }
57
58 void SensorManager::StartFetchingDeviceOrientationData(
59 DeviceOrientationHardwareBuffer* buffer) {
60 DCHECK(buffer);
61 base::AutoLock autolock(orientation_buffer_lock_);
62 orientation_buffer_ = buffer;
63
64 // No compass information, so we cannot provide absolute orientation.
65 orientation_buffer_->seqlock.WriteBegin();
66 orientation_buffer_->data.absolute = false;
67 orientation_buffer_->data.hasAbsolute = true;
68 orientation_buffer_->seqlock.WriteEnd();
69 }
70
71 void SensorManager::StopFetchingDeviceOrientationData() {
72 base::AutoLock autolock(orientation_buffer_lock_);
73 if (!orientation_buffer_)
74 return;
75 // Null message to signify that sensors are no longer available.
76 orientation_buffer_->seqlock.WriteBegin();
77 orientation_buffer_->data.allAvailableSensorsAreActive = false;
78 orientation_buffer_->seqlock.WriteEnd();
79 orientation_buffer_ = nullptr;
80 }
81
82 SensorManager::SensorManager() : orientation_buffer_(nullptr) {
83 }
84
85 SensorManager::~SensorManager() {
86 }
87
88 } // namespace content
OLDNEW
« content/public/browser/sensor_manager.h ('K') | « content/public/browser/sensor_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698