Skip to content

Commit 95a53a9

Browse files
added plasma demo to examples
Added a Plasma demo to the examples set. Fixed a bug in the new color management and added a library specific fmod implementation to work around a bug in the ESP8266 standard libraries.
1 parent ecaa513 commit 95a53a9

File tree

9 files changed

+225
-15
lines changed

9 files changed

+225
-15
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Plasma
3+
*
4+
* This is a demonstration of the plasma algorithm displayed on an RGB LED matrix.
5+
* The COLOR_SCHEME global variable allows you to easily change between some pre-made
6+
* color schemes. Using the patterm of the demonstration color schemes, it should be
7+
* easy to add others.
8+
*
9+
* The algorithm for this demo was sourced from here:
10+
*
11+
* https://www.bidouille.org/prog/plasma
12+
*
13+
*/
14+
#include <RGBLEDMatrix.h>
15+
#include <RGBColor.h>
16+
#include <RGBImage.h>
17+
18+
/* Color Schemes
19+
*
20+
* 1 - Full rainbow gradients in RGB
21+
* 2 - Red, Orange, white gradients
22+
* 3 - gray scale
23+
*
24+
*/
25+
const int COLOR_SCHEME = 1;
26+
27+
const float SPACE_STRETCH_FACTOR = 5.0;
28+
const float TIME_DILATION = 5.0;
29+
30+
RGBLEDMatrix leds(10,10);
31+
32+
int mapSineToRange( float sineValue, int rangeMax ) {
33+
return rangeMax*(sineValue+1.0)/2.0;
34+
}
35+
void drawPlasma( unsigned long counter ) {
36+
float utime = float(counter)/TIME_DILATION;
37+
38+
leds.startDrawing();
39+
for (int col = 0; col < leds.columns(); col++ ) {
40+
float x = ((float)col/((float)leds.columns()*SPACE_STRETCH_FACTOR)) - 0.5;
41+
42+
for (int row = 0; row < leds.rows(); row++ ) {
43+
float y = ((float)row/((float)leds.rows()*SPACE_STRETCH_FACTOR)) - 0.5;
44+
45+
float v1 = sin(x*10.0+utime);
46+
float v2 = sin(10.0*(x*sin(utime/2.0) + y*cos(utime/3.0)) + utime);
47+
48+
float cx = x + 0.5*sin(utime/5.0);
49+
float cy = y + 0.5*cos(utime/3.0);
50+
float v3 = sin( sqrt(100.0*(cx*cx + cy*cy) + 1.0) + utime );
51+
52+
float v = v1+v2+v3;
53+
54+
int r, g, b;
55+
switch (COLOR_SCHEME) {
56+
default:
57+
case 1:
58+
r = mapSineToRange(sin(v*PI), 255);
59+
g = mapSineToRange(sin(v*PI + 2.0*PI/3.0), 255);
60+
b = mapSineToRange(sin(v*PI + 4.0*PI/3.0), 255);
61+
break;
62+
case 2:
63+
r = 255;
64+
g = mapSineToRange(cos(v*PI), 255);
65+
b = mapSineToRange(sin(v*PI), 255);
66+
break;
67+
case 3:
68+
r = g = b = mapSineToRange(sin(v*5.0*PI), 255);
69+
break;
70+
}
71+
72+
RGBColorType color = RGBColor::fromRGB(r, g, b);
73+
74+
leds.image().pixel(row, col) = color;
75+
}
76+
}
77+
leds.stopDrawing();
78+
}
79+
80+
void setup() {
81+
leds.setup();
82+
drawPlasma(0);
83+
leds.startScanning();
84+
}
85+
86+
unsigned long loopCounter = 0;
87+
unsigned long timeCount = 0;
88+
89+
const unsigned long loopMod = 2000;
90+
91+
void loop() {
92+
leds.loop();
93+
loopCounter++;
94+
95+
if (loopCounter == loopMod) {
96+
timeCount++;
97+
drawPlasma(timeCount);
98+
loopCounter = 0;
99+
}
100+
}

