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

ash / sensor_info / sensor_types.cc [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

#include "ash/sensor_info/sensor_types.h"

#include "base/check_op.h"

namespace ash {

SensorReading::SensorReading() = default;

SensorReading::SensorReading(float x) : x(x) {
  y = std::numeric_limits<float>::quiet_NaN();
  z = std::numeric_limits<float>::quiet_NaN();
}

SensorReading::SensorReading(float x, float y, float z) : x(x), y(y), z(z) {}

SensorReading::~SensorReading() = default;

// -----------------------------------------------------------------------------
// SensorUpdate:

SensorUpdate::SensorUpdate() = default;

SensorUpdate::SensorUpdate(const SensorUpdate& update) = default;

SensorUpdate& SensorUpdate::operator=(const SensorUpdate& update) = default;

SensorUpdate::~SensorUpdate() = default;

std::vector<float> SensorUpdate::GetReadingAsVector(SensorType source) const {
  const std::optional<SensorReading>& reading = data_[static_cast<int>(source)];
  if (source == SensorType::kLidAngle) {
    return reading.has_value() ? std::vector<float>{reading->x}
                               : std::vector<float>{0.0};
  }
  return reading.has_value()
             ? std::vector<float>{reading->x, reading->y, reading->z}
             : std::vector<float>{0.0, 0.0, 0.0};
}

void SensorUpdate::Set(SensorType source, float x, float y, float z) {
  CHECK_NE(source, SensorType::kLidAngle);
  data_[static_cast<int>(source)] = SensorReading(x, y, z);
}

void SensorUpdate::Set(SensorType source, float x) {
  DCHECK_EQ(source, SensorType::kLidAngle);
  data_[static_cast<int>(source)] = SensorReading(x);
}

void SensorUpdate::Reset() {
  for (auto& i : data_) {
    i = std::nullopt;
  }
}

}  // namespace ash