Skip to content

Commit 8380506

Browse files
authored
Add period to on and off effects (#93)
1 parent 1971249 commit 8380506

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# JLed changelog (github.com/jandelgado/jled)
22

3+
## [2022-02-24] 4.10.0
4+
5+
* new: `On`, `Off` and `Set` now take an optional `duration` value, making
6+
these effects behave like any other in this regard. This allows to add
7+
an `On` effect to a `JLedSequence` for a specific amount of time.
8+
39
## [2022-02-24] 4.9.1
410

511
* fix: make sure JLedSequence methods like `Repeat` and `Forever` are chainable

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,22 @@ See the examples section below for further details.
155155

156156
#### Static on and off
157157

158-
Calling `On()` turns the LED on. To immediately turn a LED on, make a call
159-
like `JLed(LED_BUILTIN).On().Update()`.
158+
Calling `On(uint16_t period=1)` turns the LED on. To immediately turn a LED on,
159+
make a call like `JLed(LED_BUILTIN).On().Update()`. The `period` is optional
160+
and defaults to 1ms.
160161

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

164-
Use the `Set(uint8_t brightness)` method to set the brightness to the given
165-
value, i.e. `Set(255)` is equivalent to calling `On()` and `Set(0)` is
166-
equivalent to calling `Off()`.
165+
Use the `Set(uint8_t brightness, uint16_t period=1)` method to set the
166+
brightness to the given value, i.e. `Set(255)` is equivalent to calling `On()`
167+
and `Set(0)` is equivalent to calling `Off()`.
167168

168-
Technically `Set`, `On` and `Off` are effects with a period of 1ms that
169-
set the brightness to a constant value.
169+
Technically, `Set`, `On` and `Off` are effects with a default period of 1ms, that
170+
set the brightness to a constant value. Specifiying a different period has an
171+
effect on when the `Update()` method will be done updating the effect and
172+
return false (like for any other effects). This is important when for example
173+
in a `JLedSequence` the LED should stay on for a given amount of time.
170174

171175
##### Static on example
172176

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "JLed",
3-
"version": "4.9.1",
3+
"version": "4.10.0",
44
"description": "An embedded library to control LEDs",
55
"license": "MIT",
66
"frameworks": ["espidf", "arduino", "mbed"],

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=JLed
2-
version=4.9.1
2+
version=4.10.0
33
author=Jan Delgado <jdelgado[at]gmx.net>
44
maintainer=Jan Delgado <jdelgado[at]gmx.net>
55
sentence=An Arduino library to control LEDs

src/jled_base.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ class CloneableBrightnessEvaluator : public BrightnessEvaluator {
6666

6767
class ConstantBrightnessEvaluator : public CloneableBrightnessEvaluator {
6868
uint8_t val_;
69+
uint16_t duration_;
6970

7071
public:
7172
ConstantBrightnessEvaluator() = delete;
72-
explicit ConstantBrightnessEvaluator(uint8_t val) : val_(val) {}
73+
explicit ConstantBrightnessEvaluator(uint8_t val, uint16_t duration = 1)
74+
: val_(val), duration_(duration) {}
7375
BrightnessEvaluator* clone(void* ptr) const override {
7476
return new (ptr) ConstantBrightnessEvaluator(*this);
7577
}
76-
uint16_t Period() const override { return 1; }
78+
uint16_t Period() const override { return duration_; }
7779
uint8_t Eval(uint32_t) const override { return val_; }
7880
};
7981

@@ -261,17 +263,19 @@ class TJLed {
261263
bool IsLowActive() const { return bLowActive_; }
262264

263265
// turn LED on
264-
B& On() { return Set(kFullBrightness); }
266+
B& On(uint16_t duration = 1) { return Set(kFullBrightness, duration); }
265267

266268
// turn LED off
267-
B& Off() { return Set(kZeroBrightness); }
269+
B& Off(uint16_t duration = 1) { return Set(kZeroBrightness, duration); }
268270

269-
// Sets LED to given brightness
270-
B& Set(uint8_t brightness) {
271+
// Sets LED to given brightness. As for every effect, a duration can be
272+
// specified. Update() will return false after the duration elapsed.
273+
B& Set(uint8_t brightness, uint16_t duration = 1) {
271274
// note: we use placement new and therefore not need to keep track of
272275
// mem allocated
273-
return SetBrightnessEval(new (brightness_eval_buf_)
274-
ConstantBrightnessEvaluator(brightness));
276+
return SetBrightnessEval(
277+
new (brightness_eval_buf_)
278+
ConstantBrightnessEvaluator(brightness, duration));
275279
}
276280

277281
// Fade LED on
@@ -467,8 +471,14 @@ class TJLed {
467471
uint16_t delay_after_ = 0; // delay after each repetition
468472
};
469473

470-
template<typename T> T* ptr(T &obj) {return &obj;} // NOLINT
471-
template<typename T> T* ptr(T *obj) {return obj;}
474+
template <typename T>
475+
T* ptr(T& obj) { // NOLINT
476+
return &obj;
477+
}
478+
template <typename T>
479+
T* ptr(T* obj) {
480+
return obj;
481+
}
472482

473483
// a group of JLed objects which can be controlled simultanously, in parallel
474484
// or sequentially.
@@ -494,7 +504,7 @@ class TJLedSequence {
494504
if (!ptr(leds_[cur_])->Update()) {
495505
return ++cur_ < n_;
496506
}
497-
return true;;
507+
return true;
498508
}
499509

500510
void ResetLeds() {
@@ -503,7 +513,6 @@ class TJLedSequence {
503513
}
504514
}
505515

506-
507516
public:
508517
enum eMode { SEQUENCE, PARALLEL };
509518
TJLedSequence() = delete;
@@ -519,8 +528,9 @@ class TJLedSequence {
519528
return false;
520529
}
521530

522-
const auto led_running = (mode_ == eMode::PARALLEL) ? UpdateParallel()
523-
: UpdateSequentially();
531+
const auto led_running = (mode_ == eMode::PARALLEL)
532+
? UpdateParallel()
533+
: UpdateSequentially();
524534
if (led_running) {
525535
return true;
526536
}
@@ -529,7 +539,7 @@ class TJLedSequence {
529539
cur_ = 0;
530540
ResetLeds();
531541

532-
is_running_ = ++iteration_ < num_repetitions_ ||
542+
is_running_ = ++iteration_ < num_repetitions_ ||
533543
num_repetitions_ == kRepeatForever;
534544

535545
return is_running_;

0 commit comments

Comments
 (0)