Pico-Arduino
PicoTimer.h
1 #pragma once
2 
3 #include "pico/stdlib.h"
4 
5 namespace pico_arduino {
6 
7 typedef int64_t(* alarm_callback_t )(alarm_id_t id, void *user_data);
8 typedef bool(* repeating_timer_callback_t )(repeating_timer_t *rt);
9 
10 enum TimeUnit {MS,US};
11 
18 class TimerAlarm {
19  public:
20  TimerAlarm(){
21  alarm_pool_init_default();
22  ap = alarm_pool_get_default();
23  }
24 
25  ~TimerAlarm(){
26  stop();
27  }
28 
30  void start(alarm_callback_t callback, uint64_t time, TimeUnit unit = MS, void *user_data=nullptr, bool fire_if_past=true){
31  switch(unit){
32  case MS:
33  // milliseconds 1/1000 sec
34  alarm_id = alarm_pool_add_alarm_in_ms(ap, time, callback, user_data, fire_if_past);
35  break;
36  case US:
37  // microseconds 1/1000000 (10^6) sec
38  alarm_id = alarm_pool_add_alarm_in_us(ap, time, callback, user_data, fire_if_past);
39  break;
40  }
41  }
42 
44  bool stop(){
45  return alarm_pool_cancel_alarm(ap, alarm_id);
46  }
47 
48  protected:
49  alarm_id_t alarm_id=-1;
50  alarm_pool_t *ap;
51 
52 };
53 
59  public:
61  alarm_pool_init_default();
62  ap = alarm_pool_get_default();
63  }
64 
66  stop();
67  }
68 
70  bool start(repeating_timer_callback_t callback, uint64_t time, TimeUnit unit = MS, void *user_data=nullptr){
71  bool result = false;
72  switch(unit){
73  case MS:
74  result = alarm_pool_add_repeating_timer_ms(ap, time, callback, user_data, &timer);
75  break;
76  case US:
77  result = alarm_pool_add_repeating_timer_us(ap, time, callback, user_data, &timer);
78  break;
79  }
80  return result;
81  }
82 
84  bool stop(){
85  return cancel_repeating_timer(&timer);
86  }
87 
88  protected:
89  alarm_pool_t *ap;
90  repeating_timer_t timer;
91 };
92 
93 }
Alarm functions for scheduling future execution.
Definition: PicoTimer.h:18
void start(alarm_callback_t callback, uint64_t time, TimeUnit unit=MS, void *user_data=nullptr, bool fire_if_past=true)
starts the execution of the callback method after the indicated time
Definition: PicoTimer.h:30
bool stop()
stops the execution
Definition: PicoTimer.h:44
Repeating Timer functions for simple scheduling of repeated execution.
Definition: PicoTimer.h:58
bool stop()
stops the execution
Definition: PicoTimer.h:84
bool start(repeating_timer_callback_t callback, uint64_t time, TimeUnit unit=MS, void *user_data=nullptr)
starts the repeated exection of the callback methods in the indicate period
Definition: PicoTimer.h:70
Pico Arduino Framework.
Definition: Arduino.cpp:26