From daded2131896b8c82cc531b6ab96b26ee3bdc1eb Mon Sep 17 00:00:00 2001 From: sstaub Date: Wed, 4 Apr 2018 13:01:57 +0200 Subject: [PATCH] code simplified --- README.md | 19 +++++++------------ Ticker.cpp | 43 ++++++++++++++++--------------------------- examples/Example.ino | 12 ++++++------ library.json | 2 +- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 1c70ad9..a408daa 100755 --- a/README.md +++ b/README.md @@ -93,8 +93,8 @@ void loop() { timer3.update(); timer4.update(); timer5.update(); - if(timer4.counter() == 20) timer4.interval(200); - if(timer4.counter() == 80) timer4.interval(1000); + if (timer4.counter() == 20) timer4.interval(200); + if (timer4.counter() == 80) timer4.interval(1000); } void printCounter() { @@ -102,10 +102,10 @@ void printCounter() { Serial.println(timer2.counter()); } - void printCountdown() { - Serial.print("Countdowm "); - Serial.println(5 - timer3.counter()); - } +void printCountdown() { + Serial.print("Countdowm "); + Serial.println(5 - timer3.counter()); + } void printMessage() { Serial.println("Hello!"); @@ -135,12 +135,7 @@ STOPPED / RUNNING / PAUSED **Ticker(fptr callback, uint32_t timer, uint16_t repeats = 0, resolution_t resolution = MICROS)**
Creates a Ticker object - parameter callback for the function name you want to call -<<<<<<< HEAD -- parameter timer 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 interval sets the interval time in ms or us when using MICROS_MICROS with the resolution parameter ->>>>>>> 4975ea4e54a95d6d9ad649e67b3b5889e1067214 +- parameter timer sets the interval time in ms or us when using MICROS_MICROS with the resolution parameter - 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, MICROS_MICROS or MILLIS diff --git a/Ticker.cpp b/Ticker.cpp index d8c1ba6..d75a2d8 100755 --- a/Ticker.cpp +++ b/Ticker.cpp @@ -26,7 +26,7 @@ Ticker::Ticker(fptr callback, uint32_t timer, uint16_t repeat, resolution_t resolution) { this->resolution = resolution; - if(resolution == MICROS) timer = timer * 1000; + if (resolution == MICROS) timer = timer * 1000; this->timer = timer; this->repeat = repeat; this->callback = callback; @@ -39,7 +39,7 @@ Ticker::~Ticker() {} void Ticker::start() { if (callback == NULL) return; - if(resolution == MILLIS) lastTime = millis(); + if (resolution == MILLIS) lastTime = millis(); else lastTime = micros(); enabled = true; counts = 0; @@ -48,9 +48,9 @@ void Ticker::start() { void Ticker::resume() { if (callback == NULL) return; - if(resolution == MILLIS) lastTime = millis() - diffTime; + if (resolution == MILLIS) lastTime = millis() - diffTime; else lastTime = micros() - diffTime; - if(status == STOPPED) counts = 0; + if (status == STOPPED) counts = 0; enabled = true; status = RUNNING; } @@ -62,42 +62,31 @@ void Ticker::stop() { } void Ticker::pause() { - if(resolution == MILLIS) diffTime = millis() - lastTime; + if (resolution == MILLIS) diffTime = millis() - lastTime; else diffTime = micros() - lastTime; enabled = false; status = PAUSED; } void Ticker::update() { - if(tick()) callback(); + if (tick()) callback(); } bool Ticker::tick() { - if(!enabled) return false; - if(resolution == MILLIS) { + if (!enabled) return false; + if (resolution == MILLIS) { if ((millis() - lastTime) >= timer) { lastTime = millis(); - if(repeat - counts == 1) { - enabled = false; - counts++; - } - else { - counts++; - } + if (repeat - counts == 1) enabled = false; + counts++; return true; } } else { if ((micros() - lastTime) >= timer) { lastTime = micros(); - if(repeat - counts == 1) - { - enabled = false; - counts++; - } - else { - counts++; - } + if (repeat - counts == 1) enabled = false; + counts++; return true; } } @@ -105,18 +94,18 @@ bool Ticker::tick() { } void Ticker::interval(uint32_t timer) { - if(resolution == MICROS) timer = timer * 1000; + if (resolution == MICROS) timer = timer * 1000; this->timer = timer; } uint32_t Ticker::elapsed() { - if(resolution == MILLIS) return millis() - lastTime; + if (resolution == MILLIS) return millis() - lastTime; else return micros() - lastTime; } status_t Ticker::state() { - return status; - } + return status; + } uint32_t Ticker::counter() { return counts; diff --git a/examples/Example.ino b/examples/Example.ino index 64ab364..348c795 100755 --- a/examples/Example.ino +++ b/examples/Example.ino @@ -33,8 +33,8 @@ void loop() { timer3.update(); timer4.update(); timer5.update(); - if(timer4.counter() == 20) timer4.interval(200); - if(timer4.counter() == 80) timer4.interval(1000); + if (timer4.counter() == 20) timer4.interval(200); + if (timer4.counter() == 80) timer4.interval(1000); } void printCounter() { @@ -42,10 +42,10 @@ void printCounter() { Serial.println(timer2.counter()); } - void printCountdown() { - Serial.print("Countdowm "); - Serial.println(5 - timer3.counter()); - } +void printCountdown() { + Serial.print("Countdowm "); + Serial.println(5 - timer3.counter()); + } void printMessage() { Serial.println("Hello!"); diff --git a/library.json b/library.json index c2d4cb1..6e13614 100755 --- a/library.json +++ b/library.json @@ -7,7 +7,7 @@ "type": "git", "url": "https://github.com/sstaub/Ticker" }, - "version": "3.1.0", + "version": "3.1.1", "frameworks": "arduino", "platforms": "*" }