Skip to content

Instantly share code, notes, and snippets.

@maesoser
Created December 23, 2022 00:33
Show Gist options
  • Save maesoser/25bf8894f95690c28366de8a158e1f08 to your computer and use it in GitHub Desktop.
Save maesoser/25bf8894f95690c28366de8a158e1f08 to your computer and use it in GitHub Desktop.
Armbian/Raspberry led blink daemon
#include <wiringPi.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define TRAFFIC_TRESH 64
#define BLINK_DELAY 83
#define TX_LED 7 // GREEN
#define RX_LED 0 // RED
typedef struct{
long long tx;
long long rx;
long long lasttx;
long long lastrx;
bool txled;
bool rxled;
}iface_t;
void getstatus(char *dev, iface_t *if1){
FILE *netinfo = fopen("/proc/net/dev", "r");
if(netinfo == NULL){
exit(-1);
}
char buff[256];
while(fgets(buff, sizeof(buff), netinfo)){
long long tx = 0;
long long rx = 0;
char ifname[32];
sscanf( buff," %[^:]: %Lu %*u %*u %*u %*u %*u %*u %*u %Lu %*u %*u %*u %*u %*u %*u %*u",ifname, &rx, &tx );
if(strcmp(ifname,dev)==0){
//printf("%s %Lu %Lu tx/rx\n",ifname ,tx,rx);
if1->lasttx = if1->tx;
if1->lastrx = if1->rx;
if1->tx = tx;
if1->rx = rx;
}
}
if(fclose(netinfo) != 0){
exit(-1);
}
}
int main (int argc, char **argv){
if (argc!=2){
printf("ifled <iface> \n");
return -1;
}
wiringPiSetup () ;
pinMode (TX_LED, OUTPUT) ;
pinMode (RX_LED, OUTPUT) ;
digitalWrite (TX_LED,LOW);
digitalWrite (RX_LED,LOW);
iface_t ifstats;
ifstats.tx = 0;
ifstats.rx = 0;
ifstats.lasttx = 0;
ifstats.lastrx = 0;
ifstats.rxled = false;
ifstats.txled = true;
for (;;)
{
getstatus(argv[1],&ifstats);
if( (ifstats.tx - ifstats.lasttx) > TRAFFIC_TRESH ){
ifstats.txled = !ifstats.txled;
}
else{
ifstats.txled = false;
}
if ((ifstats.rx - ifstats.lastrx) > TRAFFIC_TRESH){
ifstats.rxled = !ifstats.rxled;
}
else {
ifstats.rxled = false;
}
digitalWrite(RX_LED, ifstats.rxled);
digitalWrite(TX_LED, ifstats.txled);
//printf("TX: %Lu\tRX: %Lu\n",tx,rx);
delay(BLINK_DELAY);
}
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment