User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Official C/C++ library ?

Mon Aug 31, 2015 6:40 pm

Hi,
Looking to code in C/C++ with my Raspberry Pi B+, I've found those libraries allowing to access GPIO and more :
. WiringPi : that seems "more complete" ;
. pigpio that seems more restricted but optimized for performance ;
. bcm2835 that seems "chipset specific" oriented.
. GPIO sysfs fully portable & system generic supported but maybe at the cost of some reduced performances or the loss of some specific functionnalities (?).

Maybe... certainly there are others, but my question is "is there an official C/C++" Library ?
I ask that question to be certain of choosing the one that will be maintained among new versions of RPi in the future...

The more portable and "official" seems to be the GPIO sysfs, here two howtos/tutorials on how to use it :
. Example 1 ;
. Example 2.

Thank you in advance for your answers !
Z

NB - this post has been edited to show all libraries & possibilities found & exposed during this thread.
Last edited by Zorro_X on Tue Sep 08, 2015 5:53 am, edited 2 times in total.

User avatar
buja
Posts: 448
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Official C/C++ library ?

Mon Aug 31, 2015 8:53 pm

There is at least one other C library: http://www.airspayce.com/mikem/bcm2835/

I am not aware of any "official" library.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Mon Aug 31, 2015 10:11 pm

Thank you for your answer.
So, if there is not an "official" one, maybe one "more recomended than the others" ?
To develop a product using RasPi I'll be happy and gratefull to use a Library that will not (or at least has low risk of..) stop being maintained, if you know what I mean... ;)

User avatar
kusti8
Posts: 3441
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Official C/C++ library ?

Mon Aug 31, 2015 11:01 pm

I think WiringPi isn't maintained anymore that much.

The creator of pigpio is a regular member here on the forums, and I think that's still under active development.

I'd be curious to know why you think pigpio is less complete than WiringPi.
There are 10 types of people: those who understand binary and those who don't.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Tue Sep 01, 2015 5:45 am

I said that "it seems" not "it is" les complete...
That certainly unfounded impression is just linked to the fact that WiringPi shows additional modules/wrappers like Python, Perl, Ruby, Java... Also because its "user friendly" interface, closest to the Arduino's one, makes me "hope" it is more maintained than the others... :oops:
But that's why I'm posting here my question, because I'm not certain of anything and I'm certainly wrong... ;)
So, if there is no "official C/C++" interface/Library, maybe the answer to afford a sustainable & maintainable product is to develop my own (?)

User avatar
buja
Posts: 448
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Official C/C++ library ?

Tue Sep 01, 2015 7:13 pm

Zorro_X wrote:I said that "it seems" not "it is" les complete...
.... So, if there is no "official C/C++" interface/Library, maybe the answer to afford a sustainable & maintainable product is to develop my own (?)
Hmm, how does developing another non-offical library solve any problem? And what is the problem anyway? All three libraries are actively maintained, even though not all developers are equally active on this particular forum.
My advice would be to play a bit with those existing libraries to see which one you like best and use that one for your own programs. I think they did a pretty good job. And if you have particular needs or suggestions, you can always contact the developers through their websites.
But you are free to develop your own library of course, don't let me or anybody else stop you from doing that. ;)
Last edited by buja on Tue Sep 01, 2015 8:48 pm, edited 1 time in total.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Tue Sep 01, 2015 8:31 pm

I'm sorry I did not say they're not good enough for me, they seem quite well & useful, at least the two I've found by myself. My problem is not there, my problem is if i make a product using one of those libraries ok, it will work now, but in 2 o 3 years, if I want to use my code on Raspberry Pi D+, if the Library I'm using today is not maintained anymore, I'll need to change all my source code to adapt it to the one that will be available at that moment...
So, to develop my own adds of course a lot of work, but it warrantees that it will be maintained (by me !).

So, since the begining, my question is wich one is the one that will "live longuer" ? But I understant that if there is no official C/C++ GPIO Library, there is certainly no answer to that question...

User avatar
buja
Posts: 448
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Official C/C++ library ?

Tue Sep 01, 2015 9:32 pm

I see what you mean. The risk of a developer loosing interest in maintaining the library of your choice is a real one, especially because they all seem to be single developers.

So if you have the time and the skills, developing your own, tailor made library is a valid option.

On the other hand, you could start with an existing library and only switch to another one or your own only if and when that becomes necessary. Yes, you would need to change your own source code to cope with that change, but with some clever thinking/designing/planning ahead that should be possible.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Wed Sep 02, 2015 4:30 am

buja wrote:I see what you mean. The risk of a developer loosing interest in maintaining the library of your choice is a real one, especially because they all seem to be single developers.
Exactly, that's my point !
buja wrote:So if you have the time and the skills, developing your own, tailor made library is a valid option.
Valid but expensive...
buja wrote:On the other hand, you could start with an existing library and only switch to another one or your own only if and when that becomes necessary. Yes, you would need to change your own source code to cope with that change, but with some clever thinking/designing/planning ahead that should be possible.
That's where I'm exactly now : considering which solution may be the best in my case... In the past I've already used existing libraries in other products, libraries that are not maintained anymore and even with a "preventive code design", when switching to another Library, there is allways a need to make adaptations that unfortunately are often very performance-consuming (and also time-consuming)... :(

Thank you for your help and for understanding me ! :)

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5118
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Official C/C++ library ?

Wed Sep 02, 2015 12:52 pm

The official, most portable and future-proof way to access GPIO is using the sysfs interface. You can use it if a completely different pi comes out, if you decide to use other hardware or if all GPIO libraries become outlawed.
https://www.kernel.org/doc/Documentation/gpio/sysfs.txt

There are cases where it may be too slow or might not provide the functionality you require, but that will depend on exactly what you're doing. It's more than enough for that vast majority of what people seem to do with GPIO.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Wed Sep 02, 2015 6:06 pm

Thank you ShiftPlusOne ! That add one more interesting option into my possible choices ! :D
I didn't knew about that interface, I'll take a closer look. Maybe you already know some kind of howto or example showing the usage of the GPIO sysfs interface ?
Last edited by Zorro_X on Sun Sep 06, 2015 6:17 am, edited 1 time in total.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5118
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Official C/C++ library ?

Wed Sep 02, 2015 6:11 pm


User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Thu Sep 03, 2015 6:39 am

Thankyou ShiftPlusOne, it looks great for me ! :mrgreen:

User avatar
buja
Posts: 448
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Official C/C++ library ?

Thu Sep 03, 2015 9:26 pm

By the way, PIGPIO, wiringPi and BCM2835 all come with the source code. So you can do pretty much anything you want: use it as-is in your own software or adapt it to your own needs.

But please check the licenses or contact the original developers if you are going to distribute your product. Even if there aren't any restrictions, I guess they still like to know how their products are used.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Fri Sep 04, 2015 8:16 pm

I'm aware about author's rights, even for non-licenced libraries. Don't worry, if I use one of those, I'll take care & contact their developpers...
( But no need to do so if I create my own... ;) )

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Fri Sep 11, 2015 9:58 am

Hi, it's me again, I'm taking a closer look at GPIOsysfs interface but I cannot manage to find how to get interrupts from there ? Or the only way of getting such "events" is by pooling on the status of an input ? Maybe it is some of the "limitations" you were talking about, ShiftPlusOne ?

nwnclv
Posts: 9
Joined: Thu Sep 10, 2015 1:41 pm
Location: Karjala
Contact: Website

Re: Official C/C++ library ?

Fri Sep 11, 2015 10:47 am

