Skip to content

Commit a9b8cad

Browse files
committed
Merge branch 'mc/func-return-value-interp' into next
* mc/func-return-value-interp: src/timer: more unique include guard src/timer: clean up tick loop src/timer: consistent use of const arduino-timer: handler return value interpretation Closes: contrem#1 Signed-off-by: Michael Contreras <[email protected]>
2 parents ff363ba + 0ca867b commit a9b8cad

File tree

8 files changed

+22
-27
lines changed

8 files changed

+22
-27
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void loop() {
2727
Make a function to call when the *Timer* expires
2828
```cpp
2929
bool function_to_call(void *argument /* optional argument given to in/at/every */) {
30-
return false; // to keep the timer active - true removes it (for timer.every())
30+
return true; // to repeat the action - false to stop
3131
}
3232
```
3333
@@ -104,7 +104,7 @@ auto timer = timer_create_default(); // create a timer with default settings
104104

105105
bool toggle_led(void *) {
106106
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
107-
return false; // stop repeating? false
107+
return true; // keep timer active? true
108108
}
109109

110110
void setup() {

examples/blink/blink.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ auto timer = timer_create_default(); // create a timer with default settings
1111

1212
bool toggle_led(void *) {
1313
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
14-
return false; // stop repeating? false
14+
return true; // repeat? true
1515
}
1616

1717
void setup() {

examples/blink_micros/blink_micros.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution
1111

1212
bool toggle_led(void *) {
1313
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
14-
return false; // stop repeating? false
14+
return true; // repeat? true
1515
}
1616

1717
void setup() {

examples/blink_print/blink_print.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ auto timer = timer_create_default(); // create a timer with default settings
1212

1313
bool toggle_led(void *) {
1414
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
15-
return false; // stop repeating? false
15+
return true; // repeat? true
1616
}
1717

1818
bool print_message(void *) {
1919
Serial.print("print_message: Called at: ");
2020
Serial.println(millis());
21-
return false; // stop repeating? false
21+
return true; // repeat? true
2222
}
2323

2424
void setup() {

examples/full/full.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ Timer<1, micros> u_timer;
1919

2020
bool toggle_led(void *) {
2121
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
22-
return false; // stop repeating? false
22+
return true; // repeat? true
2323
}
2424

2525
bool print_message(void *msg) {
2626
const char *m = (const char *)msg;
2727
Serial.print("print_message: ");
2828
Serial.println(m);
29-
return false; // stop repeating? false
29+
return true; // repeat? true
3030
}
3131

3232
size_t repeat_count = 1;
@@ -38,7 +38,7 @@ bool repeat_x_times(void *opaque) {
3838
Serial.print("/");
3939
Serial.println(limit);
4040

41-
return ++repeat_count > limit; // remove this task after limit reached
41+
return ++repeat_count <= limit; // remove this task after limit reached
4242
}
4343

4444
void setup() {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ void setup() {
2323
pinMode(LED_BUILTIN, OUTPUT);
2424
_timer.every(1, [](void *) -> bool {
2525
++_millis; // increase _millis every millisecond
26-
return false;
26+
return true;
2727
});
2828

2929
// should blink the led every second, regardless of wrapping
3030
timer.every(1000, [](void *) -> bool {
3131
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
32-
return false;
32+
return true;
3333
});
3434
}
3535

extras/tests/rollover/rollover.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void setup() {
2020
pinMode(LED_BUILTIN, OUTPUT);
2121
timer.every(1000, [](void *) -> bool {
2222
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
23-
return false;
23+
return true;
2424
});
2525
}
2626

src/timer.h

+10-15
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
*/
3434

35-
#ifndef _TIMER_H__
36-
#define _TIMER_H__
35+
#ifndef _CM_ARDUINO_TIMER_H__
36+
#define _CM_ARDUINO_TIMER_H__
3737

3838
#if defined(ARDUINO) && ARDUINO >= 100
3939
#include <Arduino.h>
@@ -89,19 +89,14 @@ class Timer {
8989
tick(unsigned long t)
9090
{
9191
for (size_t i = 0; i < max_tasks; ++i) {
92-
struct task *task = &tasks[i];
93-
const handler_t handler = task->handler;
94-
void *opaque = task->opaque;
95-
const unsigned long start = task->start,
96-
expires = task->expires;
97-
const unsigned long duration = t - start;
92+
struct task * const task = &tasks[i];
93+
const unsigned long duration = t - task->start;
9894

99-
if (handler && duration >= expires) {
95+
if (task->handler && duration >= task->expires) {
96+
task->repeat = task->handler(task->opaque) && task->repeat;
10097

101-
if (!task->repeat) remove(task);
102-
else task->start = t;
103-
104-
if (handler(opaque)) remove(task);
98+
if (task->repeat) task->start = t;
99+
else remove(task);
105100
}
106101
}
107102
}
@@ -132,7 +127,7 @@ class Timer {
132127
next_task_slot()
133128
{
134129
for (size_t i = 0; i < max_tasks; ++i) {
135-
struct task *slot = &tasks[i];
130+
struct task * const slot = &tasks[i];
136131
if (slot->handler == NULL) return slot;
137132
}
138133

@@ -144,7 +139,7 @@ class Timer {
144139
add_task(unsigned long start, unsigned long expires,
145140
handler_t h, void *opaque, bool repeat = 0)
146141
{
147-
struct task *slot = next_task_slot();
142+
struct task * const slot = next_task_slot();
148143

149144
if (!slot) return NULL;
150145

0 commit comments

Comments
 (0)