examples/plasma-8x8/plasma-8x8.ino

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Plasma
3+
*
4+
* This is a demonstration of the plasma algorithm displayed on an RGB LED matrix.
5+
* The COLOR_SCHEME global variable allows you to easily change between some pre-made
6+
* color schemes. Using the patterm of the demonstration color schemes, it should be
7+
* easy to add others.
8+
*
9+
* The algorithm for this demo was sourced from here:
10+
*
11+
* https://www.bidouille.org/prog/plasma
12+
*
13+
*/
14+
#include <RGBLEDMatrix.h>
15+
#include <RGBColor.h>
16+
#include <RGBImage.h>
17+
18+
/* Color Schemes
19+
*
20+
* 1 - Full rainbow gradients in RGB
21+
* 2 - Red, Orange, white gradients
22+
* 3 - gray scale
23+
*
24+
*/
25+
const int COLOR_SCHEME = 1;
26+
27+
const float SPACE_STRETCH_FACTOR = 4.0;
28+
const float TIME_DILATION = 5.0;
29+
30+
RGBLEDMatrix leds(8,8);
31+
32+
int mapSineToRange( float sineValue, int rangeMax ) {
33+
return rangeMax*(sineValue+1.0)/2.0;
34+
}
35+
void drawPlasma( unsigned long counter ) {
36+
float utime = float(counter)/TIME_DILATION;
37+
38+
leds.startDrawing();
39+
for (int col = 0; col < leds.columns(); col++ ) {
40+
float x = ((float)col/((float)leds.columns()*SPACE_STRETCH_FACTOR)) - 0.5;
41+
42+
for (int row = 0; row < leds.rows(); row++ ) {
43+
float y = ((float)row/((float)leds.rows()*SPACE_STRETCH_FACTOR)) - 0.5;
44+
45+
float v1 = sin(x*10.0+utime);
46+
float v2 = sin(10.0*(x*sin(utime/2.0) + y*cos(utime/3.0)) + utime);
47+
48+
float cx = x + 0.5*sin(utime/5.0);
49+
float cy = y + 0.5*cos(utime/3.0);
50+
float v3 = sin( sqrt(100.0*(cx*cx + cy*cy) + 1.0) + utime );
51+
52+
float v = v1+v2+v3;
53+
54+
int r, g, b;
55+
switch (COLOR_SCHEME) {
56+
default:
57+
case 1:
58+
r = mapSineToRange(sin(v*PI), 255);
59+
g = mapSineToRange(sin(v*PI + 2.0*PI/3.0), 255);
60+
b = mapSineToRange(sin(v*PI + 4.0*PI/3.0), 255);
61+
break;
62+
case 2:
63+
r = 255;
64+
g = mapSineToRange(cos(v*PI), 255);
65+
b = mapSineToRange(sin(v*PI), 255);
66+
break;
67+
case 3:
68+
r = g = b = mapSineToRange(sin(v*5.0*PI), 255);
69+
break;
70+
}
71+
72+
RGBColorType color = RGBColor::fromRGB(r, g, b);
73+
74+
leds.image().pixel(row, col) = color;
75+
}
76+
}
77+
leds.stopDrawing();
78+
}
79+
80+
void setup() {
81+
leds.setup();
82+
drawPlasma(0);
83+
leds.startScanning();
84+
}
85+
86+
unsigned long loopCounter = 0;
87+
unsigned long timeCount = 0;
88+
89+
const unsigned long loopMod = 2000;
90+
91+
void loop() {
92+
leds.loop();
93+
loopCounter++;
94+
95+
if (loopCounter == loopMod) {
96+
timeCount++;
97+
drawPlasma(timeCount);
98+
loopCounter = 0;
99+
}
100+
}

keywords.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#######################################
88

99
BaseLEDMatrix KEYWORD1
10-
RGBColorType KEYWORD1
1110
Frame KEYWORD1
1211
Glyph KEYWORD1
1312
GlyphBase KEYWORD1
@@ -20,6 +19,8 @@ MutableRGBImage KEYWORD1
2019
RGBAnimation KEYWORD1
2120
RGBAnimationBase KEYWORD1
2221
RGBAnimationSequence KEYWORD1
22+
RGBColor KEYWORD1
23+
RGBColorType KEYWORD1
2324
RGBImage KEYWORD1
2425
RGBImageBase KEYWORD1
2526
RGBLEDBitLayout KEYWORD1
@@ -34,6 +35,7 @@ TimerAction KEYWORD1
3435
#######################################
3536

3637
memcpy_smart KEYWORD2
38+
fmod KEYWORD2
3739

