| 1 | #include <stdlib.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <signal.h> |
| 4 | #include <unistd.h> |
| 5 | #include <string.h> |
| 6 | #include <stdio.h> |
| 7 | #include <time.h> |
| 8 | |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/gpio.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <sys/time.h> |
| 13 | |
| 14 | #include "seg.h" |
| 15 | |
| 16 | #define INPUT 1 << 0 |
| 17 | #define OUTPUT 1 << 1 |
| 18 | #define PULLUP 1 << 2 |
| 19 | #define PULLDOWN 1 << 3 |
| 20 | |
| 21 | struct pin2dev { |
| 22 | int pin; |
| 23 | int dev; |
| 24 | char *name; |
| 25 | }; |
| 26 | |
| 27 | int pin_cnt; |
| 28 | struct pin2dev *pinmap; |
| 29 | volatile int g0, g1, g2; |
| 30 | |
| 31 | int num2fd(int i) { |
| 32 | if (i == 0) |
| 33 | return g0; |
| 34 | else if (i == 1) |
| 35 | return g1; |
| 36 | else if (i == 2) |
| 37 | return g2; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | struct pin2dev *pin(char *name) { |
| 43 | for(int i = 0; i < pin_cnt; i++) { |
| 44 | if (! strncmp(name, pinmap[i].name, GPIOPINMAXNAME)) |
| 45 | return &pinmap[i]; |
| 46 | } |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | void set(char *name, int val) { |
| 52 | struct pin2dev *p; |
| 53 | struct gpio_pin_op op; |
| 54 | int fd; |
| 55 | |
| 56 | p = pin(name); |
| 57 | strlcpy(op.gp_name, name, GPIOPINMAXNAME); |
| 58 | op.gp_value = val; |
| 59 | fd = num2fd(p->dev); |
| 60 | |
| 61 | // printf("given name: %s\nderived name: %s\nderived pin: %d\nval: %d\nfd: %d\nreal fd: %d\n", name, p->name, |
| 62 | // p->pin, op.gp_value, fd, g1); |
| 63 | |
| 64 | // printf("g0: %d g1: %d g2: %d\n", g0, g1, g2); |
| 65 | |
| 66 | ioctl(fd, GPIOPINWRITE, &op); |
| 67 | |
| 68 | // printf("old: %d new: %d\n", op.gp_value, val); |
| 69 | } |
| 70 | |
| 71 | void pins_init() { |
| 72 | pinmap = calloc(100, sizeof(struct pin2dev)); |
| 73 | pin_cnt = 0; |
| 74 | } |
| 75 | |
| 76 | void pcfg(int devno, int pin, int val, uint8_t mode, char *name) { |
| 77 | char *fill = calloc(1, 255); |
| 78 | char *pill = calloc(1, 255); |
| 79 | char *vl = calloc(1, 255); |
| 80 | char *nm = calloc(1, 255); |
| 81 | char *md = calloc(1, 255); |
| 82 | |
| 83 | strlcpy(nm, name, GPIOPINMAXNAME); |
| 84 | |
| 85 | if (mode & OUTPUT) |
| 86 | strlcpy(md, "out", 255); |
| 87 | else if (mode & INPUT) { |
| 88 | if (mode & PULLUP) |
| 89 | strlcpy(md, "in,pu", 255); |
| 90 | else if (mode & PULLDOWN) |
| 91 | strlcpy(md, "in,pd", 255); |
| 92 | else |
| 93 | strlcpy(md, "in", 255); |
| 94 | } |
| 95 | |
| 96 | pinmap[pin_cnt].name = calloc(1, GPIOPINMAXNAME); |
| 97 | strlcpy(pinmap[pin_cnt].name, name, GPIOPINMAXNAME); |
| 98 | strlcpy(nm, name, GPIOPINMAXNAME); |
| 99 | pinmap[pin_cnt].pin = pin; |
| 100 | pinmap[pin_cnt].dev = devno; |
| 101 | pin_cnt++; |
| 102 | |
| 103 | sprintf(fill, "gpioctl gpio%d %d set %s", devno, pin, nm); |
| 104 | sprintf(pill, "gpioctl gpio%d %s set %s", devno, nm, md); |
| 105 | sprintf(vl, "gpioctl gpio%d %d %d", devno, pin, val); |
| 106 | printf("%s\n%s\n", fill, pill); |
| 107 | system(fill); |
| 108 | system(pill); |
| 109 | if (val < 0) |
| 110 | return; |
| 111 | printf("%s\n", vl); |
| 112 | system(vl); |
| 113 | } |