Skip to content

Commit

Permalink
Set() method takes brightness instead of On()
Browse files Browse the repository at this point in the history
  • Loading branch information
jandelgado committed Mar 16, 2019
1 parent 4db4ea4 commit 6643a5f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# JLed changelog

## [2019-03-10] v4.1.0

* change: clean up interface and simplify code: `On()` no longer takes an
optional brightness argument. Call `Set(uint8_t brightness)` instead.
* documentation update

## [2019-03-10] v4.0.0

In addition to the changes introduced with `v4.0.0-rc0` and `v4.0.0-rc1`, the
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

An Arduino library to control LEDs. It uses a **non-blocking** approach and can
control LEDs in simple (**on**/**off**) and complex (**blinking**,
**breathing**) ways in a **time-driven** manner.
**breathing** and more) ways in a **time-driven** manner.

JLed got some [coverage on Hackaday](https://hackaday.com/2018/06/13/simplifying-basic-led-effects/)
and someone did a [video tutorial for JLed](https://youtu.be/x5V2vdpZq1w) - Thanks!
Expand Down Expand Up @@ -38,9 +38,8 @@ void loop() {
* [PlatformIO](#platformio)
* [Usage](#usage)
* [Effects](#effects)
* [Static on](#static-on)
* [Static on and off](#static-on-and-off)
* [Static on example](#static-on-example)
* [Static off](#static-off)
* [Blinking](#blinking)
* [Blinking example](#blinking-example)
* [Breathing](#breathing)
Expand Down Expand Up @@ -135,11 +134,17 @@ See examples section below for further details.

### Effects

#### Static on
#### Static on and off

Calling `On()` turns the LED on. An optional brightness value can be supplied,
the default value is 255, which is full brightness.
To immediately turn a LED on, make a call like `JLed(LED_BUILTIN).On().Update()`.
Calling `On()` turns the LED on. To immediately turn a LED on, make a call
like `JLed(LED_BUILTIN).On().Update()`.

`Off()` works like `On()`, except that it turns the LED off, i.e. it sets the
brightness to 0.

Use the `Set(uint8_t brightness)` method to set the brightness to the given
value, i.e. `Set(255)` is equivalent to calling `On()` and `Set(0)` is
equivalent to calling `Off()`.

##### Static on example

Expand All @@ -156,11 +161,6 @@ void loop() {
}
```

#### Static off

`Off()` works like `On()`, except that it turns the LED off, i.e. it sets the
brightness to 0.

#### Blinking

In blinking mode, the LED cycles through a given number of on-off cycles, on-
Expand Down Expand Up @@ -280,9 +280,9 @@ two methods:
All time values are specified in milliseconds.

The [user_func](examples/user_func) example demonstrates a simple user provided
brightness function, while the [morse](examples/morse) shows how a more complex
application, allowing you to send morse codes (not necessarily with an LED), can
be realized.
brightness function, while the [morse](examples/morse) example shows how a more
complex application, allowing you to send morse codes (not necessarily with an
LED), can be realized.

##### User provided brightness function example

Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Off KEYWORD2
Set KEYWORD2
Blink KEYWORD2
Breathe KEYWORD2
Candle KEYWORD2
FadeOn KEYWORD2
FadeOff KEYWORD2
Repeat 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=JLed
version=v4.0.0
version=v4.1.0
author=Jan Delgado <jdelgado[at]gmx.net>
maintainer=Jan Delgado <jdelgado[at]gmx.net>
sentence=An Arduino library to control LEDs
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ env_default = nanoatmega328
;env_default = esp32

; uncomment example to build
;src_dir = examples/hello
src_dir = examples/hello
;src_dir = examples/morse
;src_dir = examples/breathe
src_dir = examples/candle
;src_dir = examples/candle
;src_dir = examples/fade_on
;src_dir = examples/fade_off
;src_dir = examples/simple_on
Expand Down
28 changes: 14 additions & 14 deletions src/jled_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,29 +254,29 @@ class TJLed {

bool Update() { return Update(hal_.millis()); }

// turn LED on
B& On(uint8_t brightness = kFullBrightness) {
// we use placement new and therefore not need to keep track of mem
// allocated
brightness_eval_ =
new (brightness_eval_buf_) ConstantBrightnessEvaluator(brightness);
return static_cast<B&>(*this);
}

// Set physical LED polarity to be low active. This inverts every
// signal physically output to a pin.
B& LowActive() { return SetFlags(FL_LOW_ACTIVE, true); }
bool IsLowActive() const { return GetFlag(FL_LOW_ACTIVE); }

// turn LED on
B& On() {
return Set(kFullBrightness);
}

// turn LED off
B& Off() {
brightness_eval_ = new (brightness_eval_buf_)
ConstantBrightnessEvaluator(kZeroBrightness);
return static_cast<B&>(*this);
return Set(kZeroBrightness);
}

// turn LED on or off, calls On() / Off()
B& Set(bool on) { return on ? On() : Off(); }
// Sets LED to given brightness
B& Set(uint8_t brightness) {
// note: we use placement new and therefore not need to keep track of
// mem allocated
brightness_eval_ =
new (brightness_eval_buf_) ConstantBrightnessEvaluator(brightness);
return static_cast<B&>(*this);
}

// Fade LED on
B& FadeOn(uint16_t duration) {
Expand Down
8 changes: 4 additions & 4 deletions test/test_jled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ TEST_CASE("On/Off function configuration", "[jled]") {
REQUIRE(jled.brightness_eval_->Eval(0) == 0);
}

SECTION("Set(true)") {
SECTION("Set(255)") {
TestableJLed jled(1);
jled.Set(true);
jled.Set(255);
REQUIRE(dynamic_cast<ConstantBrightnessEvaluator *>(
jled.brightness_eval_) != nullptr);
REQUIRE(jled.brightness_eval_->Eval(0) == 255);
}

SECTION("Set(false)") {
SECTION("Set(0)") {
TestableJLed jled(1);
jled.Set(false);
jled.Set(0);
REQUIRE(dynamic_cast<ConstantBrightnessEvaluator *>(
jled.brightness_eval_) != nullptr);
REQUIRE(jled.brightness_eval_->Eval(0) == 0);
Expand Down

0 comments on commit 6643a5f

Please sign in to comment.