+#include <stdlib.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+
+#include <sys/types.h>
+#include <sys/gpio.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include "seg.h"
+
+#define INPUT 1 << 0
+#define OUTPUT 1 << 1
+#define PULLUP 1 << 2
+#define PULLDOWN 1 << 3
+
+struct pin2dev {
+ int pin;
+ int dev;
+ char *name;
+};
+
+int pin_cnt;
+struct pin2dev *pinmap;
+volatile int g0, g1, g2;
+
+int num2fd(int i) {
+ if (i == 0)
+ return g0;
+ else if (i == 1)
+ return g1;
+ else if (i == 2)
+ return g2;
+
+ return 0;
+}
+
+struct pin2dev *pin(char *name) {
+ for(int i = 0; i < pin_cnt; i++) {
+ if (! strncmp(name, pinmap[i].name, GPIOPINMAXNAME))
+ return &pinmap[i];
+ }
+
+ return NULL;
+}
+
+void set(char *name, int val) {
+ struct pin2dev *p;
+ struct gpio_pin_op op;
+ int fd;
+
+ p = pin(name);
+ op.gp_pin = p->pin;
+ op.gp_value = val;
+ fd = num2fd(p->dev);
+
+ //printf("given name: %s\nderived name: %s\nderived pin: %d\nval: %d\nfd: %d\nreal fd: %d\n", name, p->name,
+ // p->pin, op.gp_value, fd, g1);
+
+// printf("g0: %d g1: %d g2: %d\n", g0, g1, g2);
+
+ ioctl(fd, GPIOPINWRITE, &op);
+
+// printf("old: %d new: %d\n", op.gp_value, val);
+}
+
+void pins_init() {
+ pinmap = calloc(100, sizeof(struct pin2dev));
+ pin_cnt = 0;
+}
+
+void pcfg(int devno, int pin, int val, uint8_t mode, char *name) {
+ char *fill = calloc(1, 255);
+ char *pill = calloc(1, 255);
+ char *vl = calloc(1, 255);
+ char *nm = calloc(1, 255);
+ char *md = calloc(1, 255);
+
+ usleep(1000 * 500);
+ strlcpy(nm, name, GPIOPINMAXNAME);
+
+ if (mode & OUTPUT)
+ strlcpy(md, "out", 255);
+ else if (mode & INPUT) {
+ if (mode & PULLUP)
+ strlcpy(md, "in,pu", 255);
+ else if (mode & PULLDOWN)
+ strlcpy(md, "in,pd", 255);
+ else
+ strlcpy(md, "in", 255);
+ }
+
+ pinmap[pin_cnt].name = calloc(1, GPIOPINMAXNAME);
+ strlcpy(pinmap[pin_cnt].name, name, GPIOPINMAXNAME);
+ strlcpy(nm, name, GPIOPINMAXNAME);
+ pinmap[pin_cnt].pin = pin;
+ pinmap[pin_cnt].dev = devno;
+ pin_cnt++;
+
+ sprintf(fill, "gpioctl gpio%d %d set %s", devno, pin, nm);
+ sprintf(pill, "gpioctl gpio%d %s set %s", devno, nm, md);
+ sprintf(vl, "gpioctl gpio%d %d %d", devno, pin, val);
+ printf("%s\n%s\n", fill, pill);
+ system(fill);
+ system(pill);
+ if (val < 0)
+ return;
+ printf("%s\n", vl);
+ system(vl);
+}