Skip to content

Commit

Permalink
pw_digital_io: add IsStateActive and setters
Browse files Browse the repository at this point in the history
Adds methods IsStateActive, SetStateActive, and SetStateInactive to
query/update the active state of the line. These are wrappers for
GetState and SetState.

Updated tests to verify that derived classes expose the expected API
methods.

Change-Id: Ie17d48c0fb7adf9924ec7aeada56567018a9891e
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/98080
Reviewed-by: Tom Craig <[email protected]>
Commit-Queue: Anton Markov <[email protected]>
Reviewed-by: Wyatt Hepler <[email protected]>
  • Loading branch information
antmar authored and CQ Bot Account committed Jun 27, 2022
1 parent 7e48ff5 commit 116fb0d
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 4 deletions.
65 changes: 61 additions & 4 deletions pw_digital_io/digital_io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,63 @@ static_assert(

void FakeInterruptHandler(State) {}

void TestInput(DigitalIoOptional& line) {
template <typename Line>
void TestInput(Line& line) {
ASSERT_EQ(OkStatus(), line.Enable());

auto state_result = line.GetState();
ASSERT_EQ(OkStatus(), state_result.status());
ASSERT_EQ(State::kInactive, state_result.value());

auto active_result = line.IsStateActive();
ASSERT_EQ(OkStatus(), active_result.status());
ASSERT_EQ(false, active_result.value());

ASSERT_EQ(OkStatus(), line.Disable());
}

void TestOutput(DigitalIoOptional& line) {
template <typename Line>
void TestOutput(Line& line) {
ASSERT_EQ(OkStatus(), line.Enable());

ASSERT_EQ(OkStatus(), line.SetState(State::kActive));
ASSERT_EQ(OkStatus(), line.SetState(State::kInactive));

ASSERT_EQ(OkStatus(), line.SetStateActive());
ASSERT_EQ(OkStatus(), line.SetStateInactive());

ASSERT_EQ(OkStatus(), line.Disable());
}

void TestOutputReadback(DigitalIoOptional& line) {
template <typename Line>
void TestOutputReadback(Line& line) {
ASSERT_EQ(OkStatus(), line.Enable());

ASSERT_EQ(OkStatus(), line.SetState(State::kActive));
auto state_result = line.GetState();
ASSERT_EQ(OkStatus(), state_result.status());
ASSERT_EQ(State::kActive, state_result.value());

ASSERT_EQ(OkStatus(), line.SetState(State::kInactive));
state_result = line.GetState();
ASSERT_EQ(OkStatus(), state_result.status());
ASSERT_EQ(State::kInactive, state_result.value());

ASSERT_EQ(OkStatus(), line.SetStateActive());
auto active_result = line.IsStateActive();
ASSERT_EQ(OkStatus(), active_result.status());
ASSERT_EQ(true, active_result.value());

ASSERT_EQ(OkStatus(), line.SetStateInactive());
active_result = line.IsStateActive();
ASSERT_EQ(OkStatus(), active_result.status());
ASSERT_EQ(false, active_result.value());

ASSERT_EQ(OkStatus(), line.Disable());
}

void TestInterrupt(DigitalIoOptional& line) {
template <typename Line>
void TestInterrupt(Line& line) {
ASSERT_EQ(OkStatus(), line.Enable());

ASSERT_EQ(OkStatus(),
Expand All @@ -225,6 +252,9 @@ TEST(Digital, Interrupt) {
ASSERT_EQ(true, line.provides_interrupt());

TestInterrupt(line);

DigitalIoOptional& optional_line = line;
TestInterrupt(optional_line);
}

TEST(Digital, In) {
Expand All @@ -235,6 +265,9 @@ TEST(Digital, In) {
ASSERT_EQ(false, line.provides_interrupt());

TestInput(line);

DigitalIoOptional& optional_line = line;
TestInput(optional_line);
}

TEST(Digital, InInterrupt) {
Expand All @@ -246,6 +279,10 @@ TEST(Digital, InInterrupt) {

TestInput(line);
TestInterrupt(line);

DigitalIoOptional& optional_line = line;
TestInput(optional_line);
TestInterrupt(optional_line);
}

TEST(Digital, Out) {
Expand All @@ -256,6 +293,9 @@ TEST(Digital, Out) {
ASSERT_EQ(false, line.provides_interrupt());

TestOutput(line);

DigitalIoOptional& optional_line = line;
TestOutput(optional_line);
}

TEST(Digital, OutInterrupt) {
Expand All @@ -267,6 +307,10 @@ TEST(Digital, OutInterrupt) {

TestOutput(line);
TestInterrupt(line);

DigitalIoOptional& optional_line = line;
TestOutput(optional_line);
TestInterrupt(optional_line);
}

TEST(Digital, InOut) {
Expand All @@ -277,7 +321,13 @@ TEST(Digital, InOut) {
ASSERT_EQ(false, line.provides_interrupt());

TestInput(line);
TestOutput(line);
TestOutputReadback(line);

DigitalIoOptional& optional_line = line;
TestInput(optional_line);
TestOutput(optional_line);
TestOutputReadback(optional_line);
}

TEST(DigitalIo, InOutInterrupt) {
Expand All @@ -288,8 +338,15 @@ TEST(DigitalIo, InOutInterrupt) {
ASSERT_EQ(true, line.provides_interrupt());

TestInput(line);
TestOutput(line);
TestOutputReadback(line);
TestInterrupt(line);

DigitalIoOptional& optional_line = line;
TestInput(optional_line);
TestOutput(optional_line);
TestOutputReadback(optional_line);
TestInterrupt(optional_line);
}

} // namespace
Expand Down
69 changes: 69 additions & 0 deletions pw_digital_io/public/pw_digital_io/digital_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,54 @@ class DigitalIoOptional {
//
Status SetState(State state) { return DoSetState(state); }

// Check if the line is in the active state.
//
// The line is in the active state when GetState() returns State::kActive.
//
// This method is not thread-safe and cannot be used in interrupt handlers.
//
// Returns:
//
// OK - true if the line is in the active state, otherwise false.
// FAILED_PRECONDITION - The line has not been enabled.
// Other status codes as defined by the backend.
//
Result<bool> IsStateActive() {
PW_TRY_ASSIGN(const State state, GetState());
return state == State::kActive;
}

// Sets the line to the active state. Equivalent to SetState(State::kActive).
//
// Callers are responsible to wait for the voltage level to settle after this
// call returns.
//
// This method is not thread-safe and cannot be used in interrupt handlers.
//
// Returns:
//
// OK - the state has been set.
// FAILED_PRECONDITION - The line has not been enabled.
// Other status codes as defined by the backend.
//
Status SetStateActive() { return SetState(State::kActive); }

// Sets the line to the inactive state. Equivalent to
// SetState(State::kInactive).
//
// Callers are responsible to wait for the voltage level to settle after this
// call returns.
//
// This method is not thread-safe and cannot be used in interrupt handlers.
//
// Returns:
//
// OK - the state has been set.
// FAILED_PRECONDITION - The line has not been enabled.
// Other status codes as defined by the backend.
//
Status SetStateInactive() { return SetState(State::kInactive); }

// Set an interrupt handler to execute when an interrupt is triggered, and
// Configure the condition for triggering the interrupt.
//
Expand Down Expand Up @@ -247,7 +295,10 @@ class DigitalInterrupt
private:
// Unavailable functionality
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

// These overrides invoke PW_CRASH.
Status DoSetState(State) final;
Expand All @@ -266,6 +317,7 @@ class DigitalIn : public DigitalIoOptional,
public:
// Available functionality
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;

protected:
constexpr DigitalIn()
Expand All @@ -278,6 +330,8 @@ class DigitalIn : public DigitalIoOptional,
using DigitalIoOptional::EnableInterruptHandler;
using DigitalIoOptional::SetInterruptHandler;
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

// These overrides invoke PW_CRASH.
Status DoSetState(State) final;
Expand All @@ -303,6 +357,7 @@ class DigitalInInterrupt
using DigitalIoOptional::DisableInterruptHandler;
using DigitalIoOptional::EnableInterruptHandler;
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;
using DigitalIoOptional::SetInterruptHandler;

protected:
Expand All @@ -312,6 +367,8 @@ class DigitalInInterrupt
private:
// Unavailable functionality
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

// These overrides invoke PW_CRASH.
Status DoSetState(State) final;
Expand All @@ -329,6 +386,8 @@ class DigitalOut : public DigitalIoOptional,
public:
// Available functionality
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

protected:
constexpr DigitalOut()
Expand All @@ -340,6 +399,7 @@ class DigitalOut : public DigitalIoOptional,
using DigitalIoOptional::DisableInterruptHandler;
using DigitalIoOptional::EnableInterruptHandler;
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;
using DigitalIoOptional::SetInterruptHandler;

// These overrides invoke PW_CRASH.
Expand Down Expand Up @@ -368,6 +428,8 @@ class DigitalOutInterrupt
using DigitalIoOptional::EnableInterruptHandler;
using DigitalIoOptional::SetInterruptHandler;
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

protected:
constexpr DigitalOutInterrupt()
Expand All @@ -376,6 +438,7 @@ class DigitalOutInterrupt
private:
// Unavailable functionality
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;

// These overrides invoke PW_CRASH.
Result<State> DoGetState() final;
Expand All @@ -395,7 +458,10 @@ class DigitalInOut
public:
// Available functionality
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

protected:
constexpr DigitalInOut()
Expand Down Expand Up @@ -430,8 +496,11 @@ class DigitalInOutInterrupt
using DigitalIoOptional::DisableInterruptHandler;
using DigitalIoOptional::EnableInterruptHandler;
using DigitalIoOptional::GetState;
using DigitalIoOptional::IsStateActive;
using DigitalIoOptional::SetInterruptHandler;
using DigitalIoOptional::SetState;
using DigitalIoOptional::SetStateActive;
using DigitalIoOptional::SetStateInactive;

protected:
constexpr DigitalInOutInterrupt()
Expand Down

0 comments on commit 116fb0d

Please sign in to comment.