コード例 #1
0
/*
 * 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;
}
コード例 #2
0
ファイル: lcdialog.c プロジェクト: rstephan/lcdialog
int ButtonGet(void)
{
	int ret;
	int fd1;
	int fd2;
	int fd3;
	long int start;
	long int end;
#if 0
	struct pollfd pollKey[3];
	int retPoll;
	char buffer[2];
#endif
	uint8_t val1;
	uint8_t val2;
	uint8_t val3;
	uint8_t val1_old = 0;
	uint8_t val2_old = 0;
	uint8_t val3_old = 0;
	int result = 0;
	
	ret = 0;
	ret += GpioFdOpen(PIN_KEY_1, &fd1);
	ret += GpioFdOpen(PIN_KEY_2, &fd2);
	ret += GpioFdOpen(PIN_KEY_3, &fd3);

	if (ret != 3) {
		return -1;
	}
#if 0
	bzero(pollKey, sizeof(pollKey));
	pollKey[0].fd = fd1;
	pollKey[0].events = POLLPRI;
	pollKey[1].fd = fd2;
	pollKey[1].events = POLLPRI;
	pollKey[2].fd = fd3;
	pollKey[2].events = POLLPRI;
	
	
	while (!ende) {
		retPoll = poll(pollKey, 3, 1000);
		if (retPoll < 0) {
			if (errno == EINTR) {
				continue;
			} else {
				printf("Error: %i\n", errno);
				break;
			}
		} else if (retPoll == 0) {
			// timeout
			//printf(".\n");
		} else {
			if (pollKey[0].revents & POLLPRI) {
				ret = read(fd1, buffer, 2);
				printf("Taste 1: %c\n", buffer[0]);
				usleep(DEBOUNCE * 1000);
				lseek(fd1, 0, SEEK_SET);
				result = '1';
				break;
			}
			if (pollKey[1].revents & POLLPRI) {
				ret = read(fd2, buffer, 2);
				printf("Taste 2: %c\n", buffer[0]);
				usleep(DEBOUNCE * 1000);
				lseek(fd2, 0, SEEK_SET);
				result = '2';
				break;
			}
			if (pollKey[2].revents & POLLPRI) {
				ret = read(fd3, buffer, 2);
				printf("Taste 3: %c\n", buffer[0]);
				usleep(DEBOUNCE * 1000);
				lseek(fd3, 0, SEEK_SET);
				result = '3';
				break;
			}
		}
	}
	GpioFdClose(fd1);
	GpioFdClose(fd2);
	GpioFdClose(fd3);
#endif

	start = GetTime();
	while (!gEnde) {
		if (gConfig.mTimeout) {
			end = GetTime();
			if (end - start >= gConfig.mTimeout) {
				result = ASCII_CAN;
				break;
			}
		}
		GpioGetValue(PIN_KEY_1, &val1);
		GpioGetValue(PIN_KEY_2, &val2);
		GpioGetValue(PIN_KEY_3, &val3);
		
		if (val1 != val1_old && val1 == 0) {
			usleep(DEBOUNCE * 1000);
			result = '1';
			break;
		}
		if (val2 != val2_old && val2 == 0) {
			usleep(DEBOUNCE * 1000);
			result = '2';
			break;
		}
		if (val3 != val3_old && val3 == 0) {
			usleep(DEBOUNCE * 1000);
			result = '3';
			break;
		}
		
		val1_old = val1;
		val2_old = val2;
		val3_old = val3;
		usleep(50 * 1000);
	}

	return result;
}