Skip to content

Commit

Permalink
feat: extend gpio interrupt interface to enable immediate interrupt h…
Browse files Browse the repository at this point in the history
…andlers (#721)
  • Loading branch information
daantimmer authored Oct 4, 2024
1 parent 7821a57 commit 95ddf85
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 27 deletions.
7 changes: 4 additions & 3 deletions hal/interfaces/Gpio.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hal/interfaces/Gpio.hpp"
#include "infra/util/Function.hpp"

namespace hal
{
Expand All @@ -20,9 +21,9 @@ namespace hal
return pin.Get();
}

void InputPin::EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger)
void InputPin::EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger, InterruptType type)
{
pin.EnableInterrupt(action, trigger);
pin.EnableInterrupt(action, trigger, type);
}

void InputPin::DisableInterrupt()
Expand Down Expand Up @@ -133,7 +134,7 @@ namespace hal
void DummyPin::ResetConfig()
{}

void DummyPin::EnableInterrupt(const infra::Function<void()>& actionOnInterrupt, InterruptTrigger trigger)
void DummyPin::EnableInterrupt(const infra::Function<void()>& actionOnInterrupt, InterruptTrigger trigger, InterruptType type)
{}

void DummyPin::DisableInterrupt()
Expand Down
13 changes: 10 additions & 3 deletions hal/interfaces/Gpio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HAL_GPIO_HPP

#include "infra/util/Function.hpp"
#include <cstdint>

namespace hal
{
Expand All @@ -12,6 +13,12 @@ namespace hal
bothEdges
};

enum class InterruptType : uint8_t
{
dispatched,
immediate
};

enum class PinConfigType : uint8_t
{
input,
Expand Down Expand Up @@ -40,7 +47,7 @@ namespace hal
virtual void Config(PinConfigType config, bool startOutputState) = 0;
virtual void ResetConfig() = 0;

virtual void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger) = 0;
virtual void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger, InterruptType type = InterruptType::dispatched) = 0;
virtual void DisableInterrupt() = 0;
};

Expand All @@ -54,7 +61,7 @@ namespace hal

bool Get() const;

void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger);
void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger, InterruptType type = InterruptType::dispatched);
void DisableInterrupt();

private:
Expand Down Expand Up @@ -110,7 +117,7 @@ namespace hal
void Config(PinConfigType config) override;
void Config(PinConfigType config, bool startOutputState) override;
void ResetConfig() override;
void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger) override;
void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger, InterruptType type = InterruptType::dispatched) override;
void DisableInterrupt() override;
};

Expand Down
21 changes: 11 additions & 10 deletions hal/interfaces/test_doubles/GpioMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HAL_GPIO_MOCK_HPP

#include "hal/interfaces/Gpio.hpp"
#include "infra/util/Function.hpp"
#include "gmock/gmock.h"

namespace hal
Expand All @@ -10,16 +11,16 @@ namespace hal
: public GpioPin
{
public:
MOCK_CONST_METHOD0(Get, bool());
MOCK_METHOD1(Set, void(bool value));
MOCK_CONST_METHOD0(GetOutputLatch, bool());
MOCK_METHOD0(SetAsInput, void());
MOCK_CONST_METHOD0(IsInput, bool());
MOCK_METHOD1(Config, void(PinConfigType config));
MOCK_METHOD2(Config, void(PinConfigType config, bool startOutputState));
MOCK_METHOD0(ResetConfig, void());
MOCK_METHOD2(EnableInterrupt, void(const infra::Function<void()>& action, InterruptTrigger trigger));
MOCK_METHOD0(DisableInterrupt, void());
MOCK_METHOD(bool, Get, (), (const override));
MOCK_METHOD(void, Set, (bool value), (override));
MOCK_METHOD(bool, GetOutputLatch, (), (const override));
MOCK_METHOD(void, SetAsInput, (), (override));
MOCK_METHOD(bool, IsInput, (), (const override));
MOCK_METHOD(void, Config, (PinConfigType config), (override));
MOCK_METHOD(void, Config, (PinConfigType config, bool startOutputState), (override));
MOCK_METHOD(void, ResetConfig, (), (override));
MOCK_METHOD(void, EnableInterrupt, (const infra::Function<void()>& action, InterruptTrigger trigger, InterruptType type), (override));
MOCK_METHOD(void, DisableInterrupt, (), (override));
};
}

