The code below uses inotify to watch an input pin and trigger when the "value" of the pin changes. Copy the code to inotify.c then:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/inotify.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))
int count = 0;
void *bgTask(void* argv)
{
int inotifyFd, wd, j;
char buf[BUF_LEN] __attribute__ ((aligned(8)));
ssize_t numRead;
char *p;
struct inotify_event *event;
inotifyFd = inotify_init(); /* Create inotify instance */
if (inotifyFd == -1) {
printf("Unable to initialize inotify\n");
return 0;
}
wd = inotify_add_watch(inotifyFd, ((char**)argv)[1], IN_ALL_EVENTS);
if (wd == -1) {
printf("bgTask failed to add watch on file %s\n", ((char**)argv)[1]);
return 0;
}
printf("Watching %s using wd %d\n", ((char**)argv)[1], wd);
while (1) { /* Read events forever */
int n = read(inotifyFd, buf, BUF_LEN);
if (n <= 0) {
printf("read() from inotify fd returned 0!");
return 0;
}
count++;
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2 || strcmp(argv[1], "--help") == 0) {
printf("%s filename\n", argv[0]);
return -1;
}
pthread_t thread;
pthread_create(&thread, NULL, bgTask, (void*)argv);
int beginCnt = count;
while (1) {
sleep(1);
int endCnt = count;
int diff = endCnt - beginCnt;
beginCnt = count;
printf("cnt = %d\n", diff);
}
return 0;
}