3840
#######################################
3941
# Constants (LITERAL1)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Shift Register LED Matrix Lib
2-
version=0.9.0
2+
version=1.0.0
33
author=Michael Kamprath <michael@kamprath.net>
44
maintainer=Michael Kamprath <michael@kamprath.net>
55
sentence=A driver for LED matrices that use shift registers to control rows and columns.

src/Glyph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ MutableGlyph::MutableGlyph(
159159

160160
MutableGlyph::MutableGlyph( const GlyphBase& other )
161161
: GlyphBase(other),
162-
_bits( (bool*)memcpy_smart(
162+
_bits( (bool*)SRLEDMatrixUtils::memcpy_smart(
163163
new bool[(other.rows()*other.columns())],
164164
other.bits(),
165165
(other.rows()*other.columns())*sizeof(bool),

src/RGBColor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <math.h>
2020
#include "RGBColor.h"
21+
#include "SRLEDMatrixUtils.h"
2122

2223
#if TWELVE_BIT_COLOR
2324

@@ -31,7 +32,7 @@
3132

3233
#endif
3334

34-
RGBColorType RGBColor::fromRGB(char red, char green, char blue) {
35+
RGBColorType RGBColor::fromRGB(int red, int green, int blue) {
3536
RGBColorType r = red/RGB_SCALE_VALUE;
3637
RGBColorType g = green/RGB_SCALE_VALUE;
3738
RGBColorType b = blue/RGB_SCALE_VALUE;
@@ -42,7 +43,7 @@ RGBColorType RGBColor::fromRGB(char red, char green, char blue) {
4243
}
4344

4445
RGBColorType RGBColor::fromHSV(float hue, float saturation, float value) {
45-
float H = ( hue < 0 ? ( fmod(hue,360) + 360.0) : fmod(hue,360) );
46+
float H = ( hue < 0 ? ( SRLEDMatrixUtils::fmod(hue,360) + 360.0) : SRLEDMatrixUtils::fmod(hue,360) );
4647

4748
float C = value*saturation;
4849
float X = C*(1 - fabs(float(int(H/60.0)%2-1)) );
@@ -85,7 +86,7 @@ RGBColorType RGBColor::fromHSV(float hue, float saturation, float value) {
8586
}
8687

8788
RGBColorType RGBColor::fromHSL(float hue, float saturation, float lightness) {
88-
float H = ( hue < 0 ? ( fmod(hue,360) + 360.0) : fmod(hue,360) );
89+
float H = ( hue < 0 ? ( SRLEDMatrixUtils::fmod(hue,360) + 360.0) : SRLEDMatrixUtils::fmod(hue,360) );
8990

9091
float C = (1.0 - fabs(2.0*lightness - 1.0))*saturation;
9192
float X = C*(1 - fabs(float(int(H/60.0)%2-1)) );

src/RGBColor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ namespace RGBColor {
139139
* @param blue blue value between 0 and 255
140140
* @return A RGBColorType value that closely approximates the passed color.
141141
*/
142-
RGBColorType fromRGB(char red, char green, char blue);
142+
RGBColorType fromRGB(int red, int green, int blue);
143143

144144

145145
/**

src/SRLEDMatrixUtils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "SRLEDMatrixUtils.h"
2525

2626

27-
void * memcpy_smart(
27+
void * SRLEDMatrixUtils::memcpy_smart(
2828
void * dest,
2929
const void * src,
3030
size_t n,
@@ -37,3 +37,7 @@ void * memcpy_smart(
3737
return memcpy(dest, src, n);
3838
}
3939
}
40+
41+
float SRLEDMatrixUtils::fmod(float x, float y) {
42+
return x - y * floor(x/y);
43+
}

src/SRLEDMatrixUtils.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717
// along with Shift Register LED Matrix Project. If not, see <http://www.gnu.org/licenses/>.
1818
#ifndef __SRLEDMATRIXUTILS_H__
1919
#define __SRLEDMATRIXUTILS_H__
20+
#include <Arduino.h>
2021

22+
namespace SRLEDMatrixUtils {
23+
void * memcpy_smart(
24+
void * dest,
25+
const void * src,
26+
size_t n,
27+
bool isSrcProgMem
28+
);
2129

22-
void * memcpy_smart(
23-
void * dest,
24-
const void * src,
25-
size_t n,
26-
bool isSrcProgMem
27-
);
28-
30+
float fmod(float value, float modulo);
2931

32+
}
3033
#endif // __RGBLEDMATRIXUTILS_H__
3134

0 commit comments

Comments
 (0)