use "edge" parameter (https://www.kernel.org/doc/Documentation/gpio/sysfs.txt read this) and epoll (with EPOLLET | EPOLLPRI), for example, for getting events from the file "value".

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Official C/C++ library ?

Fri Sep 11, 2015 10:56 am

Zorro_X wrote:Hi, it's me again, I'm taking a closer look at GPIOsysfs interface but I cannot manage to find how to get interrupts from there ? Or the only way of getting such "events" is by pooling on the status of an input ? Maybe it is some of the "limitations" you were talking about, ShiftPlusOne ?
This example waits for one interrupt and expects the sysfs stuff to be done from the command line (simply because I didn't want to have to run the program with root privileges).

Code: Select all

/* 2014-07-06
   wfi.c

   gcc -o wfi wfi.c

   ./wfi [gpio]
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <poll.h>

#define GPIO 4

int main(int argc, char *argv[])
{
   char str[256];
   struct timeval tv;
   struct pollfd pfd;
   int fd, gpio;
   char buf[8];

   /*
      Prior calls assumed.
      sudo sh -c "echo 4      >/sys/class/gpio/export"
      sudo sh -c "echo in     >/sys/class/gpio/gpio4/direction"
      sudo sh -c "echo rising >/sys/class/gpio/gpio4/edge"
   */

   if (argc > 1) gpio = atoi(argv[1]);
   else          gpio = GPIO;

   sprintf(str, "/sys/class/gpio/gpio%d/value", gpio);

   if ((fd = open(str, O_RDONLY)) < 0)
   {
      fprintf(stderr, "Failed, gpio %d not exported.\n", gpio);
      exit(1);
   }

   pfd.fd = fd;

   pfd.events = POLLPRI;

   lseek(fd, 0, SEEK_SET);    /* consume any prior interrupt */
   read(fd, buf, sizeof buf);

   poll(&pfd, 1, -1);         /* wait for interrupt */

   lseek(fd, 0, SEEK_SET);    /* consume interrupt */
   read(fd, buf, sizeof buf);

   exit(0);
}

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Fri Sep 11, 2015 11:19 am

ok thank you both, nwnclv & joan. If I understand well, in both cases the only way to get "interrupted" is by polling on the value, even by using the "edge" interface, or did I miss something :?:

User avatar
joan
Posts: 13412
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Official C/C++ library ?

Fri Sep 11, 2015 11:26 am

Zorro_X wrote:ok thank you both, nwnclv & joan. If I understand well, in both cases the only way to get "interrupted" is by polling on the value, even by using the "edge" interface, or did I miss something :?:
The call has a misleading name, it does not use polling.

The call adds your process to the kernel list of processes to be woken if that GPIO edge interrupt happens (typically the list will only have one entry - your process).

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Fri Sep 11, 2015 5:26 pm

ok, so the process is suspended ?
what about "subscribe" to an interrupt then continue to do something else (processing, precomputing, ...) while waiting for the interruption ? could it be multithreaded (pthread ?) or it nessesary needs to be "per-process" ?
the example shown by you (joan) looks like a non-blocking subscription, or once again did I miss something ?

nwnclv
Posts: 9
Joined: Thu Sep 10, 2015 1:41 pm
Location: Karjala
Contact: Website

Re: Official C/C++ library ?

Fri Sep 11, 2015 5:39 pm

yes, the process is suspended and yes, you could use multithreading. One thread (with poll or epoll) can wait 1..∞ (not really) devices for the events :) In my project I use 1 thread for all events(files, times, etc...) .. for many clients.

User avatar
Zorro_X
Posts: 12
Joined: Mon Aug 31, 2015 6:29 pm
Contact: Website

Re: Official C/C++ library ?

Fri Sep 11, 2015 6:07 pm

ok, I need to give it a try now !
Thank you for your help guys ! :)

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Official C/C++ library ?

Thu Sep 17, 2015 11:18 am

kusti8 wrote:I think WiringPi isn't maintained anymore that much.

The creator of pigpio is a regular member here on the forums, and I think that's still under active development.

I'd be curious to know why you think pigpio is less complete than WiringPi.
FWIW: wiringPi is maintained - it's just that it hasn't had to be maintained for some time now - ie. no new critical bugs have been found for some time time and no new bits of hardware has been added.

However I do have a new release planned soon - hopefully by the end of September. Nothing really new just a bit of a tidy up.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
kusti8
Posts: 3441
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Official C/C++ library ?

Thu Sep 17, 2015 11:50 am

[email protected] wrote:
kusti8 wrote:I think WiringPi isn't maintained anymore that much.

The creator of pigpio is a regular member here on the forums, and I think that's still under active development.

I'd be curious to know why you think pigpio is less complete than WiringPi.
FWIW: wiringPi is maintained - it's just that it hasn't had to be maintained for some time now - ie. no new critical bugs have been found for some time time and no new bits of hardware has been added.

However I do have a new release planned soon - hopefully by the end of September. Nothing really new just a bit of a tidy up.

-Gordon
Sorry about that. I understand. :oops:
There are 10 types of people: those who understand binary and those who don't.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 2 guests