Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //utilisse le driver mcp23017 /boot/config.txt dtoverlay=mcp23017,addr=0x20,gpiopin=24
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <unistd.h>
- #define GPIO_DIR "/sys/class/gpio"
- #define GPIO_ADDR 496 //Addresse base pour le driver mcp23017 496 , le fin 511
- using namespace std;
- class mcp23017{
- public:
- mcp23017 ();
- ~mcp23017();
- void gpioSet(int gpio, int value);
- int gpioRead(int gpio);
- void gpioUnexport();//Delete All PORTS GPIO dans /sys/class/gpio
- enum {INPUT,OUTPUT};
- enum {ON,OFF};
- private:
- void configureMcp23017();//Configure PORTA,B Axisat 8IN & 8OUT
- void gpioExport(int gpio);//Cree gpioNNN ,miroir PORTA,B dans sys/class/gpio
- void gpioDirection(int gpio, int direction); //0 INPUT 1 OUTPUT
- bool error(bool ok,string dir);
- int read;
- };
- //----------------------------------------------------------------------
- mcp23017::mcp23017(){read=0;configureMcp23017();}//Constructeur
- //mcp23017::~mcp23017(){gpioUnexport();}//Destructeur de la classe efface PORTs
- mcp23017::~mcp23017(){}//Destructeur de la classe efface PORTs
- void mcp23017::configureMcp23017(){//Configure PORTA,B Axisat 8IN & 8OUT
- size_t offset(0);
- do{
- gpioExport(GPIO_ADDR+offset);//Cree miroir PORTA,B dans sys/class/gpio
- //usleep(5000);
- sleep(1);
- gpioDirection ( GPIO_ADDR+offset ,offset <8 ?INPUT:OUTPUT);//configure PORTA,B
- }while (offset++ <15);
- }
- void mcp23017::gpioExport(int gpio){//Cree gpioNNN ,miroir PORTA,B dans sys/class/gpio
- std::fstream buf;
- string dir(GPIO_DIR"/export");
- buf.open (dir,std::fstream::out);
- if (!error(!buf.fail()," EXPORT "+dir+to_string(gpio)) ){return ;}
- buf<<gpio;
- buf.close();
- }
- void mcp23017::gpioDirection(int gpio, int direction){//0 INPUT 1 OUTPUT
- std::fstream buf;
- string dir(GPIO_DIR+string("/gpio")+to_string(gpio)+string("/direction"));
- buf.open (dir,std::fstream::out);
- if (!error(!buf.fail()," DIR "+dir) ){return ;}
- buf<<string ((direction==OUTPUT) ? "out":"in");
- buf.close();
- }
- void mcp23017::gpioSet(int gpio, int value){
- std::fstream buf;
- string dir(GPIO_DIR+string("/gpio")+to_string(gpio)+string("/value"));
- buf.open (dir, std::fstream::out);
- if (!error(!buf.fail()," SET "+dir) ){return ;}
- buf<<value;
- buf.close();
- }
- int mcp23017::gpioRead(int gpio){
- std::fstream buf;
- string dir(GPIO_DIR+string("/gpio")+to_string(gpio)+string("/value"));
- buf.open (dir, std::fstream::in );
- if (!error(!buf.fail()," READ "+dir) ){return -1;}
- buf>>read;
- buf.close();
- return read;
- }
- void mcp23017::gpioUnexport(){//Delete All PORTS GPIO dans /sys/class/gpio
- std::fstream buf;
- int offset(0);
- string dir(GPIO_DIR"/unexport");
- do{
- buf.open ( dir,std::fstream::out);// Overture /sys/class/gpio/unexport
- if (!error(!buf.fail()," DEL "+dir+to_string(GPIO_ADDR+offset)) ){return ;}
- buf<<(to_string(GPIO_ADDR+offset));//Ecrire dans unexport le Num à effacer gpioNNN
- buf.close();
- }while ((offset++)<15);
- }
- bool mcp23017::error(bool ok,string dir){
- if (ok) {cout<<endl<<" OPEN OK "<<dir<<endl;return true;}//OK
- else {cout<<endl<<" Error OPEN!!! "<<dir<<endl;return false;}
- }
- //----------------------------------------------------------------------
- //----------------------------------------------------------------------
- int main (){
- mcp23017 mcp;//Configure 1 MCP23017 ...cree sys/class/gpio
- for (int offset=15;offset >=0;offset--){ //Test IN & OUT
- if (offset<8){//Read
- cout<<mcp.gpioRead(GPIO_ADDR+offset)<<" = IN"<<offset<<endl;
- }else{ mcp.gpioSet(GPIO_ADDR+offset,1);}//Sortie =1
- }
- mcp.gpioUnexport();//Efface ports crees aver export dans sys/class/gpio
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement