c++ gpio raspberry pi - Raspberry Pi Forums


hello :)
tried c++ program make on , off led , compiles dont have effect gpio

code: select all

#include <unistd.h> #include <string.h> #include <stdio.h> #include <fcntl.h>  class linuxfile { private:     int m_handle;  public:     linuxfile(const char *pfile, int flags = o_rdwr)     {         m_handle = open(pfile, flags);     }      ~linuxfile()     {         if (m_handle != -1)             close(m_handle);     }      size_t write(const void *pbuffer, size_t size)     {         return write(m_handle, pbuffer, size);     }      size_t read(void *pbuffer, size_t size)     {         return read(m_handle, pbuffer, size);     }      size_t write(const char *ptext)     {         return write(ptext, strlen(ptext));     }      size_t write(int number)     {         char sznum[32];         snprintf(sznum, sizeof(sznum), "%d", number);         return write(sznum);     } };  class linuxgpioexporter { protected:     int m_number;  public:     linuxgpioexporter(int number)         : m_number(number)     {         linuxfile("/sys/class/gpio/export", o_wronly).write(number);     }      ~linuxgpioexporter()     {         linuxfile("/sys/class/gpio/unexport",                 o_wronly).write(m_number);     } };  class linuxgpio : public linuxgpioexporter { public:     linuxgpio(int number)         : linuxgpioexporter(number)     {     }      void setvalue(bool value)     {         char szfn[128];         snprintf(szfn, sizeof(szfn),             "/sys/class/gpio/gpio%d/value", m_number);         linuxfile(szfn).write(value ? "1" : "0");     }      void setdirection(bool isoutput)     {         char szfn[128];         snprintf(szfn, sizeof(szfn),             "/sys/class/gpio/gpio%d/direction", m_number);         linuxfile(szfn).write(isoutput ? "out" : "in");     } };  int main(int argc, char *argv[]) {     linuxgpio gpio8(8);     gpio8.setdirection(true);     bool on = true;     (;;)     {         printf("switching %s led...\n", on ? "on" : "off");         gpio8.setvalue(on);         on = !on;         sleep(1);     } }
got code here http://visualgdb.com/tutorials/raspberry/led/

i wonder if need run executable using sudo?


raspberrypi



Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial