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