//https://editor.soulmatelights.com/gallery/1584-watercolor
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include <FastLED.h>
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 32;
#define DATA_PIN 3
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 255
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds[NUM_LEDS + 1];
uint16_t XY (uint8_t x, uint8_t y) {
// any out of bounds address maps to the first hidden pixel
if ( (x >= kMatrixWidth) || (y >= kMatrixHeight) ) {
return (NUM_LEDS);
}
uint16_t nop = NUM_LEDS;
const uint16_t XYTable[] = {
nop, nop, nop, 288, 289, 290, 291, 292, 293, 294, 295, 296, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, 5, 6, 7, 8, nop, nop, nop,
nop, nop, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, nop, nop,
nop, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, nop,
321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, nop, nop, nop, nop, nop, nop, nop, nop, nop, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 192, 193, 194, 195, 196, 197, 198, 199, 200, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
nop, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, nop,
nop, nop, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, nop, nop,
nop, nop, nop, 375, 376, 377, 378, 379, 380, 381, 382, 383, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 87, 88, 89, 90, 91, 92, 93, 94, 95, nop, nop, nop,
nop, nop, nop, 384, 385, 386, 387, 388, 389, 390, 391, 392, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 96, 97, 98, 99, 100, 101, 102, 103, 104, nop, nop, nop,
nop, nop, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, nop, nop,
nop, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, nop,
417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 279, 280, 281, 282, 283, 284, 285, 286, 287, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, nop, nop, nop, nop, nop, nop, nop, nop, nop, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158,
nop, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, nop,
nop, nop, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, nop, nop,
nop, nop, nop, 471, 472, 473, 474, 475, 476, 477, 478, 479, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 183, 184, 185, 186, 187, 188, 189, 190, 191, nop, nop, nop
};
uint16_t i = (y * kMatrixWidth) + x;
uint16_t j = XYTable[i];
// return j;
return i; // for testing with a raster order rectangular matrix
}
float x[kMatrixWidth / 2];
float y[kMatrixWidth / 2];
byte hue[kMatrixWidth / 2];
float radius[kMatrixWidth / 2];
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
// draw a filled circle using a signed-distance-field
void sdf_circle(float ball_x, float ball_y, float ball_radius, CRGB colour) {
// transition affects how many pixels wide the soft edge of the ball is
float ball_transition = 1.f;
float total_radius = ball_radius + ball_transition;
// find the smallest rectangle to cover the ball
int16_t minx = ceil(ball_x - total_radius);
int16_t maxx = ceil(ball_x + total_radius);
int16_t miny = ceil(ball_y - total_radius);
int16_t maxy = ceil(ball_y + total_radius);
// if it's entirely off-screen, bail out early
if (maxx < 0 || maxy < 0 || minx >= kMatrixWidth || miny >= kMatrixHeight)
return;
// clamp the rectangle to valid screen coordinates
minx = MAX(minx, 0);
maxx = MIN(maxx, kMatrixWidth);
miny = MAX(miny, 0);
maxy = MIN(maxy, kMatrixHeight);
// account for the ball overlapping the left or top edges of the screen
float offset_y = -total_radius,
offset_x_init = -total_radius;
if (ball_x - total_radius < 0) offset_x_init -= ball_x - total_radius;
if (ball_y - total_radius < 0) offset_y -= ball_y - total_radius;
// visit each pixel in the rectangle
float max_square = total_radius * total_radius;
for (uint16_t screen_y = miny; screen_y < maxy; screen_y++) {
float offset_x = offset_x_init;
for (uint16_t screen_x = minx; screen_x < maxx; screen_x++) {
CRGB dimmed_colour = colour;
float sum_squares = offset_x * offset_x + offset_y * offset_y;
if (sum_squares <= max_square) { // inside the circle?
float c = 0;
float distance = sqrtf(sum_squares) - ball_radius;
if (distance > 0 && distance <= ball_transition) {
c = distance / ball_transition;
}
fadeToBlackBy(&dimmed_colour, 1, 255.f * c);
leds[XY(screen_x, screen_y)] += dimmed_colour;
}
offset_x++;
}
offset_y++;
}
}
bool iSsetup = 1;
void draw() {
fadeToBlackBy(leds, NUM_LEDS, 24);
// FastLED.clear();
if (iSsetup) {
for (byte i = 0; i < kMatrixWidth / 2; i++) {
x[i] = random(0, (kMatrixWidth - 1) * 10) / 10.f;
y[i] = random8(0, kMatrixHeight + 8);
hue[i] = random8();
radius[i] = random8(6, kMatrixWidth) / 7.f;
}
iSsetup = 0;
}
float ms = millis();
for (byte i = 0; i < 7 /*kMatrixWidth / 2*/; i++) {
y[i] -= inoise8(ms, i * 100) / 255.f;
x[i] += inoise8(ms, 0, i * 100) / 63.75f - 2.f;
if (y[i] < -radius[i] ) {
radius[i] = random8(16, kMatrixWidth) / 7.f;
y[i] = kMatrixHeight + radius[i] + 1;
x[i] = random(kMatrixWidth * 10) / 10.f;
}
sdf_circle(x[i], y[i], radius[i], CHSV(hue[i], 255, 96));
}
// blur2d(leds, kMatrixWidth, kMatrixHeight, 128);
}
void loop() {
draw();
FastLED.show();
if (1) { // show frame timings
static uint32_t fps_ms;
static uint8_t frame;
if (++frame == 50) {
uint32_t time_ms = millis() - fps_ms;
Serial.print(time_ms / float(frame));
Serial.print("ms\t");
Serial.print(frame * 1000.f / time_ms);
Serial.println("FPS");
fps_ms = millis();
frame = 0;
}
}
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
neopixels:DOUT
neopixels:VDD
neopixels:VSS
neopixels:DIN