Skip to content

Commit fcefadf

Browse files
committed
Function name is changed from tick() to left(), optimization
1 parent 6e6f9bd commit fcefadf

File tree

1 file changed

+44
-63
lines changed

1 file changed

+44
-63
lines changed

src/arduino-timer.h

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include <limits.h>
4545

4646
#ifndef TIMER_MAX_TASKS
47-
#define TIMER_MAX_TASKS 0x10
47+
#define TIMER_MAX_TASKS 0x10
4848
#endif
4949

5050
#define _timer_foreach_task(T, task) \
@@ -57,42 +57,37 @@
5757
_timer_foreach_task(const struct task *, T)
5858

5959
template <
60-
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
60+
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
6161
unsigned long (*time_func)() = millis, /* time function for timer */
62-
typename T = void * /* handler argument type */
63-
>
62+
typename T = void * /* handler argument type */
63+
>
6464
class Timer {
65-
public:
66-
67-
typedef uintptr_t Task; /* public task handle */
65+
public:
66+
typedef uintptr_t Task; /* public task handle */
6867
typedef bool (*handler_t)(T opaque); /* task handler func signature */
6968

7069
/* Calls handler with opaque as argument in delay units of time */
7170
Task
72-
in(unsigned long delay, handler_t h, T opaque = T())
73-
{
71+
in(unsigned long delay, handler_t h, T opaque = T()) {
7472
return task_id(add_task(time_func(), delay, h, opaque));
7573
}
7674

7775
/* Calls handler with opaque as argument at time */
7876
Task
79-
at(unsigned long time, handler_t h, T opaque = T())
80-
{
77+
at(unsigned long time, handler_t h, T opaque = T()) {
8178
const unsigned long now = time_func();
8279
return task_id(add_task(now, time - now, h, opaque));
8380
}
8481

8582
/* Calls handler with opaque as argument every interval units of time */
8683
Task
87-
every(unsigned long interval, handler_t h, T opaque = T())
88-
{
84+
every(unsigned long interval, handler_t h, T opaque = T()) {
8985
return task_id(add_task(time_func(), interval, h, opaque, interval));
9086
}
9187

9288
/* Cancel the timer task */
9389
void
94-
cancel(Task &task)
95-
{
90+
cancel(Task &task) {
9691
if (!task) return;
9792

9893
timer_foreach_task(t) {
@@ -107,17 +102,15 @@ class Timer {
107102

108103
/* Cancel all timer tasks */
109104
void
110-
cancel()
111-
{
105+
cancel() {
112106
timer_foreach_task(t) {
113107
remove(t);
114108
}
115109
}
116110

117111
/* Left until the task ends */
118112
unsigned long
119-
ticks(const Task &task)
120-
{
113+
left(const Task &task) {
121114
auto lft = 0ul;
122115
if (!task)
123116
return lft;
@@ -127,10 +120,8 @@ class Timer {
127120
const unsigned long now = time_func();
128121
const unsigned long duration = now - t->start;
129122

130-
if (duration >= t->expires)
131-
break;
132-
133-
lft = t->expires - duration;
123+
if (duration < t->expires)
124+
lft = t->expires - duration;
134125
break;
135126
}
136127
}
@@ -140,15 +131,14 @@ class Timer {
140131

141132
/* Ticks the timer forward - call this function in loop() */
142133
unsigned long
143-
tick()
144-
{
134+
tick() {
145135
tick<void>();
146136
return ticks();
147137
}
148138

149-
template <typename R> void
150-
tick()
151-
{
139+
template <typename R>
140+
void
141+
tick() {
152142
timer_foreach_task(task) {
153143
if (task->handler) {
154144
const unsigned long t = time_func();
@@ -157,17 +147,18 @@ class Timer {
157147
if (duration >= task->expires) {
158148
task->repeat = task->handler(task->opaque) && task->repeat;
159149

160-
if (task->repeat) task->start = t;
161-
else remove(task);
150+
if (task->repeat)
151+
task->start = t;
152+
else
153+
remove(task);
162154
}
163155
}
164156
}
165157
}
166158

167159
/* Ticks until the next event */
168160
unsigned long
169-
ticks() const
170-
{
161+
ticks() const {
171162
unsigned long ticks = ULONG_MAX, elapsed;
172163
const unsigned long start = time_func();
173164

@@ -188,16 +179,17 @@ class Timer {
188179

189180
elapsed = time_func() - start;
190181

191-
if (elapsed >= ticks || ticks == ULONG_MAX) ticks = 0;
192-
else ticks -= elapsed;
182+
if (elapsed >= ticks || ticks == ULONG_MAX)
183+
ticks = 0;
184+
else
185+
ticks -= elapsed;
193186

194187
return ticks;
195188
}
196189

197190
/* Number of active tasks in the timer */
198191
size_t
199-
size() const
200-
{
192+
size() const {
201193
size_t s = 0;
202194

203195
timer_foreach_const_task(task) {
@@ -209,8 +201,7 @@ class Timer {
209201

210202
/* True if there are no active tasks */
211203
bool
212-
empty() const
213-
{
204+
empty() const {
214205
timer_foreach_const_task(task) {
215206
if (task->handler) return false;
216207
}
@@ -220,23 +211,20 @@ class Timer {
220211

221212
Timer() : ctr(0), tasks{} {}
222213

223-
private:
224-
214+
private:
225215
size_t ctr;
226216

227217
struct task {
228218
handler_t handler; /* task handler callback func */
229-
T opaque; /* argument given to the callback handler */
219+
T opaque; /* argument given to the callback handler */
230220
unsigned long start,
231-
expires; /* when the task expires */
221+
expires; /* when the task expires */
232222
size_t repeat, /* repeat task */
233-
id;
223+
id;
234224
} tasks[max_tasks];
235225

236-
inline
237-
void
238-
remove(struct task *task)
239-
{
226+
inline void
227+
remove(struct task *task) {
240228
task->handler = NULL;
241229
task->opaque = T();
242230
task->start = 0;
@@ -245,36 +233,30 @@ class Timer {
245233
task->id = 0;
246234
}
247235

248-
inline
249-
Task
250-
task_id(const struct task * const t)
251-
{
236+
inline Task
237+
task_id(const struct task *const t) {
252238
const Task id = reinterpret_cast<Task>(t);
253239

254240
return id ? id ^ t->id : id;
255241
}
256242

257-
inline
258-
struct task *
259-
next_task_slot()
260-
{
243+
inline struct task *
244+
next_task_slot() {
261245
timer_foreach_task(slot) {
262246
if (slot->handler == NULL) return slot;
263247
}
264248

265249
return NULL;
266250
}
267251

268-
inline
269-
struct task *
252+
inline struct task *
270253
add_task(unsigned long start, unsigned long expires,
271-
handler_t h, T opaque, bool repeat = 0)
272-
{
273-
struct task * const slot = next_task_slot();
254+
handler_t h, T opaque, bool repeat = 0) {
255+
struct task *const slot = next_task_slot();
274256

275257
if (!slot) return NULL;
276258

277-
if (++ctr == 0) ++ctr; // overflow
259+
if (++ctr == 0) ++ctr; // overflow
278260

279261
slot->id = ctr;
280262
slot->handler = h;
@@ -289,8 +271,7 @@ class Timer {
289271

290272
/* create a timer with the default settings */
291273
inline Timer<>
292-
timer_create_default()
293-
{
274+
timer_create_default() {
294275
return Timer<>();
295276
}
296277

0 commit comments

Comments
 (0)