Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laelaps::LaeGpio Class Reference

Laelaps Odroid GPIO base class. More...

#include <laeGpio.h>

Inheritance diagram for laelaps::LaeGpio:
laelaps::LaeAux5VOutEnable laelaps::LaeAuxBattOutEnable laelaps::LaeI2CMuxReset laelaps::LaeMotorCtlrEnable laelaps::LaeWatchDogReset

Public Types

enum  TriState {
  UNKNOWN = -1,
  LOW = 0,
  HIGH = 1
}
 GPIO tri-state value. More...
 
enum  Direction {
  INPUT = GPIO_DIR_IN,
  OUTPUT = GPIO_DIR_OUT
}
 GPIO direction. More...
 

Public Member Functions

 LaeGpio (const std::string &strTag, const int gpio, const LaeGpio::Direction dir)
 Default intialization constructor. More...
 
virtual ~LaeGpio ()
 Destructor.
 
virtual void sync ()
 Synchronized this with GPIO hardware state.
 
virtual int writeValue (const LaeGpio::TriState value)
 Write value to gpio. More...
 
virtual int writeValue (const int value)
 Write value to gpio. More...
 
virtual int readValue (int &value)
 Read current value of gpio. More...
 
int hasValue () const
 Get the current shadowed gpio value. More...
 
bool isConfigured () const
 Is the exported GPIO number configured to match this ojbect? More...
 

Protected Member Functions

bool checkConfig ()
 Check if the exported GPIO exists in /sys/class and that it has been configured to match this objects configuration. More...
 

Protected Attributes

std::string m_gpioTag
 identifying tag
 
int m_gpioNum
 exported GPIO number
 
LaeGpio::Direction m_gpioDir
 GPIO direction.
 
int m_gpioVal
 shadowed value
 
bool m_gpioCfg
 GPIO is [not] configured (correctly)
 

Detailed Description

Laelaps Odroid GPIO base class.

Note
The configuration of the GPIO is assumed done elsewhere (e.g. at system initialization time).

Definition at line 75 of file laeGpio.h.

Member Enumeration Documentation

enum laelaps::LaeGpio::Direction

GPIO direction.

Enumerator
INPUT 

input direction

OUTPUT 

output direction

Definition at line 91 of file laeGpio.h.

92  {
93  INPUT = GPIO_DIR_IN, ///< input direction
94  OUTPUT = GPIO_DIR_OUT ///< output direction
95  };
output direction
Definition: laeGpio.h:94
input direction
Definition: laeGpio.h:93
enum laelaps::LaeGpio::TriState

GPIO tri-state value.

Enumerator
UNKNOWN 

unknown state value

LOW 

low value

HIGH 

high value

Definition at line 81 of file laeGpio.h.

82  {
83  UNKNOWN = -1, ///< unknown state value
84  LOW = 0, ///< low value
85  HIGH = 1 ///< high value
86  };
low value
Definition: laeGpio.h:84
unknown state value
Definition: laeGpio.h:83
high value
Definition: laeGpio.h:85

Constructor & Destructor Documentation

LaeGpio::LaeGpio ( const std::string &  strTag,
const int  gpio,
const LaeGpio::Direction  dir 
)

Default intialization constructor.

No file I/O operations are perform.

Parameters
strTagTag identifier associated with GPIO pin.
gpioExported GPIO number.
dirGPIO direction.

Definition at line 72 of file laeGpio.cxx.

References m_gpioCfg, m_gpioVal, and UNKNOWN.

74  :
75  m_gpioTag(strTag), m_gpioNum(gpio), m_gpioDir(dir)
76 {
78  m_gpioCfg = false;
79 }
LaeGpio::Direction m_gpioDir
GPIO direction.
Definition: laeGpio.h:179
int m_gpioNum
exported GPIO number
Definition: laeGpio.h:178
unknown state value
Definition: laeGpio.h:83
bool m_gpioCfg
GPIO is [not] configured (correctly)
Definition: laeGpio.h:181
int m_gpioVal
shadowed value
Definition: laeGpio.h:180
std::string m_gpioTag
identifying tag
Definition: laeGpio.h:177

Member Function Documentation

bool LaeGpio::checkConfig ( )
protected

Check if the exported GPIO exists in /sys/class and that it has been configured to match this objects configuration.

Returns
Returns true or false.

Definition at line 138 of file laeGpio.cxx.

References INPUT, m_gpioDir, and m_gpioNum.

Referenced by sync().

139 {
140  char buf[MAX_PATH];
141  int mode;
142 
143  gpioMakeDirname(m_gpioNum, buf, sizeof(buf));
144 
145  string strValFileName(buf);
146  string strDirFileName(buf);
147 
148  strValFileName += "/value";
149  strDirFileName += "/direction";
150 
151  if( m_gpioDir == LaeGpio::INPUT )
152  {
153  mode = R_OK;
154  }
155  else
156  {
157  mode = R_OK|W_OK;
158  }
159 
160  return access(strValFileName.c_str(), mode) == 0;
161 }
LaeGpio::Direction m_gpioDir
GPIO direction.
Definition: laeGpio.h:179
int m_gpioNum
exported GPIO number
Definition: laeGpio.h:178
input direction
Definition: laeGpio.h:93
int laelaps::LaeGpio::hasValue ( ) const
inline

