Linux C игнорирует событие в течение x секунд - PullRequest
0 голосов
/ 03 мая 2018

У меня есть следующий код Arduino:

uint32_t hold_time=600000;
uint32_t curr_time;
uint32_t last_event;
bool on_hold=false;


beginning of main loop

 curr_time = millis();

 if (on_hold && curr_time - last_event >= hold_time) {        
        on_hold = false;
    }

...

if (!on_hold)
  {
    run the function();
    on_hold = true; // Ignore this function for 1 minute
  }

Это в основном будет выполнять основной цикл много раз, но run_the_function (); только когда он разблокирован, поэтому в этом примере раз в минуту. Я хотел бы добиться того же в стандартной POSIX C, которая также работает на BSD.

1 Ответ

0 голосов
/ 03 мая 2018

Так как вы попросили POSIX, я дам вам POSIX. Это пример кода, который может запускать таймер без использования pthreads, но только через таймеры, предоставляемые ОС. Он запускает определенную функцию каждые 2 секунды. Вы можете настроить его так, чтобы он запускался каждые 60 секунд, если хотите. Я тщательно прокомментировал код и надеюсь, что он достаточно прост для понимания:

#include <stdlib.h> // For declaration of exit
#include <stdio.h> // For printf function
#include <signal.h> // Will be used for the signal callbacks
#include <time.h> // Timer and current time stuff

#define TIMER_SECONDS 2 // To test more rapidly I will wait 
                        // only for 2 seconds instead of a minute...

int counter = 0; // Whe want to call the timer a limited number 
                 // of time for this example

// BEFORE READING THIS, READ THE MAIN:
// This function is your "run_the_function" callback. As for now, 
// it has no arguments and returns nothing. It asks to the system the current
// time and prints it, just to check if the timer works. It uses **printf**
// and this should be avoided in signal handlers!
static void run_the_function() {
  time_t rawtime;                    // This is the current time variable
  struct tm * timeinfo;              // This is a strut that contains the time 
                                     // broken down to its components
  time( &rawtime );                  // To get from the system the current time
  timeinfo = localtime ( &rawtime ); // For splitting the time in its components
  printf("Timer CALLED %d times :: %s", ++counter, asctime(timeinfo));
}

// BEFORE READING THIS, READ THE MAIN
// This is the signal handler, a function that is called when the timer
// signals that has finished to count
static void timer_callback(int sig,  siginfo_t *si, void *uc) {
  run_the_function();
}

int main() {
  timer_t timer_id;              // An unique identifier for the timer that you are creating
  struct itimerspec intervals;   // Specify the intervals for the timer that we are creating
  struct sigevent timer_event;   // The structure that handles the event generated by the timer
  struct sigaction timer_action; // The action for the timer event

  // First you need to implement the action to do when the timer reaches zero,
  // then you need to say that you want an event for a timer that reaches zero,
  // and only at the end you set the timer.
  // The function "sigaction" connects your timer event to the timer signal SIGRTMIN.
  // The timer_event.sigev_signo instructs to create an EVENT for the signal SIGRTMIN, and
  // for that event you prepared a custom action.
  // The timer sends the SIGRTMIN signal every time it reaches zero, and when you
  // create it, you connect it to the timer_event.

  // Now we define what is the action to perform
  timer_action.sa_flags = SA_SIGINFO;               // The action to perform is to run a callback
  timer_action.sa_sigaction = timer_callback;       // The callback is "timer_callback"
  sigemptyset(&timer_action.sa_mask);               // And we are initializing the event structure
  if (sigaction(SIGRTMIN, &timer_action, NULL) < 0) // We are binding this action 
    exit(1);                                        // to a timer event (SIGRTMIN)

  timer_event.sigev_notify = SIGEV_SIGNAL; // Instruct the event that it is related to
                                           // a signal. 
  timer_event.sigev_signo = SIGRTMIN;      // Instruct the event that the signal to track is SIGRTMIN

  // At this point we are ready to create the timer, that uses the REAL TIME CLOCK of your
  // system. When it reaches zero it raise a timer_event, and it also sets the id of the
  // created timer.
  if (timer_create(CLOCK_REALTIME, &timer_event, &timer_id) < 0)
    exit(1);

  // We now need to define the times for the timer. Intervals is composed by
  // two structure: it_value, that contains the current time (or the starting time
  // for the first run of your timer) and it_intervals, the time at which it will be
  // reset after completing one lap. If you set it_interval to zero, the timer runs only
  // one time. If you set it_value to zero, the timer does not run.
  intervals.it_value.tv_sec = TIMER_SECONDS;
  intervals.it_value.tv_nsec = 0;
  intervals.it_interval.tv_sec = TIMER_SECONDS;
  intervals.it_interval.tv_nsec = 0;

  // Let's setup the time and the interval of the timer, so that it starts...
  if (timer_settime(timer_id, 0, &intervals, NULL) < 0)
    exit(1);

  // And now we have only to wait for the timer... Easy, isn't it?
  printf("Let's go!\n");
  while(counter < 5) { /* Do your stuff here*/ };

  return 0;
}

Вам необходимо скомпилировать его:

gcc test.c -lrt -o test

и запустите его с:

./test

Let's go!
Timer CALLED 1 times :: Thu May  3 15:48:29 2018
Timer CALLED 2 times :: Thu May  3 15:48:31 2018
Timer CALLED 3 times :: Thu May  3 15:48:33 2018
Timer CALLED 4 times :: Thu May  3 15:48:35 2018
Timer CALLED 5 times :: Thu May  3 15:48:37 2018
...