pw_analog#

Analog-to-digital converter libraries and utilities

Stable C++

pw_analog contains interfaces and utility functions for using the ADC.

Features#

pw::analog::AnalogInput#

The common interface for obtaining ADC samples. This interface represents a single analog input or channel. Users will need to supply their own ADC driver implementation in order to configure and enable the ADC peripheral. Users are responsible for managing multithreaded access to the ADC driver if the ADC services multiple channels.

pw::analog::MicrovoltInput#

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 configure and enable the ADC peripheral in order to provide the reference voltages and to configure and enable the ADC peripheral where needed. Users are responsible for managing multithreaded access to the ADC driver if the ADC services multiple channels.

pw::analog::GmockAnalogInput#

gMock of AnalogInput used for testing and mocking out the AnalogInput.

pw::analog::GmockMicrovoltInput#

gMock of MicrovoltInput used for testing and mocking out the MicrovoltInput.

API reference#

pw::analog::AnalogInput#

class AnalogInput#

Base interface for getting analog-to-digital (ADC) samples from one ADC channel in a thread-safe manner.

The ADC backend interface is up to the user to define and implement for now. This gives flexibility for the ADC driver implementation.

AnalogInput controls a specific input / channel where the ADC peripheral may be shared across multiple channels that may be controlled by multiple threads. The implementer of this pure virtual interface is responsible for ensuring thread safety and access at the driver level.

Subclassed by pw::analog::MicrovoltInput

Public Functions

inline Result<int32_t> TryReadFor(chrono::SystemClock::duration timeout)#

Blocks until the specified timeout duration has elapsed or the ADC sample has been returned, whichever comes first.

This method is thread safe.

Returns:

Code

Description

OK

Returns a sample.

RESOURCE_EXHAUSTED

ADC peripheral in use.

DEADLINE_EXCEEDED

Timed out waiting for a sample.

Other statuses left up to the implementer.

virtual Result<int32_t> TryReadUntil(chrono::SystemClock::time_point deadline) = 0#

Blocks until the deadline time has been reached or the ADC sample has been returned, whichever comes first.

This method is thread safe.

Returns:

Code

Description

OK

Returns a sample on success.

RESOURCE_EXHAUSTED

ADC peripheral in use.

DEADLINE_EXCEEDED

Timed out waiting for a sample.

Other statuses left up to the implementer.

virtual Limits GetLimits() const = 0#
Returns:

The range of the ADC sample. These values do not change at runtime.

struct Limits#

Specifies the sample range. These values do not change at runtime.

Public Members

int32_t min#

The minimum of the sample range.

int32_t max#

The maximum of the sample range.

pw::analog::GmockAnalogInput#


#include "gmock/gmock.h"
#include "pw_analog/analog_input.h"

namespace pw::analog {

class GmockAnalogInput : public AnalogInput {
 public:
  MOCK_METHOD(pw::Result<int32_t>,
              TryReadUntil,
              (pw::chrono::SystemClock::time_point deadline),
              (override));

  MOCK_METHOD(Limits, GetLimits, (), (const, override));
};

pw::analog::MicrovoltInput#

class MicrovoltInput : public pw::analog::AnalogInput#

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.

Public Functions

inline Result<int32_t> TryReadMicrovoltsFor(chrono::SystemClock::duration timeout)#

Blocks until the specified timeout duration has elapsed or the voltage sample has been returned, whichever comes first.

This method is thread-safe.

Returns:

Code

Description

OK

Returns a voltage sample in microvolts (uV) on success.

RESOURCE_EXHAUSTED

ADC peripheral in use.

DEADLINE_EXCEEDED

Timed out waiting for a sample.

Other statuses left up to the implementer.

inline Result<int32_t> TryReadMicrovoltsUntil(chrono::SystemClock::time_point deadline)#

Blocks until the deadline time has been reached or the voltage sample has been returned, whichever comes first.

This method is thread-safe.

Returns:

Code

Description

OK

Returns a voltage sample in microvolts (uV) on success.

RESOURCE_EXHAUSTED

ADC peripheral in use.

DEADLINE_EXCEEDED

Timed out waiting for a sample.

Other statuses left up to the implementer.

struct References#

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.

Public Members

int32_t max_voltage_uv#

Microvolts at AnalogInput::Limits::max.

int32_t min_voltage_uv#

Microvolts at AnalogInput::Limits::min.

pw::analog::GmockMicrovoltInput#


#include "gmock/gmock.h"
#include "pw_analog/microvolt_input.h"

namespace pw::analog {

class GmockMicrovoltInput : public MicrovoltInput {
 public:
  MOCK_METHOD(pw::Result<int32_t>,
              TryReadUntil,
              (pw::chrono::SystemClock::time_point deadline),
              (override));

  MOCK_METHOD(Limits, GetLimits, (), (const, override));

  MOCK_METHOD(References, GetReferences, (), (const, override));
};