9 static void setport(int p, int v)
11 char path[256], line[256];
15 sprintf(path, "/sys/class/gpio/gpio%i/direction", p);
16 if((fp = fopen(path, "r")) == NULL)
18 rv = !!fgets(line, sizeof(line), fp);
20 if(!rv || strcmp(line, "out\n")) {
21 if((fp = fopen(path, "w")) == NULL)
23 fprintf(fp, "out\n"); fflush(fp);
25 errx(1, "gpio%i: could not set to output", p);
28 sprintf(path, "/sys/class/gpio/gpio%i/value", p);
29 if((fp = fopen(path, "w")) == NULL)
31 fprintf(fp, "%i\n", v); fflush(fp);
33 errx(1, "gpio%i: could not set value", p);
37 int main(int argc, char **argv)
42 for(i = 1; i < argc; i++) {
43 if((p = strchr(argv[i], '=')) == NULL) {
44 fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]);
47 port = strtol(argv[i], &e, 10);
49 fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]);
52 val = strtol(p + 1, &e, 10);
54 fprintf(stderr, "gpio: %s: not of the form PORT=VAL\n", argv[i]);