Updated to use pigpio library over the wiringpi library

This commit is contained in:
John Vidler 2020-05-26 06:59:18 +01:00
parent bca55dfa8c
commit 483c72215b
4 changed files with 116 additions and 39 deletions

24
Makefile Normal file
View File

@ -0,0 +1,24 @@
# tool macros
CC := gcc
CFLAGS := -Wall -O3 -pthread -lpigpio -lrt
# default rule
default: all
# phony rules
.PHONY: all
all: netledPi actledPi hddledPi
netledPi:
$(CC) $(CFLAGS) -o netledPi netledPi.c
hddledPi:
$(CC) $(CFLAGS) -o hddledPi hddledPi.c
actledPi:
$(CC) $(CFLAGS) -o actledPi actledPi.c
.PHONY: clean
clean:
@rm -f *.o netledPi hddledPi actledPi

View File

@ -1,6 +1,13 @@
# PiLEDlights # PiLEDlights
For the Raspberry Pi - Make LEDs blink on network and/or SD card/USB storage activity. For the Raspberry Pi - Make LEDs blink on network and/or SD card/USB storage activity.
## Modified to use the piGPIO library
I've modified these sources to use the piGPIO library instead of the unsupported wiringPi library.
The original README text is here for reference, but the actual parameters and values are now different (netleds, for example now has a distinct tx and rx LED)
## Original README
hddledPi blinks a LED connected to a GPIO pin on any mass storage access. Not only on SD card access, but also on USB thumbdrive and hard drive activity. hddledPi blinks a LED connected to a GPIO pin on any mass storage access. Not only on SD card access, but also on USB thumbdrive and hard drive activity.
netledPi blinks a LED connected to a GPIO pin when there is activity on any network interface. Not only the built-in ethernet interface, but also on any other USB ethernet or WiFi interface. netledPi blinks a LED connected to a GPIO pin when there is activity on any network interface. Not only the built-in ethernet interface, but also on any other USB ethernet or WiFi interface.
actledPi blinks the Pi's ACT led on all mass storage I/O, i.e. not only the SD card. actledPi blinks the Pi's ACT led on all mass storage I/O, i.e. not only the SD card.

View File

@ -61,11 +61,11 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <wiringPi.h> #include <pigpio.h>
static unsigned int o_refresh = 20; /* milliseconds */ static unsigned int o_refresh = 20; /* milliseconds */
static unsigned int o_gpiopin = 10; /* wiringPi numbering scheme */ static unsigned int o_gpiopin = 4; /* BCM numbering scheme */
static int o_detach = 0; static int o_detach = 0;
static volatile sig_atomic_t running = 1; static volatile sig_atomic_t running = 1;
@ -125,9 +125,9 @@ void led(int on) {
return; return;
if (on) { if (on) {
digitalWrite (o_gpiopin, HIGH); gpioWrite( o_gpiopin, PI_HIGH );
} else { } else {
digitalWrite (o_gpiopin, LOW); gpioWrite( o_gpiopin, PI_LOW );
} }
current = on; current = on;
@ -163,7 +163,7 @@ error_t parse_options(int key, char *arg, struct argp_state *state) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
struct argp_option options[] = { struct argp_option options[] = {
{ "detach", 'd', NULL, 0, "Detach from terminal" }, { "detach", 'd', NULL, 0, "Detach from terminal" },
{ "pin", 'p', "VALUE", 0, "GPIO pin where LED is connected (default: wiringPi pin 10, physical pin 24 on the P1 header)" }, { "pin", 'p', "VALUE", 0, "GPIO pin where LED is connected (default: BCM 4, physical pin 7 on the P1 header)" },
{ "refresh", 'r', "VALUE", 0, "Refresh interval (default: 20 ms)" }, { "refresh", 'r', "VALUE", 0, "Refresh interval (default: 20 ms)" },
{ 0 }, { 0 },
}; };
@ -184,8 +184,12 @@ int main(int argc, char **argv) {
delay.tv_sec = o_refresh / 1000; delay.tv_sec = o_refresh / 1000;
delay.tv_nsec = 1000000 * (o_refresh % 1000); delay.tv_nsec = 1000000 * (o_refresh % 1000);
wiringPiSetup () ; /* If we can't set up pigpio, then just bail */
pinMode (o_gpiopin, OUTPUT) ; if( gpioInitialise() < 0 ) {
fprintf( stderr, "Unable to setup the piGPIO library. STOP." );
return -1;
}
gpioSetMode( o_gpiopin, PI_OUTPUT );
/* Open the vmstat file */ /* Open the vmstat file */
@ -196,7 +200,7 @@ int main(int argc, char **argv) {
} }
/* Ensure the LED is off */ /* Ensure the LED is off */
led(LOW); led(0);
/* Save the current I/O stat values */ /* Save the current I/O stat values */
if (activity(vmstat) < 0) if (activity(vmstat) < 0)
@ -240,7 +244,10 @@ int main(int argc, char **argv) {
} }
/* Ensure the LED is off */ /* Ensure the LED is off */
led(LOW); led(0);
/* Halt any library functions */
gpioTerminate();
status = EXIT_SUCCESS; status = EXIT_SUCCESS;

View File

@ -61,17 +61,46 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <wiringPi.h> #include <pigpio.h>
static unsigned int o_refresh = 20; /* milliseconds */ static unsigned int o_refresh = 20; /* milliseconds */
static unsigned int o_gpiopin = 11; /* wiringPi numbering scheme */ static unsigned int o_gpiopin_tx = 2;
static unsigned int o_gpiopin_rx = 3;
static int o_detach = 0; static int o_detach = 0;
static volatile sig_atomic_t running = 1; static volatile sig_atomic_t running = 1;
static char *line = NULL; static char *line = NULL;
static size_t len = 0; static size_t len = 0;
/* Update the TX LED */
void tx_led(int on) {
static int tx_current = 1; /* Ensure the LED turns off on first call */
if (tx_current == on)
return;
if (on)
gpioWrite( o_gpiopin_tx, PI_HIGH );
else
gpioWrite( o_gpiopin_tx, PI_LOW );
tx_current = on;
}
/* Update the RX LED */
void rx_led(int on) {
static int rx_current = 1; /* Ensure the LED turns off on first call */
if (rx_current == on)
return;
if (on)
gpioWrite( o_gpiopin_rx, PI_HIGH );
else
gpioWrite( o_gpiopin_rx, PI_LOW );
rx_current = on;
}
/* Reread the netdevices file */ /* Reread the netdevices file */
int activity(FILE *netdevices) { int activity(FILE *netdevices) {
static unsigned int prev_inpackets, prev_outpackets; static unsigned int prev_inpackets, prev_outpackets;
@ -118,28 +147,23 @@ int activity(FILE *netdevices) {
} }
/* Anything changed? */ /* Anything changed? */
result = (prev_inpackets != inpackets) || if( prev_inpackets != inpackets )
(prev_outpackets != outpackets); rx_led( PI_HIGH );
else
rx_led( PI_LOW );
if( prev_outpackets != outpackets )
tx_led( PI_HIGH );
else
tx_led( PI_LOW );
prev_inpackets = inpackets; prev_inpackets = inpackets;
prev_outpackets = outpackets; prev_outpackets = outpackets;
return result; return 0;
} }
/* Update the LED */
void led(int on) {
static int current = 1; /* Ensure the LED turns off on first call */
if (current == on)
return;
if (on) {
digitalWrite (o_gpiopin, HIGH);
} else {
digitalWrite (o_gpiopin, LOW);
}
current = on;
}
/* Signal handler -- break out of the main loop */ /* Signal handler -- break out of the main loop */
void shutdown(int sig) { void shutdown(int sig) {
@ -158,9 +182,15 @@ error_t parse_options(int key, char *arg, struct argp_state *state) {
argp_failure(state, EXIT_FAILURE, 0, argp_failure(state, EXIT_FAILURE, 0,
"refresh interval must be at least 10"); "refresh interval must be at least 10");
break; break;
case 'p': case 3:
o_gpiopin = strtol(arg, NULL, 10); o_gpiopin_tx = strtol(arg, NULL, 10);
if ((o_gpiopin < 0) || (o_gpiopin > 29)) if ((o_gpiopin_tx < 0) || (o_gpiopin_tx > 29))
argp_failure(state, EXIT_FAILURE, 0,
"pin number must be between 0 and 29");
break;
case 4:
o_gpiopin_rx = strtol(arg, NULL, 10);
if ((o_gpiopin_rx < 0) || (o_gpiopin_rx > 29))
argp_failure(state, EXIT_FAILURE, 0, argp_failure(state, EXIT_FAILURE, 0,
"pin number must be between 0 and 29"); "pin number must be between 0 and 29");
break; break;
@ -169,9 +199,11 @@ error_t parse_options(int key, char *arg, struct argp_state *state) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
struct argp_option options[] = { struct argp_option options[] = {
{ "detach", 'd', NULL, 0, "Detach from terminal" }, { "detach", 'd', NULL, 0, "Detach from terminal" },
{ "pin", 'p', "VALUE", 0, "GPIO pin where LED is connected (default: wiringPi pin 11, physical pin 25 on the P1 header)" }, { "tx" , 0, "VALUE", 0, "GPIO pin where LED is connected (transmit data) (default: BCM 2 physical pin 3 on the P1 header)" },
{ "rx", 1, "VALUE", 0, "GPIO pin where LED is connected (receive data) (default: BCM 3 physical pin 5 on the P1 header)" },
{ "refresh", 'r', "VALUE", 0, "Refresh interval (default: 20 ms)" }, { "refresh", 'r', "VALUE", 0, "Refresh interval (default: 20 ms)" },
{ 0 }, { 0 },
}; };
@ -192,8 +224,13 @@ int main(int argc, char **argv) {
delay.tv_sec = o_refresh / 1000; delay.tv_sec = o_refresh / 1000;
delay.tv_nsec = 1000000 * (o_refresh % 1000); delay.tv_nsec = 1000000 * (o_refresh % 1000);
wiringPiSetup () ; /* If we can't set up pigpio, then just bail */
pinMode (o_gpiopin, OUTPUT) ; if( gpioInitialise() < 0 ) {
fprintf( stderr, "Unable to setup the piGPIO library. STOP." );
return -1;
}
gpioSetMode( o_gpiopin_tx, PI_OUTPUT );
gpioSetMode( o_gpiopin_rx, PI_OUTPUT );
/* Open the netdevices file */ /* Open the netdevices file */
@ -204,7 +241,8 @@ int main(int argc, char **argv) {
} }
/* Ensure the LED is off */ /* Ensure the LED is off */
led(LOW); tx_led(0);
rx_led(0);
/* Save the current I/O stat values */ /* Save the current I/O stat values */
if (activity(netdevices) < 0) if (activity(netdevices) < 0)
@ -238,21 +276,22 @@ int main(int argc, char **argv) {
/* Loop until signal received */ /* Loop until signal received */
while (running) { while (running) {
int a;
if (nanosleep(&delay, NULL) < 0) if (nanosleep(&delay, NULL) < 0)
break; break;
a = activity(netdevices); if( activity(netdevices) < 0 )
if (a < 0)
break; break;
led(a);
} }
/* Ensure the LED is off */ /* Ensure the LED is off */
led(LOW); tx_led(0);
rx_led(0);
status = EXIT_SUCCESS; status = EXIT_SUCCESS;
out: out:
/* Halt any library functions */
gpioTerminate();
if (line) free(line); if (line) free(line);
if (netdevices) fclose(netdevices); if (netdevices) fclose(netdevices);
return status; return status;