first commit
This commit is contained in:
21
libraries/Ticker/LICENSE
Executable file
21
libraries/Ticker/LICENSE
Executable file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Stefan Staub
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
344
libraries/Ticker/README.md
Executable file
344
libraries/Ticker/README.md
Executable file
@@ -0,0 +1,344 @@
|
||||
# Arduino Ticker Library v4.x.x
|
||||
**Advice: for use with ESP boards and mbed based Arduino boards like Arduino Nano RP2040 Connect and Raspberry Pi Pico (using the official Arduino core) the TickTwo library [https://github.com/sstaub/TickTwo](https://github.com/sstaub/TickTwo) is recommanded**
|
||||
|
||||
The **Arduino Ticker Library** allows you to create easily Ticker callbacks, which can call a function in a predetermined interval. You can change the number of repeats of the callbacks, if repeats is 0 the ticker runs in endless mode. Works like a "thread", where a secondary function will run when necessary. The library use no interupts of the hardware timers and works with the **micros() / millis()** function. You are not (really) limited in the number of Tickers.
|
||||
|
||||
## New in v4.0
|
||||
- added get interval function
|
||||
- added remaining function
|
||||
- added support for functional callbacks, only for ARM and ESP devices, e.g.<br> "Examples/FunctionalARM/FunctionalARM.ino"
|
||||
|
||||
## New in v3.1
|
||||
- added set interval function
|
||||
|
||||
## New in v3.0
|
||||
- radical simplified API
|
||||
- generally you have to declare all settings in the constructor
|
||||
- deleted many set and get functions
|
||||
- if you need former functionality please use the version 2.1
|
||||
|
||||
## New in v2.1
|
||||
- You can change the interval time to microseconds.
|
||||
```cpp
|
||||
Ticker tickerObject(callbackFunction, 100, 0, MICROS_MICROS) // interval is now 100us
|
||||
```
|
||||
- smaller improvments
|
||||
|
||||
## New in v2.0
|
||||
- You can determine the number of repeats, instead of modes.
|
||||
- The internal resolution is now **micros()**, this works with intervals up to 70 minutes. For longer intervals you can change the resolution to **millis()**.
|
||||
```cpp
|
||||
Ticker tickerObject(callbackFunction, 1000, 0, MILLIS)
|
||||
```
|
||||
- unified data types and smaller improvments
|
||||
|
||||
## Installation
|
||||
|
||||
1. "Download":https://github.com/sstaub/Ticker/archive/master.zip the Master branch from GitHub.
|
||||
2. Unzip and modify the folder name to "Ticker"
|
||||
3. Move the modified folder on your Library folder (On your `Libraries` folder inside Sketchbooks or Arduino software).
|
||||
|
||||
|
||||
## How to use
|
||||
|
||||
First, include the TimerObject to your project:
|
||||
|
||||
```cpp
|
||||
#include "Ticker.h"
|
||||
```
|
||||
|
||||
Now, you can create a new object in setup():
|
||||
|
||||
```cpp
|
||||
Ticker tickerObject(callbackFunction, 1000);
|
||||
tickerObject.start(); //start the ticker.
|
||||
```
|
||||
|
||||
In your loop(), add:
|
||||
|
||||
```cpp
|
||||
tickerObject.update(); //it will check the Ticker and if necessary, it will run the callback function.
|
||||
```
|
||||
|
||||
|
||||
## IMPORTANT
|
||||
If you use delay(), the Ticker will be ignored! You cannot use delay() command with the TimerObject. Instead of using delay, you can use the Ticker itself. For example, if you need that your loop run twice per second, just create a Ticker with 500 ms. It will have the same result that delay(500), but your code will be always state.
|
||||
|
||||
## Example
|
||||
|
||||
Complete example. Here we created five timers, you can run it and test the result in the Serial monitor and the on board LED.
|
||||
|
||||
```cpp
|
||||
#include "Ticker.h"
|
||||
|
||||
void printMessage();
|
||||
void printCounter();
|
||||
void printCountdown();
|
||||
void blink();
|
||||
void printCountUS();
|
||||
|
||||
bool ledState;
|
||||
int counterUS;
|
||||
|
||||
Ticker timer1(printMessage, 0, 1); // once, immediately
|
||||
Ticker timer2(printCounter, 1000, 0, MILLIS); // internal resolution is milli seconds
|
||||
Ticker timer3(printCountdown, 1000, 5); // 5 times, every second
|
||||
Ticker timer4(blink, 500); // changing led every 500ms
|
||||
Ticker timer5(printCountUS, 100, 0, MICROS_MICROS); // the interval time is 100us and the internal resolution is micro seconds
|
||||
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
Serial.begin(9600);
|
||||
delay(2000);
|
||||
timer1.start();
|
||||
timer2.start();
|
||||
timer3.start();
|
||||
timer4.start();
|
||||
timer5.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
timer1.update();
|
||||
timer2.update();
|
||||
timer3.update();
|
||||
timer4.update();
|
||||
timer5.update();
|
||||
if (timer4.counter() == 20) timer4.interval(200);
|
||||
if (timer4.counter() == 80) timer4.interval(1000);
|
||||
}
|
||||
|
||||
void printCounter() {
|
||||
Serial.print("Counter ");
|
||||
Serial.println(timer2.counter());
|
||||
}
|
||||
|
||||
void printCountdown() {
|
||||
Serial.print("Countdowm ");
|
||||
Serial.println(5 - timer3.counter());
|
||||
}
|
||||
|
||||
void printMessage() {
|
||||
Serial.println("Hello!");
|
||||
}
|
||||
|
||||
void blink() {
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
ledState = !ledState;
|
||||
}
|
||||
|
||||
void printCountUS() {
|
||||
counterUS++;
|
||||
if (counterUS == 10000) {
|
||||
Serial.println("10000 * 100us");
|
||||
counterUS = 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
### States
|
||||
|
||||
```cpp
|
||||
enum status_t {
|
||||
STOPPED,
|
||||
RUNNING,
|
||||
PAUSED
|
||||
};
|
||||
```
|
||||
|
||||
### Constructors
|
||||
|
||||
```cpp
|
||||
Ticker::Ticker(fptr callback, uint32_t timer, uint16_t repeats, interval_t mode)
|
||||
```
|
||||
|
||||
Creates a Ticker object
|
||||
|
||||
- **callback** for the function name you want to call
|
||||
- **timer** set the interval time in ms or us depending from mode
|
||||
- **repeats** set the number of repeats the callback should executed, 0 is endless (default)
|
||||
- **mode** set the interval resolution to MILLIS, MICROS_MICROS or MICROS (default)
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
Ticker timer(blink, 1000); // calls function blink() every second, internal resolution is micros, running endless
|
||||
Ticker timer(blink, 1000, 5); // calls function blink() every second, internal resolution is micros, only 5 repeats
|
||||
Ticker timer(blink, 1000, 0, MILLIS); // calls function blink() every second, internal resolution is millis, running endless
|
||||
Ticker timer(blink, 1000, 0, MICROS_MICROS); // calls function blink() every 1000 microsecond, internal resolution is micros, running endless
|
||||
```
|
||||
|
||||
### Destructor
|
||||
|
||||
```cpp
|
||||
Ticker::~Ticker()
|
||||
```
|
||||
Destructor for Ticker object
|
||||
|
||||
## Class Functions
|
||||
|
||||
### Ticker Start
|
||||
|
||||
```cpp
|
||||
void Ticker::start()
|
||||
```
|
||||
|
||||
Start the Ticker. Will count the interval from the moment that you start it. If it is paused, it will restart the Ticker.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
timer.start();
|
||||
```
|
||||
|
||||
### Ticker Resume
|
||||
|
||||
```cpp
|
||||
void Ticker::resume()
|
||||
```
|
||||
|
||||
Resume the Ticker. If not started, it will start it. If paused, it will resume it. For example, in a Ticker of 5 seconds, if it was paused in 3 seconds, the resume in continue in 3 seconds. Start will set passed time to 0 and restart until get 5 seconds.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
timer.resume();
|
||||
```
|
||||
|
||||
### Ticker Pause
|
||||
|
||||
```cpp
|
||||
void Ticker::pause()
|
||||
```
|
||||
|
||||
Pause the Ticker, so you can resume it.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
timer.pause();
|
||||
```
|
||||
|
||||
### Ticker Stop
|
||||
|
||||
```cpp
|
||||
void Ticker::stop()
|
||||
```
|
||||
|
||||
Stop the Ticker.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
timer.stop();
|
||||
```
|
||||
|
||||
### Ticker Update
|
||||
|
||||
```cpp
|
||||
void Ticker::update()
|
||||
```
|
||||
|
||||
Must to be called in the main while() loop, it will check the Ticker, and if necessary, will run the callback.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
while(1) {
|
||||
timer.update();
|
||||
1. }
|
||||
```
|
||||
|
||||
### Ticker set Interval Time
|
||||
|
||||
```cpp
|
||||
void Ticker::interval(uint32_t timer)
|
||||
```
|
||||
|
||||
Changes the interval time of the Ticker. Depending from the mode it can millis or micro seconds.
|
||||
|
||||
- **timer** set the interval time in ms or us depending from mode
|
||||
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
timer.interval(500); // new interval time
|
||||
```
|
||||
|
||||
### Ticker get Interval Time
|
||||
|
||||
```cpp
|
||||
uint32_t Ticker::interval()
|
||||
```
|
||||
|
||||
Get the interval time of the Ticker. Depending from the mode it can millis or micro seconds.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
uint32_t intervalTime;
|
||||
intervalTime = timer.interval(); // get the interval time
|
||||
```
|
||||
|
||||
### Ticker State
|
||||
|
||||
```cpp
|
||||
status_t Ticker::state()
|
||||
```
|
||||
|
||||
Returns the state of the Ticker.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
status_t status;
|
||||
status = timer.state();
|
||||
```
|
||||
|
||||
### Ticker Elapsed Time
|
||||
|
||||
```cpp
|
||||
uint32_t Ticker::elapsed()
|
||||
```
|
||||
|
||||
Returns the time passed since the last tick in ms or us depending on mode.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
uint32_t elapse;
|
||||
elapse = timer.elapsed();
|
||||
```
|
||||
|
||||
### Ticker Remaining Time
|
||||
|
||||
```cpp
|
||||
uint32_t Ticker::remaining()
|
||||
```
|
||||
|
||||
Returns the remaining time to the next tick in ms or us depending on mode.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
uint32_t remain;
|
||||
remain = timer.remaining();
|
||||
```
|
||||
|
||||
### Ticker Counter
|
||||
|
||||
```cpp
|
||||
uint32_t Ticker::counter()
|
||||
```
|
||||
|
||||
Get the number of executed callbacks.
|
||||
|
||||
**Example**
|
||||
|
||||
```cpp
|
||||
uint32_t count;
|
||||
count = timer.counter();
|
||||
```
|
||||
115
libraries/Ticker/Ticker.cpp
Executable file
115
libraries/Ticker/Ticker.cpp
Executable file
@@ -0,0 +1,115 @@
|
||||
/* Ticker library code is placed under the MIT license
|
||||
* Copyright (c) 2018 Stefan Staub
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "Ticker.h"
|
||||
|
||||
Ticker::Ticker(fptr callback, uint32_t timer, uint32_t repeat, resolution_t resolution) {
|
||||
this->resolution = resolution;
|
||||
if (resolution == MICROS) timer = timer * 1000;
|
||||
this->timer = timer;
|
||||
this->repeat = repeat;
|
||||
this->callback = callback;
|
||||
enabled = false;
|
||||
lastTime = 0;
|
||||
counts = 0;
|
||||
}
|
||||
|
||||
Ticker::~Ticker() {}
|
||||
|
||||
void Ticker::start() {
|
||||
if (callback == NULL) return;
|
||||
if (resolution == MILLIS) lastTime = millis();
|
||||
else lastTime = micros();
|
||||
enabled = true;
|
||||
counts = 0;
|
||||
status = RUNNING;
|
||||
}
|
||||
|
||||
void Ticker::resume() {
|
||||
if (callback == NULL) return;
|
||||
if (resolution == MILLIS) lastTime = millis() - diffTime;
|
||||
else lastTime = micros() - diffTime;
|
||||
if (status == STOPPED) counts = 0;
|
||||
enabled = true;
|
||||
status = RUNNING;
|
||||
}
|
||||
|
||||
void Ticker::stop() {
|
||||
enabled = false;
|
||||
counts = 0;
|
||||
status = STOPPED;
|
||||
}
|
||||
|
||||
void Ticker::pause() {
|
||||
if (resolution == MILLIS) diffTime = millis() - lastTime;
|
||||
else diffTime = micros() - lastTime;
|
||||
enabled = false;
|
||||
status = PAUSED;
|
||||
}
|
||||
|
||||
void Ticker::update() {
|
||||
if (tick()) callback();
|
||||
}
|
||||
|
||||
bool Ticker::tick() {
|
||||
if (!enabled) return false;
|
||||
uint32_t currentTime = (resolution == MILLIS) ? millis() : micros();
|
||||
if ((currentTime - lastTime) >= timer) {
|
||||
lastTime = currentTime;
|
||||
if (repeat - counts == 1 && counts != 0xFFFFFFFF) {
|
||||
enabled = false;
|
||||
status = STOPPED;
|
||||
}
|
||||
counts++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Ticker::interval(uint32_t timer) {
|
||||
if (resolution == MICROS) timer *= 1000;
|
||||
this->timer = timer;
|
||||
}
|
||||
|
||||
uint32_t Ticker::interval() {
|
||||
if (resolution == MILLIS) return timer / 1000;
|
||||
else return timer;
|
||||
}
|
||||
|
||||
uint32_t Ticker::elapsed() {
|
||||
if (resolution == MILLIS) return millis() - lastTime;
|
||||
else return micros() - lastTime;
|
||||
}
|
||||
|
||||
uint32_t Ticker::remaining() {
|
||||
return timer - elapsed();
|
||||
}
|
||||
|
||||
status_t Ticker::state() {
|
||||
return status;
|
||||
}
|
||||
|
||||
uint32_t Ticker::counter() {
|
||||
return counts;
|
||||
}
|
||||
160
libraries/Ticker/Ticker.h
Executable file
160
libraries/Ticker/Ticker.h
Executable file
@@ -0,0 +1,160 @@
|
||||
/* Ticker library code is placed under the MIT license
|
||||
* Copyright (c) 2018 Stefan Staub
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TICKER_H
|
||||
#define TICKER_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
/** Ticker internal resolution
|
||||
*
|
||||
* @param MICROS default, the resoöution is in micro seconds, max is 70 minutes, the real resoltuion is 4 microseconds at 16MHz CPU cycle
|
||||
* @param MILLIS set the resolution to millis, for longer cycles over 70 minutes
|
||||
*
|
||||
*/
|
||||
enum resolution_t {
|
||||
MICROS,
|
||||
MILLIS,
|
||||
MICROS_MICROS
|
||||
};
|
||||
|
||||
/** Ticker status
|
||||
*
|
||||
* @param STOPPED default, ticker is stopped
|
||||
* @param RUNNIBG ticker is running
|
||||
* @param PAUSED ticker is paused
|
||||
*
|
||||
*/
|
||||
enum status_t {
|
||||
STOPPED,
|
||||
RUNNING,
|
||||
PAUSED};
|
||||
|
||||
#if defined(__arm__) || defined(ESP8266) || defined(ESP32)
|
||||
#include <functional>
|
||||
using fptr = std::function<void()>;
|
||||
#else
|
||||
typedef void (*fptr)();
|
||||
#endif
|
||||
|
||||
|
||||
class Ticker {
|
||||
|
||||
public:
|
||||
|
||||
/** create a Ticker object
|
||||
*
|
||||
* @param callback the name of the function to call
|
||||
* @param timer interval length in ms or us
|
||||
* @param repeat default 0 -> endless, repeat > 0 -> number of repeats
|
||||
* @param resolution default MICROS for tickers under 70min, use MILLIS for tickers over 70 min
|
||||
*
|
||||
*/
|
||||
Ticker(fptr callback, uint32_t timer, uint32_t repeat = 0, resolution_t resolution = MICROS);
|
||||
|
||||
/** destructor for the Ticker object
|
||||
*
|
||||
*/
|
||||
~Ticker();
|
||||
|
||||
/** start the ticker
|
||||
*
|
||||
*/
|
||||
void start();
|
||||
|
||||
/** resume the ticker. If not started, it will start it.
|
||||
*
|
||||
*/
|
||||
void resume();
|
||||
|
||||
/** pause the ticker
|
||||
*
|
||||
*/
|
||||
void pause();
|
||||
|
||||
/** stops the ticker
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/** must to be called in the main loop(), it will check the Ticker, and if necessary, will run the callback
|
||||
*
|
||||
*/
|
||||
void update();
|
||||
|
||||
/**
|
||||
* @brief set the interval timer
|
||||
*
|
||||
* @param timer interval length in ms or us
|
||||
*/
|
||||
void interval(uint32_t timer);
|
||||
|
||||
/**
|
||||
* @brief get the interval time
|
||||
*
|
||||
* @returns the interval time
|
||||
*/
|
||||
uint32_t interval();
|
||||
|
||||
/** actual ellapsed time
|
||||
*
|
||||
* @returns the elapsed time after the last tick
|
||||
*
|
||||
*/
|
||||
uint32_t elapsed();
|
||||
|
||||
/** time remaining to the next tick
|
||||
*
|
||||
* @returns the remaining time to the next tick in ms or us depending from mode
|
||||
*
|
||||
*/
|
||||
uint32_t remaining();
|
||||
|
||||
/** get the state of the ticker
|
||||
*
|
||||
* @returns the state of the ticker: STOPPED, RUNNING or PAUSED
|
||||
*/
|
||||
status_t state();
|
||||
|
||||
/** get the numbers of executed repeats
|
||||
*
|
||||
* @returns the number of executed repeats
|
||||
*
|
||||
*/
|
||||
uint32_t counter();
|
||||
|
||||
private:
|
||||
bool tick();
|
||||
bool enabled;
|
||||
uint32_t timer;
|
||||
uint32_t repeat;
|
||||
resolution_t resolution = MICROS;
|
||||
uint32_t counts;
|
||||
status_t status;
|
||||
fptr callback;
|
||||
uint32_t lastTime;
|
||||
uint32_t diffTime;
|
||||
};
|
||||
|
||||
#endif
|
||||
79
libraries/Ticker/examples/FunctionalARM/FunctionalARM.ino
Normal file
79
libraries/Ticker/examples/FunctionalARM/FunctionalARM.ino
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <Ticker.h>
|
||||
|
||||
void func() {
|
||||
Serial.println("func.");
|
||||
}
|
||||
|
||||
Ticker ticker1(func, 1000, 0, MILLIS);
|
||||
|
||||
// Simple class with (non static) method
|
||||
class A {
|
||||
public:
|
||||
A(bool flag) : flag(flag) {}
|
||||
|
||||
void func() {
|
||||
Serial.printf("A::func: %s.\n", flag ? "true" : "false");
|
||||
}
|
||||
bool flag;
|
||||
};
|
||||
|
||||
A a1(true);
|
||||
|
||||
// use lambda to capture a1 and execute a1.func()
|
||||
|
||||
Ticker ticker2([](){a1.func();}, 1000, 0, MILLIS);
|
||||
|
||||
A a2(false);
|
||||
|
||||
Ticker ticker3([](){a2.func();}, 1000, 0, MILLIS);
|
||||
|
||||
// Class that registers its own method as a callback when it's instantiated.
|
||||
class B {
|
||||
public:
|
||||
B(bool flag) : flag(flag), ticker{[this](){this->func();}, 1000, 0, MILLIS} {
|
||||
ticker.start();
|
||||
}
|
||||
|
||||
void func() {
|
||||
Serial.printf("B::func: %s.\n", flag ? "true" : "false");
|
||||
}
|
||||
bool flag;
|
||||
Ticker ticker;
|
||||
};
|
||||
|
||||
B b(true);
|
||||
|
||||
// Class that acts like a function (functor)
|
||||
class C {
|
||||
public:
|
||||
C(int num) : num(num){}
|
||||
|
||||
// you can call an instance directly with parenthesis and this is executed
|
||||
void operator()() const {
|
||||
Serial.printf("C(): %d.\n", num);
|
||||
}
|
||||
int num;
|
||||
};
|
||||
|
||||
C c(4);
|
||||
|
||||
Ticker ticker4(c, 1000, 0, MILLIS);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println();
|
||||
|
||||
ticker1.start();
|
||||
ticker2.start();
|
||||
ticker3.start();
|
||||
ticker4.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ticker1.update();
|
||||
ticker2.update();
|
||||
ticker3.update();
|
||||
b.ticker.update();
|
||||
ticker4.update();
|
||||
}
|
||||
65
libraries/Ticker/examples/Ticker/Ticker.ino
Executable file
65
libraries/Ticker/examples/Ticker/Ticker.ino
Executable file
@@ -0,0 +1,65 @@
|
||||
#include "Ticker.h"
|
||||
|
||||
void printMessage();
|
||||
void printCounter();
|
||||
void printCountdown();
|
||||
void blink();
|
||||
void printCountUS();
|
||||
|
||||
bool ledState;
|
||||
int counterUS;
|
||||
|
||||
Ticker timer1(printMessage, 0, 1);
|
||||
Ticker timer2(printCounter, 1000, 0, MILLIS);
|
||||
Ticker timer3(printCountdown, 1000, 5);
|
||||
Ticker timer4(blink, 500);
|
||||
Ticker timer5(printCountUS, 100, 0, MICROS_MICROS);
|
||||
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
Serial.begin(9600);
|
||||
delay(2000);
|
||||
timer1.start();
|
||||
timer2.start();
|
||||
timer3.start();
|
||||
timer4.start();
|
||||
timer5.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
timer1.update();
|
||||
timer2.update();
|
||||
timer3.update();
|
||||
timer4.update();
|
||||
timer5.update();
|
||||
if (timer4.counter() == 20) timer4.interval(200);
|
||||
if (timer4.counter() == 80) timer4.interval(1000);
|
||||
}
|
||||
|
||||
void printCounter() {
|
||||
Serial.print("Counter ");
|
||||
Serial.println(timer2.counter());
|
||||
}
|
||||
|
||||
void printCountdown() {
|
||||
Serial.print("Countdowm ");
|
||||
Serial.println(5 - timer3.counter());
|
||||
}
|
||||
|
||||
void printMessage() {
|
||||
Serial.println("Hello!");
|
||||
}
|
||||
|
||||
void blink() {
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
ledState = !ledState;
|
||||
}
|
||||
|
||||
void printCountUS() {
|
||||
counterUS++;
|
||||
if (counterUS == 10000) {
|
||||
Serial.println("10000 * 100us");
|
||||
counterUS = 0;
|
||||
}
|
||||
}
|
||||
11
libraries/Ticker/keywords.txt
Executable file
11
libraries/Ticker/keywords.txt
Executable file
@@ -0,0 +1,11 @@
|
||||
Ticker KEYWORD1
|
||||
start KEYWORD2
|
||||
resume KEYWORD2
|
||||
pause KEYWORD2
|
||||
stop KEYWORD2
|
||||
update KEYWORD2
|
||||
interval KEYWORD2
|
||||
state KEYWORD2
|
||||
counter KEYWORD2
|
||||
elapsed KEYWORD2
|
||||
remaining KEYWORD2
|
||||
14
libraries/Ticker/library.json
Executable file
14
libraries/Ticker/library.json
Executable file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Ticker",
|
||||
"keywords": "Ticker, Timer, Threads",
|
||||
"description": "A library for creating Tickers which can call repeating functions. Replaces delay() with non-blocking functions.",
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/sstaub/Ticker"
|
||||
},
|
||||
"version": "4.4.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
||||
|
||||
9
libraries/Ticker/library.properties
Normal file
9
libraries/Ticker/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=Ticker
|
||||
version=4.4.0
|
||||
author=Stefan Staub
|
||||
maintainer=Stefan Staub <email@domain.com>
|
||||
sentence=A library for creating Tickers which can call repeating functions. Replaces delay() with non-blocking functions.
|
||||
paragraph=The Arduino Ticker Library allows you to create easily Ticker callbacks, which can call a function in a predetermined interval. You can change the number of repeats of the callbacks, if repeats is 0 the ticker runs in endless mode. Works like a "thread", where a secondary function will run when necessary. The library use no interupts of the hardware timers and works with the micros() / millis() function. You are not (really) limited in the number of Tickers.
|
||||
category=Timing
|
||||
url=https://github.com/sstaub/Ticker
|
||||
architectures=*
|
||||
Reference in New Issue
Block a user