Many good write-ups and examples at http://abyz.me.uk/rpi/pigpio/index.html. There are comparisons between the various libraries there. There are also alternatives to the full blown daemon or socket implementations at the site. See the Examples page http://abyz.me.uk/rpi/pigpio/examples.html and especially the Tiny GPIO access library (about 1/2 - 2/3 down the examples page)
I do not like running applications as root, so I've moved away, to the greatest extent possible from the bcm2835 library as well as the current gpiod daemons in favor of the /dev/gpiomem access provided by tinygpio. Sure, there are some things you will need the larger library for, but for 95% of it, you can use an ioctl interface and avoid the need to run your programs as root.
Here is a fun little example that blinks a LED attached to GPIO4 (by default). You can set the GPIO pin number from the command line as well as the number of micrseconds the blink is ON and OFF, and the number of repetitions. Just put the tiny GPIO sources and include together with this file (I called it tglibtest.c)
You can compile it with the following assuming the source in tglibtest.c and the tiny gpio source and include in tinygpio.c and tinygpio.h:
The just run the executable. ./tglibtest, or for a bit more fun, change up the way it blinks the LED attached to GPIO4 (or whichever you attach it to)
Good luck with your coding. (let me know if you have problems, I can't recall how much I modified tinygpio.[ch]. I eventually just turned it into a shared library to link against...)
I do not like running applications as root, so I've moved away, to the greatest extent possible from the bcm2835 library as well as the current gpiod daemons in favor of the /dev/gpiomem access provided by tinygpio. Sure, there are some things you will need the larger library for, but for 95% of it, you can use an ioctl interface and avoid the need to run your programs as root.
Here is a fun little example that blinks a LED attached to GPIO4 (by default). You can set the GPIO pin number from the command line as well as the number of micrseconds the blink is ON and OFF, and the number of repetitions. Just put the tiny GPIO sources and include together with this file (I called it tglibtest.c)
Code:
#define _GNU_SOURCE#include <stdio.h>#include <stdint.h>#include <unistd.h>#include "tinygpio.h"#define GPIOMAX 53void blink (unsigned pin, unsigned on_usec, unsigned off_usec, unsigned cnt){ int i = 0; while (cnt--) { gpioWrite (pin, 1); usleep (on_usec); printf ("blink: %d\r", ++i); fflush (stdout); gpioWrite (pin, 0); usleep (off_usec); } putchar ('\n');}int main (int argc, char **argv){ unsigned pin = 4, /* gpio pin - argv[1] */ on_usec = 50000, /* blink on microseconds - argv[2] */ off_usec = 200000, /* blink off microseconds - argv[3] */ repeat = 20; /* number of blinks - argv[4] */ int rtn; /* function return */ if (argc > 1) { /* if pin argument provided, set pin */ unsigned tmp; if (sscanf (argv[1], "%u", &tmp) != 1) { fprintf (stderr, "error: invalid unsigned pin value for argv[1] (%s)\n", argv[1]); return 1; } pin = tmp; } if (argc > 2) { /* if microseconds ON time provided, set on_usec */ unsigned tmp; if (sscanf (argv[2], "%u", &tmp) != 1) { fprintf (stderr, "error: invalid microseconds ON time for argv[2] (%s)\n", argv[2]); return 1; } on_usec = tmp; } if (argc > 3) { /* if microseconds OFF time provided, set off_usec */ unsigned tmp; if (sscanf (argv[3], "%u", &tmp) != 1) { fprintf (stderr, "error: invalid microseconds OFF time for argv[3] (%s)\n", argv[3]); return 1; } off_usec = tmp; } if (argc > 4) { /* if no. of blinks to repeat provided, set repeat */ unsigned tmp; if (sscanf (argv[4], "%u", &tmp) != 1) { fprintf (stderr, "error: invalid unsigned repeat value for argv[4] (%s)\n", argv[4]); return 1; } repeat = tmp; } if (GPIOMAX < pin) { /* validate pin no. is valid */ fprintf (stderr, "error: invalid gpio pin '%u' (0-53 allowed)\n", pin); return 1; } rtn = gpioInitialise(); /* initialize tinygpio lib */ if (rtn < 0) { /* validate initiaization succeeded */ fprintf (stderr, "error: gpioInitialise() returned %d\n", rtn); return 1; } printf("Pi model = %u, Pi revision = %u\n", *pimodel, *pirev); gpioSetMode (pin, PI_OUTPUT); /* set output mode on pin */ if ((rtn = gpioGetMode (pin)) != PI_OUTPUT) { /* validate mode set */ fprintf (stderr, "error get_mode != PI_OUTPUT : %d\n", rtn); return 1; } blink (pin, on_usec, off_usec, repeat); /* blink LED attached to pin */}
Code:
$ gcc -Wall -Wextra -o tglibtest -std=c11 tglibtest.c tinygpio.c
Code:
$ ./tglibtest 4 15000 60000 60
Statistics: Posted by drankinatty — Tue Apr 09, 2024 6:18 am