Skip to content

Commit

Permalink
Complete rewrite to support all boards by removing fancy c++ features,
Browse files Browse the repository at this point in the history
…fixes #3, fixes #4
  • Loading branch information
Aasim-A committed Jul 14, 2021
1 parent 4a0f1c2 commit 3591ab1
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 104 deletions.
98 changes: 89 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ Download the repository as .zip and include it as a new library into the IDE sel

Go to libraries and type [AsyncTimer](https://platformio.org/lib/show/11569/AsyncTimer) into the search bar and add it to your project.


# Getting Started
Simply include the library into your sketch and make one instance of `AsyncTimer` and add the setup function to `void setup()` and the handler to `void loop()` and then start using it!


#### Example:

```c++
Expand All @@ -53,13 +55,19 @@ void loop()
}
```

### NOTE: only one instance must be created and it must be outside any function, as shown in the example above.
> ⚠NOTE⚠: only one instance must be created and it must be outside any function, as shown in the example above.
> ⚠NOTE⚠: The default timer capacity is 10, if you wish to increase it, you have to use non-default initializer:
```c++
AsyncTimer t = AsyncTimer(22);
```

# API

## setTimeout(callbackFunction, delayInMs)

`setTimeout` takes two arguments, the first one is the function to call after waiting, the second one is the time in milliseconds to wait before executing the function. It returns an `unsigned short` id of the timeout.
`setTimeout` takes two arguments, the first one is the function to call after waiting, the second one is the time in milliseconds to wait before executing the function. It returns an `unsigned short` id of the timeout. If the `timeout` creation was unseccussfull, it returns `0`.
It will run only once unless canceled.

#### Example:
Expand All @@ -69,7 +77,7 @@ It will run only once unless canceled.
```c++
AsyncTimer t;

t.setTimeout([&]() {
t.setTimeout([]() {
Serial.println("Hello world!");
}, 2000);
// "Hello world!" will be printed to the Serial once after 2 seconds
Expand All @@ -91,7 +99,7 @@ t.setTimeout(functionToCall, 2000);

## setInterval(callbackFunction, delayInMs)

`setInterval` takes the same parameters as `setTimeout` and returns an `unsigned short` id of the interval, unlike `setTimeout`, it will keep executing the code forever unless canceled.
`setInterval` takes the same parameters as `setTimeout` and returns an `unsigned short` id of the interval, unlike `setTimeout`, it will keep executing the code forever unless canceled. If the `interval` creation was unseccussfull, it returns `0`.

#### Example:

Expand All @@ -100,7 +108,7 @@ t.setTimeout(functionToCall, 2000);
```c++
AsyncTimer t;

t.setInterval([&]() {
t.setInterval([]() {
Serial.println("Hello world!");
}, 2000);
// "Hello world!" will be printed to the Serial every 2 seconds
Expand All @@ -120,9 +128,77 @@ t.setInterval(functionToCall, 2000);
// "Hello world!" will be printed to the Serial every 2 seconds
```

## changeDelay(intervalOrTimeoutId, delayInMs)
Changes the delay value of an active `intervalOrTimeout`.

`changeDelay` takes two arguments, the `id` returned from `setTimeout` or `setInterval` function and the new `delayValue` in `ms`, returns `void`.

#### Example:

- Changing the delay of `setInterval`:

```c++
AsyncTimer t;

unsigned short intervalId = t.setInterval([]() {
Serial.println("Hello world!");
}, 2000);

t.setTimeout([]() {
t.changeDelay(intervalId, 3500);
// Now the interval runs every 3500ms instead of the old 2000ms
}, 7000);
```

## delay(intervalOrTimeoutId, delayInMs)
Delays the execution of an active `intervalOrTimeout`.

`delay` takes two arguments, the `id` returned from `setTimeout` or `setInterval` function and the new `delayValue` in `ms`, returns `void`.

#### Example:

- Delaying the execution of `setInterval`:

```c++
AsyncTimer t;

unsigned short intervalId = t.setInterval([]() {
Serial.println("Hello world!");
}, 2000);

t.setTimeout([]() {
t.delay(intervalId, 3500);
// Now the interval will be delayed by an extra 3500ms,
// afterwords, it will continue executing normally.
}, 7000);
```

## reset(intervalOrTimeoutId)
Resets the wait time of an active `intervalOrTimeout`.

`delay` takes one argument, the `id` returned from `setTimeout` or `setInterval` function, returns `void`.

#### Example:

- Resetting the wait time of `setInterval`:

```c++
AsyncTimer t;

unsigned short intervalId = t.setInterval([]() {
Serial.println("Hello world!");
}, 2000);

t.setTimeout([]() {
t.reset(intervalId);
// Now the interval will be reset, this means that it will
// execute exactly 2000ms after the reset function call.
}, 7000);
```

## cancel(intervalOrTimeoutId)

It cancels the execution of a timeout or an interval.
Cancels the execution of a timeout or an interval.

`cancel` takes one argument, the `id` returned from `setTimeout` or `setInterval` function and returns `void`.

Expand All @@ -133,12 +209,12 @@ It cancels the execution of a timeout or an interval.
```c++
AsyncTimer t;

unsigned short intervalId = t.setInterval([&]() {
unsigned short intervalId = t.setInterval([]() {
Serial.println("Hello world!");
}, 2000);

// Cancel the interval after 7 seconds:
t.setTimeout([&]() {
t.setTimeout([]() {
t.cancel(intervalId);
}, 7000);
```
Expand All @@ -149,20 +225,24 @@ t.setTimeout([&]() {
AsyncTimer t;

// This timeout will never run
unsigned short timeoutId = t.setTimeout([&]() {
unsigned short timeoutId = t.setTimeout([]() {
Serial.println("Hello world!");
}, 3000);

// Cancel the timeout before it's executed
t.cancel(timeoutId);
```

# Limitations
- Capturing lambda functions do not work.

# Examples

- BlinkUsingInterval - Blink led using `setInterval`.
- SerialMsgUsingTimeout - Send a message to the serial monitor using `setTimeout` 10 seconds after booting.
- CancelInterval - Cancel an interval using `cancel`.
- CancelTimeout - Cancel a timeout using `cancel`.
- DebounceUsingTimeout - Debounce button using a `delay`.

# License

Expand Down
13 changes: 4 additions & 9 deletions examples/BlinkUsingInterval/BlinkUsingInterval.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

AsyncTimer t;

void setup()
{
void setup() {
t.setup();

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

t.setInterval([&]() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}, 1000);
t.setInterval([]() { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); },
1000);
}

void loop()
{
t.handle();
}
void loop() { t.handle(); }
17 changes: 5 additions & 12 deletions examples/CancelInterval/CancelInterval.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@

AsyncTimer t;

void setup()
{
void setup() {
t.setup();

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

unsigned short intervalId = t.setInterval([&]() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}, 1000);
unsigned short intervalId = t.setInterval(
[]() { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); }, 1000);

t.setTimeout([&]() {
t.cancel(intervalId);
}, 10000);
t.setTimeout([]() { t.cancel(intervalId); }, 10000);
}

void loop()
{
t.handle();
}
void loop() { t.handle(); }
17 changes: 5 additions & 12 deletions examples/CancelTimeout/CancelTimeout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@

AsyncTimer t;

void setup()
{
void setup() {
t.setup();
Serial.begin(9600);

unsigned short timeoutId = t.setTimeout([&]() {
Serial.println("Message after 10 seconds from booting");
}, 10000);
unsigned short timeoutId = t.setTimeout(
[]() { Serial.println("Message after 10 seconds from booting"); }, 10000);

// Cancel instantly:
// t.cancel(timeoutId);

// Or cancel using another timeout after 3 seconds:
t.setTimeout([&]() {
t.cancel(timeoutId);
}, 3000);
t.setTimeout([]() { t.cancel(timeoutId); }, 3000);
}

void loop()
{
t.handle();
}
void loop() { t.handle(); }
32 changes: 32 additions & 0 deletions examples/DebounceUsingTimeout/DebounceUsingTimeout.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Debounce button using a timeout and delay function

#include <AsyncTimer.h>

#define BUTTON_PIN 4

AsyncTimer t;

void setup() {
t.setup();
Serial.begin(9600);

pinMode(BUTTON_PIN, INPUT);
}

unsigned short timeoutId = 0;

void loop() {
t.handle();

if (digitalRead(BUTTON_PIN) == HIGH) {
if (timeoutId != 0)
t.reset(timeoutId);
else
timeoutId = t.setTimeout(
[]() {
Serial.println("Button pressed!");
timeoutId = 0;
},
50);
}
}
13 changes: 4 additions & 9 deletions examples/SerialMsgUsingTimeout/SerialMsgUsingTimeout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@

AsyncTimer t;

void setup()
{
void setup() {
t.setup();
Serial.begin(9600);

t.setTimeout([&]() {
Serial.println("Message after 10 seconds from booting");
}, 10000);
t.setTimeout(
[]() { Serial.println("Message after 10 seconds from booting"); }, 10000);
}

void loop()
{
t.handle();
}
void loop() { t.handle(); }
3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
setup KEYWORD2
setTimeout KEYWORD2
setInterval KEYWORD2
changeDelay KEYWORD2
delay KEYWORD2
reset KEYWORD2
cancel KEYWORD2
handle KEYWORD2

Expand Down
3 changes: 2 additions & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AsyncTimer",
"version": "1.0.2",
"version": "2.0.0",
"description": "JavaScript-like async timing functions (setTimeout, setInterval).",
"keywords": [
"settimeout",
Expand All @@ -16,6 +16,7 @@
"esp8266",
"arduino",
"uno",
"Seeeduino",
"javascript"
],
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AsyncTimer
version=1.0.2
version=2.0.0
author=Aasim-A
maintainer=Aasim-A <github.com/Aasim-A>
sentence=JavaScript-like async timing functions (setTimeout, setInterval).
Expand Down
Loading

0 comments on commit 3591ab1

Please sign in to comment.