if you work on linux you must know that every thing in linux is file.
you must write the linux device driver and load as module the you can call that as open file and write your command in that file.
file gpio.h
#ifndef GPIO_H
#define GPIO_H
#include <qstring.h>
#include <qobject.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//Header pin number definitions
#define PIN9 160
#define PIN10 161
#define PIN11 162
#define PIN12 163
#define PIN13 164
#define PIN14 165
#define PIN15 166
#define PIN16 192
#define PIN17 193
#define PIN18 195
#define PIN19 197
#define PIN20 198
#define PIN21 199
#define PIN22 201
#define PIN23 202
#define PIN24 203
#define PIN25 139
#define PIN26 140
#define PIN27 141
#define PIN28 194
#define PIN29 142
#define PIN30 143
#define PIN31 32
#define PIN32 33
#define PIN33 233
#define PIN34 234
class GPIO : public QObject
{
Q_OBJECT
public:
GPIO(int pin); //pin is the pin nuber on con4 of the FriendlyARM board
~GPIO();
enum Direction {In,Out,Err};
int openPin();
int closePin();
int setDirection(Direction direction); //set direction in/out. returns
0 if ok and -1 on error
int getDirection(); //returns direction
int setState(bool state);
void setValue(bool value);
bool getState();
public:
private:
Direction _direction;
int _state;
int _pinNumber;
QString _directionString;
QString _valueString;
QString _strPin;
};
#endif // GPIO_H
file gpio.c
#include "mini2440Gpio.h"
//------------------------------------------------------------------------------
GPIO::GPIO(int pin)
{ _pinNumber = pin;
_valueString = QString("/sys/class/gpio/gpio%1/value").arg(pin);
_directionString =
QString("/sys/class/gpio/gpio%1/direction").arg(pin);
_strPin = QString("%1").arg(pin);
}
//------------------------------------------------------------------------------
GPIO::~GPIO()
{
FILE * fp;
//This will create the folder /sys/class/gpio/gpio37
if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
return;
rewind(fp); //Set pointer to begining of the
file
fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() ,
fp);
fclose(fp);
}
//------------------------------------------------------------------------------
int GPIO::openPin()
{
FILE * fp;
//This will create the folder /sys/class/gpio/gpio37
if ((fp = fopen("/sys/class/gpio/export", "ab")) == NULL) return
-1;
rewind(fp);//Set pointer to begining of the file
fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() , fp);
fclose(fp);
return 0;
}
//------------------------------------------------------------------------------
int GPIO::closePin()
{
FILE * fp;
//This will create the folder /sys/class/gpio/gpio37
if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
return -1;
rewind(fp);//Set pointer to begining of the file
fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() ,
fp);
fclose(fp);
return 0;
}
//------------------------------------------------------------------------------
int GPIO::setDirection(Direction direction)
{ //set direction in/out. returns 0 if ok and -1 on error
FILE * fp;
if ((fp = fopen(_directionString.toLatin1(), "rb+")) == NULL)
return -1;
rewind(fp);//Set pointer to begining of the file
if(direction == In) fwrite("in", sizeof(char),2, fp);
if(direction == Out) fwrite("out", sizeof(char),3, fp);
fclose(fp);
return 0;
}
//------------------------------------------------------------------------------
/*
int GPIO::getDirection()
{//returns direction
}*/
//------------------------------------------------------------------------------
int GPIO::setState(bool state)
{//state is 0 or 1. No effect if other value. returns the new state or -1
on error
FILE * fp;
if ((fp = fopen(_valueString .toLatin1(), "rb+")) == NULL) return -1;
rewind(fp);//Set pointer to begining of the file
if(state) fwrite("high", sizeof(char),4, fp);
else fwrite("low", sizeof(char),3, fp);
fclose(fp);
return 0;
}
//------------------------------------------------------------------------------
bool GPIO::getState()
{ //returns 1 or 0 - the pin state. Returns -1 on error
FILE * fp;
char value;
if ((fp = fopen(_valueString.toLatin1(), "rb+")) == NULL) return
false;
rewind(fp);//Set pointer to begining of the file
fread(&value, sizeof(char),1, fp);
fclose(fp);
if(value=='1') return true;
if(value=='0') return false;
return false;
}
//------------------------------------------------------------------------------
to use gpio class you just declare like this:
GPIO test = new GPIO(PIN32);
test->openPin(); // open pin
test->setDirection(GPIO::Out); // set direction to output
test->setState(false); // set value to low.
test->closePin();//close pin
delete test; // delete pointer to avoid leak memory.
Have funs!