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

ash / sensor_info / sensor_types.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#ifndef ASH_SENSOR_INFO_SENSOR_TYPES_H_
#define ASH_SENSOR_INFO_SENSOR_TYPES_H_

#include <optional>
#include <vector>

#include "ash/ash_export.h"
#include "base/observer_list_types.h"

namespace ash {

// Location of sensor. There are three types of sensors: gyroscope,
// accelerometer, lid_angle.
enum class SensorLocation {
  kLid,
  kBase,
  kOther,
};

// Sensor types.
enum class SensorType {
  kLidAngle = 0,
  kAccelerometerBase = 1,
  kAccelerometerLid = 2,
  kGyroscopeBase = 3,
  kGyroscopeLid = 4,
  kSensorTypeCount = 5,
};

// Stores one sensor's reading info.
struct ASH_EXPORT SensorReading {
  SensorReading();
  // We use SensorReading(x) for lid_angle sensor, and SensorReading(x, y, z)
  // for gyroscope and accelerometer.
  explicit SensorReading(float x);
  SensorReading(float x, float y, float z);
  ~SensorReading();

  // The readings from this sensor.
  float x;
  float y;
  float z;
};

// Stores all present sensors' reading info.
class ASH_EXPORT SensorUpdate {
 public:
  SensorUpdate();
  SensorUpdate(const SensorUpdate& update);
  SensorUpdate& operator=(const SensorUpdate& update);
  ~SensorUpdate();

  // Returns true if `source` has a valid value in this update.
  bool has(SensorType source) const {
    return data_[static_cast<int>(source)].has_value();
  }
  // Returns the last known value for |source|.
  const std::optional<SensorReading>& get(SensorType source) const {
    return data_[static_cast<int>(source)];
  }

  // Returns the last known value for `source` as a vector.
  std::vector<float> GetReadingAsVector(SensorType source) const;
  // We use Set(x) for lid_angle sensor, and Set(x, y, z) for gyroscope and
  // accelerometer.
  void Set(SensorType source, float x);
  void Set(SensorType source, float x, float y, float z);
  // Clear content in data_.
  void Reset();

 protected:
  std::optional<SensorReading>
      data_[static_cast<int>(SensorType::kSensorTypeCount)];
};

// Class for all potential observers for sensor updates.
class ASH_EXPORT SensorObserver : public base::CheckedObserver {
 public:
  // SensorProvider will gather updates from AccelGyroSamplesObserver. Then
  // SensorProvider will call OnSensorUpdated to notify SensorObserver.
  virtual void OnSensorUpdated(const SensorUpdate& update) = 0;
};

}  // namespace ash

#endif  // ASH_SENSOR_INFO_SENSOR_TYPES_H_