RocketLogger  1.0
gpio.c
Go to the documentation of this file.
1 
5 #include "gpio.h"
6 
12 int gpio_unexport(int num) {
13  // open gpio value file
14  int fd = open(GPIO_PATH "unexport", O_WRONLY);
15  if (fd < 0) {
16  rl_log(ERROR, "could not open GPIO unexport file");
17  return FAILURE;
18  }
19 
20  // export gpio
21  char buf[5] = "";
22  int len = snprintf(buf, sizeof(buf),"%d",num);
23  write(fd, buf, len);
24 
25  // close file
26  close(fd);
27 
28  return SUCCESS;
29 }
30 
31 
37 int gpio_export(int num) {
38  // open gpio value file
39  int fd = open(GPIO_PATH "export", O_WRONLY);
40  if (fd < 0) {
41  rl_log(ERROR, "could not open GPIO export file");
42  return FAILURE;
43  }
44 
45  // export gpio
46  char buf[5] = "";
47  int len = snprintf(buf, sizeof(buf),"%d",num);
48  write(fd, buf, len);
49 
50  // close file
51  close(fd);
52 
53  return SUCCESS;
54 }
55 
62 int gpio_dir(int num, rl_direction dir) {
63 
64  // open gpio direction file
65  char file_name[MAX_PATH_LENGTH];
66  sprintf(file_name, GPIO_PATH "gpio%d/direction",num);
67  int fd = open(file_name, O_WRONLY);
68  if (fd < 0) {
69  rl_log(ERROR, "could not open GPIO direction file");
70  return FAILURE;
71  }
72 
73  // set direction
74  if(dir == OUT) {
75  write(fd, "out", 4);
76  } else {
77  write(fd, "in", 3);
78  }
79  // close file
80  close(fd);
81 
82  return SUCCESS;
83 }
84 
85 
92 int gpio_interrupt(int num, rl_edge edge) {
93 
94  // open gpio edge file
95  char file_name[MAX_PATH_LENGTH];
96  sprintf(file_name, GPIO_PATH "gpio%d/edge",num);
97  int fd = open(file_name, O_WRONLY);
98  if (fd < 0) {
99  rl_log(ERROR, "could not open GPIO edge file");
100  return FAILURE;
101  }
102 
103  // set edge
104  switch (edge) {
105  case NONE:
106  write(fd, "none", 5);
107  break;
108  case RISING:
109  write(fd, "rising", 7);
110  break;
111  case FALLING:
112  write(fd, "falling", 8);
113  break;
114  case BOTH:
115  write(fd, "both", 5);
116  break;
117 
118  }
119 
120  // close file
121  close(fd);
122 
123  return SUCCESS;
124 }
125 
132 int gpio_set_value(int num, int val) {
133 
134  // open gpio value file
135  char file_name[MAX_PATH_LENGTH];
136  sprintf(file_name, GPIO_PATH "gpio%d/value",num);
137  int fd = open(file_name, O_WRONLY);
138  if (fd < 0) {
139  rl_log(ERROR, "could not open GPIO value file");
140  return FAILURE;
141  }
142 
143  // set value
144  if(val == 0) {
145  write(fd, "0", 2);
146  } else {
147  write(fd, "1", 2);
148  }
149 
150  // close file
151  close(fd);
152 
153  return SUCCESS;
154 }
155 
161 int gpio_get_value(int num) {
162 
163  // open gpio value file
164  char file_name[MAX_PATH_LENGTH];
165  sprintf(file_name, GPIO_PATH "gpio%d/value",num);
166  int fd = open(file_name, O_RDONLY);
167  if (fd < 0) {
168  rl_log(ERROR, "could not open GPIO value file");
169  return FAILURE;
170  }
171 
172  // read value
173  char buf[2] = "";
174  read(fd, &buf, 1);
175 
176  // close file
177  close(fd);
178 
179  return atoi(buf);
180 }
181 
188 int gpio_wait_interrupt(int num, int timeout) {
189 
190  // open gpio value file
191  char file_name[MAX_PATH_LENGTH];
192  sprintf(file_name, GPIO_PATH "gpio%d/value",num);
193  int fd = open(file_name, O_RDONLY);
194  if (fd < 0) {
195  rl_log(ERROR, "could not open GPIO value file");
196  return FAILURE;
197  }
198 
199  // set up polling struct
200  struct pollfd fds;
201  fds.fd = fd;
202  fds.events = POLLPRI;
203  int nfds = 1;
204  int ret;
205 
206  // dummy read (enables blocking polling)
207  char buf[2] = "";
208  read(fds.fd, &buf, 1);
209 
210  // wait on gpio change
211  ret = poll(&fds, nfds, timeout);
212  if (ret < 0) {
213  rl_log(ERROR, "GPIO poll failed");
214  return FAILURE;
215  }
216 
217  // wait for signal to settle
218  usleep(MIN_BUTTON_TIME);
219 
220  // read value
221  lseek(fds.fd, 0, SEEK_SET);
222  read(fds.fd, &buf, 1);
223 
224  close(fd);
225 
226  return atoi(buf);
227 }
enum edge rl_edge
int gpio_get_value(int num)
Definition: gpio.c:161
#define GPIO_PATH
Path to linux GPIO device files.
Definition: gpio.h:16
#define FAILURE
Definition: types.h:53
#define SUCCESS
Definition: types.h:50
int gpio_export(int num)
Definition: gpio.c:37
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:12
Interrupt on falling edge.
Definition: gpio.h:35
Interrupt on rising edge.
Definition: gpio.h:34
#define MAX_PATH_LENGTH
Maximum path length in characters.
Definition: types.h:76
int gpio_interrupt(int num, rl_edge edge)
Definition: gpio.c:92
int gpio_set_value(int num, int val)
Definition: gpio.c:132
int gpio_wait_interrupt(int num, int timeout)
Definition: gpio.c:188
GPIO write mode.
Definition: gpio.h:26
#define MIN_BUTTON_TIME
Minimal time a button needs to be pressed (in µs)
Definition: gpio.h:18
int gpio_unexport(int num)
Definition: gpio.c:12
Interrupt on both edges.
Definition: gpio.h:36
Error.
Definition: types.h:167
edge
Definition: gpio.h:32
No interrupt.
Definition: gpio.h:33
int gpio_dir(int num, rl_direction dir)
Definition: gpio.c:62
enum direction rl_direction