From 3850058aa07df5bb542822eac24d50c961fdacec Mon Sep 17 00:00:00 2001 From: root Date: Sun, 21 Jun 2015 17:16:54 +0100 Subject: Initial version --- rotor.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 rotor.c (limited to 'rotor.c') diff --git a/rotor.c b/rotor.c new file mode 100644 index 0000000..f6121f4 --- /dev/null +++ b/rotor.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define MAX_BUF 64 + +struct timespec diff(struct timespec start, struct timespec end) +{ + struct timespec temp; + if ((end.tv_nsec-start.tv_nsec)<0) { + temp.tv_sec = end.tv_sec-start.tv_sec-1; + temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; + } else { + temp.tv_sec = end.tv_sec-start.tv_sec; + temp.tv_nsec = end.tv_nsec-start.tv_nsec; + } + return temp; +} + +int main( int argc, char** argv ) +{ + struct pollfd fdset[2]; + + int nfds = 2; + int gpio_fd, timeout, rc; + char buf[MAX_BUF]; + int len; + int number=0; + struct timespec now_ts, then_ts, diff_ts; + bool firstInt; // the next interrupt is the first of the sequence + + + gpio_fd = open( "/sys/class/gpio/gpio15_pb2/value", O_RDONLY | O_NONBLOCK ); + timeout = 500; + + + clock_gettime(CLOCK_MONOTONIC, &then_ts); + + firstInt=true; + + while (1) { + + memset((void*)fdset, 0, sizeof(fdset)); + memset((void*)buf,0,sizeof(buf)); + + fdset[0].fd = 0; + fdset[0].events = POLLIN; + + fdset[1].fd = gpio_fd; + fdset[1].events = POLLPRI; + + rc = poll(fdset, nfds, timeout); + + if (rc < 0) { + fprintf(stderr,"\npoll() failed!\n"); + return -1; + } + + // scaduto il timeout senza interrupt + if (rc == 0) { + // printf("."); + if (number > 0) { + printf("%d\n\n", number % 10); + number=0; + } + } + + if (fdset[1].revents & POLLPRI) { + clock_gettime(CLOCK_MONOTONIC, &now_ts); + // printf("%d %d - ", now_ts.tv_sec, now_ts.tv_nsec); + diff_ts = diff(then_ts, now_ts); + then_ts = now_ts; + lseek(fdset[1].fd,0,SEEK_SET); + len = read(fdset[1].fd, buf, MAX_BUF); +// printf("\npoll() GPIO %d interrupt occurred\n", 34); + strtok(buf,"\n"); + + int sec=diff_ts.tv_sec; + float msec=diff_ts.tv_nsec/1000000.0; + + + printf("value [%s] sec %d msec %f\n",buf,sec,msec); + + if (firstInt || msec > 90) number++; + firstInt=false; + } + + if (fdset[0].revents & POLLIN) { + (void)read(fdset[0].fd, buf, 1); + fprintf(stderr, "\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); + } + + fflush(stdout); + } + + +} -- cgit v1.2.3