aboutsummaryrefslogtreecommitdiff
path: root/pw_analog/public/pw_analog/microvolt_input.h
blob: a38eb5b89e5286808fd87084a9e542f39a79c5bb (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
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once
#include "pw_analog/analog_input.h"
#include "pw_chrono/system_clock.h"
#include "pw_result/result.h"
#include "pw_status/try.h"

namespace pw::analog {

/// The common interface for obtaining voltage samples in microvolts. This
/// interface represents a single voltage input or channel. Users will need to
/// supply their own ADC driver implementation in order to provide the reference
/// voltages and to configure and enable the ADC peripheral where needed. Users
/// are responsible for managing multi-threaded access to the ADC driver if the
/// ADC services multiple channels.
class MicrovoltInput : public AnalogInput {
 public:
  /// Specifies the maximum and minimum microvolt range the analog input can
  /// measure. The reference voltage difference cannot be bigger than
  /// `sizeof(int32_t)` which should be just above 2000V. These values do not
  /// change at run time. Inversion of `min` or `max` is supported.
  struct References {
    /// Microvolts at `AnalogInput::Limits::max`.
    int32_t max_voltage_uv;
    /// Microvolts at `AnalogInput::Limits::min`.
    int32_t min_voltage_uv;
  };

  ~MicrovoltInput() override = default;

  /// Blocks until the specified timeout duration has elapsed or the voltage
  /// sample has been returned, whichever comes first.
  ///
  /// This method is thread-safe.
  ///
  /// @returns
  /// * A voltage sample in microvolts (uV) on success.
  /// * @pw_status{RESOURCE_EXHAUSTED} - ADC peripheral in use.
  /// * @pw_status{DEADLINE_EXCEEDED} - Timed out waiting for a sample.
  /// * Other statuses left up to the implementer.
  Result<int32_t> TryReadMicrovoltsFor(chrono::SystemClock::duration timeout) {
    return TryReadMicrovoltsUntil(
        chrono::SystemClock::TimePointAfterAtLeast(timeout));
  }

  /// Blocks until the deadline time has been reached or the voltage sample has
  /// been returned, whichever comes first.
  ///
  /// This method is thread-safe.
  ///
  /// @returns
  /// * A voltage sample in microvolts (uV) on success.
  /// * @pw_status{RESOURCE_EXHAUSTED} - ADC peripheral in use.
  /// * @pw_status{DEADLINE_EXCEEDED} - Timed out waiting for a sample.
  /// * Other statuses left up to the implementer.
  Result<int32_t> TryReadMicrovoltsUntil(
      chrono::SystemClock::time_point deadline) {
    PW_TRY_ASSIGN(const int32_t sample, TryReadUntil(deadline));

    const References reference = GetReferences();
    const AnalogInput::Limits limits = GetLimits();

    constexpr int64_t kMaxReferenceDiffUv = std::numeric_limits<int32_t>::max();

    if (std::abs(static_cast<int64_t>(reference.max_voltage_uv) -
                 static_cast<int64_t>(reference.min_voltage_uv)) >
        kMaxReferenceDiffUv) {
      return pw::Status::Internal();
    }

    return (((static_cast<int64_t>(sample) - static_cast<int64_t>(limits.min)) *
             (reference.max_voltage_uv - reference.min_voltage_uv)) /
            (limits.max - limits.min)) +
           reference.min_voltage_uv;
  }

 private:
  // Returns the reference voltage needed to calculate the voltage.
  // These values do not change at run time.
  virtual References GetReferences() const = 0;
};

}  // namespace pw::analog