Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit d4b900c

Browse files
committed
immediate stopping of effects
+ smaller API only core business in lib
1 parent 5ba873a commit d4b900c

23 files changed

+495
-998
lines changed

.travis.yml

-24
This file was deleted.

README.md

+9-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# esp32WS2811
22

3-
Arduino Library for ESP32 to drive WS2811/WS2811 RGB leds using the RMT peripheral.
3+
Arduino Library for ESP32 to drive WS2811/WS2812 RGB leds using the RMT peripheral. The lib is lean and only drives the leds.
44

55
## Hardware
66

@@ -59,60 +59,33 @@ void setup() {
5959
Change LED colours:
6060

6161
```C++
62+
Colour colour = yourLedString.getPixel(size_t index); // gives you the Colour of the led on index.
6263
yourLedString.setPixel(size_t index, uint32_t red, uint32_t green, uint32_t blue);
63-
yourLedString.show(); // this updates the LEDs
64+
yourLedString.show(); // this actually makes the LEDs light up
6465
```
6566

6667
A number of helpers methods are available:
6768

6869
```C++
69-
void clearAll(); // turns off all LEDs
70-
void setAll(uint32_t red, uint32_t green, uint32_t blue); // gives all LEDs the specified colour
71-
void set Bightness (uint8_t brightness, bool reset = false) // set maximum brightness (in %), reset updates immediately
70+
yourLedString.clearAll(); // turns off all LEDs
71+
yourLedString.setAll(Colour colour); // // gives all LEDs the specified colour
72+
yourLedString.setAll(uint32_t red, uint32_t green, uint32_t blue); // gives all LEDs the specified colour
7273
```
7374

7475
Keep in mind that all these methods require to call `show()` afterwards.
7576

7677
## Effects
7778

78-
Starting and stopping an effect is done by.
79+
Starting and stopping an effect is done by:
7980

8081
```C++
8182
void startEffect(WS2811Effect* effect);
8283
void stopEffect();
8384
```
8485
85-
You don't have to stop a runnign effect before starting a new one.
86-
87-
Available effects are in the "Effects" directory. Creating your own is done by creating an effect class:
88-
89-
```C++
90-
// this effect will light a random LED, delay and turn off
91-
class MyNewEffect : public WS2811Effect {
92-
public:
93-
explicit FadeColours(int delay) :
94-
_delay(delay) {}
95-
void run(WS2811* strip, size_t numLeds) {
96-
strip->clearAll(); // clear all, we don't know the current state...
97-
strip->show();
98-
strip->setPixel(random(0, numLeds), 0, 255, 0);
99-
strip->show();
100-
delay(_delay);
101-
}
102-
103-
private:
104-
int _delay;
105-
};
106-
```
107-
108-
using this effect in your code as follows:
109-
110-
```C++
111-
MyNewEffect myNewEffect(500); // make sure it doesn't go out of scope
112-
ws2811.startEffect(&myNewEffect);
113-
```
86+
You don't have to stop a running effect before starting a new one. The effect stops immediately and does not wait for it's routine to complete.
11487
11588
# Credits
11689
11790
The RMT driver for WS2811/WS2812 comes from @nkolban [https://github.com/nkolban/esp32-snippets](https://github.com/nkolban/esp32-snippets).
118-
Effects are heavily inspired and copied from [https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/](https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/)
91+
Effects are heavily inspired and/or copied from [https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/](https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/)

examples/allEffects/allEffects.ino

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include <esp32WS2811.h>
77

8-
BlinkLed signal({0, 127, 0}, 1, 500);
9-
108
WS2811 ws2811(18, 50);
119
std::vector<WS2811Effect*> effects;
1210
Ticker timer;
@@ -22,20 +20,17 @@ void setup() {
2220
Serial.println("Booting");
2321

2422
// add effects
25-
effects.push_back(new TwinkleRandom(10, 1000));
26-
effects.push_back(new RandomColours(1000));
27-
effects.push_back(new RandomColours(100));
28-
effects.push_back(new FadeColours(100, 1000));
29-
effects.push_back(new LarsonScanner({255, 0, 0}, 4, 10, 5));
30-
effects.push_back(new SnowSparkle({20, 17, 10}, 3, 100, 500));
23+
effects.push_back(new Circus(1000));
24+
effects.push_back(new SnowSparkle({82, 56, 13}, 3, 100, 500));
3125

3226
// output enable level shifter
3327
pinMode(23, OUTPUT);
3428
digitalWrite(23, HIGH);
3529

30+
// start led strip
3631
ws2811.begin();
37-
ws2811.startEffect(&signal);
3832

33+
// effect starts in 15 seconds...
3934
timer.attach(15, nextEffect);
4035
}
4136

keywords.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55
WS2811 KEYWORD1
66

7-
TwinkleRandom KEYWORD1
87
RandomColours KEYWORD1
9-
FadeColours KEYWORD1
10-
LarsonScanner KEYWORD1
11-
BlinkLed KEYWORD1
128

139
#######################################
1410
# Methods and Functions (KEYWORD2)
1511
#######################################
1612

1713
begin KEYWORD2
14+
numLeds KEYWORD2
1815
show KEYWORD2
1916
setPixel KEYWORD2
17+
getPixel KEYWORD2
2018
clearAll KEYWORD2
2119
setAll KEYWORD2
22-
setBrightness KEYWORD2
2320
startEffect KEYWORD2
2421
stopEffect KEYWORD2
2522

2623
#######################################
2724
# Constants (LITERAL1)
2825
#######################################
2926

30-
#CONSTANT LITERAL2
27+
28+
#######################################
29+
# Constants (LITERAL2)
30+
#######################################

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "esp32WS2811oWiFi",
2+
"name": "esp32WS2811",
33
"version": "0.1.0",
44
"keywords": "Arduino, esp32, WS2811, WS2812, RMT",
55
"description": "Arduino library for ESP32 to drive WS2811 LEDs using the RMT peripheral",

src/Effects/BlinkLed.h

-64
This file was deleted.

src/Effects/RandomColours.cpp renamed to src/Effects/Circus.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
2323
*/
2424

25-
#include "RandomColours.h"
25+
#include "Circus.h"
2626

27-
RandomColours::RandomColours(uint32_t delay) :
28-
_delay(delay) {}
27+
Circus::Circus(uint32_t interval) :
28+
_interval(interval) {}
2929

30-
void RandomColours::run(WS2811* ws2811, size_t numLeds) {
31-
Colour colour;
30+
void Circus::_setup() {
31+
_ledstrip->clearAll();
32+
_ledstrip->show();
33+
}
34+
35+
void Circus::_loop() {
36+
size_t numLeds = _ledstrip->numLeds();
3237
for (size_t i = 0; i < numLeds; ++i) {
33-
colour = colours[random(0, 13)];
34-
ws2811->setPixel(i, colour.red, colour.green, colour.blue);
38+
_ledstrip->setPixel(i, colours[random(0, 12)]);
3539
}
36-
ws2811->show();
37-
delay(_delay);
40+
_ledstrip->show();
41+
delay(_interval);
3842
}

src/Effects/BlinkLed.cpp renamed to src/Effects/Circus.h

+18-15
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
2323
*/
2424

25-
#include "BlinkLed.h"
26-
27-
BlinkLed::BlinkLed(Colour colour, size_t pixel, uint32_t delay) :
28-
_colour(colour),
29-
_pixel(pixel),
30-
_delay(delay) {}
31-
32-
void BlinkLed::run(WS2811* ws2811, size_t numLeds) {
33-
ws2811->setAll(0, 0, 0);
34-
ws2811->show();
35-
delay(_delay);
36-
ws2811->setPixel(0, _colour.red, _colour.green, _colour.blue);
37-
ws2811->show();
38-
delay(_delay);
39-
}
25+
#pragma once
26+
27+
#include <stddef.h>
28+
#include <Arduino.h> // millis, random...
29+
#include "Effect.h"
30+
#include "Colour.h"
31+
32+
class Circus : public WS2811Effect {
33+
public:
34+
explicit Circus(uint32_t interval);
35+
36+
private:
37+
void _setup();
38+
void _loop();
39+
40+
private:
41+
uint32_t _interval;
42+
};

src/Effects/Colour.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ Colour::Colour(uint8_t r, uint8_t g, uint8_t b) :
3434
green(g),
3535
blue(b) {}
3636

37-
Colour::Colour(const Colour& c) {
38-
red = c.red;
39-
green = c.green;
40-
blue = c.blue;
41-
}
42-
4337
Colour colours[] {
4438
{0xFF, 0x00, 0x00}, /// red
4539
{0x80, 0x00, 0x00}, /// maroon

src/Effects/Colour.h

-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ class Colour {
5353
*/
5454
Colour(uint8_t r, uint8_t g, uint8_t b);
5555

56-
/**
57-
* @brief Copy constructor.
58-
*
59-
* @param c Colour to be copied
60-
*/
61-
Colour(const Colour& c);
6256
uint8_t red; ///< red value, 0-255
6357
uint8_t green; ///< green value, 0-255
6458
uint8_t blue; ///< blue value, 0-255

src/Effects/Effect.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Effect.h"
2+
3+
WS2811Effect::WS2811Effect() :
4+
_task(nullptr),
5+
_ledstrip(nullptr) {}
6+
7+
WS2811Effect::~WS2811Effect() {
8+
stop();
9+
}
10+
11+
void WS2811Effect::start(WS2811* ledstrip) {
12+
_ledstrip = ledstrip;
13+
xTaskCreate((TaskFunction_t)&_effectTask, "effectTask", 2048, this, 1, &_task);
14+
}
15+
16+
void WS2811Effect::stop() {
17+
if (!_task) return;
18+
vTaskDelete(_task);
19+
}
20+
21+
void WS2811Effect::_effectTask(WS2811Effect* e) {
22+
e->_setup();
23+
while(1) {
24+
e->_loop();
25+
}
26+
vTaskDelete(nullptr);
27+
}

0 commit comments

Comments
 (0)