new stuff
[bort-panel.git] / pins.h
CommitLineData
3bacb654
IS
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
21struct pin2dev {
22 int pin;
23 int dev;
24 char *name;
25};
26
27int pin_cnt;
28struct pin2dev *pinmap;
29volatile int g0, g1, g2;
30
31int 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
42struct 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
51void set(char *name, int val) {
52 struct pin2dev *p;
53 struct gpio_pin_op op;
54 int fd;
55
56 p = pin(name);
57 op.gp_pin = p->pin;
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
71void pins_init() {
72 pinmap = calloc(100, sizeof(struct pin2dev));
73 pin_cnt = 0;
74}
75
76void 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 usleep(1000 * 500);
84 strlcpy(nm, name, GPIOPINMAXNAME);
85
86 if (mode & OUTPUT)
87 strlcpy(md, "out", 255);
88 else if (mode & INPUT) {
89 if (mode & PULLUP)
90 strlcpy(md, "in,pu", 255);
91 else if (mode & PULLDOWN)
92 strlcpy(md, "in,pd", 255);
93 else
94 strlcpy(md, "in", 255);
95 }
96
97 pinmap[pin_cnt].name = calloc(1, GPIOPINMAXNAME);
98 strlcpy(pinmap[pin_cnt].name, name, GPIOPINMAXNAME);
99 strlcpy(nm, name, GPIOPINMAXNAME);
100 pinmap[pin_cnt].pin = pin;
101 pinmap[pin_cnt].dev = devno;
102 pin_cnt++;
103
104 sprintf(fill, "gpioctl gpio%d %d set %s", devno, pin, nm);
105 sprintf(pill, "gpioctl gpio%d %s set %s", devno, nm, md);
106 sprintf(vl, "gpioctl gpio%d %d %d", devno, pin, val);
107 printf("%s\n%s\n", fill, pill);
108 system(fill);
109 system(pill);
110 if (val < 0)
111 return;
112 printf("%s\n", vl);
113 system(vl);
114}