Skip to content

Commit

Permalink
Do not fade or blink when passed zero [times]
Browse files Browse the repository at this point in the history
  • Loading branch information
alextaujenis committed Mar 7, 2016
1 parent f5fa4ea commit 46b1c51
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
#Arduino Light Library v2.1.6 [![Build Passing](https://raw.githubusercontent.com/alextaujenis/RobotsBigData/gh-pages/src/images/passing.png)](https://github.com/alextaujenis/RBD_Light/blob/master/extras/unit_test/unit_test.ino)
#Arduino Light Library v2.1.7 [![Build Passing](https://raw.githubusercontent.com/alextaujenis/RobotsBigData/gh-pages/src/images/passing.png)](https://github.com/alextaujenis/RBD_Light/blob/master/extras/unit_test/unit_test.ino)
Control many lights and blink without delay using commands such as on(), off(), blink(), and fade().

* [Documentation](http://robotsbigdata.com/docs-arduino-light.html)
Expand Down
2 changes: 1 addition & 1 deletion examples/blink_without_delay/blink_without_delay.ino
@@ -1,4 +1,4 @@
// Arduino RBD Light Library v2.1.6 Example - Blink the Arduino LED without delay.
// Arduino RBD Light Library v2.1.7 Example - Blink the Arduino LED without delay.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License
Expand Down
2 changes: 1 addition & 1 deletion examples/fade_without_delay/fade_without_delay.ino
@@ -1,4 +1,4 @@
// Arduino RBD Light Library v2.1.6 Example - Fade the Arduino LED without delay.
// Arduino RBD Light Library v2.1.7 Example - Fade the Arduino LED without delay.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License
Expand Down
35 changes: 30 additions & 5 deletions extras/unit_test/unit_test.ino
@@ -1,4 +1,4 @@
// Arduino RBD Light Library v2.1.6 - Unit test coverage.
// Arduino RBD Light Library v2.1.7 - Unit test coverage.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License
Expand Down Expand Up @@ -515,6 +515,14 @@ bool digitalIsOff() { return !digitalIsOn(); }
testCleanup();
}

test(blink_zero_times_should_not_turn_the_light_on) {
light.blink(10,10,0);
light.update();
assertTrue(isOff());
// cleanup
testCleanup();
}

// blink: overloaded constructor
test(overloaded_blink_should_turn_the_light_on_and_off_forever) {
light.blink(10,10);
Expand Down Expand Up @@ -599,6 +607,23 @@ bool digitalIsOff() { return !digitalIsOn(); }
testCleanup();
}

test(fade_zero_times_should_not_ramp_the_light) {
// fade the light
light.fade(100,100,100,100,0);
light.update();
delay(50);
light.update();
delay(65);
light.update();
assertTrue(isOff());
delay(135);
light.update();
delay(51);
light.update();
assertTrue(isOff());
testCleanup();
}

// fade: overloaded constructor
test(overloaded_fade_should_ramp_the_light_up_and_down_forever) {
// calibrate pulse time
Expand All @@ -609,22 +634,22 @@ bool digitalIsOff() { return !digitalIsOn(); }
light.update();
delay(50);
light.update();
assertWithinTolerance(getPulseTime(),pulse1,10);
assertWithinTolerance(getPulseTime(),pulse1,15);
delay(65);
light.update();
assertTrue(isOn());
delay(135);
light.update();
assertWithinTolerance(getPulseTime(),pulse1,10);
assertWithinTolerance(getPulseTime(),pulse1,15);
delay(51);
light.update();
assertTrue(isOff());
delay(149);
light.update();
assertWithinTolerance(getPulseTime(),pulse1,10);
assertWithinTolerance(getPulseTime(),pulse1,15);
delay(200);
light.update();
assertWithinTolerance(getPulseTime(),pulse1,10);
assertWithinTolerance(getPulseTime(),pulse1,15);
testCleanup();
}

Expand Down
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=RBD_Light
version=2.1.6
version=2.1.7
author=Alex Taujenis <alex.taujenis@gmail.com>
maintainer=Alex Taujenis <alex.taujenis@gmail.com>
sentence=Control many lights.
Expand Down
36 changes: 27 additions & 9 deletions src/RBD_Light.cpp
@@ -1,4 +1,4 @@
// Arduino RBD Light Library v2.1.6 - Control many lights.
// Arduino RBD Light Library v2.1.7 - Control many lights.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License
Expand Down Expand Up @@ -63,34 +63,48 @@ namespace RBD {

void Light::blink(unsigned long on_time, unsigned long off_time, int times) {
_forever = false;
_times = times;

_on_timer.setTimeout(on_time);
_off_timer.setTimeout(off_time);
_times = times;
_stopEverything();
_startBlinking();
}

// unlimited times
void Light::blink(unsigned long on_time, unsigned long off_time) {
blink(on_time, off_time, 0);
_forever = true;
_times = 0;

_on_timer.setTimeout(on_time);
_off_timer.setTimeout(off_time);
_stopEverything();
_startBlinking();
}

void Light::fade(unsigned long up_time, unsigned long on_time, unsigned long down_time, unsigned long off_time, int times) {
_forever = false;
_times = times;

_up_timer.setTimeout(up_time);
_on_timer.setTimeout(on_time);
_down_timer.setTimeout(down_time);
_off_timer.setTimeout(off_time);
_times = times;
_stopEverything();
_startFading();
}

// unlimited times
void Light::fade(unsigned long up_time, unsigned long on_time, unsigned long down_time, unsigned long off_time) {
fade(up_time, on_time, down_time, off_time, 0);
_forever = true;
_times = 0;

_up_timer.setTimeout(up_time);
_on_timer.setTimeout(on_time);
_down_timer.setTimeout(down_time);
_off_timer.setTimeout(off_time);
_stopEverything();
_startFading();
}


Expand Down Expand Up @@ -222,17 +236,21 @@ namespace RBD {
}

void Light::_startFading() {
_up_timer.restart();
_state = _RISING;
_fading = true;
if(_times > 0 || _forever) {
_up_timer.restart();
_state = _RISING;
_fading = true;
}
}

void Light::_stopFading() {
_fading = false;
}

void Light::_startBlinking() {
_blinking = true;
if(_times > 0 || _forever) {
_blinking = true;
}
}

void Light::_stopBlinking() {
Expand Down
2 changes: 1 addition & 1 deletion src/RBD_Light.h
@@ -1,4 +1,4 @@
// Arduino RBD Light Library v2.1.6 - Control many lights.
// Arduino RBD Light Library v2.1.7 - Control many lights.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License
Expand Down

0 comments on commit 46b1c51

Please sign in to comment.