4

I just started using c++ bindings of libgpiod library and have problem with settings gpios. I know, that I can create long vector of values, and apply it in all at once, but I would like to be able to set their direction, and control them separately. How can I do that?

What I tried is this:

First: Working code with applying all values at once:

#include <gpiod.hpp>

int main(int argc, char **argv)
    {

    ::gpiod::chip chip("gpiochip0");
    auto lines = chip.get_all_lines();

    ::gpiod::line_request requestOutputs = {
        argv[0],
        ::gpiod::line_request::DIRECTION_OUTPUT,
        0
    };

    int value_to_be_set = 0xAAAAAAA ; //example value
    ::std::vector<int> values;

    for (int i = 0; i < 32; i++)
    {
        values.push_back((value_to_be_set >> i) & 1UL);
    }

    lines.request(requestOutputs, values);
    lines.release();

    return EXIT_SUCCESS;
}

Second, my approach to do that I want:

#include <gpiod.hpp>

int main(int argc, char **argv)
{
    ::gpiod::chip chip("gpiochip0");
    auto lines = chip.get_all_lines();

    ::gpiod::line_request requestOutputs = {
        argv[0],
        ::gpiod::line_request::DIRECTION_OUTPUT,
        0
    };
    lines.request(requestOutputs);

    int value_to_be_set = 0xAAAAAAA; //example value

    for (int i = 0; i < 32; i++)
    {
        // This does not set value :(
        lines.get(i).set_value((value_to_be_set >> i) & 1UL);
    }

    lines.release();

    return EXIT_SUCCESS;
}
Staszek
  • 849
  • 11
  • 28
  • What happens when you run the second example? What exception gets thrown? What does e.what() say? hint: that somebody else is using those GPIO pins. Consider yourself fortunate. Some of those GPIO pins control system-critical functionality. Actually setting them would be a disaster. The mystery (honestly), is why the first example doesn't fail. – Robin Davies Aug 13 '21 at 17:07
  • Well, I actually had an answer to that (from the library author), but unfortunately didn't post it back then, and now I don't remember :( The first example was just fine, and AFAIR the second one didn't throw any error, just didn't work. AFAIR, after this line: `auto lines = chip.get_all_lines();` I have to use all the lines, and to use them separately I have to use some other way, probably request them separately. If you need that, I can try to get my working code (form previous workplace). – Staszek Aug 14 '21 at 10:39

2 Answers2

2

I also could not find a simple C++ example to toggle a single GPIO line using the latest Raspberry PI libraries.

There is a multi-line example below but this is not what was originally asked: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx

Below is an example that will cause GPIO17 to go high then low to create a single line output pulse.

// Use gpio drivers to toggle a single GPIO
// line on Raspberry Pi

// Use following commands to install prerequisites and build
// sudo apt install gpiod
// sudo apt install libgpiod-dev
// g++ -Wall -o gpio gpip.cpp -lgpiodcxx

#include <iostream>
#include <gpiod.hpp>
#include <unistd.h>
 
int main(void)
{ 
   ::gpiod::chip chip("gpiochip0");
   
   auto line = chip.get_line(17);  // GPIO17
   line.request({"example", gpiod::line_request::DIRECTION_OUTPUT, 0},1);  
   
   sleep(0.1);
   
   line.set_value(0);
   line.release();
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
jeff28273
  • 422
  • 5
  • 11
0

also don't forget to build with the flag -lgpiodcxx (for c++) or -lgpiod (for c)

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • This doesn't answer the question, and should be a comment instead. – Dada Dec 30 '21 at 07:41
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30701785) – Sercan Dec 30 '21 at 11:14