BlackLib::BlackGPIO Class Reference

Interacts with end user, to use GPIO. More...

#include <BlackGPIO.h>

Inheritance diagram for BlackLib::BlackGPIO:

Public Types

enum  flags {
  exportFileErr = 0,
  exportErr = 1,
  directionFileErr = 2,
  directionErr = 3,
  readErr = 4,
  writeErr = 5,
  forcingErr = 6
}
 

Public Member Functions

 BlackGPIO (gpioName pn, direction pd, workingMode wm=SecureMode)
 Constructor of BlackGPIO class. More...
 
virtual ~BlackGPIO ()
 Destructor of BlackGPIO class. More...
 
std::string getValue ()
 Reads value of gpio pin as string type. More...
 
int getNumericValue ()
 Reads value of gpio pin as int type. More...
 
gpioName getName ()
 Exports name of gpio pin. More...
 
direction getDirection ()
 Exports direction of gpio pin. More...
 
bool setValue (digitalValue v)
 Sets value of GPIO pin. More...
 
bool isHigh ()
 Checks value of GPIO pin. More...
 
void toggleValue ()
 Toggles value of GPIO pin. More...
 
void setWorkingMode (workingMode newWM)
 Changes working mode. More...
 
workingMode getWorkingMode ()
 Exports working mode value. More...
 
bool fail ()
 Is used for general debugging. More...
 
bool fail (BlackGPIO::flags f)
 Is used for specific debugging. More...
 
BlackGPIOoperator>> (std::string &readToThis)
 Reads value of gpio pin as string type with ">>" operator. More...
 
BlackGPIOoperator>> (int &readToThis)
 Reads value of gpio pin as int type with ">>" operator. More...
 
BlackGPIOoperator<< (digitalValue value)
 Sets value of GPIO pin with "<<" operator. More...
 

Private Member Functions

bool isExported ()
 Checks the export state of GPIO pin. More...
 
bool isDirectionSet ()
 Checks direction of GPIO pin. More...
 
bool isReady ()
 Checks ready state of GPIO pin. More...
 
- Private Member Functions inherited from BlackLib::BlackCoreGPIO
 BlackCoreGPIO (gpioName pin, direction dir)
 Constructor of BlackCoreGPIO class. More...
 
virtual ~BlackCoreGPIO ()
 Destructor of BlackCoreGPIO class. More...
 
std::string getDirectionFilePath ()
 Exports direction file path to derived class. More...
 
std::string getValueFilePath ()
 Exports value file path to derived class. More...
 
errorCoreGPIOgetErrorsFromCoreGPIO ()
 Exports errorCoreGPIO struct to derived class. More...
 

Private Attributes

errorGPIOgpioErrors
 is used to hold the errors of BlackGPIO class More...
 
gpioName pinName
 is used to hold the selected GPIO pin name More...
 
direction pinDirection
 is used to hold the selected GPIO pin direction More...
 
workingMode workMode
 is used to hold the selected working mode More...
 
std::string valuePath
 is used to hold the value file path More...
 

Detailed Description

Interacts with end user, to use GPIO.

This class is end node to use GPIO. End users interact with gpio pins from this class. It includes public functions to set and get properties of GPIO.

Example
EXAMPLE PROJECT FILE TREE:

   myGpioProject
   |-> src
       |-> BlackLib
           |-> BlackDirectory
               |-> BlackDirectory.cpp
               |-> BlackDirectory.h
           |-> BlackThread
               |-> BlackThread.cpp
               |-> BlackThread.h
           |-> BlackMutex
               |-> BlackMutex.cpp
               |-> BlackMutex.h
           |-> BlackTime
               |-> BlackTime.cpp
               |-> BlackTime.h
           |-> BlackADC
               |-> BlackADC.cpp
               |-> BlackADC.h
           |-> BlackGPIO
               |-> BlackGPIO.cpp
               |-> BlackGPIO.h
           |-> BlackI2C
               |-> BlackI2C.cpp
               |-> BlackI2C.h
           |-> BlackPWM
               |-> BlackPWM.cpp
               |-> BlackPWM.h
           |-> BlackSPI
               |-> BlackSPI.cpp
               |-> BlackSPI.h
           |-> BlackUART
               |-> BlackUART.cpp
               |-> BlackUART.h
           |-> BlackCore.cpp
           |-> BlackCore.h
           |-> BlackDef.h
           |-> BlackErr.h
           |-> BlackLib.h
       |-> myGpioProject.cpp


If BlackLib source files are located in your project like above example project file tree, you have to include BlackGPIO.h or another source files with adding this line to your project file (myGpioProject.cpp at the example):
#include "BlackLib/BlackGPIO/BlackGPIO.h"


