Skip to content

Commit

Permalink
update to v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sstaub committed Mar 21, 2018
1 parent 095a668 commit 97ae933
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# Arduino Ticker Library v2.0
# Arduino Ticker Library v2.1

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.
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 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 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 internal resolution to **millis()**. ``` Ticker tickerObject(callbackFunction, 1000, 0, MILLIS) ```
- The internal resolution is now **micros()**, this works with intervals up to 70 minutes. For longer intervals you can change the resolution to **millis()**. ``` Ticker tickerObject(callbackFunction, 1000, 0, MILLIS) ```
- unified data types and smaller improvments

## New in v2.1
- You can change the interval time to microseconds. ``` Ticker tickerObject(callbackFunction, 100, 0, MICROS_MICROS) // interval is now 100us```
- smaller improvments

## Installation

1. "Download": https://github.com/sstaub/Ticker/archive/master.zip the Master branch from GitHub.
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 Ticker library to your project:
First, include the TimerObject to your project:

```
#include "Ticker.h"
Expand Down Expand Up @@ -45,7 +48,7 @@ If you use delay(), the Ticker will be ignored! You cannot use delay() command w

## Example

Complete example. Here we created four timers, you can run it and test the result in the Serial monitor and the on board LED.
Complete example. Here we created three timers, you can run it and test the result in the Serial monitor and the on board LED.

```
#include "Ticker.h"
Expand All @@ -57,27 +60,25 @@ void blink();
bool ledState;
Ticker timer1(printMessage, 0, 1); // calls 'printMessage' one time, immediataly
Ticker timer2(printCounter, 1000); // calls 'printCounter' infinitely, every second
Ticker timer3(printCountdown, 1000, 5); // calls 'printCountdown' 5 times, every second
Ticker timer4; // creates a Ticker object named timer4
Ticker timer1(printMessage, 0, 1);
Ticker timer2(printCounter, 1000);
Ticker timer3(printCountdown, 1000, 5);
Ticker timer4;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
delay(2000);
timer4.setCallback(blink); // sets the callback of timer4 to function 'blink'
timer4.setInterval(500); // sets the intervall of timer4 to 500 ms
// start the Tickers
timer4.setCallback(blink);
timer4.setInterval(500);
timer1.start();
timer2.start();
timer3.start();
timer4.start();
}
void loop() {
// update the Tickers
timer1.update();
timer2.update();
timer3.update();
Expand All @@ -89,10 +90,10 @@ void printCounter() {
Serial.println(timer2.getRepeatsCounter());
}
void printCountdown() {
Serial.print("Countdowm ");
Serial.println(timer3.getRepeats() - timer3.getRepeatsCounter());
}
void printCountdown() {
Serial.print("Countdowm ");
Serial.println(timer3.getRepeats() - timer3.getRepeatsCounter());
}
void printMessage() {
Serial.println("Hello!");
Expand All @@ -117,6 +118,7 @@ Creates a Ticker object without parameters.
Creates a Ticker object
- parameter callback for the function name you want to call
- parameter interval sets the interval time in ms
- parameter interval resolution can changed to us instead of ms with setting the parameter resolution to MICROS_MICROS
- parameter repeats sets the number of repeats the callback should executed, 0 is endless
- parameter resolution sets the internal resolution of the Ticker, it can MICROS or MILLIS

Expand Down
36 changes: 17 additions & 19 deletions Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Ticker::Ticker(fptr callback, uint32_t interval, uint16_t repeats, resolution_t
Ticker::~Ticker() {}

void Ticker::init(fptr callback, uint32_t interval, uint16_t repeats, resolution_t resolution) {
_resolution = resolution;
this->resolution = resolution;
setInterval(interval);
setRepeats(repeats);
setCallback(callback);
Expand All @@ -46,7 +46,7 @@ void Ticker::init(fptr callback, uint32_t interval, uint16_t repeats, resolution

void Ticker::start() {
if (getCallback() == NULL) return;
if(_resolution == MILLIS) lastTime = millis();
if(resolution == MILLIS) lastTime = millis();
else lastTime = micros();
enabled = true;
counter = 0;
Expand All @@ -55,7 +55,7 @@ void Ticker::start() {

void Ticker::resume() {
if (getCallback() == NULL) return;
if(_resolution == MILLIS) lastTime = millis() - diffTime;
if(resolution == MILLIS) lastTime = millis() - diffTime;
else lastTime = micros() - diffTime;
if(state == STOPPED) counter = 0;
enabled = true;
Expand All @@ -69,7 +69,7 @@ void Ticker::stop() {
}

void Ticker::pause() {
if(_resolution == MILLIS) diffTime = millis() - lastTime;
if(resolution == MILLIS) diffTime = millis() - lastTime;
else diffTime = micros() - lastTime;
enabled = false;
state = PAUSED;
Expand All @@ -81,10 +81,10 @@ void Ticker::update() {

bool Ticker::tick() {
if(!enabled) return false;
if(_resolution == MILLIS) {
if ((millis() - lastTime) >= _interval) {
if(resolution == MILLIS) {
if ((millis() - lastTime) >= interval) {
lastTime = millis();
if(_repeats - counter == 1) {
if(repeats - counter == 1) {
enabled = false;
counter++;
}
Expand All @@ -95,10 +95,9 @@ bool Ticker::tick() {
}
}
else {
if ((micros() - lastTime) >= _interval) {
if ((micros() - lastTime) >= interval) {
lastTime = micros();
//if(countdown == 1)
if(_repeats - counter == 1)
if(repeats - counter == 1)
{
enabled = false;
counter++;
Expand All @@ -113,38 +112,37 @@ bool Ticker::tick() {
}

void Ticker::setInterval(uint32_t interval) {
if(_resolution == MILLIS) _interval = interval;
else _interval = interval * 1000;
if((resolution == MILLIS) || (resolution == MICROS_MICROS)) this->interval = interval;
else this->interval = interval * 1000;
}

void Ticker::setCallback(fptr callback) {
call = callback;
}

void Ticker::setRepeats(uint16_t repeats) {
_repeats = repeats;
this->repeats = repeats;
}

uint32_t Ticker::getElapsedTime() {
//return millis() - lastTime;
if(_resolution == MILLIS) return millis() - lastTime;
if(resolution == MILLIS) return millis() - lastTime;
else return micros() - lastTime;
}

status_t Ticker::getState() {
return state;
}
return state;
}

uint32_t Ticker::getInterval() {
return _interval;
return interval;
}

fptr Ticker::getCallback() {
return call;
}

uint16_t Ticker::getRepeats() {
return _repeats;
return repeats;
}

uint16_t Ticker::getRepeatsCounter() {
Expand Down
8 changes: 4 additions & 4 deletions Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @param MILLIS set the resolution to millis, for longer cycles over 70 minutes
*
*/
enum resolution_t {MICROS, MILLIS};
enum resolution_t {MICROS, MILLIS, MICROS_MICROS};

/** Ticker status
*
Expand Down Expand Up @@ -162,9 +162,9 @@ class Ticker {
void init(fptr callback, uint32_t interval, uint16_t repeats, resolution_t resolution);
bool tick();
bool enabled;
uint32_t _interval;
uint16_t _repeats;
resolution_t _resolution = MICROS;
uint32_t interval;
uint16_t repeats;
resolution_t resolution = MICROS;
uint32_t counter;
status_t state;
fptr call;
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "git",
"url": "https://github.com/sstaub/Ticker"
},
"version": "2.0.2",
"version": "2.1.0",
"frameworks": "arduino",
"platforms": "*"
}
Expand Down

0 comments on commit 97ae933

Please sign in to comment.