SimpleTimer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SimpleTimer.h
8  *
9  * @author: 13 August 2018 - mikee47 <mike@sillyhouse.net>
10  *
11  * This class wraps the OS timer functions, in a class called SimpleTimer.
12  *
13 */
14 
15 #pragma once
16 
17 #include "esp_systemapi.h"
18 #include "CallbackTimer.h"
19 #include <Platform/Clocks.h>
20 #include <driver/os_timer.h>
21 
32 class OsTimerApi : public CallbackTimerApi<OsTimerApi>
33 {
34 public:
35  using Clock = OsTimerClock;
36  using TickType = uint32_t;
37  using TimeType = uint32_t;
38 
39  static constexpr const char* typeName()
40  {
41  return "OsTimerApi";
42  }
43 
44  static constexpr TickType minTicks()
45  {
46  // Note: The minimum specified by the SDK is 100us
47  return 1;
48  }
49 
50  static constexpr TickType maxTicks()
51  {
52  return 0x7FFFFFFF;
53  }
54 
55  __forceinline bool isArmed() const
56  {
57  return os_timer_expire(&osTimer) != 0;
58  }
59 
60  TickType ticks() const
61  {
62  uint64_t expiry = os_timer_expire(&osTimer);
63  if(expiry == 0) {
64  return 0;
65  }
66 
67  int remain = expiry - Clock::ticks();
68  return (remain > 0) ? remain : 0;
69  }
70 
72  {
73  disarm();
74  os_timer_done(&osTimer);
75  }
76 
77  __forceinline void IRAM_ATTR setCallback(TimerCallback callback, void* arg)
78  {
79  os_timer_setfn(&osTimer, callback, arg);
80  }
81 
82  __forceinline void IRAM_ATTR setInterval(TickType interval)
83  {
84  this->interval = interval;
85  }
86 
87  __forceinline TickType IRAM_ATTR getInterval() const
88  {
89  return interval;
90  }
91 
92  __forceinline void IRAM_ATTR arm(bool repeating)
93  {
94  os_timer_arm_ticks(&osTimer, interval, repeating);
95  }
96 
97  __forceinline void IRAM_ATTR disarm()
98  {
99  if(isArmed()) {
100  os_timer_disarm(&osTimer);
101  }
102  }
103 
104 private:
105  os_timer_t osTimer = OS_TIMER_DEFAULT();
106  TickType interval = 0;
107 };
108 
114 
#define OS_TIMER_DEFAULT()
Definition: Esp32/Components/driver/include/driver/os_timer.h:21
void os_timer_setfn(os_timer_t *ptimer, os_timer_func_t *pfunction, void *parg)
void os_timer_done(os_timer_t *ptimer)
void os_timer_disarm(os_timer_t *ptimer)
Implements common system callback timer API.
Definition: SimpleTimer.h:33
void setCallback(TimerCallback callback, void *arg)
Definition: SimpleTimer.h:77
TickType getInterval() const
Definition: SimpleTimer.h:87
static constexpr const char * typeName()
Definition: SimpleTimer.h:39
void setInterval(TickType interval)
Definition: SimpleTimer.h:82
uint32_t TimeType
Definition: SimpleTimer.h:37
static constexpr TickType maxTicks()
Definition: SimpleTimer.h:50
void arm(bool repeating)
Definition: SimpleTimer.h:92
TickType ticks() const
Definition: SimpleTimer.h:60
bool isArmed() const
Definition: SimpleTimer.h:55
void disarm()
Definition: SimpleTimer.h:97
static constexpr TickType minTicks()
Definition: SimpleTimer.h:44
~OsTimerApi()
Definition: SimpleTimer.h:71
uint32_t TickType
Definition: SimpleTimer.h:36
OsTimerClock Clock
Definition: SimpleTimer.h:35
void(*)(void *arg) TimerCallback
Interrupt-compatible C callback function pointer.
Definition: CallbackTimer.h:23
#define os_timer_expire
Definition: Esp32/Components/driver/include/driver/os_timer.h:53
#define os_timer_arm_ticks
Definition: Esp32/Components/driver/include/driver/os_timer.h:52
Callback timer API class template.
Definition: CallbackTimer.h:30
This is the structure used by the Espressif timer API.
Definition: Rp2040/Components/driver/include/driver/os_timer.h:28