If BlackLib source files are located at same level with your project file (myGpioProject.cpp at the example), you have to include BlackGPIO.h or another source files with adding this line to your project file:

// Filename: myGpioProject.cpp
// Author: Yiğit Yüce - ygtyce@gmail.com
#include <iostream>
#include "BlackLib/BlackGPIO/BlackGPIO.h"
int main()
{
std::cout << myGpio.getValue();
return 0;
}


You can use "using namespace BlackLib" also. You can get rid of writing "BlackLib::", with using this method.
// Filename: myGpioProject.cpp
// Author: Yiğit Yüce - ygtyce@gmail.com
#include <iostream>
#include "BlackLib/BlackGPIO/BlackGPIO.h"
using namespace BlackLib;
int main()
{
std::cout << myGpio.getValue();
return 0;
}

Member Enumeration Documentation

This enum is used to define GPIO debugging flags.

Enumerator
exportFileErr 

enumeration for errorCoreGPIO::exportFileError status

exportErr 

enumeration for errorGPIO::exportError status

directionFileErr 

enumeration for errorCoreGPIO::directionFileError status

directionErr 

enumeration for errorGPIO::directionError status

readErr 

enumeration for errorGPIO::readError status

writeErr 

enumeration for errorGPIO::writeError status

forcingErr 

enumeration for errorGPIO::forcingError status

Constructor & Destructor Documentation

BlackLib::BlackGPIO::BlackGPIO ( gpioName  pn,
direction  pd,
workingMode  wm = SecureMode 
)

Constructor of BlackGPIO class.

This function initializes BlackCoreGPIO class with entered parameters and errorGPIO struct. Then it sets value file path variable.

Parameters
[in]pngpio pin name(enum)
[in]pdgpio pin direction(enum)
[in]wmworking mode(enum), default value is SecureMode
Example
// Pin:30 - Direction:Out - Working Mode:SecureMode
// Pin:60 - Direction:Out - Working Mode:FastMode
// Pin:40 - Direction:In - Working Mode:SecureMode
myGpio.getValue();
myGpio2.getValue();
myGpioPtr->getValue();
See also
gpioName
direction
workingMode
BlackLib::BlackGPIO::~BlackGPIO ( )
virtual

Destructor of BlackGPIO class.

This function deletes errorGPIO struct pointer.

Member Function Documentation

bool BlackLib::BlackGPIO::fail ( )

Is used for general debugging.

Returns
True if any error occured, else false.
Example
myGpio.getNumericValue();
if( myGpio.fail() )
{
std::cout << "ERROR OCCURED" << std::endl;
}
else
{
std::cout << "EVERYTHING IS OK" << std::endl;
}
// Possible Output:
// EVERYTHING IS OK
See also
errorGPIO
bool BlackLib::BlackGPIO::fail ( BlackGPIO::flags  f)

Is used for specific debugging.

You can use this function, after call BlackGPIO member functions in your code. The input parameter is used for finding out status of selected error.

Parameters
[in]fspecific error type (enum)
Returns
Value of selected error.
Example
{
std::cout << "GPIO30: BlackGPIO INITIALIZATION FAILED" << std::endl;
}
myGpio.setValue(BlackLib::high);
{
std::cout << "GPIO30: You are in secure mode and isReady() function failed." << std::endl;
}
{
std::cout << "GPIO30: You are tried to set some value to input type pin."
<< " This operation is not valid." << std::endl;
}
{
std::cout << "GPIO30: You could not write anything." << std::endl;
}
{
std::cout << "GPIO60: BlackGPIO INITIALIZATION FAILED" << std::endl;
}
myGpio2.setValue(BlackLib::high);
{
std::cout << "GPIO60: This is not meaningful. Because you are in fast mode and these flags" << std::endl
<< "could not change in fast mode." << std::endl;
}
{
std::cout << "GPIO60: You are tried to set some value to input type pin."
<< " This operation is not valid." << std::endl;
}
if( myGpio2.fail(BlackLib::BlackGPIO::writeErr) )
{
std::cout << "GPIO60: You could not write anything." << std::endl;
}
myGpio2.getValue();
if( ! myGpio2.fail(BlackLib::BlackGPIO::readErr) )
{
std::cout << "GPIO60: You are read some value from GPIO60 successfully." << std::endl;
}
// Possible Output:
// GPIO60: You are tried to set some value to input type pin. This operation is not valid.
// GPIO60: You could not write anything.
// GPIO60: You are read some value from GPIO60 successfully.
See also
errorGPIO
direction BlackLib::BlackGPIO::getDirection ( )

Exports direction of gpio pin.

