1 // Copyright 2021 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_chrono/system_clock.h" 17 #include "pw_result/result.h" 18 19 namespace pw::analog { 20 21 // Base interface for getting ADC samples from one ADC channel in a thread 22 // safe manner. 23 // 24 // The ADC backend interface is up to the user to define and implement for now. 25 // This gives the flexibility for the ADC driver implementation. 26 // 27 // AnalogInput controls a specific input/channel where the ADC peripheral may be 28 // shared across multiple channels that may be controlled by multiple threads. 29 // The implementer of this pure virtual interface is responsible for ensuring 30 // thread safety and access at the driver level. 31 class AnalogInput { 32 public: 33 // Limits struct that specifies the min and max of the sample range. 34 // These values do not change at run time. 35 struct Limits { 36 int32_t min; 37 int32_t max; 38 }; 39 40 virtual ~AnalogInput() = default; 41 42 // Blocks until the specified timeout duration has elapsed or the ADC sample 43 // has been returned, whichever comes first. 44 // 45 // This method is thread safe. 46 // 47 // Returns: 48 // Sample. 49 // ResourceExhuasted: ADC peripheral in use. 50 // DeadlineExceedded: Timed out waiting for a sample. 51 // Other statuses left up to the implementer. TryReadFor(chrono::SystemClock::duration timeout)52 Result<int32_t> TryReadFor(chrono::SystemClock::duration timeout) { 53 return TryReadUntil(chrono::SystemClock::TimePointAfterAtLeast(timeout)); 54 } 55 56 // Blocks until the deadline time has been reached or the ADC sample 57 // has been returned, whichever comes first. 58 // 59 // This method is thread safe. 60 // 61 // Returns: 62 // Sample. 63 // ResourceExhuasted: ADC peripheral in use. 64 // DeadlineExceedded: Timed out waiting for a sample. 65 // Other statuses left up to the implementer. 66 virtual Result<int32_t> TryReadUntil( 67 chrono::SystemClock::time_point deadline) = 0; 68 69 // Returns the range of the ADC sample. 70 // These values do not change at run time. 71 virtual Limits GetLimits() const = 0; 72 }; 73 74 } // namespace pw::analog 75