2014-10-08 2 views
0

나는 비글 뼈가 있고 내 프로그램에서 gpio 핀을 읽고 싶다. 파일은 주어진 순간에 1 또는 0을 포함합니다. 내 C 프로그램에서 나는 그 핀이 로우 (0) 일 때마다 CPU 호그가되지 않게하고, 하이 (1) 일 때 코드를 실행하는 sleep 함수로 영원히 돌아가는 while 루프를 가지고있다. 나는 이것이 자원의 매우 낭비라고 느낀다. 이 파일이 1 일 때 코드를 실행할 수있는 방법이 있습니까? 나는 비글 뼈가 배터리로 작동 할 때 폴링을 좋아하지 않습니다.폴링하지 않고 파일의 변경 사항을 찾는 방법은 무엇입니까?

답변

0

예를 들어 libev을 사용하여 알림 메커니즘을 사용하십시오.

기본적인 아이디어는 데이터를 사용할 수 있다면 콜백이 호출된다는 것입니다.

이것이 작동하지 않으면 inotify와 같이 다른 API를 사용해야 할 수도 있습니다. libev 워드 프로세서의

예 의례 :

// a single header file is required 
#include <ev.h> 

#include <stdio.h> // for puts 

// every watcher type has its own typedef'd struct 
// with the name ev_TYPE 
ev_io stdin_watcher; 
ev_timer timeout_watcher; 

// all watcher callbacks have a similar signature 
// this callback is called when data is readable on stdin 
static void 
stdin_cb (EV_P_ ev_io *w, int revents) 
{ 
puts ("stdin ready"); 
// for one-shot events, one must manually stop the watcher 
// with its corresponding stop function. 
ev_io_stop (EV_A_ w); 

// this causes all nested ev_run's to stop iterating 
ev_break (EV_A_ EVBREAK_ALL); 
} 

// another callback, this time for a time-out 
static void 
timeout_cb (EV_P_ ev_timer *w, int revents) 
{ 
puts ("timeout"); 
// this causes the innermost ev_run to stop iterating 
ev_break (EV_A_ EVBREAK_ONE); 
} 

int 
main (void) 
{ 
// use the default event loop unless you have special needs 
struct ev_loop *loop = EV_DEFAULT; 

// initialise an io watcher, then start it 
// this one will watch for stdin to become readable 
ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); 
ev_io_start (loop, &stdin_watcher); 

// initialise a timer watcher, then start it 
// simple non-repeating 5.5 second timeout 
ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); 
ev_timer_start (loop, &timeout_watcher); 

// now wait for events to arrive 
ev_run (loop, 0); 

// break was called, so exit 
return 0; 
}