Skip to content

Commit 806d030

Browse files
committed
Merge branch 'mc/deprecate-timer.h'
* mc/deprecate-timer.h: extras: update include file examples: update include file README: update include file library: include arduino-timer.h src:timer: deprecate timer.h file with warning rename timer.h -> arduino-timer.h
2 parents 88cdd9b + 3dd1819 commit 806d030

File tree

10 files changed

+214
-211
lines changed

10 files changed

+214
-211
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Simple *non-blocking* timer library for calling functions **in / at / every** sp
66

77
Include the library and create a *Timer* instance.
88
```cpp
9-
#include <timer.h>
9+
#include <arduino-timer.h>
1010

1111
auto timer = timer_create_default();
1212
```
@@ -105,7 +105,7 @@ void cancel(Timer<>::Task &task);
105105

106106
[Check out the instructions](https://www.arduino.cc/en/Guide/Libraries) from Arduino.
107107

108-
**OR** copy **src/timer.h** into your project folder *(you won't get managed updates this way)*.
108+
**OR** copy **src/arduino-timer.h** into your project folder *(you won't get managed updates this way)*.
109109

110110
### Examples
111111

@@ -114,7 +114,7 @@ Found in the **examples/** folder.
114114
The simplest example, blinking an LED every second *(from examples/blink)*:
115115

116116
```cpp
117-
#include <timer.h>
117+
#include <arduino-timer.h>
118118

119119
auto timer = timer_create_default(); // create a timer with default settings
120120

examples/blink/blink.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*/
77

8-
#include <timer.h>
8+
#include <arduino-timer.h>
99

1010
auto timer = timer_create_default(); // create a timer with default settings
1111

examples/blink_micros/blink_micros.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*/
77

8-
#include <timer.h>
8+
#include <arduino-timer.h>
99

1010
Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution
1111

examples/blink_print/blink_print.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88

9-
#include <timer.h>
9+
#include <arduino-timer.h>
1010

1111
auto timer = timer_create_default(); // create a timer with default settings
1212

examples/full/full.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*/
1212

13-
#include <timer.h>
13+
#include <arduino-timer.h>
1414

1515
auto timer = timer_create_default(); // create a timer with default settings
1616
Timer<> default_timer; // save as above

extras/tests/rollover-generic/rollover-generic.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Test timer rollover handling
33
*/
44

5-
#include <timer.h>
5+
#include <arduino-timer.h>
66

77
unsigned long wrapping_millis();
88

extras/tests/rollover/rollover.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
#include <util/atomic.h>
6-
#include <timer.h>
6+
#include <arduino-timer.h>
77

88
auto timer = timer_create_default();
99

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ paragraph=Simple non-blocking timer library for calling functions in / at / ever
88
category=Timing
99
url=https://github.com/contrem/arduino-timer
1010
architectures=*
11-
includes=timer.h
11+
includes=arduino-timer.h

src/arduino-timer.h

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/**
2+
arduino-timer - library for delaying function calls
3+
4+
Copyright (c) 2018, Michael Contreras
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are
9+
met:
10+
11+
1. Redistributions of source code must retain the above copyright
12+
notice, this list of conditions and the following disclaimer.
13+
14+
2. Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
18+
3. Neither the name of the copyright holder nor the names of its
19+
contributors may be used to endorse or promote products derived from
20+
this software without specific prior written permission.
21+
22+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23+
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
#ifndef _CM_ARDUINO_TIMER_H__
36+
#define _CM_ARDUINO_TIMER_H__
37+
38+
#if defined(ARDUINO) && ARDUINO >= 100
39+
#include <Arduino.h>
40+
#else
41+
#include <WProgram.h>
42+
#endif
43+
44+
#ifndef TIMER_MAX_TASKS
45+
#define TIMER_MAX_TASKS 0x10
46+
#endif
47+
48+
template <
49+
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
50+
unsigned long (*time_func)() = millis, /* time function for timer */
51+
typename T = void * /* handler argument type */
52+
>
53+
class Timer {
54+
public:
55+
56+
typedef uintptr_t Task; /* public task handle */
57+
typedef bool (*handler_t)(T opaque); /* task handler func signature */
58+
59+
/* Calls handler with opaque as argument in delay units of time */
60+
Task
61+
in(unsigned long delay, handler_t h, T opaque = T())
62+
{
63+
return task_id(add_task(time_func(), delay, h, opaque));
64+
}
65+
66+
/* Calls handler with opaque as argument at time */
67+
Task
68+
at(unsigned long time, handler_t h, T opaque = T())
69+
{
70+
const unsigned long now = time_func();
71+
return task_id(add_task(now, time - now, h, opaque));
72+
}
73+
74+
/* Calls handler with opaque as argument every interval units of time */
75+
Task
76+
every(unsigned long interval, handler_t h, T opaque = T())
77+
{
78+
return task_id(add_task(time_func(), interval, h, opaque, interval));
79+
}
80+
81+
/* Cancel the timer task */
82+
void
83+
cancel(Task &task)
84+
{
85+
if (!task) return;
86+
87+
for (size_t i = 0; i < max_tasks; ++i) {
88+
struct task * const t = &tasks[i];
89+
90+
if (t->handler && (t->id ^ task) == (uintptr_t)t) {
91+
remove(t);
92+
break;
93+
}
94+
}
95+
96+
task = (Task)NULL;
97+
}
98+
99+
/* Ticks the timer forward - call this function in loop() */
100+
unsigned long
101+
tick()
102+
{
103+
unsigned long ticks = (unsigned long)-1;
104+
105+
for (size_t i = 0; i < max_tasks; ++i) {
106+
struct task * const task = &tasks[i];
107+
108+
if (task->handler) {
109+
const unsigned long t = time_func();
110+
const unsigned long duration = t - task->start;
111+
112+
if (duration >= task->expires) {
113+
task->repeat = task->handler(task->opaque) && task->repeat;
114+
115+
if (task->repeat) task->start = t;
116+
else remove(task);
117+
} else {
118+
const unsigned long remaining = task->expires - duration;
119+
ticks = remaining < ticks ? remaining : ticks;
120+
}
121+
}
122+
}
123+
124+
return ticks == (unsigned long)-1 ? 0 : ticks;
125+
}
126+
127+
private:
128+
129+
size_t ctr;
130+
131+
struct task {
132+
handler_t handler; /* task handler callback func */
133+
T opaque; /* argument given to the callback handler */
134+
unsigned long start,
135+
expires; /* when the task expires */
136+
size_t repeat, /* repeat task */
137+
id;
138+
} tasks[max_tasks];
139+
140+
inline
141+
void
142+
remove(struct task *task)
143+
{
144+
task->handler = NULL;
145+
task->opaque = T();
146+
task->start = 0;
147+
task->expires = 0;
148+
task->repeat = 0;
149+
task->id = 0;
150+
}
151+
152+
inline
153+
Task
154+
task_id(const struct task * const t)
155+
{
156+
const Task id = (Task)t;
157+
158+
return id ? id ^ t->id : id;
159+
}
160+
161+
inline
162+
struct task *
163+
next_task_slot()
164+
{
165+
for (size_t i = 0; i < max_tasks; ++i) {
166+
struct task * const slot = &tasks[i];
167+
if (slot->handler == NULL) return slot;
168+
}
169+
170+
return NULL;
171+
}
172+
173+
inline
174+
struct task *
175+
add_task(unsigned long start, unsigned long expires,
176+
handler_t h, T opaque, bool repeat = 0)
177+
{
178+
struct task * const slot = next_task_slot();
179+
180+
if (!slot) return NULL;
181+
182+
if (++ctr == 0) ++ctr; // overflow
183+
184+
slot->id = ctr;
185+
slot->handler = h;
186+
slot->opaque = opaque;
187+
slot->start = start;
188+
slot->expires = expires;
189+
slot->repeat = repeat;
190+
191+
return slot;
192+
}
193+
};
194+
195+
/* create a timer with the default settings */
196+
inline Timer<>
197+
timer_create_default()
198+
{
199+
return Timer<>();
200+
}
201+
202+
#endif

0 commit comments

Comments
 (0)