41 static pthread_t isr_handler_thread;
42 static int isr_handler_flag;
66 gpio_edge (
int pin,
char *edge)
71 sprintf(filename,
"/sys/class/gpio/gpio%d/edge", pin);
72 file = fopen(filename,
"w");
75 debug(
"[%s] Can't open file (edge): %s\n", __func__, filename);
79 fwrite(edge,
sizeof (
char), strlen (edge) + 1, file);
99 sprintf(filename,
"/sys/class/gpio/gpio%d/value", pin);
100 file = open(filename, O_RDWR | O_NONBLOCK);
103 debug(
"[%s] Can't open file (value): %s\n", __func__, filename);
128 char filename[35], pinStr[2];
132 file = fopen(
"/sys/class/gpio/export",
"w");
135 debug(
"[%s] Can't open file (export)\n", __func__);
139 sprintf(pinStr,
"%d", pin);
141 fwrite(pinStr,
sizeof (
char), strlen (pinStr), file);
145 sprintf(filename,
"/sys/class/gpio/gpio%d/direction", pin);
146 file = fopen(filename,
"w");
149 debug(
"[%s] Can't open file (direction)\n", __func__);
156 fwrite(
"out",
sizeof (
char), 3, file);
160 fwrite(
"in",
sizeof (
char), 2, file);
164 debug(
"[%s] Can't set pin direction.\n", __func__);
182 isr_handler (
void *isr)
184 struct pollfd fdset[2];
185 int nfds = 2, gpio_fd, rc;
190 if ( isr_handler_flag )
192 printf(
"isr_handler running\n");
196 gpio_fd = gpio_valfd((
int) i.pin);
201 memset((
void *) fdset, 0,
sizeof (fdset));
203 fdset[0].fd = STDIN_FILENO;
204 fdset[0].events = POLLIN;
206 fdset[1].fd = gpio_fd;
207 fdset[1].events = POLLPRI;
209 rc = poll(fdset, nfds, 1000);
213 debug(
"\npoll() failed!\n");
219 debug(
"poll() timeout.\n");
220 if ( isr_handler_flag == 0 )
222 debug(
"exiting isr_handler (timeout)");
227 if ( fdset[1].revents & POLLPRI )
230 if ( -1 == read(fdset[1].fd, buf, 64) )
232 debug(
"read failed for interrupt");
239 if ( fdset[0].revents & POLLIN )
241 if ( -1 == read(fdset[0].fd, buf, 1) )
243 debug(
"read failed for stdin read");
247 printf(
"\npoll() stdin read 0x%2.2X\n", (
unsigned int) buf[0]);
255 debug(
"exiting isr_handler (flag)");
283 gpio_edge(pin, mode);
287 isr_handler_flag = 1;
288 pthread_create(&isr_handler_thread, NULL, isr_handler, (
void *) i);
289 pthread_tryjoin_np(isr_handler_thread, NULL);
307 isr_handler_flag = 0;
327 file = gpio_valfd (pin);
331 if ( write(file,
"0", (
sizeof(
char) * 1)) == -1 )
333 debug(
"[%s] Can't write to GPIO pin", __func__);
339 if ( write(file,
"1", (
sizeof(
char) * 1)) == -1 )
341 debug(
"[%s] Can't write to GPIO pin", __func__);
347 debug(
"[%s] Wrong value for the GPIO pin", __func__);
372 file = gpio_valfd (pin);
374 if (read (file, &valStr, 1) == 1)
377 debug (
"[%s] valStr: %s, val: %d\n", __func__, valStr, val);
381 debug (
"[%s] Can't read pin value", __func__);
403 file = fopen(
"/sys/class/gpio/unexport",
"w");
406 debug(
"[%s] Can't open file (unexport)\n", __func__);
410 sprintf(pinStr,
"%d", pin);
411 fwrite(pinStr,
sizeof (
char), strlen (pinStr), file);