Expand Down
8 changes: 7 additions & 1 deletion hal/interfaces/test_doubles/GpioStub.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "hal/interfaces/test_doubles/GpioStub.hpp"
#include "hal/interfaces/Gpio.hpp"
#include "infra/timer/Timer.hpp"
#include "infra/util/CompareMembers.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/Optional.hpp"
#include <utility>
#include <vector>

namespace hal
{
Expand Down Expand Up @@ -50,7 +56,7 @@ namespace hal
void GpioPinStub::ResetConfig()
{}

void GpioPinStub::EnableInterrupt(const infra::Function<void()>& actionOnInterrupt, InterruptTrigger trigger)
void GpioPinStub::EnableInterrupt(const infra::Function<void()>& actionOnInterrupt, InterruptTrigger trigger, InterruptType type)
{
triggerOnChange = std::make_pair(actionOnInterrupt, trigger);
}
Expand Down
10 changes: 4 additions & 6 deletions hal/interfaces/test_doubles/GpioStub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
#define HAL_GPIO_STUB_HPP

#include "hal/interfaces/Gpio.hpp"
#include "infra/timer/TimerService.hpp"
#include "infra/timer/Timer.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/Optional.hpp"
#include "infra/util/VariantDetail.hpp"
#include <array>
#include <functional>
#include <map>
#include <utility>
#include <vector>

namespace hal
Expand All @@ -26,7 +24,7 @@ namespace hal
void Config(PinConfigType config) override;
void Config(PinConfigType config, bool startOutputState) override;
void ResetConfig() override;
void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger) override;
void EnableInterrupt(const infra::Function<void()>& action, InterruptTrigger trigger, InterruptType type) override;
void DisableInterrupt() override;

void SetStubState(bool value);
Expand Down
9 changes: 7 additions & 2 deletions services/util/GpioPinInverted.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "services/util/GpioPinInverted.hpp"
#include "hal/interfaces/Gpio.hpp"
#include "infra/util/Function.hpp"
#include <array>
#include <cassert>
#include <cstdint>

namespace services
{
Expand Down Expand Up @@ -46,12 +51,12 @@ namespace services
pin.ResetConfig();
}

void GpioPinInverted::EnableInterrupt(const infra::Function<void()>& action, hal::InterruptTrigger trigger)
void GpioPinInverted::EnableInterrupt(const infra::Function<void()>& action, hal::InterruptTrigger trigger, hal::InterruptType type)
{
static const std::array<hal::InterruptTrigger, 3> inverse = { hal::InterruptTrigger::fallingEdge, hal::InterruptTrigger::risingEdge,
hal::InterruptTrigger::bothEdges };
assert(static_cast<uint8_t>(trigger) < inverse.size());
pin.EnableInterrupt(action, inverse[static_cast<uint8_t>(trigger)]);
pin.EnableInterrupt(action, inverse[static_cast<uint8_t>(trigger)], type);
}

void GpioPinInverted::DisableInterrupt()
Expand Down
4 changes: 2 additions & 2 deletions services/util/GpioPinInverted.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SERVICES_GPIO_PIN_INVERTED_HPP

#include "hal/interfaces/Gpio.hpp"
#include "infra/timer/Timer.hpp"
#include "infra/util/Function.hpp"

namespace services
{
Expand All @@ -20,7 +20,7 @@ namespace services
void Config(hal::PinConfigType config) override;
void Config(hal::PinConfigType config, bool startOutputState) override;
void ResetConfig() override;
void EnableInterrupt(const infra::Function<void()>& action, hal::InterruptTrigger trigger) override;
void EnableInterrupt(const infra::Function<void()>& action, hal::InterruptTrigger trigger, hal::InterruptType type) override;
void DisableInterrupt() override;

private:
Expand Down

0 comments on commit 95ddf85

Please sign in to comment.