-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathFastLed_Functions.ino
More file actions
134 lines (112 loc) · 6.65 KB
/
FastLed_Functions.ino
File metadata and controls
134 lines (112 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Modified example copied from FastLED 3.0 Branch - originally written by Daniel Garcia
* This example shows how to use some of FastLED's functions with the SmartMatrix Library
* using the SmartMatrix buffers directly instead of FastLED's buffers.
* FastLED's dithering and color balance features can't be used this way, but SmartMatrix can draw in
* 36-bit color and so dithering may not provide much advantage.
*
* This example requires FastLED 3.0 or higher. If you are having trouble compiling, see
* the troubleshooting instructions here:
* https://github.com/pixelmatix/SmartMatrix/#external-libraries
*/
// uncomment one line to select your MatrixHardware configuration - configuration header needs to be included before <SmartMatrix.h>
//#include <MatrixHardware_Teensy3_ShieldV4.h> // SmartLED Shield for Teensy 3 (V4)
//#include <MatrixHardware_Teensy4_ShieldV5.h> // SmartLED Shield for Teensy 4 (V5)
//#include <MatrixHardware_Teensy3_ShieldV1toV3.h> // SmartMatrix Shield for Teensy 3 V1-V3
//#include <MatrixHardware_Teensy4_ShieldV4Adapter.h> // Teensy 4 Adapter attached to SmartLED Shield for Teensy 3 (V4)
//#include <MatrixHardware_ESP32_V0.h> // This file contains multiple ESP32 hardware configurations, edit the file to define GPIOPINOUT (or add #define GPIOPINOUT with a hardcoded number before this #include)
//#include "MatrixHardware_Custom.h" // Copy an existing MatrixHardware file to your Sketch directory, rename, customize, and you can include it like this
#include <SmartMatrix.h>
#include <FastLED.h>
#define COLOR_DEPTH 24 // Choose the color depth used for storing pixels in the layers: 24 or 48 (24 is good for most sketches - If the sketch uses type `rgb24` directly, COLOR_DEPTH must be 24)
const uint16_t kMatrixWidth = 32; // Set to the width of your display, must be a multiple of 8
const uint16_t kMatrixHeight = 32; // Set to the height of your display
const uint8_t kRefreshDepth = 36; // Tradeoff of color quality vs refresh rate, max brightness, and RAM usage. 36 is typically good, drop down to 24 if you need to. On Teensy, multiples of 3, up to 48: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48. On ESP32: 24, 36, 48
const uint8_t kDmaBufferRows = 4; // known working: 2-4, use 2 to save RAM, more to keep from dropping frames and automatically lowering refresh rate. (This isn't used on ESP32, leave as default)
const uint8_t kPanelType = SM_PANELTYPE_HUB75_32ROW_MOD16SCAN; // Choose the configuration that matches your panels. See more details in MatrixCommonHub75.h and the docs: https://github.com/pixelmatix/SmartMatrix/wiki
const uint32_t kMatrixOptions = (SM_HUB75_OPTIONS_NONE); // see docs for options: https://github.com/pixelmatix/SmartMatrix/wiki
const uint8_t kBackgroundLayerOptions = (SM_BACKGROUND_OPTIONS_NONE);
const uint8_t kScrollingLayerOptions = (SM_SCROLLING_OPTIONS_NONE);
SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions);
SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions);
SMARTMATRIX_ALLOCATE_SCROLLING_LAYER(scrollingLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kScrollingLayerOptions);
// The 32bit version of our coordinates
static uint16_t x;
static uint16_t y;
static uint16_t z;
// We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
// use the z-axis for "time". speed determines how fast time moves forward. Try
// 1 for a very slow moving effect, or 60 for something that ends up looking like
// water.
// uint16_t speed = 1; // almost looks like a painting, moves very slowly
uint16_t speed = 20; // a nice starting speed, mixes well with a scale of 100
// uint16_t speed = 33;
// uint16_t speed = 100; // wicked fast!
// Scale determines how far apart the pixels in our noise matrix are. Try
// changing these values around to see how it affects the motion of the display. The
// higher the value of scale, the more "zoomed out" the noise iwll be. A value
// of 1 will be so zoomed in, you'll mostly see solid colors.
// uint16_t scale = 1; // mostly just solid colors
// uint16_t scale = 4011; // very zoomed out and shimmery
uint16_t scale = 31;
// This is the array that we keep our computed noise values in
const uint16_t kMatrixLongestSide = (kMatrixWidth > kMatrixHeight) ? kMatrixWidth : kMatrixHeight;
uint8_t noise[kMatrixLongestSide][kMatrixLongestSide];
void setup() {
// Enable printing FPS count information
Serial.begin(115200);
matrix.addLayer(&backgroundLayer);
matrix.addLayer(&scrollingLayer);
matrix.begin();
// Wait for Serial to be ready
delay(1000);
backgroundLayer.setBrightness(128);
// Initialize our coordinates to some random values
x = random16();
y = random16();
z = random16();
// Show off smart matrix scrolling text
scrollingLayer.setMode(wrapForward);
scrollingLayer.setColor({0xff, 0xff, 0xff});
scrollingLayer.setSpeed(15);
scrollingLayer.setFont(font6x10);
scrollingLayer.start("SmartMatrix & FastLED", -1);
scrollingLayer.setOffsetFromTop((kMatrixHeight/2) - 5);
}
// Fill the x/y array of 8-bit noise values using the inoise8 function.
void fillnoise8() {
for(int i = 0; i < kMatrixLongestSide; i++) {
int ioffset = scale * i;
for(int j = 0; j < kMatrixLongestSide; j++) {
int joffset = scale * j;
noise[i][j] = inoise8(x + ioffset,y + joffset,z);
}
}
z += speed;
}
void loop() {
static uint8_t circlex = 0;
static uint8_t circley = 0;
// if sketch uses swapBuffers(false), wait to get a new backBuffer() pointer after the swap is done:
while(backgroundLayer.isSwapPending());
rgb24 *buffer = backgroundLayer.backBuffer();
static uint8_t ihue=0;
fillnoise8();
for(int i = 0; i < kMatrixWidth; i++) {
for(int j = 0; j < kMatrixHeight; j++) {
// We use the value at the (i,j) coordinate in the noise
// array for our brightness, and the flipped value from (j,i)
// for our pixel's hue.
buffer[kMatrixWidth*j + i] = (rgb24)CRGB(CHSV(noise[j][i],255,noise[i][j]));
// You can also explore other ways to constrain the hue used, like below
// buffer[kMatrixHeight*j + i] = CRGB(CHSV(ihue + (noise[j][i]>>2),255,noise[i][j]));
}
}
ihue+=1;
backgroundLayer.fillCircle(circlex % kMatrixWidth, circley % kMatrixHeight, 6, (rgb24)CRGB(CHSV(ihue+128,255,255)));
circlex += random16(2);
circley += random16(2);
// buffer is filled completely each time, use swapBuffers without buffer copy to save CPU cycles
backgroundLayer.swapBuffers(false);
matrix.countFPS(); // print the loop() frames per second to Serial
}