RocketLogger  1.1
gpio.c
Go to the documentation of this file.
1 
31 #include "gpio.h"
32 
38 int gpio_unexport(int num) {
39  // open gpio value file
40  int fd = open(GPIO_PATH "unexport", O_WRONLY);
41  if (fd < 0) {
42  rl_log(ERROR, "could not open GPIO unexport file");
43  return FAILURE;
44  }
45 
46  // export gpio
47  char buf[5] = "";
48  int len = snprintf(buf, sizeof(buf), "%d", num);
49  write(fd, buf, len);
50 
51  // close file
52  close(fd);
53 
54  return SUCCESS;
55 }
56 
62 int gpio_export(int num) {
63  // open gpio value file
64  int fd = open(GPIO_PATH "export", O_WRONLY);
65  if (fd < 0) {
66  rl_log(ERROR, "could not open GPIO export file");
67  return FAILURE;
68  }
69 
70  // export gpio
71  char buf[5] = "";
72  int len = snprintf(buf, sizeof(buf), "%d", num);
73  write(fd, buf, len);
74 
75  // close file
76  close(fd);
77 
78  return SUCCESS;
79 }
80 
87 int gpio_dir(int num, rl_direction dir) {
88 
89  // open gpio direction file
90  char file_name[MAX_PATH_LENGTH];
91  sprintf(file_name, GPIO_PATH "gpio%d/direction", num);
92  int fd = open(file_name, O_WRONLY);
93  if (fd < 0) {
94  rl_log(ERROR, "could not open GPIO direction file");
95  return FAILURE;
96  }
97 
98  // set direction
99  if (dir == OUT) {
100  write(fd, "out", 4);
101  } else {
102  write(fd, "in", 3);
103  }
104  // close file
105  close(fd);
106 
107  return SUCCESS;
108 }
109 
116 int gpio_interrupt(int num, rl_edge edge) {
117 
118  // open gpio edge file
119  char file_name[MAX_PATH_LENGTH];
120  sprintf(file_name, GPIO_PATH "gpio%d/edge", num);
121  int fd = open(file_name, O_WRONLY);
122  if (fd < 0) {
123  rl_log(ERROR, "could not open GPIO edge file");
124  return FAILURE;
125  }
126 
127  // set edge
128  switch (edge) {
129  case NONE:
130  write(fd, "none", 5);
131  break;
132  case RISING:
133  write(fd, "rising", 7);
134  break;
135  case FALLING:
136  write(fd, "falling", 8);
137  break;
138  case BOTH:
139  write(fd, "both", 5);
140  break;
141  }
142 
143  // close file
144  close(fd);
145 
146  return SUCCESS;
147 }
148 
155 int gpio_set_value(int num, int val) {
156 
157  // open gpio value file
158  char file_name[MAX_PATH_LENGTH];
159  sprintf(file_name, GPIO_PATH "gpio%d/value", num);
160  int fd = open(file_name, O_WRONLY);
161  if (fd < 0) {
162  rl_log(ERROR, "could not open GPIO value file");
163  return FAILURE;
164  }
165 
166  // set value
167  if (val == 0) {
168  write(fd, "0", 2);
169  } else {
170  write(fd, "1", 2);
171  }
172 
173  // close file
174  close(fd);
175 
176  return SUCCESS;
177 }
178 
184 int gpio_get_value(int num) {
185 
186  // open gpio value file
187  char file_name[MAX_PATH_LENGTH];
188  sprintf(file_name, GPIO_PATH "gpio%d/value", num);
189  int fd = open(file_name, O_RDONLY);
190  if (fd < 0) {
191  rl_log(ERROR, "could not open GPIO value file");
192  return FAILURE;
193  }
194 
195  // read value
196  char buf[2] = "";
197  read(fd, &buf, 1);
198 
199  // close file
200  close(fd);
201 
202  return atoi(buf);
203 }
204 
211 int gpio_wait_interrupt(int num, int timeout) {
212 
213  // open gpio value file
214  char file_name[MAX_PATH_LENGTH];
215  sprintf(file_name, GPIO_PATH "gpio%d/value", num);
216  int fd = open(file_name, O_RDONLY);
217  if (fd < 0) {
218  rl_log(ERROR, "could not open GPIO value file");
219  return FAILURE;
220  }
221 
222  // set up polling struct
223  struct pollfd fds;
224  fds.fd = fd;
225  fds.events = POLLPRI;
226  int nfds = 1;
227  int ret;
228 
229  // dummy read (enables blocking polling)
230  char buf[2] = "";
231  read(fds.fd, &buf, 1);
232 
233  // wait on gpio change
234  ret = poll(&fds, nfds, timeout);
235  if (ret < 0) {
236  rl_log(ERROR, "GPIO poll failed");
237  return FAILURE;
238  }
239 
240  // wait for signal to settle
241  usleep(MIN_BUTTON_TIME);
242 
243  // read value
244  lseek(fds.fd, 0, SEEK_SET);
245  read(fds.fd, &buf, 1);
246 
247  close(fd);
248 
249  return atoi(buf);
250 }
enum edge rl_edge
int gpio_get_value(int num)
Definition: gpio.c:184
#define GPIO_PATH
Path to linux GPIO device files.
Definition: gpio.h:45
#define FAILURE
Definition: types.h:77
#define SUCCESS
Definition: types.h:74
int gpio_export(int num)
Definition: gpio.c:62
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:38
Interrupt on falling edge.
Definition: gpio.h:63
Interrupt on rising edge.
Definition: gpio.h:62
#define MAX_PATH_LENGTH
Maximum path length in characters.
Definition: types.h:101
int gpio_interrupt(int num, rl_edge edge)
Definition: gpio.c:116
int gpio_set_value(int num, int val)
Definition: gpio.c:155
int gpio_wait_interrupt(int num, int timeout)
Definition: gpio.c:211
GPIO write mode.
Definition: gpio.h:54
#define MIN_BUTTON_TIME
Minimal time a button needs to be pressed (in µs)
Definition: gpio.h:47
int gpio_unexport(int num)
Definition: gpio.c:38
Interrupt on both edges.
Definition: gpio.h:64
Error.
Definition: types.h:199
edge
Definition: gpio.h:60
No interrupt.
Definition: gpio.h:61
int gpio_dir(int num, rl_direction dir)
Definition: gpio.c:87
enum direction rl_direction