Skip to content

Commit f6ccd9d

Browse files
authored
optionally return last brightness value from Update() (#131)
add new example examples/last_brightness
1 parent 41e5276 commit f6ccd9d

12 files changed

Lines changed: 313 additions & 134 deletions

File tree

.github/workflows/test.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ on:
77
- master
88

99
name: run tests
10-
11-
concurrency:
12-
group: ${{ github.head_ref }}
13-
cancel-in-progress: true
14-
1510
jobs:
1611
lint:
1712
runs-on: ubuntu-latest

CHANGELOG.md

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

3+
## [2024-12-01] 4.15.0
4+
5+
* new: `Update()` methods now optionally return the last brightness value
6+
calculated and written out to the LED. See `examples/last_brightness`
7+
38
## [2024-09-21] 4.14
49

5-
* new: make `Jled::Update(unit32_t t)` public, allowing optimizations and
10+
* new: make `JLed::Update(unit32_t t)` public, allowing optimizations and
611
simplified tests
712

813
## [2023-09-10] 4.13.1

README.md

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<table><tr><td>
2-
<b>Preferring Python?</b> I just released <a href="https://github.com/jandelgado/jled-circuitpython">jled-circuitpython</a>,
1+
<table><tr><td>
2+
<b>Preferring Python?</b> I just released <a href="https://github.com/jandelgado/jled-circuitpython">jled-circuitpython</a>,
33
a JLed implementation for CircuitPython and MicroPython.
44
</td></tr></table>
55

@@ -91,8 +91,8 @@ void loop() {
9191
* [Arduino framework](#arduino-framework)
9292
* [Raspberry Pi Pico](#raspberry-pi-pico)
9393
* [Example sketches](#example-sketches)
94-
* [PlatformIO](#platformio-1)
95-
* [Arduino IDE](#arduino-ide-1)
94+
* [Building examples with PlatformIO](#building-examples-with-platformio)
95+
* [Building examples with the Arduino IDE](#building-examples-with-the-arduino-ide)
9696
* [Extending](#extending)
9797
* [Support new hardware](#support-new-hardware)
9898
* [Unit tests](#unit-tests)
@@ -194,9 +194,9 @@ Use the `Set(uint8_t brightness, uint16_t period=1)` method to set the
194194
brightness to the given value, i.e., `Set(255)` is equivalent to calling `On()`
195195
and `Set(0)` is equivalent to calling `Off()`.
196196

197-
Technically, `Set`, `On` and `Off` are effects with a default period of 1ms, that
197+
Technically, `Set`, `On` and `Off` are effects with a default period of 1ms, that
198198
set the brightness to a constant value. Specifying a different period has an
199-
effect on when the `Update()` method will be done updating the effect and
199+
effect on when the `Update()` method will be done updating the effect and
200200
return false (like for any other effects). This is important when for example
201201
in a `JLedSequence` the LED should stay on for a given amount of time.
202202

@@ -268,25 +268,25 @@ auto led = JLed(13).Breathe(500, 1000, 500).DelayAfter(1000).Forever();
268268

269269
#### Candle
270270

271-
In candle mode, the random flickering of a candle or fire is simulated.
271+
In candle mode, the random flickering of a candle or fire is simulated.
272272
The builder method has the following signature:
273273
`Candle(uint8_t speed, uint8_t jitter, uin16_t period)`
274274

275-
* `speed` - controls the speed of the effect. 0 for fastest, increasing speed
275+
* `speed` - controls the speed of the effect. 0 for fastest, increasing speed
276276
divides into halve per increment. The default value is 7.
277277
* `jitter` - the amount of jittering. 0 none (constant on), 255 maximum. Default
278278
value is 15.
279279
* `period` - Period of effect in ms. The default value is 65535 ms.
280280

281281
The default settings simulate a candle. For a fire effect for example use
282-
call the method with `Candle(5 /*speed*/, 100 /* jitter*/)`.
282+
call the method with `Candle(5 /*speed*/, 100 /* jitter*/)`.
283283

284284
##### Candle example
285285

286286
```c++
287287
#include <jled.h>
288288

289-
// Candle on LED pin 13 (PWM capable).
289+
// Candle on LED pin 13 (PWM capable).
290290
auto led = JLed(13).Candle();
291291

292292
void setup() { }
@@ -363,7 +363,7 @@ two methods:
363363
as an unsigned byte, where 0 means LED off and 255 means full brightness.
364364
* `uint16_t Period() const` - period of the effect.
365365

366-
All time values are specified in milliseconds.
366+
All time values are specified in milliseconds.
367367

368368
The [user_func](examples/user_func) example demonstrates a simple user provided
369369
brightness function, while the [morse](examples/morse) example shows how a more
@@ -410,10 +410,29 @@ specified by `DelayAfter()` method.
410410
411411
##### Update
412412
413-
Call `Update()` or `Update(uint32_t t)` periodically to update the state of the
414-
LED. `Update` returns `true` if the effect is active, and `false` when it
415-
finished. `Update()` is a shortcut to call `Update(uint32_t t)` with the
416-
current time.
413+
Call `Update(int16_t *pLast=nullptr)` or `Update(uint32_t t, int16_t *pLast=nullptr)`
414+
to periodically update the state of the LED.
415+
416+
`Update` returns `true`, if the effect is active, or `false` when it finished.
417+
`Update()` is a shortcut to call `Update(uint32_t t)` with the current time in
418+
milliseconds.
419+
420+
To obtain the value of the last written brightness value (after applying min-
421+
and max-brightness transformations), pass an additional optional pointer
422+
`*pLast` , where this value will be stored, when it was written. Example:
423+
424+
```c++
425+
int16_t lastVal = -1;
426+
led.Update(&lastVal);
427+
if (lastVal != -1) {
428+
// the LED was updated with the brightness value now stored in lastVal
429+
...
430+
}
431+
```
432+
433+
Most of the time just calling `Update()` without any parameters is what you want.
434+
435+
See [last_brightness](examples/last_brightness) example for a working example.
417436

418437
##### IsRunning
419438

@@ -453,16 +472,16 @@ will be inverted by JLed (i.e., instead of x, the value of 255-x will be set).
453472

454473
##### Minimum- and Maximum brightness level
455474

456-
The `MaxBrightness(uint8_t level)` method is used to set the maximum brightness
457-
level of the LED. A level of 255 (the default) is full brightness, while 0
475+
The `MaxBrightness(uint8_t level)` method is used to set the maximum brightness
476+
level of the LED. A level of 255 (the default) is full brightness, while 0
458477
effectively turns the LED off. In the same way, the `MinBrightness(uint8_t level)`
459478
method sets the minimum brightness level. The default minimum level is 0. If
460479
minimum or maximum brightness levels are set, the output value is scaled to be
461480
within the interval defined by `[minimum brightness, maximum brightness]`: a
462481
value of 0 will be mapped to the minimum brightness level, a value of 255 will
463482
be mapped to the maximum brightness level.
464483

465-
The `uint_8 MaxBrightness() const` method returns the current maximum
484+
The `uint_8 MaxBrightness() const` method returns the current maximum
466485
brightness level. `uint8_t MinBrightness() const` returns the current minimum
467486
brightness level.
468487

@@ -500,10 +519,10 @@ The `JLedSequence` provides the following methods:
500519
else `false`.
501520
* Use the `Repeat(n)` method to specify the number of repetitions. The default
502521
value is 1 repetition. The `Forever()` methods sets to repeat the sequence
503-
forever.
504-
* `Stop()` - turns off all `JLed` objects controlled by the sequence and
522+
forever.
523+
* `Stop()` - turns off all `JLed` objects controlled by the sequence and
505524
stops the sequence. Further calls to `Update()` will have no effect.
506-
* `Reset()` - Resets all `JLed` objects controlled by the sequence and
525+
* `Reset()` - Resets all `JLed` objects controlled by the sequence and
507526
the sequence, resulting in a start-over.
508527

509528
## Framework notes
@@ -518,7 +537,7 @@ framework:
518537
platform=ststm32
519538
board = nucleo_f401re
520539
framework = mbed
521-
build_flags = -Isrc
540+
build_flags = -Isrc
522541
src_filter = +<../../src/> +<./>
523542
upload_protocol=stlink
524543
```
@@ -570,8 +589,8 @@ so it should be avoided and is normally not necessary.
570589
For completeness, the full signature of the Esp32Hal constructor is
571590

572591
```
573-
Esp32Hal(PinType pin,
574-
int chan = kAutoSelectChan,
592+
Esp32Hal(PinType pin,
593+
int chan = kAutoSelectChan,
575594
uint16_t freq = 5000,
576595
ledc_timer_t timer = LEDC_TIMER_0)
577596
```
@@ -600,9 +619,14 @@ necessary to upload sketches to the microcontroller.
600619

601620
### Raspberry Pi Pico
602621

603-
When using JLed on a Raspberry Pi Pico, the Pico-SDK and tools must be
604-
installed. The Pico supports up to 16 PWM channels in parallel. See
605-
the [pico-demo](examples/raspi_pico) for an example and build instructions.
622+
When using JLed on a Raspberry Pi Pico, the Pico-SDK and tools can be
623+
used. The Pico supports up to 16 PWM channels in parallel. See
624+
the [pico-demo](examples/raspi_pico) for an example and build instructions when
625+
the Pico-SDK is used.
626+
627+
A probably easier approach is to use the Arduino platform. See
628+
[platformio.ini](platformio.ini) for details (look for
629+
`env:raspberrypi_pico_w`, which targets the Raspberry Pi Pico W.
606630

607631
## Example sketches
608632

@@ -621,6 +645,7 @@ Example sketches are provided in the [examples](examples/) directory.
621645
* [Controlling multiple LEDs sequentially](examples/sequence)
622646
* [Simple User provided effect](examples/user_func)
623647
* [Morsecode example](examples/morse)
648+
* [Last brightness value example](examples/last_brightness)
624649
* [Custom HAL example](examples/custom_hal)
625650
* [Custom PCA9685 HAL](https://github.com/jandelgado/jled-pca9685-hal)
626651
* [Dynamically switch sequences](https://github.com/jandelgado/jled-example-switch-sequence)
@@ -629,9 +654,9 @@ Example sketches are provided in the [examples](examples/) directory.
629654
* [ESP32 ESP-IDF example](https://github.com/jandelgado/jled-esp-idf-example)
630655
* [ESP32 ESP-IDF PlatformIO example](https://github.com/jandelgado/jled-esp-idf-platformio-example)
631656

632-
### PlatformIO
657+
### Building examples with PlatformIO
633658

634-
To build an example using [the PlatformIO ide](http://platformio.org/),
659+
To build an example using [the PlatformIO ide](http://platformio.org/),
635660
uncomment the example to be built in the [platformio.ini](platformio.ini)
636661
project file, e.g.:
637662

@@ -642,7 +667,7 @@ src_dir = examples/hello
642667
;src_dir = examples/breathe
643668
```
644669

645-
### Arduino IDE
670+
### Building examples with the Arduino IDE
646671

647672
To build an example sketch in the Arduino IDE, select an example from
648673
the `File` > `Examples` > `JLed` menu.
@@ -670,8 +695,8 @@ the host-based provided unit tests [is provided here](test/README.md).
670695
* add code
671696
* add [unit test(s)](test/)
672697
* add [documentation](README.md)
673-
* make sure the cpp [linter](https://github.com/cpplint/cpplint) does not
674-
report any problems (run `make lint`). Hint: use `clang-format` with the
698+
* make sure the cpp [linter](https://github.com/cpplint/cpplint) does not
699+
report any problems (run `make lint`). Hint: use `clang-format` with the
675700
provided [settings](.clang-format)
676701
* commit changes
677702
* submit a PR

devbox.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"packages": [
3-
"python@3.11",
3+
"python@3.13",
44
"lcov@1.16",
55
"pipx",
66
"cpplint@2.0.0"
77
],
88
"shell": {
9-
"init_hook": [
10-
"echo 'Welcome to devbox!' > /dev/null"
9+
"init_hook": [
10+
". $VENV_DIR/bin/activate"
1111
],
1212
"scripts": {
1313
"test": [

devbox.lock

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,60 @@
8989
"resolved": "github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c#pipx",
9090
"source": "nixpkg"
9191
},
92-
"python@3.11": {
93-
"last_modified": "2024-03-22T11:26:23Z",
94-
"plugin_version": "0.0.3",
95-
"resolved": "github:NixOS/nixpkgs/a3ed7406349a9335cb4c2a71369b697cecd9d351#python3",
92+
"python@3.13": {
93+
"last_modified": "2024-11-28T07:51:56Z",
94+
"plugin_version": "0.0.4",
95+
"resolved": "github:NixOS/nixpkgs/226216574ada4c3ecefcbbec41f39ce4655f78ef#python313",
9696
"source": "devbox-search",
97-
"version": "3.11.8",
97+
"version": "3.13.0",
9898
"systems": {
9999
"aarch64-darwin": {
100-
"store_path": "/nix/store/c05vbvkjxarxkws9zkwrcwrzlsx9nd68-python3-3.11.8"
100+
"outputs": [
101+
{
102+
"name": "out",
103+
"path": "/nix/store/fbyrkq5n04a9hn5zs26vrmqjzdx73d4g-python3-3.13.0",
104+
"default": true
105+
}
106+
],
107+
"store_path": "/nix/store/fbyrkq5n04a9hn5zs26vrmqjzdx73d4g-python3-3.13.0"
101108
},
102109
"aarch64-linux": {
103-
"store_path": "/nix/store/pxzzyri1wbq7kc7pain665g94afkl4ww-python3-3.11.8"
110+
"outputs": [
111+
{
112+
"name": "out",
113+
"path": "/nix/store/jbz9fj3sp5c8bf0s6d0bkjjj9mslxsrc-python3-3.13.0",
114+
"default": true
115+
},
116+
{
117+
"name": "debug",
118+
"path": "/nix/store/60jgy93wj50wwimmhm2p53pzaiap8ypm-python3-3.13.0-debug"
119+
}
120+
],
121+
"store_path": "/nix/store/jbz9fj3sp5c8bf0s6d0bkjjj9mslxsrc-python3-3.13.0"
104122
},
105123
"x86_64-darwin": {
106-
"store_path": "/nix/store/1zaap1xxxvw2ypsgh1mfxb3wzdd49873-python3-3.11.8"
124+
"outputs": [
125+
{
126+
"name": "out",
127+
"path": "/nix/store/c7j1vxcdcqswsddm5m1n2n4z5zfhmbq2-python3-3.13.0",
128+
"default": true
129+
}
130+
],
131+
"store_path": "/nix/store/c7j1vxcdcqswsddm5m1n2n4z5zfhmbq2-python3-3.13.0"
107132
},
108133
"x86_64-linux": {
109-
"store_path": "/nix/store/7wz6hm9i8wljz0hgwz1wqmn2zlbgavrq-python3-3.11.8"
134+
"outputs": [
135+
{
136+
"name": "out",
137+
"path": "/nix/store/0b83hlniyfbpha92k2j0w93mxdalv8kb-python3-3.13.0",
138+
"default": true
139+
},
140+
{
141+
"name": "debug",
142+
"path": "/nix/store/xzhxhqs8my0yvfi09aj1s9i1s9nrmpvg-python3-3.13.0-debug"
143+
}
144+
],
145+
"store_path": "/nix/store/0b83hlniyfbpha92k2j0w93mxdalv8kb-python3-3.13.0"
110146
}
111147
}
112148
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Stops an effect when a button is pressed (and hold). When the button is
2+
// released, the LED will fade to off with starting the brightness value it had
3+
// when the effect was stopped.
4+
//
5+
// dependency: arduinogetstarted/ezButton@1.0.6 to control the button
6+
//
7+
// Copyright 2024 by Jan Delgado. All rights reserved.
8+
// https://github.com/jandelgado/jled
9+
//
10+
#include <ezButton.h> // arduinogetstarted/ezButton@1.0.6
11+
#include <jled.h>
12+
13+
constexpr auto LED_PIN = 16;
14+
constexpr auto BUTTON_PIN = 18;
15+
16+
auto button = ezButton(BUTTON_PIN);
17+
18+
// start with a pulse effect
19+
auto led =
20+
JLed(LED_PIN).DelayBefore(1000).Breathe(2000).Forever().MinBrightness(25);
21+
22+
void setup() {}
23+
24+
void loop() {
25+
static int16_t lastBrightness = 0;
26+
27+
button.loop();
28+
led.Update(&lastBrightness);
29+
30+
if (button.isPressed()) {
31+
// when the button is pressed, stop the effect on led, but keep the LED
32+
// on with it's current brightness ...
33+
led.Stop(JLed::KEEP_CURRENT);
34+
} else if (button.isReleased()) {
35+
// when the button is released, fade from the last brightness to 0
36+
led = JLed(LED_PIN).Fade(lastBrightness, 0, 1000);
37+
}
38+
}

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.14",
3+
"version": "4.15.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.14
2+
version=4.15.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

0 commit comments

Comments
 (0)