Returns
BlackGPIO::pinDirection variable.
Example
std::cout << "Values: input=1, output=2, bothDirection=3" << std::endl << std::endl;
std::cout << "My gpio direction: " << static_cast<int>(myGpio.getDirection()) << std::endl;
std::cout << "My gpio direction: " << (int)myGpio.getDirection() << std::endl;
std::cout << "My gpio direction: " << myGpio.getDirection() << std::endl;
// Possible Output:
// Values: input=1, output=2, bothDirection=3
//
// My gpio direction: 2
// My gpio direction: 2
// My gpio direction: 2
gpioName BlackLib::BlackGPIO::getName ( )

Exports name of gpio pin.

Returns
BlackGPIO::pinName variable.
Example
std::cout << "My gpio name: GPIO_" << static_cast<int>(myGpio.getName()) << std::endl;
std::cout << "My gpio name: GPIO_" << (int)myGpio.getName() << std::endl;
std::cout << "My gpio name: GPIO_" << myGpio.getName() << std::endl;
// Possible Output:
// My gpio name: GPIO_30
// My gpio name: GPIO_30
// My gpio name: GPIO_30
int BlackLib::BlackGPIO::getNumericValue ( )

Reads value of gpio pin as int type.

If working mode is selected SecureMode, this function checks pin ready state by calling isReady() function. If pin is not ready, function returns with BlackLib::GPIO_PIN_NOT_READY_INT value. If working mode is selected FastMode, ready state checking will skip. Then it reads specified file from path, where defined at BlackGPIO::valuePath variable. This file holds gpio pin value.

Returns
int type GPIO pin value. If file opening fails, it returns BlackLib::FILE_COULD_NOT_OPEN_INT or if pin isn't ready, it returns BlackLib::GPIO_PIN_NOT_READY_INT.
Example
int val = myGpio.getNumericValue();
std::cout << "Gpio30 Value: " << val;
// Possible Output:
// Gpio30 Value: 1
std::string BlackLib::BlackGPIO::getValue ( )
virtual

Reads value of gpio pin as string type.

If working mode is selected SecureMode, this function checks pin ready state by calling isReady() function. If pin is not ready, function returns with BlackLib::GPIO_PIN_NOT_READY_STRING value. If working mode is selected FastMode, ready state checking will skip. Then it reads specified file from path, where defined at BlackGPIO::valuePath variable. This file holds gpio pin value.

Returns
string type GPIO pin value. If file opening fails, it returns BlackLib::FILE_COULD_NOT_OPEN_STRING or if pin isn't ready, it returns BlackLib::GPIO_PIN_NOT_READY_STRING.
Example
std::string val = myGpio.getValue();
std::cout << "Gpio30 Value: " << val;
// Possible Output:
// Gpio30 Value: 1

Implements BlackLib::BlackCoreGPIO.

workingMode BlackLib::BlackGPIO::getWorkingMode ( )

Exports working mode value.

Returns
BlackLib::workingMode variable.
Example
std::cout << "Your current working mode is "
<< ( (myGpio.getWorkingMode() == BlackLib::SecureMode) ? "Secure Mode" : "Fast Mode" ) << std::endl;
// Possible Output:
// Your current working mode is Secure Mode
bool BlackLib::BlackGPIO::isDirectionSet ( )
private

Checks direction of GPIO pin.

This function reads specified file from path, where defined at BlackGPIO::directionPath variable.

Returns
True if direction file can open successfully and its value is equal to defined direction when class initialization, else false.
bool BlackLib::BlackGPIO::isExported ( )
private

Checks the export state of GPIO pin.

This function reads specified file from path, where defined at BlackGPIO::valuePath variable. If this file could open successfully, this means pin is exported successfully.

Returns
False if file opening fails, else true.
bool BlackLib::BlackGPIO::isHigh ( )

Checks value of GPIO pin.

This function calls getNumericValue() function and evaluates return value.

Returns
True if return value equals 1, else false.
Example
myGpio.setValue(BlackLib::high);
std::cout << std::boolalpha << myGpio.isHigh() << std::endl;
if( myGpio.isHigh() )
{
std::cout << "Do something with me, i am high." << std::endl;
}
myGpio.setValue(BlackLib::low);
std::cout << ( myGpio.isHigh() ? "yeap" : "nope") << std::endl;
// Possible Output:
// true
// Do something with me, i am high.
// nope
See also
getNumericValue()
bool BlackLib::BlackGPIO::isReady ( )
private

Checks ready state of GPIO pin.

This function calls isExported() and isDirectionSet() functions and then evaluates return values of these functions.

Returns
True if both functions return true, else false.
See also
isExported()
isDirectionSet()
BlackGPIO & BlackLib::BlackGPIO::operator<< ( digitalValue  value)

Sets value of GPIO pin with "<<" operator.

If pin direction is not output, function does nothing. If working mode is selected SecureMode, this function checks pin ready state by calling isReady() function. If pin is not ready, function does nothing. If working mode is selected FastMode, ready state checking will skip. Then the input parameter is saved to value file.

