Zephyr C++ Toolkit
Loading...
Searching...
No Matches
zct::GpioMock Class Reference

#include <GpioMock.hpp>

Inheritance diagram for zct::GpioMock:
Collaboration diagram for zct::GpioMock:

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.
 
- Public Member Functions inherited from zct::IGpio
 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
 
- Protected Attributes inherited from zct::IGpio
const charm_name
 
Direction m_direction
 
LogicMode m_logicMode
 
PullMode m_pullMode
 
InterruptMode m_interruptMode
 
std::function< void()> m_interruptUserCallback
 

Additional Inherited Members

- Public Types inherited from zct::IGpio
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
}
 

Detailed Description

Implements a mock GPIO pin.

Constructor & Destructor Documentation

◆ GpioMock()

zct::GpioMock::GpioMock ( const char name,
Direction  direction = Direction::Input 
)

Create a new mock GPIO pin. Default direction is input.

Parameters
nameThe name of the GPIO. Used for logging purposes.
directionThe direction of the GPIO.

◆ ~GpioMock()

zct::GpioMock::~GpioMock ( )

Destroy the GPIO. Does nothing.

Member Function Documentation

◆ callInterruptHandlerIfNeeded()

void zct::GpioMock::callInterruptHandlerIfNeeded ( bool  oldLogicalValue,
bool  oldPhysicalValue,
bool  newLogicalValue,
bool  newPhysicalValue 
)
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.

Parameters
oldLogicalValueThe old logical value of the GPIO.
oldPhysicalValueThe old physical value of the GPIO.
newLogicalValueThe new logical value of the GPIO.
newPhysicalValueThe new physical value of the GPIO.

◆ configureInterrupt()

void zct::GpioMock::configureInterrupt ( InterruptMode  interruptMode,
std::function< void()>  callback 
)
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.

Parameters
interruptModeThe interrupt mode to set.
callbackThe callback to call when the interrupt occurs.

Implements zct::IGpio.

◆ configurePinBasedOnSettings()

void zct::GpioMock::configurePinBasedOnSettings ( )
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.

◆ getPhysical()

bool zct::GpioMock::getPhysical ( ) const
overridevirtual

Get the physical value of the GPIO.

This ignores the logic mode of the GPIO and returns the physical value directly.

Returns
The physical value of the GPIO.

Implements zct::IGpio.

◆ mockSetInput()

void zct::GpioMock::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.

Has no effect if the GPIO is configured as an output.

See also
mockSetInputPhysical()
Parameters
logicalValueThe logical value to set the GPIO to.

◆ mockSetInputPhysical()

void zct::GpioMock::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.

Has no effect if the GPIO is configured as an output.

See also
mockSetInput()
Parameters
physicalValueThe physical value to set the GPIO to.

◆ setLogicMode()

void zct::GpioMock::setLogicMode ( LogicMode  logicMode)
overridevirtual

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.

Parameters
logicModeThe logic mode to set.

Reimplemented from zct::IGpio.

◆ setPhysical()

void zct::GpioMock::setPhysical ( bool  value)
overridevirtual

Set the physical value of the GPIO.

This ignores the logic mode of the GPIO and sets the physical value directly.

Parameters
valueThe physical value to set.

Implements zct::IGpio.

Member Data Documentation

◆ m_logicalValue

bool zct::GpioMock::m_logicalValue
protected

Because it is a mock GPIO, we store the value of the GPIO here.


The documentation for this class was generated from the following files: