Skip to content

Commit

Permalink
Add isStopped, slight refactor, bump minor version
Browse files Browse the repository at this point in the history
  • Loading branch information
alextaujenis committed Nov 23, 2015
1 parent 1e8b90a commit a811a75
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Arduino Timer Library v1.0.5
#Arduino Timer Library v1.1.0
Manage many timed events.

* [Documentation](http://robotsbigdata.com/docs-arduino-timer.html)
Expand Down
2 changes: 1 addition & 1 deletion examples/interval_timer/interval_timer.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Arduino RBD Timer Library v1.0.5 Example - A three second interval timer that prints to serial.
// Arduino RBD Timer Library v1.1.0 Example - A three second interval timer that prints to serial.
// https://github.com/alextaujenis/RBD_Timer
// Copyright 2015 Alex Taujenis
// MIT License
Expand Down
Binary file modified extras/RBD_Timer.zip
Binary file not shown.
45 changes: 34 additions & 11 deletions extras/unit_test/unit_test.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Arduino RBD Timer Library v1.0.5 - Unit test coverage.
// Arduino RBD Timer Library v1.1.0 - Unit test coverage.
// https://github.com/alextaujenis/RBD_Timer
// Copyright 2015 Alex Taujenis
// MIT License
Expand All @@ -13,7 +13,7 @@ RBD::Timer timer;
assertTrue(timer.isExpired());
assertFalse(timer.isActive());
}

// setTimeout
test(setTimeout_should_set_the_timeout_in_milliseconds) {
timer.setTimeout(100);
Expand Down Expand Up @@ -66,7 +66,7 @@ RBD::Timer timer;

assertFalse(timer.isActive());
}

// isExpired
test(isExpired_should_return_false_if_time_is_available) {
timer.setTimeout(1);
Expand Down Expand Up @@ -94,7 +94,33 @@ RBD::Timer timer;

assertTrue(timer.isExpired());
}


// isStopped
test(isStopped_should_return_true_if_stopped) {
timer.setTimeout(1);
timer.restart();
timer.stop();

assertTrue(timer.isStopped());
}

test(isStopped_should_return_false_if_active) {
timer.setTimeout(1);
timer.restart();

assertTrue(timer.isActive());
assertFalse(timer.isStopped());
}

test(isStopped_should_return_false_if_expired) {
timer.setTimeout(1);
timer.restart();
delay(1);

assertTrue(timer.isExpired());
assertFalse(timer.isStopped());
}

// onRestart
test(onRestart_should_return_true_if_the_timer_expires) {
timer.setTimeout(1);
Expand Down Expand Up @@ -178,13 +204,12 @@ RBD::Timer timer;
assertFalse(timer.onExpired());
}


// stop
test(isActive_and_onActive_should_return_false_after_stop) {
timer.setTimeout(1);
timer.restart();
timer.restart();
timer.stop();

assertFalse(timer.isActive());
assertFalse(timer.onActive());
}
Expand All @@ -193,23 +218,21 @@ RBD::Timer timer;
timer.setTimeout(1);
timer.restart();
timer.stop();

delay(1);

assertFalse(timer.isExpired());
assertFalse(timer.onExpired());
}


test(onRestart_should_return_false_after_stop) {
timer.setTimeout(1);
timer.restart();
timer.stop();

delay(1);

assertFalse(timer.onRestart());
}


// getValue
test(getValue_should_return_the_time_passed_since_restart) {
timer.setTimeout(2);
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ Timer KEYWORD1
setTimeout KEYWORD2
setHertz KEYWORD2
restart KEYWORD2
stop KEYWORD2
isActive KEYWORD2
isExpired KEYWORD2
isStopped KEYWORD2
onRestart KEYWORD2
onActive KEYWORD2
onExpired KEYWORD2
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=RBD_Timer
version=1.0.5
version=1.1.0
author=Alex Taujenis <[email protected]>
maintainer=Alex Taujenis <[email protected]>
sentence=Manage many timed events.
Expand Down
23 changes: 13 additions & 10 deletions src/RBD_Timer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Arduino RBD Timer Library v1.0.5 - Manage many timed events.
// Arduino RBD Timer Library v1.1.0 - Manage many timed events.
// https://github.com/alextaujenis/RBD_Timer
// Copyright 2015 Alex Taujenis
// MIT License
Expand All @@ -7,11 +7,6 @@
#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer

namespace RBD {

Timer::Timer()
: _state(EXPIRED)
{}

void Timer::setTimeout(unsigned long value) {
_timeout = (value > 0) ? value : 1;
}
Expand All @@ -30,7 +25,7 @@ namespace RBD {
}

void Timer::stop() {
_state = INACTIVE;
_state = STOPPED;
}

bool Timer::isActive() {
Expand All @@ -43,8 +38,12 @@ namespace RBD {
return _state == EXPIRED;
}

bool Timer::isStopped() {
return _state == STOPPED;
}

bool Timer::onRestart() {
if( isExpired() ) {
if(isExpired()) {
restart();
return true;
}
Expand Down Expand Up @@ -80,9 +79,13 @@ namespace RBD {
int Timer::getInversePercentValue() {
return 100 - getPercentValue();
}



// private

void Timer::_updateState() {
if( (_state == ACTIVE) && (getValue() >= _timeout) )
if(_state == ACTIVE && getValue() >= _timeout) {
_state = EXPIRED;
}
}
}
11 changes: 6 additions & 5 deletions src/RBD_Timer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Arduino RBD Timer Library v1.0.5 - Manage many timed events.
// Arduino RBD Timer Library v1.1.0 - Manage many timed events.
// https://github.com/alextaujenis/RBD_Timer
// Copyright 2015 Alex Taujenis
// MIT License
Expand All @@ -11,13 +11,13 @@
namespace RBD {
class Timer {
public:
Timer();
void setTimeout(unsigned long value); // set/change how long until the timer expires in milliseconds
void setHertz(int value); // set/change how many times the timer can be restarted in one second
void restart(); // reset and start the timer
void stop(); // stop (i.e. cancel) the timer
void stop(); // stop the timer
bool isActive(); // check if time is left
bool isExpired(); // returns true if time has run out
bool isStopped(); // returns true if the timer is stopped
bool onRestart(); // returns true if the timer is expired and restarts the timer automatically
bool onActive(); // returns true once the timer is active, then waits for it to expire and go active again
bool onExpired(); // returns true once the timer is expired, then waits for it to go active and expire again
Expand All @@ -26,12 +26,13 @@ namespace RBD {
int getPercentValue(); // how much time has passed as a percentage of the interval
int getInversePercentValue(); // how much time is left as a percentage of the interval
private:
void _updateState(); // update state according to current state and current value
enum {INACTIVE, ACTIVE, EXPIRED} _state; // timer internal state
unsigned long _timeout; // how long this timer should run for
unsigned long _waypoint; // the point in time the timer was started or reset
bool _has_been_active = false; // helps fire the onActive event only once
bool _has_been_expired = false; // helps fire the onExpired event only once
enum {ACTIVE, EXPIRED, STOPPED} _state = EXPIRED; // timer internal state
void _updateState(); // update state according to current state and current value
};
}

#endif

0 comments on commit a811a75

Please sign in to comment.