Skip to content

Instantly share code, notes, and snippets.

@andrewvc
Created May 14, 2015 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewvc/825918680b8777c4fa72 to your computer and use it in GitHub Desktop.
Save andrewvc/825918680b8777c4fa72 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <unistd.h>
typedef struct
{
int num;
int state;
} Pin;
Pin * each_pin (int pinc, Pin pins[], Pin (*fun)(Pin, int))
{
int i;
Pin p;
for (i=0;i<pinc;i++) {
p = pins[i];
pins[i] = (*fun)(p, i);
}
return pins;
}
Pin print_pin(Pin p, int idx)
{
printf("The pin is %i\n", p.num);
return p;
}
Pin setup_pin(Pin p, int idx)
{
int new_state = (idx % 2 == 0) ? HIGH : LOW;
printf("Setting up GPIO %i as OUTPUT\n", p.num);
pinMode(p.num, OUTPUT);
p.state = new_state;
digitalWrite(p.num, p.state);
return p;
}
Pin flip_pin(Pin p, int idx)
{
p.state = (p.state == HIGH) ? LOW : HIGH;
digitalWrite(p.num, p.state);
return p;
}
Pin make_pin(num)
{
Pin p = {num, LOW};
return p;
}
int main (int argc, char *argv[])
{
int pinc = argc - 1;
Pin pins[argc];
int i;
for (i=1; i < argc; i++)
{
char *arg = argv[i];
pins[i-1] = make_pin(atoi(arg));
printf("Arg is %s\n", arg);
}
printf("Arg1 is %i", atoi(argv[1]) );
wiringPiSetup();
each_pin(pinc, pins, &print_pin);
each_pin(pinc, pins, &setup_pin);
for (;;)
{
each_pin(pinc, pins, &flip_pin);
usleep(300000);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment