summaryrefslogtreecommitdiffstats
path: root/content/browser/device_sensors/sensor_manager_chromeos.cc
blob: 36bdadd81fea719fc52bd3b76484a1709ef4ca8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/device_sensors/sensor_manager_chromeos.h"

#include <math.h>

#include "chromeos/accelerometer/accelerometer_reader.h"
#include "chromeos/accelerometer/accelerometer_types.h"
#include "content/browser/device_sensors/inertial_sensor_consts.h"
#include "ui/gfx/geometry/vector3d_f.h"

namespace {
// Conversion ratio from radians to degrees.
const double kRad2deg = 180.0 / M_PI;
}

namespace content {

SensorManagerChromeOS::SensorManagerChromeOS()
    : motion_buffer_(nullptr), orientation_buffer_(nullptr) {
}

SensorManagerChromeOS::~SensorManagerChromeOS() {
}

void SensorManagerChromeOS::StartFetchingDeviceMotionData(
    DeviceMotionHardwareBuffer* buffer) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!motion_buffer_);
  motion_buffer_ = buffer;

  motion_buffer_->seqlock.WriteBegin();
  // The interval between updates is the longer of the rate set on the buffer,
  // and the rate at which AccelerometerReader polls the sensor.
  motion_buffer_->data.interval =
      std::max(kInertialSensorIntervalMicroseconds / 1000,
               chromeos::AccelerometerReader::kDelayBetweenReadsMs);
  motion_buffer_->seqlock.WriteEnd();

  if (!orientation_buffer_)
    StartObservingAccelerometer();
}

bool SensorManagerChromeOS::StopFetchingDeviceMotionData() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!motion_buffer_)
    return false;

  // Make sure to indicate that the sensor data is no longer available.
  motion_buffer_->seqlock.WriteBegin();
  motion_buffer_->data.allAvailableSensorsAreActive = false;
  motion_buffer_->seqlock.WriteEnd();

  motion_buffer_ = nullptr;

  if (!orientation_buffer_)
    StopObservingAccelerometer();
  return true;
}

void SensorManagerChromeOS::StartFetchingDeviceOrientationData(
    DeviceOrientationHardwareBuffer* buffer) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!orientation_buffer_);
  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();

  if (!motion_buffer_)
    StartObservingAccelerometer();
}

bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!orientation_buffer_)
    return false;
  // Make sure to indicate that the sensor data is no longer available.
  orientation_buffer_->seqlock.WriteBegin();
  orientation_buffer_->data.allAvailableSensorsAreActive = false;
  orientation_buffer_->seqlock.WriteEnd();
  orientation_buffer_ = nullptr;

  if (!motion_buffer_)
    StopObservingAccelerometer();
  return true;
}

void SensorManagerChromeOS::OnAccelerometerUpdated(
    scoped_refptr<const chromeos::AccelerometerUpdate> update) {
  DCHECK(thread_checker_.CalledOnValidThread());
  chromeos::AccelerometerSource source;
  if (update->has(chromeos::ACCELEROMETER_SOURCE_SCREEN))
    source = chromeos::ACCELEROMETER_SOURCE_SCREEN;
  else if (update->has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD))
    source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD;
  else
    return;

  double x = update->get(source).x;
  double y = update->get(source).y;
  double z = update->get(source).z;

  GenerateMotionEvent(x, y, z);
  GenerateOrientationEvent(x, y, z);
}

void SensorManagerChromeOS::StartObservingAccelerometer() {
  chromeos::AccelerometerReader::GetInstance()->AddObserver(this);
}

void SensorManagerChromeOS::StopObservingAccelerometer() {
  chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
}

void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) {
  if (!motion_buffer_)
    return;

  motion_buffer_->seqlock.WriteBegin();
  motion_buffer_->data.accelerationIncludingGravityX = x;
  motion_buffer_->data.hasAccelerationIncludingGravityX = true;
  motion_buffer_->data.accelerationIncludingGravityY = y;
  motion_buffer_->data.hasAccelerationIncludingGravityY = true;
  motion_buffer_->data.accelerationIncludingGravityZ = z;
  motion_buffer_->data.hasAccelerationIncludingGravityZ = true;
  motion_buffer_->data.allAvailableSensorsAreActive = true;
  motion_buffer_->seqlock.WriteEnd();
}

void SensorManagerChromeOS::GenerateOrientationEvent(double x,
                                                     double y,
                                                     double z) {
  if (!orientation_buffer_)
    return;

  // Create a unit vector for trigonometry
  // TODO(jonross): Stop reversing signs for vector components once
  // accelerometer values have been fixed. crbug.com/431391
  // Ternaries are to remove -0.0f which gives incorrect trigonometrical
  // results.
  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());

  // Convert beta and gamma to fit the intervals in 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();
}

}  // namespace content