summaryrefslogtreecommitdiffstats
path: root/ui/chromeos/accelerometer/accelerometer_util.cc
blob: 9368b81dd57cfd079c6de5586af497dc2503c93b (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
// 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 "ui/chromeos/accelerometer/accelerometer_util.h"

#include <cmath>

#include "chromeos/accelerometer/accelerometer_types.h"
#include "ui/gfx/geometry/vector3d_f.h"

namespace {

// The maximum deviation from the acceleration expected due to gravity for which
// the device will be considered stable: 1g.
const float kDeviationFromGravityThreshold = 1.0f;

// The mean acceleration due to gravity on Earth in m/s^2.
const float kMeanGravity = 9.80665f;

}  // namespace

namespace ui {

const gfx::Vector3dF ConvertAccelerometerReadingToVector3dF(
    const chromeos::AccelerometerReading& reading) {
  return gfx::Vector3dF(reading.x, reading.y, reading.z);
}

bool IsAccelerometerReadingStable(const chromeos::AccelerometerUpdate& update,
                                  chromeos::AccelerometerSource source) {
  return update.has(source) &&
         std::abs(ConvertAccelerometerReadingToVector3dF(update.get(source))
                      .Length() -
                  kMeanGravity) <= kDeviationFromGravityThreshold;
}

}  // namespace ui