void ButtonInit(void) { GpioExport(PIN_KEY_1); GpioExport(PIN_KEY_2); GpioExport(PIN_KEY_3); GpioSetDirection(PIN_KEY_1, 0); GpioSetDirection(PIN_KEY_2, 0); GpioSetDirection(PIN_KEY_3, 0); GpioSetEdge(PIN_KEY_1, edge_falling); GpioSetEdge(PIN_KEY_2, edge_falling); GpioSetEdge(PIN_KEY_3, edge_falling); #ifdef RPI_PULLUP GpioRpiSetup(); GpioRpiSetPullUpDown(PIN_KEY_1, PUD_UP); GpioRpiSetPullUpDown(PIN_KEY_2, PUD_UP); GpioRpiSetPullUpDown(PIN_KEY_3, PUD_UP); #endif #ifdef BPI_PULLUP GpioSetPullResistor(PIN_KEY_1, pull_up); GpioSetPullResistor(PIN_KEY_2, pull_up); GpioSetPullResistor(PIN_KEY_3, pull_up); #endif }
/* * 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; }