Skip to content

Commit

Permalink
code simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
sstaub committed Apr 4, 2018
1 parent 6200cd0 commit daded21
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ 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() {
Serial.print("Counter ");
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!");
Expand Down Expand Up @@ -135,12 +135,7 @@ STOPPED / RUNNING / PAUSED
**Ticker(fptr callback, uint32_t timer, uint16_t repeats = 0, resolution_t resolution = MICROS)**<br>
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

Expand Down
43 changes: 16 additions & 27 deletions Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -62,61 +62,50 @@ 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;
}
}
return false;
}

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;
Expand Down
12 changes: 6 additions & 6 deletions examples/Example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ 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() {
Serial.print("Counter ");
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!");
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": "3.1.0",
"version": "3.1.1",
"frameworks": "arduino",
"platforms": "*"
}
Expand Down

0 comments on commit daded21

Please sign in to comment.