Get the current shadowed gpio value.

Returns
Returns LaeGpio::TriState value as an integer.

Definition at line 157 of file laeGpio.h.

References m_gpioVal.

Referenced by laelaps::LaeMotorCtlrEnable::isEnabled(), laelaps::LaeAuxBattOutEnable::isEnabled(), laelaps::LaeAux5VOutEnable::isEnabled(), laelaps::LaeWatchDogReset::reset(), and laelaps::LaeI2CMuxReset::reset().

158  {
159  return m_gpioVal;
160  }
int m_gpioVal
shadowed value
Definition: laeGpio.h:180
bool laelaps::LaeGpio::isConfigured ( ) const
inline

Is the exported GPIO number configured to match this ojbect?

Ass Laelaps evolves, the test can distinguish between different platform versions. For example, Laelaps 2.0 does not have a gpio pin dedicated to enabling power to the motor controllers, while subsequent version do.

Returns
Returns true or false.

Definition at line 171 of file laeGpio.h.

References m_gpioCfg.

Referenced by laelaps::LaeMotorCtlrEnable::disable(), laelaps::LaeAuxBattOutEnable::disable(), laelaps::LaeAux5VOutEnable::disable(), laelaps::LaeMotorCtlrEnable::enable(), laelaps::LaeAuxBattOutEnable::enable(), laelaps::LaeAux5VOutEnable::enable(), laelaps::LaeWatchDogReset::reset(), laelaps::LaeI2CMuxReset::reset(), sync(), laelaps::LaeMotorCtlrEnable::sync(), laelaps::LaeWatchDogReset::sync(), laelaps::LaeI2CMuxReset::sync(), laelaps::LaeAuxBattOutEnable::sync(), and laelaps::LaeAux5VOutEnable::sync().

172  {
173  return m_gpioCfg;
174  }
bool m_gpioCfg
GPIO is [not] configured (correctly)
Definition: laeGpio.h:181
int LaeGpio::readValue ( int &  value)
virtual

Read current value of gpio.

Parameters
[out]valueLOW/HIGH integer value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 115 of file laeGpio.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, m_gpioNum, m_gpioTag, and m_gpioVal.

Referenced by sync(), and writeValue().

116 {
117  int val;
118  int rc;
119 
120  if( (val = gpioRead(m_gpioNum)) != RC_ERROR )
121  {
122  value = val;
123  m_gpioVal = val;
124  rc = LAE_OK;
125 
126  LOGDIAG3("%s GPIO %d: current value is %d.",
127  m_gpioTag.c_str(), m_gpioNum, m_gpioVal);
128  }
129  else
130  {
131  LOGERROR("%s GPIO %d: Failed to read pin.", m_gpioTag.c_str(), m_gpioNum);
132  rc = -LAE_ECODE_IO;
133  }
134 
135  return rc;
136 }
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
int m_gpioNum
exported GPIO number
Definition: laeGpio.h:178
int m_gpioVal
shadowed value
Definition: laeGpio.h:180
std::string m_gpioTag
identifying tag
Definition: laeGpio.h:177
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
virtual int laelaps::LaeGpio::writeValue ( const LaeGpio::TriState  value)
inlinevirtual

Write value to gpio.

Parameters
valueLOW/HIGH tri-state value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 129 of file laeGpio.h.

References readValue().

Referenced by laelaps::LaeMotorCtlrEnable::disable(), laelaps::LaeAuxBattOutEnable::disable(), laelaps::LaeAux5VOutEnable::disable(), laelaps::LaeMotorCtlrEnable::enable(), laelaps::LaeAuxBattOutEnable::enable(), laelaps::LaeAux5VOutEnable::enable(), laelaps::LaeWatchDogReset::reset(), and laelaps::LaeI2CMuxReset::reset().

130  {
131  return writeValue((const int)value);
132  }
virtual int writeValue(const LaeGpio::TriState value)
Write value to gpio.
Definition: laeGpio.h:129
int LaeGpio::writeValue ( const int  value)
virtual

Write value to gpio.

Parameters
valueLOW/HIGH integer value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 92 of file laeGpio.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, m_gpioNum, m_gpioTag, and m_gpioVal.

93 {
94  int rc;
95  int val = value? 1: 0;
96 
97  if( gpioWrite(m_gpioNum, val) == OK )
98  {
99  m_gpioVal = val;
100  rc = LAE_OK;
101 
102  LOGDIAG3("%s GPIO %d: new value is %d.",
103  m_gpioTag.c_str(), m_gpioNum, m_gpioVal);
104  }
105  else
106  {
107  LOGERROR("%s GPIO %d: Failed to write %d to pin.",
108  m_gpioTag.c_str(), m_gpioNum, value);
109  rc = -LAE_ECODE_IO;
110  }
111 
112  return rc;
113 }
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
int m_gpioNum
exported GPIO number
Definition: laeGpio.h:178
int m_gpioVal
shadowed value
Definition: laeGpio.h:180
std::string m_gpioTag
identifying tag
Definition: laeGpio.h:177
static const int LAE_OK
not an error, success
Definition: laelaps.h:71

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