FastLED 3.7.8
Loading...
Searching...
No Matches
SmartMatrix.ino
Go to the documentation of this file.
1
4
5/* This example demos a rectangular LED matrix with moving noise.
6 It requires the SmartMatrix library in addition to FastLED.
7 This SmartMatrix library is only available on Teensy boards at the moment.
8 It can be found at https://github.com/pixelmatix/SmartMatrix
9*/
10#include <SmartMatrix.h>
11#include <FastLED.h>
12
13#define kMatrixWidth 32
14#define kMatrixHeight 32
15const bool kMatrixSerpentineLayout = false;
16
17#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
18
19CRGB leds[kMatrixWidth * kMatrixHeight];
20
21
22uint16_t XY( uint8_t x, uint8_t y)
23{
24 uint16_t i;
25
26 if( kMatrixSerpentineLayout == false) {
27 i = (y * kMatrixWidth) + x;
28 }
29
30 if( kMatrixSerpentineLayout == true) {
31 if( y & 0x01) {
32 // Odd rows run backwards
33 uint8_t reverseX = (kMatrixWidth - 1) - x;
34 i = (y * kMatrixWidth) + reverseX;
35 } else {
36 // Even rows run forwards
37 i = (y * kMatrixWidth) + x;
38 }
39 }
40
41 return i;
42}
43
44// The 32bit version of our coordinates
45static uint16_t x;
46static uint16_t y;
47static uint16_t z;
48
49// We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
50// use the z-axis for "time". speed determines how fast time moves forward. Try
51// 1 for a very slow moving effect, or 60 for something that ends up looking like
52// water.
53// uint16_t speed = 1; // almost looks like a painting, moves very slowly
54uint16_t speed = 20; // a nice starting speed, mixes well with a scale of 100
55// uint16_t speed = 33;
56// uint16_t speed = 100; // wicked fast!
57
58// Scale determines how far apart the pixels in our noise matrix are. Try
59// changing these values around to see how it affects the motion of the display. The
60// higher the value of scale, the more "zoomed out" the noise iwll be. A value
61// of 1 will be so zoomed in, you'll mostly see solid colors.
62
63// uint16_t scale = 1; // mostly just solid colors
64// uint16_t scale = 4011; // very zoomed out and shimmery
65uint16_t scale = 31;
66
67// This is the array that we keep our computed noise values in
68uint8_t noise[kMatrixWidth][kMatrixHeight];
69
70void setup() {
71 // uncomment the following lines if you want to see FPS count information
72 // Serial.begin(38400);
73 // Serial.println("resetting!");
74 delay(3000);
75 FastLED.addLeds<SMART_MATRIX>(leds,NUM_LEDS);
77
78 // Initialize our coordinates to some random values
79 x = random16();
80 y = random16();
81 z = random16();
82
83 // Show off smart matrix scrolling text
84 pSmartMatrix->setScrollMode(wrapForward);
85 pSmartMatrix->setScrollColor({0xff, 0xff, 0xff});
86 pSmartMatrix->setScrollSpeed(15);
87 pSmartMatrix->setScrollFont(font6x10);
88 pSmartMatrix->scrollText("Smart Matrix & FastLED", -1);
89 pSmartMatrix->setScrollOffsetFromEdge(10);
90}
91
92// Fill the x/y array of 8-bit noise values using the inoise8 function.
93void fillnoise8() {
94 for(int i = 0; i < kMatrixWidth; i++) {
95 int ioffset = scale * i;
96 for(int j = 0; j < kMatrixHeight; j++) {
97 int joffset = scale * j;
98 noise[i][j] = inoise8(x + ioffset,y + joffset,z);
99 }
100 }
101 z += speed;
102}
103
104
105void loop() {
106 static uint8_t circlex = 0;
107 static uint8_t circley = 0;
108
109 static uint8_t ihue=0;
110 fillnoise8();
111 for(int i = 0; i < kMatrixWidth; i++) {
112 for(int j = 0; j < kMatrixHeight; j++) {
113 // We use the value at the (i,j) coordinate in the noise
114 // array for our brightness, and the flipped value from (j,i)
115 // for our pixel's hue.
116 leds[XY(i,j)] = CHSV(noise[j][i],255,noise[i][j]);
117
118 // You can also explore other ways to constrain the hue used, like below
119 // leds[XY(i,j)] = CHSV(ihue + (noise[j][i]>>2),255,noise[i][j]);
120 }
121 }
122 ihue+=1;
123
124 // N.B. this requires SmartMatrix modified w/triple buffering support
125 pSmartMatrix->fillCircle(circlex % 32,circley % 32,6,CRGB(CHSV(ihue+128,255,255)));
126 circlex += random16(2);
127 circley += random16(2);
128 FastLED.show();
129 // delay(10);
130}
void * pSmartMatrix
Pointer to the matrix object when using the Smart Matrix Library.
Definition FastLED.cpp:19
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:21
central include file for FastLED, defines the CFastLED class/object
void setBrightness(uint8_t scale)
Set the global brightness scaling.
Definition FastLED.h:715
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:59
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:47
uint16_t XY(uint8_t, uint8_t)
Forward declaration of the function "XY" which must be provided by the application for use in two-dim...
Definition Noise.ino:22
uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z)
8-Bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:484
LIB8STATIC uint16_t random16()
Generate a 16-bit random number.
Definition random8.h:50
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:11
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:25