/* * Main */ int main(int argc, char **argv, char **envp) { struct pollfd fdset[2]; int nfds = 2; int gpioFd, timeout, rc; char *buf[maxBuff]; unsigned int gpio, led; bool toggleLED = true; int len; if (argc < 2) { printf("Usage: gpio-int <gpio-pin>\n\n"); printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); exit(-1); } gpio = atoi(argv[1]); led = atoi(argv[2]); GpioExport(gpio); GpioExport(led); GpioSetDir(gpio, 0); GpioSetDir(led, 1); GpioSetEdge(gpio, "rising"); gpioFd = GpioFdOpen(gpio); timeout = pollTimeout; while (1) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[1].fd = gpioFd; fdset[1].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0) { printf("\npoll() failed!\n"); return -1; } if (rc == 0) { printf("."); } if (fdset[1].revents & POLLPRI) { len = read(fdset[1].fd, buf, maxBuff); printf("\npoll() GPIO %d interrupt occurred\n", gpio); if (toggleLED) GpioSetValue(led,0); else GpioSetValue(led,1); toggleLED =! toggleLED; } if (fdset[0].revents & POLLIN) { (void)read(fdset[0].fd, buf, 1); printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); } fflush(stdout); } GpioFdClose(gpioFd); return 0; }
void setup() { // TODO: Add your code here GpioSetDir(led, OUTPUT); // Configure the pin for OUTPUT so you can turn on the LED. }