Parameters
[in]&valuenew pin value(enum)
Example
myGpio << BlackLib::high ;
std::cout << std::boolalpha << myGpio.isHigh() << std::endl;
myGpio << BlackLib::low ;
std::cout << std::boolalpha << myGpio.isHigh() << std::endl;
// Possible Output:
// true
// false
See also
digitalValue
BlackGPIO & BlackLib::BlackGPIO::operator>> ( std::string &  readToThis)

Reads value of gpio pin as string type with ">>" operator.

If working mode is selected SecureMode, this function checks pin ready state by calling isReady() function. If working mode is selected FastMode, ready state checking will skip. Then it reads specified file from path, where defined at BlackGPIO::valuePath variable, with ">>" operator. This file holds gpio pin value.

Parameters
[in]&readToThisread value and save this value to this variable. If file opening fails, this functions sets BlackLib::FILE_COULD_NOT_OPEN_STRING to variable or if pin isn't ready, it sets BlackLib::GPIO_PIN_NOT_READY_STRING.
Example
myGpio << BlackLib::high;
std::string val;
myGpio >> val;
std::cout << "Current value: " << val << std::endl;
// Possible Output:
// Current value: 1
BlackGPIO & BlackLib::BlackGPIO::operator>> ( int &  readToThis)

Reads value of gpio pin as int type with ">>" operator.

If working mode is selected SecureMode, this function checks pin ready state by calling isReady() function. If working mode is selected FastMode, ready state checking will skip. Then it reads specified file from path, where defined at BlackGPIO::valuePath variable, with ">>" operator. This file holds gpio pin value.

Parameters
[in]&readToThisread value and save this value to this variable. If file opening fails, this functions sets BlackLib::FILE_COULD_NOT_OPEN_INT to variable or if pin isn't ready, it sets BlackLib::GPIO_PIN_NOT_READY_INT.
Example
myGpio << BlackLib::high;
int val;
myGpio >> val;
std::cout << "Current value: " << val << std::endl;
// Possible Output:
// Current value: 1
bool BlackLib::BlackGPIO::setValue ( digitalValue  v)

Sets value of GPIO pin.

If pin direction is not output, function returns with false value. If working mode is selected SecureMode, this function checks pin ready state by calling isReady() function. If pin is not ready, function returns with false value. If working mode is selected FastMode, ready state checking will skip. Then the input parameter is converted to 1 or 0 and this value is saved to value file.

Parameters
[in]vnew pin value(enum)
Returns
True if setting new value is successful, else false.
Example
if( myGpio.setValue(BlackLib::high) )
{
std::cout << "Gpio 30 set high successfully." << std::endl;
}
else
{
std::cout << "Gpio 30 couldn't set." << std::endl;
}
if( myGpio2.setValue(BlackLib::low) )
{
std::cout << "Gpio 60 set low successfully (This is not possible)." << std::endl;
}
else
{
std::cout << std::endl << "Gpio 60 couldn't set. Because this pin's direction is input."
<< std::endl << "You can't write anything to input type pin." << std::endl;
}
// Possible Output:
// Gpio 30 set high successfully.
//
// Gpio 60 couldn't set. Because this pin's direction is input.
// You can't write anything to input type pin.
See also
digitalValue
void BlackLib::BlackGPIO::setWorkingMode ( workingMode  newWM)

Changes working mode.

This function sets new working mode value to BlackGPIO::workingMode variable.

Example
myGpio.setWorkingMode(BlackLib::FastMode);
std::cout << "Now you can be more faster than the previous one, but MAYBE." << std::endl;
// Possible Output:
// Now you can be more faster than the previous one, but MAYBE.
void BlackLib::BlackGPIO::toggleValue ( )

Toggles value of GPIO pin.

If pin direction is output, this function sets pin value to 1 or 0, by value of current state.

Example
myGpio.setValue(BlackLib::high);
std::cout << "Before toggle: " << std::boolalpha << myGpio.isHigh() << std::endl;
myGpio.toggleValue();
std::cout << "After toggle: " << std::boolalpha << myGpio.isHigh() << std::endl;
// Possible Output:
// Before toggle: true
// After toggle: false

Member Data Documentation

errorGPIO* BlackLib::BlackGPIO::gpioErrors
private

is used to hold the errors of BlackGPIO class

direction BlackLib::BlackGPIO::pinDirection
private

is used to hold the selected GPIO pin direction

gpioName BlackLib::BlackGPIO::pinName
private

is used to hold the selected GPIO pin name

std::string BlackLib::BlackGPIO::valuePath
private

is used to hold the value file path

workingMode BlackLib::BlackGPIO::workMode
private

is used to hold the selected working mode


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