Skip to content

Commit 29af7d3

Browse files
Format code with WebKit coding-style
1 parent 3d21acf commit 29af7d3

File tree

8 files changed

+137
-117
lines changed

8 files changed

+137
-117
lines changed

Test_6_delays/Test_6_delays.ino

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1+
#include "avdweb_VirtualDelay.h"
12
#include <Arduino.h>
23
#include <Streaming.h>
3-
#include "avdweb_VirtualDelay.h"
44

55
void setup() { Serial.begin(9600); }
66

7-
void loop() {
8-
static VirtualDelay delay1, delay2, delay3, delay4, delay5, delay6;
9-
DO_ONCE(Serial << "\nDO_ONCE 1";
10-
delay1.start(200); // start sequence delay1 delay2 delay3
11-
delay4.start(550); // start one-shot delay4
12-
delay5.start(1250); // start one-shot delay5
13-
)
14-
if (delay4.elapsed()) Serial << "\nONE-SHOT 550ms " << millis();
15-
if (delay5.elapsed()) Serial << "\nONE-SHOT 1250ms " << millis();
7+
void loop()
8+
{
9+
static VirtualDelay delay1, delay2, delay3, delay4, delay5, delay6;
10+
DO_ONCE(Serial << "\nDO_ONCE 1";
11+
delay1.start(200); // start sequence delay1 delay2 delay3
12+
delay4.start(550); // start one-shot delay4
13+
delay5.start(1250); // start one-shot delay5
14+
)
15+
if (delay4.elapsed())
16+
Serial << "\nONE-SHOT 550ms " << millis();
17+
if (delay5.elapsed())
18+
Serial << "\nONE-SHOT 1250ms " << millis();
1619

17-
if (millis() > 2250)
18-
DO_ONCE(Serial << "\nDO_ONCE 2 2250ms "
19-
<< millis()) // test a second DO_ONCE
20+
if (millis() > 2250)
21+
DO_ONCE(Serial << "\nDO_ONCE 2 2250ms "
22+
<< millis()) // test a second DO_ONCE
2023

21-
delay6.start(750);
22-
if (delay6.elapsed()) Serial << "\n Repeat delay6 750ms " << millis();
24+
delay6.start(750);
25+
if (delay6.elapsed())
26+
Serial << "\n Repeat delay6 750ms " << millis();
2327

24-
if (delay1.elapsed()) // sequence with deadlock
25-
{
26-
Serial << "\nsequence delay1 200ms " << millis();
27-
delay2.start(100);
28-
}
29-
if (delay2.elapsed()) {
30-
Serial << "\nsequence delay2 100ms " << millis();
31-
delay3.start(400);
32-
}
33-
if (delay3.elapsed()) {
34-
Serial << "\nsequence delay3 400ms " << millis();
35-
delay1.start(200);
36-
}
28+
if (delay1.elapsed()) // sequence with deadlock
29+
{
30+
Serial << "\nsequence delay1 200ms " << millis();
31+
delay2.start(100);
32+
}
33+
if (delay2.elapsed()) {
34+
Serial << "\nsequence delay2 100ms " << millis();
35+
delay3.start(400);
36+
}
37+
if (delay3.elapsed()) {
38+
Serial << "\nsequence delay3 400ms " << millis();
39+
delay1.start(200);
40+
}
3741
}

Test_micros/Test_micros.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ VirtualDelay delay_us(micros);
44

55
void setup() { Serial.begin(9600); }
66

7-
void loop() {
8-
DO_ONCE(delay_us.start(1000000)) // 1s
9-
if (delay_us.elapsed()) Serial.print(micros());
7+
void loop()
8+
{
9+
DO_ONCE(delay_us.start(1000000)) // 1s
10+
if (delay_us.elapsed())
11+
Serial.print(micros());
1012
}

Test_rollover/Test_rollover.ino

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#include "avdweb_VirtualDelay.h"
12
#include <Albert.h>
23
#include <Arduino.h>
34
#include <Streaming.h>
45
#include <util/atomic.h>
5-
#include "avdweb_VirtualDelay.h"
66

77
VirtualDelay delay_ms;
88
VirtualDelay delay_us(micros);
@@ -11,29 +11,33 @@ unsigned long t0_ms, t0_us;
1111
long ms = 1000;
1212
long us = 2000000;
1313

14-
void setMillis(unsigned long ms) {
15-
extern unsigned long timer0_millis;
16-
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer0_millis = ms; }
14+
void setMillis(unsigned long ms)
15+
{
16+
extern unsigned long timer0_millis;
17+
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer0_millis = ms; }
1718
}
1819

19-
void setup() {
20-
Serial.begin(9600);
20+
void setup()
21+
{
22+
Serial.begin(9600);
2123

22-
setMillis(-ms); // rollover bug values: -x ... -1
23-
// setMillis(-1); // rollover bug values: -x ... -1
24+
setMillis(-ms); // rollover bug values: -x ... -1
25+
// setMillis(-1); // rollover bug values: -x ... -1
2426

25-
t0_ms = millis();
26-
t0_us = micros();
27-
Serial << "\nTest VirtualDelay ms " << ms << "ms";
28-
Serial << "\nTest VirtualDelay us " << us << "us";
29-
Serial << "\nChanged t0_ms=" << t0_ms << "ms";
27+
t0_ms = millis();
28+
t0_us = micros();
29+
Serial << "\nTest VirtualDelay ms " << ms << "ms";
30+
Serial << "\nTest VirtualDelay us " << us << "us";
31+
Serial << "\nChanged t0_ms=" << t0_ms << "ms";
3032
}
3133

32-
void loop() {
33-
DO_ONCE // do only one time in the loop
34-
(delay_ms.start(ms); delay_us.start(us);) if (delay_ms.elapsed()) Serial
35-
<< "\n"
36-
<< millis() - t0_ms << "ms"; // result is ~ 1021ms
37-
if (delay_us.elapsed())
38-
Serial << "\n" << micros() - t0_us << "us"; // result is ~ 2021884us
34+
void loop()
35+
{
36+
DO_ONCE // do only one time in the loop
37+
(delay_ms.start(ms); delay_us.start(us);) if (delay_ms.elapsed()) Serial
38+
<< "\n"
39+
<< millis() - t0_ms << "ms"; // result is ~ 1021ms
40+
if (delay_us.elapsed())
41+
Serial << "\n"
42+
<< micros() - t0_us << "us"; // result is ~ 2021884us
3943
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
#include <Arduino.h>
21
#include "avdweb_VirtualDelay.h"
2+
#include <Arduino.h>
33

44
const byte ledPin = 13;
55
bool b;
6-
VirtualDelay singleDelay; // default = millis
6+
VirtualDelay singleDelay; // default = millis
77

88
void setup() { pinMode(ledPin, OUTPUT); }
99

10-
void loop() {
11-
singleDelay.start(400); // calls while running are ignored
12-
if (singleDelay.elapsed())
13-
digitalWrite(ledPin, b = !b); // blink the onboard LED 400ms, 400ms off
10+
void loop()
11+
{
12+
singleDelay.start(400); // calls while running are ignored
13+
if (singleDelay.elapsed())
14+
digitalWrite(ledPin, b = !b); // blink the onboard LED 400ms, 400ms off
1415
}

avdweb_VirtualDelay.cpp

+23-19
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,32 @@ set timeOut _____|_____________________
2929

3030
#include "avdweb_VirtualDelay.h"
3131

32-
VirtualDelay::VirtualDelay(unsigned long (*timerFunctionPtr)())
33-
: timerFunctionPtr(timerFunctionPtr) {}
32+
VirtualDelay::VirtualDelay(
33+
unsigned long (*timerFunctionPtr)())
34+
: timerFunctionPtr(timerFunctionPtr)
35+
{
36+
}
3437

35-
void VirtualDelay::start(signed long delay) // 0...2^31
38+
void VirtualDelay::start(signed long delay) // 0...2^31
3639
{
37-
if (!running) {
38-
running = 1;
39-
timeOut = (*timerFunctionPtr)() + abs(delay);
40-
}
40+
if (!running) {
41+
running = 1;
42+
timeOut = (*timerFunctionPtr)() + abs(delay);
43+
}
4144
}
4245

43-
bool VirtualDelay::elapsed() {
44-
if (running) {
45-
// bug
46-
// if((signed long)(*timerFunctionPtr)() >= timeOut)
47-
// not the same as
48-
if ((signed long)((*timerFunctionPtr)() - timeOut) >= 0) {
49-
// fix rollover bug:
50-
// https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
51-
running = 0;
52-
return 1; // timer is elapsed
46+
bool VirtualDelay::elapsed()
47+
{
48+
if (running) {
49+
// bug
50+
// if((signed long)(*timerFunctionPtr)() >= timeOut)
51+
// not the same as
52+
if ((signed long)((*timerFunctionPtr)() - timeOut) >= 0) {
53+
// fix rollover bug:
54+
// https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
55+
running = 0;
56+
return 1; // timer is elapsed
57+
}
5358
}
54-
}
55-
return 0; // still in delay timer
59+
return 0; // still in delay timer
5660
}

avdweb_VirtualDelay.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html
2323
#endif
2424

2525
class VirtualDelay {
26-
public:
27-
VirtualDelay(unsigned long (*timerFunctionPtr)() = millis);
28-
void start(signed long delay);
29-
bool elapsed();
26+
public:
27+
VirtualDelay(unsigned long (*timerFunctionPtr)() = millis);
28+
void start(signed long delay);
29+
bool elapsed();
3030

31-
bool running = 0;
32-
unsigned long timeOut, (*timerFunctionPtr)();
31+
bool running = 0;
32+
unsigned long timeOut, (*timerFunctionPtr)();
3333
};
3434

35-
#define DO_ONCE(x) \
36-
{ \
37-
static bool b; \
38-
if (!b) { \
39-
b = 1; \
40-
x; \
41-
} \
42-
}
35+
#define DO_ONCE(x) \
36+
{ \
37+
static bool b; \
38+
if (!b) { \
39+
b = 1; \
40+
x; \
41+
} \
42+
}
4343

4444
#endif

testOneShot/testOneShot.ino

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
#include "avdweb_VirtualDelay.h"
12
#include <Arduino.h>
23
#include <Streaming.h>
3-
#include "avdweb_VirtualDelay.h"
44

55
VirtualDelay delay1;
66

7-
void setup() {
8-
Serial.begin(9600);
9-
Serial << "\ntestOneShot 2s\n";
7+
void setup()
8+
{
9+
Serial.begin(9600);
10+
Serial << "\ntestOneShot 2s\n";
1011
}
1112

12-
void loop() {
13-
DO_ONCE(delay1.start(2000)) // do only one time in the loop
14-
if (delay1.elapsed()) Serial << millis() << "ms";
13+
void loop()
14+
{
15+
DO_ONCE(delay1.start(2000)) // do only one time in the loop
16+
if (delay1.elapsed())
17+
Serial << millis() << "ms";
1518
}

testSequence/testSequence.ino

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
#include "avdweb_VirtualDelay.h"
12
#include <Arduino.h>
23
#include <Streaming.h>
3-
#include "avdweb_VirtualDelay.h"
44

55
VirtualDelay delay1, delay2, delay3;
66

7-
void setup() {
8-
Serial.begin(9600);
9-
Serial << "\ntestSequence";
7+
void setup()
8+
{
9+
Serial.begin(9600);
10+
Serial << "\ntestSequence";
1011
}
1112

12-
void loop() {
13-
if (delay1.elapsed()) // this sequence has a deadlock
14-
{
15-
Serial << "\ndelay1 200ms " << millis();
16-
delay2.start(100);
17-
}
18-
if (delay2.elapsed()) {
19-
Serial << "\ndelay2 100ms " << millis();
20-
delay3.start(400);
21-
}
22-
if (delay3.elapsed()) {
23-
Serial << "\ndelay3 400ms " << millis();
24-
delay1.start(200);
25-
}
26-
DO_ONCE(delay1.start(200)); // breaks the deadlock, you can start with any
27-
// delay object you want e.g. delay2.start(0);
13+
void loop()
14+
{
15+
if (delay1.elapsed()) // this sequence has a deadlock
16+
{
17+
Serial << "\ndelay1 200ms " << millis();
18+
delay2.start(100);
19+
}
20+
if (delay2.elapsed()) {
21+
Serial << "\ndelay2 100ms " << millis();
22+
delay3.start(400);
23+
}
24+
if (delay3.elapsed()) {
25+
Serial << "\ndelay3 400ms " << millis();
26+
delay1.start(200);
27+
}
28+
DO_ONCE(delay1.start(200)); // breaks the deadlock, you can start with any
29+
// delay object you want e.g. delay2.start(0);
2830
}

0 commit comments

Comments
 (0)