Singleton object used to manage GPIO. More...
#include <sg_GpioManagement.hpp>
Public Types | |
enum | EDirection { EDirection::kUnknown = 0, EDirection::kInput, EDirection::kOutput } |
enum | EValue { EValue::kInvalid = -1, EValue::kLow = 0, EValue::kHigh = 1 } |
typedef std::map< GPIO, EDirection > | MGPIODirection |
Public Member Functions | |
~GpioManagement (void) | |
Destructor. More... | |
GpioManagement & | cleanup (void) |
Perform any necessary cleanup, such as releasing (unexporting) any GPIOs that were originally exported by this instance of the SG++ library. More... | |
SGPIO | get_exported (void) |
Get the set of exported GPIO. More... | |
MGPIODirection | get_direction_map (void) |
Get the map of exported GPIO and the direction for each one. More... | |
GpioManagement & | gpio_export (const GPIO gpio, const EDirection direction=EDirection::kOutput) |
Tell the Linux kernel to export a GPIO. More... | |
GpioManagement & | gpio_unexport (const GPIO gpio) |
Tell the Linux kernel a GPIO is no longer needed. More... | |
GpioManagement & | set_direction (const GPIO gpio, const EDirection direction=EDirection::kOutput) |
Tell the Linux kernel if a GPIO will be used to reading or writing. More... | |
bool | is_exported (const GPIO gpio, int number_of_times_to_check=1) |
See if the given GPIO has been exported. More... | |
GpioManagement & | set_value (const GPIO gpio, const EValue &value) |
Set the given GPIO low or high. More... | |
EValue | get_value (const GPIO gpio) |
Get the given GPIO value. More... | |
GpioManagement & | set_value (const GPIO gpio, const bool set_to_high) |
Set the given GPIO high when TRUE and to low when FALSE . More... | |
GpioManagement & | set_low (const GPIO gpio) |
Set the given GPIO high when TRUE and to low when FALSE . More... | |
GpioManagement & | set_high (const GPIO gpio) |
Set the given GPIO high when TRUE and to low when FALSE . More... | |
bool | get_value (const GPIO gpio, bool &value) |
Alias for get_value(). More... | |
bool | get_bool (const GPIO gpio) |
Alias for get_value(). More... | |
Static Public Member Functions | |
static GpioManagement & | get (void) |
Get the singleton. More... | |
Public Attributes | |
bool | unexport_all_gpio_on_destruction |
Determine whether all exported GPIO are automatically unexported on destruction. More... | |
Friends | |
class | SGpp |
Normally GpioManagement in created and managed by SGpp. More... | |
Singleton object used to manage GPIO.
Obtain a copy of the singleton through SG::SGpp::gpio.
typedef std::map<GPIO, EDirection> SG::GpioManagement::MGPIODirection |
|
strong |
|
strong |
SG::GpioManagement::~GpioManagement | ( | void | ) |
Destructor.
References SG::SGpp::cleanup().
SG::GpioManagement & SG::GpioManagement::cleanup | ( | void | ) |
Perform any necessary cleanup, such as releasing (unexporting) any GPIOs that were originally exported by this instance of the SG++ library.
References SG::SGpp::gpio.
Referenced by SG::SGpp::cleanup().
|
static |
Get the singleton.
|
inline |
Alias for get_value().
TRUE
for high
and FALSE
for low
. References get_value().
Referenced by SG::Button::is_down(), and SG::LEDBar::send_data().
SG::GpioManagement::MGPIODirection SG::GpioManagement::get_direction_map | ( | void | ) |
Get the map of exported GPIO and the direction for each one.
This only includes the GPIOs exported by this instance of the SG++ library.
SG::SGPIO SG::GpioManagement::get_exported | ( | void | ) |
Get the set of exported GPIO.
This only includes the GPIOs exported by this instance of the SG++ library.
SG::GpioManagement::EValue SG::GpioManagement::get_value | ( | const GPIO | gpio | ) |
Get the given GPIO value.
The GPIO must be exported.
std::system_error | if the GPIO value file cannot be opened with read access. |
References kHigh, and SG::BeagleBone::Detect::to_string().
Referenced by get_bool(), and set_high().
|
inline |
Alias for get_value().
TRUE
for high
and FALSE
for low
. References get_value(), and kHigh.
Referenced by get_value().
SG::GpioManagement & SG::GpioManagement::gpio_export | ( | const GPIO | gpio, |
const EDirection | direction = EDirection::kOutput |
||
) |
Tell the Linux kernel to export a GPIO.
std::invalid_argument | if the specified GPIO has already been exported. |
std::system_error | if the GPIO export file cannot be opened with write access. |
std::runtime_error | if the specified GPIO fails to export after several tries. |
References SG::SGpp::gpio, SG::kUnknown, and SG::BeagleBone::Detect::to_string().
Referenced by SG::Button::Button(), SG::Buzzer::Buzzer(), SG::ChainableRGBLED::ChainableRGBLED(), SG::LEDBar::LEDBar(), and SG::VariableColorLED::VariableColorLED().
SG::GpioManagement & SG::GpioManagement::gpio_unexport | ( | const GPIO | gpio | ) |
Tell the Linux kernel a GPIO is no longer needed.
std::system_error | if the GPIO unexport file cannot be opened with write access. |
Referenced by SG::Button::~Button(), SG::Buzzer::~Buzzer(), SG::ChainableRGBLED::~ChainableRGBLED(), SG::LEDBar::~LEDBar(), and SG::VariableColorLED::~VariableColorLED().
bool SG::GpioManagement::is_exported | ( | const GPIO | gpio, |
int | number_of_times_to_check = 1 |
||
) |
See if the given GPIO has been exported.
If the GPIO was just exported, it may be necessary to retry several times, where it pauses briefly between each attempt. By default, it only checks once.
true
if the GPIO is already exported. false
if the GPIO has not been exported. /sys/class/gpio/gpioN
where "N" is the GPIO number. References SG::BeagleBone::Detect::to_string().
SG::GpioManagement & SG::GpioManagement::set_direction | ( | const GPIO | gpio, |
const EDirection | direction = EDirection::kOutput |
||
) |
Tell the Linux kernel if a GPIO will be used to reading or writing.
A GPIO must first be exported for this to work. This normally doesn't need to be called unless the direction is changing, since gpio_export() allows the direction to be set when the GPIO is originally exported.
invalid_argument | if the specified GPIO has not been exported. |
std::system_error | if the GPIO direction file cannot be opened with write access. |
References SG::SGpp::gpio, and SG::BeagleBone::Detect::to_string().
|
inline |
Set the given GPIO high when TRUE
and to low when FALSE
.
The GPIO must be exported, and the direction must be set to EDirection::kOutput.
References get_value(), and set_value().
Referenced by SG::GroveGPIO::send_byte(), SG::LEDBar::send_data(), SG::VariableColorLED::turn_off(), SG::Buzzer::turn_off(), SG::Buzzer::turn_on(), and SG::VariableColorLED::turn_on().
|
inline |
Set the given GPIO high when TRUE
and to low when FALSE
.
The GPIO must be exported, and the direction must be set to EDirection::kOutput.
References set_value().
Referenced by SG::GroveGPIO::send_byte(), SG::LEDBar::send_data(), SG::Buzzer::turn_off(), SG::VariableColorLED::turn_off(), SG::VariableColorLED::turn_on(), and SG::Buzzer::turn_on().
SG::GpioManagement & SG::GpioManagement::set_value | ( | const GPIO | gpio, |
const EValue & | value | ||
) |
Set the given GPIO low or high.
The GPIO must be exported, and the direction must be set to EDirection::kOutput.
invalid_argument | if the value is anything other than kLow or kHigh. |
invalid_argument | if the specified GPIO has not been exported. |
std::system_error | if the GPIO value file cannot be opened with write access. |
References SG::BeagleBone::Detect::to_string().
Referenced by SG::LEDBar::send_data(), set_high(), and set_low().
|
inline |
Set the given GPIO high when TRUE
and to low when FALSE
.
The GPIO must be exported, and the direction must be set to EDirection::kOutput.
|
friend |
Normally GpioManagement in created and managed by SGpp.
bool SG::GpioManagement::unexport_all_gpio_on_destruction |
Determine whether all exported GPIO are automatically unexported on destruction.
This only includes GPIOs that were exported by the SG++ library. Default is true
to ensure all GPIOs are released. The equivalent functionality can be obtained at any time by manually calling cleanup().