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 = Timer2Clock;
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 int(osTimer.timer_next) != -1;
58  }
59 
60  TickType ticks() const
61  {
62  if(!isArmed()) {
63  return 0;
64  }
65 
66  auto remain = int(osTimer.timer_expire - Clock::ticks());
67  return (remain > 0) ? remain : 0;
68  }
69 
71  {
72  disarm();
73  }
74 
75  __forceinline void IRAM_ATTR setCallback(TimerCallback callback, void* arg)
76  {
77  os_timer_setfn(&osTimer, callback, arg);
78  }
79 
80  __forceinline void IRAM_ATTR setInterval(TickType interval)
81  {
82  this->interval = interval;
83  }
84 
86  {
87  return interval;
88  }
89 
90  __forceinline void IRAM_ATTR arm(bool repeating)
91  {
92  os_timer_arm_ticks(&osTimer, interval, repeating);
93  }
94 
95  __forceinline void IRAM_ATTR disarm()
96  {
97  if(isArmed()) {
98  os_timer_disarm(&osTimer);
99  }
100  }
101 
102 private:
103  os_timer_t osTimer = {
104  reinterpret_cast<os_timer_t*>(-1), // Disarmed
105  };
106  TickType interval = 0;
107 };
108 
114 
#define __forceinline
Definition: sming_attr.h:13
void disarm()
Definition: SimpleTimer.h:95
void os_timer_arm_ticks(os_timer_t *ptimer, uint32_t ticks, bool repeat_flag)
Set a software timer using the Timer2 tick value.
void(* TimerCallback)(void *arg)
Interrupt-compatible C callback function pointer.
Definition: CallbackTimer.h:23
static constexpr TickType minTicks()
Definition: SimpleTimer.h:44
void setCallback(TimerCallback callback, void *arg)
Definition: SimpleTimer.h:75
Clock implementation for Hardware Timer 2.
Definition: Clocks.h:55
TickType ticks() const
Definition: SimpleTimer.h:60
Callback timer class template.
Definition: CallbackTimer.h:70
uint32_t TickType
Definition: SimpleTimer.h:36
void arm(bool repeating)
Definition: SimpleTimer.h:90
Implements common system callback timer API.
Definition: SimpleTimer.h:32
~OsTimerApi()
Definition: SimpleTimer.h:70
TickType getInterval() const
Definition: SimpleTimer.h:85
bool isArmed() const
Definition: SimpleTimer.h:55
Callback timer API class template.
Definition: CallbackTimer.h:30
uint32_t TimeType
Definition: SimpleTimer.h:37
static uint32_t ticks()
Definition: Clocks.h:61
static constexpr TickType maxTicks()
Definition: SimpleTimer.h:50
void setInterval(TickType interval)
Definition: SimpleTimer.h:80
static constexpr const char * typeName()
Definition: SimpleTimer.h:39