element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RIoTboard
  • Products
  • Dev Tools
  • Single-Board Computers
  • RIoTboard
  • More
  • Cancel
RIoTboard
Forum Code Blocks and C++ programming...
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join RIoTboard to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 2 replies
  • Subscribers 22 subscribers
  • Views 535 views
  • Users 0 members are here
Related

Code Blocks and C++ programming...

mubase
mubase over 11 years ago

Hi all. After following Selsinorks' tutorials on getting Debian working on the riotboard and then getting the Gnome environment up and running with the tutorial here:

Debian on RIoTboard - After basic install, adding a GUI Desktop and other usefull stuff

I have successfully installed Codeblocks working with C/C++.

Codeblocks installed easily : sudo apt-get install codeblocks.

After this I installed g++ (as codeblocks uses the g++ compiler to build..)

It compiles no problem and after following the post on GPIOs:

GPIO Ports on RIoT Board

I changed this code here:

 

#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 gpio27(112);     // here is the line I changed to reflect the gpio port (pin 5... as seen in the GPIO tutorial.)

    gpio27.SetDirection(true);

    bool on = true;

    for (;;)

    {

        printf("Switching %s the LED...\n", on ? "on" : "off");

        gpio27.SetValue(on);

        on = !on;

        sleep(1);

    }

}

 

So we now have codeblocks and the flashing LED hello world type program working. image

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 11 years ago +1
    Well done following the gpio explanation, not sure I can follow it anymore As long as you're using the newer kernel then most of the issues around pinmux discussed in that thread no longer apply, generally…
  • Former Member
    Former Member over 11 years ago

    Well done following the gpio explanation, not sure I can follow it anymore image   As long as you're using the newer kernel then most of the issues around pinmux discussed in that thread no longer apply, generally gpio's will just work using the sysfs interface on 3.15 or later

     

    Want to use a different pin?  You can calculate the number to use like this, take the GPIO<X>_<Y> number of the pin from the first table here http://www.element14.com/community/community/knode/single-board_computers/riotboard/blog/2014/05/10/part-4-riotboard-expansion-connector-pinmux-settings

     

    then do (x-1)*32 + y  so for pin 5 which is GPIO4_16 you get 112 etc..

     

    The other pins that are marked as UART or PWM can't easily be used this way, or rather they can but only if you want to rebuild the devicetree without those features.

     

    How to use the /sys/class/gpio interface is documented in the kernel source here https://github.com/selsinork/linux/blob/riotboard-3.15/Documentation/gpio/sysfs.txt

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • tusharp
    tusharp over 11 years ago

    good piece of code, however it makes sense to use the easy method of sysfs.

    native c/c++ code are defnitely  faster than scripts controlling sysfs, anyways gpio_to_irq can only be done from native codeimage .

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube