Zephyr C++ Toolkit
|
#include <GpioMock.hpp>
Public Member Functions | |
GpioMock (const char *name, Direction direction=Direction::Input) | |
Create a new mock GPIO pin. Default direction is input. | |
~GpioMock () | |
Destroy the GPIO. Does nothing. | |
void | setPhysical (bool value) override |
Set the physical value of the GPIO. | |
bool | getPhysical () const override |
Get the physical value of the GPIO. | |
void | configureInterrupt (InterruptMode interruptMode, std::function< void()> callback) override |
Configure an interrupt on the GPIO. | |
void | setLogicMode (LogicMode logicMode) override |
Set the logic mode of the GPIO. | |
void | mockSetInput (bool logicalValue) |
Use this to pretend to be an external signal changing the state of an input GPIO. This sets the logical value of the GPIO. | |
void | mockSetInputPhysical (bool physicalValue) |
Use this to pretend to be an external signal changing the state of an input GPIO. This sets the physical value of the GPIO. | |
![]() | |
IGpio (const char *name, Direction direction=Direction::Input, LogicMode logicMode=LogicMode::ActiveHigh, PullMode pullMode=PullMode::None) | |
Create a new GPIO. | |
virtual | ~IGpio ()=default |
Destroy the GPIO. | |
virtual void | set (bool value) |
virtual bool | get () const |
virtual void | setDirection (Direction direction) |
virtual void | setPullMode (PullMode pullMode) |
Protected Member Functions | |
void | configurePinBasedOnSettings () override |
Configure the pin based on the current settings. | |
void | callInterruptHandlerIfNeeded (bool oldLogicalValue, bool oldPhysicalValue, bool newLogicalValue, bool newPhysicalValue) |
Helper function to determine and call the interrupt handler if needed. Calculates if it needs to be called based on the old and new GPIO values. In the real GPIO this would be done by the Zephyr GPIO driver. | |
Protected Attributes | |
bool | m_logicalValue |
![]() | |
const char * | m_name |
Direction | m_direction |
LogicMode | m_logicMode |
PullMode | m_pullMode |
InterruptMode | m_interruptMode |
std::function< void()> | m_interruptUserCallback |
Additional Inherited Members | |
![]() | |
enum class | Direction { Input , Output } |
enum class | LogicMode { ActiveHigh , ActiveLow } |
enum class | PullMode { None , PullUp , PullDown } |
enum class | InterruptMode { Disable , EdgeRising , EdgeFalling , EdgeBoth , LevelLow , LevelHigh , LevelToInactive , LevelToActive , LevelInactive , LevelActive } |
Implements a mock GPIO pin.
zct::GpioMock::GpioMock | ( | const char * | name, |
Direction | direction = Direction::Input |
||
) |
Create a new mock GPIO pin. Default direction is input.
name | The name of the GPIO. Used for logging purposes. |
direction | The direction of the GPIO. |
zct::GpioMock::~GpioMock | ( | ) |
Destroy the GPIO. Does nothing.
|
protected |
Helper function to determine and call the interrupt handler if needed. Calculates if it needs to be called based on the old and new GPIO values. In the real GPIO this would be done by the Zephyr GPIO driver.
oldLogicalValue | The old logical value of the GPIO. |
oldPhysicalValue | The old physical value of the GPIO. |
newLogicalValue | The new logical value of the GPIO. |
newPhysicalValue | The new physical value of the GPIO. |
|
overridevirtual |
Configure an interrupt on the GPIO.
The mock implementation of this just stores the interrupt mode and callback into the member variables.
Due to mock limitations, when the GPIO is an input the callback will be called correctly for edge based interrupt modes (it will be called when mockSetInput() or mockSetInputPhysical() are called), but not for level based interrupt modes.
interruptMode | The interrupt mode to set. |
callback | The callback to call when the interrupt occurs. |
Implements zct::IGpio.
|
overrideprotectedvirtual |
Configure the pin based on the current settings.
The real GPIO will call the Zephyr gpio_pin_configure_dt() function. The mock GPIO will do nothing.
Implements zct::IGpio.
|
overridevirtual |
Get the physical value of the GPIO.
This ignores the logic mode of the GPIO and returns the physical value directly.
Implements zct::IGpio.
Use this to pretend to be an external signal changing the state of an input GPIO. This sets the logical value of the GPIO.
Has no effect if the GPIO is configured as an output.
logicalValue | The logical value to set the GPIO to. |
Use this to pretend to be an external signal changing the state of an input GPIO. This sets the physical value of the GPIO.
Has no effect if the GPIO is configured as an output.
physicalValue | The physical value to set the GPIO to. |
Set the logic mode of the GPIO.
We override this in the mock because if we change the logic mode we need to preserve the current physical value, not logical. For example, a GPIO input starts of at 0V, inactive. If we change the logic mode to active low, the physical value will still be 0V, but the logical value will be active.
This could also cause logical level interrupts to be triggered.
logicMode | The logic mode to set. |
Reimplemented from zct::IGpio.
Set the physical value of the GPIO.
This ignores the logic mode of the GPIO and sets the physical value directly.
value | The physical value to set. |
Implements zct::IGpio.
|
protected |
Because it is a mock GPIO, we store the value of the GPIO here.