2019-05-29 22:49:54 +00:00
|
|
|
#include "effect_matrix.h"
|
2019-06-04 03:55:24 +00:00
|
|
|
#include "my_color_palettes.h"
|
2019-05-29 22:49:54 +00:00
|
|
|
#include "functions.h"
|
|
|
|
|
2019-06-19 20:22:03 +00:00
|
|
|
MatrixEffectColumn::MatrixEffectColumn(Window* win, uint8_t dir, bool rand) {
|
2019-06-07 04:24:16 +00:00
|
|
|
window = win;
|
2019-06-19 20:22:03 +00:00
|
|
|
_direction = dir;
|
|
|
|
_random_direction = rand;
|
|
|
|
restart(true);
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 20:22:03 +00:00
|
|
|
void MatrixEffectColumn::restart(bool completely_random) {
|
|
|
|
if (_random_direction) {
|
|
|
|
_direction = random8(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (completely_random) {
|
2019-09-25 04:32:35 +00:00
|
|
|
x = random8(window->width)<<8;
|
|
|
|
y = random8(window->height)<<8;
|
2019-06-19 20:22:03 +00:00
|
|
|
} else {
|
|
|
|
switch(_direction) {
|
|
|
|
case DIR_NORTH:
|
2019-09-25 04:32:35 +00:00
|
|
|
x = random8(window->width)<<8;
|
|
|
|
y = (window->height - 1)<<8;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
case DIR_EAST:
|
|
|
|
x = 0;
|
2019-09-25 04:32:35 +00:00
|
|
|
y = random8(window->height)<<8;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
case DIR_SOUTH:
|
2019-09-25 04:32:35 +00:00
|
|
|
x = random8(window->width)<<8;
|
2019-06-19 20:22:03 +00:00
|
|
|
y = 0;
|
|
|
|
break;
|
|
|
|
case DIR_WEST:
|
2019-09-25 04:32:35 +00:00
|
|
|
x = (window->width - 1)<<8;
|
|
|
|
y = random8(window->height)<<8;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 04:13:55 +00:00
|
|
|
length = random8(settings.effects.matrix.length_min, settings.effects.matrix.length_max);
|
2019-06-07 04:24:16 +00:00
|
|
|
running = true;
|
2019-10-02 04:13:55 +00:00
|
|
|
speed = random8(settings.effects.matrix.speed_min, settings.effects.matrix.speed_max);
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 04:29:32 +00:00
|
|
|
void MatrixEffectColumn::advance(uint16_t ms) {
|
2019-06-19 20:22:03 +00:00
|
|
|
switch(_direction) {
|
|
|
|
case DIR_NORTH:
|
2019-10-01 04:29:32 +00:00
|
|
|
y-=speed * ms;
|
2019-10-10 04:43:01 +00:00
|
|
|
if ((y>>8) + length < 0) running=false;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
case DIR_EAST:
|
2019-10-01 04:29:32 +00:00
|
|
|
x+=speed * ms;
|
2019-09-25 04:32:35 +00:00
|
|
|
if ((x>>8) - length > window->width) running=false;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
case DIR_SOUTH:
|
2019-10-01 04:29:32 +00:00
|
|
|
y+=speed * ms;
|
2019-09-25 04:32:35 +00:00
|
|
|
if ((y>>8) - length > window->height) running=false;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
case DIR_WEST:
|
2019-10-01 04:29:32 +00:00
|
|
|
x-=speed * ms;
|
2019-10-10 04:43:01 +00:00
|
|
|
if ((x>>8) + length < 0) running=false;
|
2019-06-19 20:22:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MatrixEffectColumn::draw() {
|
2019-09-25 04:32:35 +00:00
|
|
|
int16_t xdir = 0;
|
|
|
|
int16_t ydir = 0;
|
2019-06-19 20:22:03 +00:00
|
|
|
switch (_direction) {
|
2019-09-25 04:32:35 +00:00
|
|
|
case DIR_NORTH: ydir = 0xFF; break;
|
|
|
|
case DIR_EAST: xdir = -0xFF; break;
|
|
|
|
case DIR_SOUTH: ydir = -0xFF; break;
|
|
|
|
case DIR_WEST: xdir = 0xFF; break;
|
2019-06-19 20:22:03 +00:00
|
|
|
}
|
2019-06-07 04:24:16 +00:00
|
|
|
for(int i=0; i<length; i++) {
|
2019-06-16 10:43:05 +00:00
|
|
|
CRGB color = _getColor(i);
|
2019-09-25 04:32:35 +00:00
|
|
|
window->setSubPixel(x+(xdir*i), y+(ydir*i), &color);
|
2019-06-04 04:51:36 +00:00
|
|
|
}
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 04:29:32 +00:00
|
|
|
void MatrixEffectColumn::loop(uint16_t ms) {
|
2019-06-07 04:24:16 +00:00
|
|
|
if (!running) {
|
|
|
|
if (random8() < 20) {
|
|
|
|
// Start the column again.
|
2019-06-19 20:22:03 +00:00
|
|
|
restart(false);
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
2019-09-25 04:32:35 +00:00
|
|
|
} else {
|
2019-10-01 04:29:32 +00:00
|
|
|
advance(ms);
|
2019-09-25 04:32:35 +00:00
|
|
|
draw();
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 10:43:05 +00:00
|
|
|
CRGB MatrixEffectColumn::_getColor(uint8_t i) {
|
|
|
|
CRGB color;
|
|
|
|
if (i==0) {
|
|
|
|
color = CRGB(255, 255, 255);
|
|
|
|
} else {
|
|
|
|
color = CHSV(83, 255, 255 * (length - i) / length);
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRGB RainbowMatrixEffectColumn::_getColor(uint8_t i) {
|
|
|
|
CRGB color;
|
|
|
|
if (i==0) {
|
|
|
|
color = CRGB(255, 255, 255);
|
|
|
|
} else {
|
|
|
|
color = CHSV(255 * x / window->width, 255, 255 * (length - i) / length);
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRGB RandomMatrixEffectColumn::_getColor(uint8_t i) {
|
|
|
|
CRGB color;
|
|
|
|
//Serial.print("RandomMatrixEffectColumn::_getColor, hue="); Serial.println(_hue);
|
|
|
|
if (i==0) {
|
|
|
|
color = CRGB(255, 255, 255);
|
|
|
|
} else {
|
|
|
|
color = CHSV(_hue, 255, 255 * (length - i) / length);
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:22:03 +00:00
|
|
|
void RandomMatrixEffectColumn::restart(bool completely_random) {
|
|
|
|
MatrixEffectColumn::restart(completely_random);
|
2019-06-16 10:43:05 +00:00
|
|
|
_hue = random8();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-29 22:49:54 +00:00
|
|
|
boolean MatrixEffect::can_be_shown_with_clock() { return true; };
|
|
|
|
|
|
|
|
MatrixEffect::MatrixEffect() {
|
2019-06-18 16:09:05 +00:00
|
|
|
_columns = new MatrixEffectColumn* [window->width];
|
2019-06-19 20:22:03 +00:00
|
|
|
for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, MatrixEffectColumn::DIR_SOUTH);
|
2019-06-16 10:43:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:00:18 +00:00
|
|
|
RandomMatrixEffect::RandomMatrixEffect() {
|
2019-10-04 10:08:01 +00:00
|
|
|
// No need to initialize _columns, because that will have been done by ctor of MatrixEffect.
|
|
|
|
for (int i=0; i<window->width; i++) {
|
|
|
|
delete _columns[i];
|
|
|
|
_columns[i] = new RandomMatrixEffectColumn(window, random8(4), true);
|
|
|
|
}
|
2019-06-16 10:43:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:00:18 +00:00
|
|
|
RainbowMatrixEffect::RainbowMatrixEffect() {
|
2019-10-04 10:08:01 +00:00
|
|
|
// No need to initialize _columns, because that will have been done by ctor of MatrixEffect.
|
|
|
|
for (int i=0; i<window->width; i++) {
|
2019-10-04 13:58:56 +00:00
|
|
|
delete _columns[i];
|
2019-10-04 10:08:01 +00:00
|
|
|
_columns[i] = new RainbowMatrixEffectColumn(window, MatrixEffectColumn::DIR_SOUTH);
|
|
|
|
}
|
2019-06-15 12:17:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:09:05 +00:00
|
|
|
MatrixEffect::~MatrixEffect() {
|
2019-06-16 10:43:05 +00:00
|
|
|
for (int i=0; i<window->width; i++) {
|
|
|
|
delete _columns[i];
|
|
|
|
}
|
2019-06-15 12:17:06 +00:00
|
|
|
delete[] _columns;
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 04:29:32 +00:00
|
|
|
void MatrixEffect::loop(uint16_t ms) {
|
2019-06-11 17:48:09 +00:00
|
|
|
window->clear();
|
2019-10-01 04:29:32 +00:00
|
|
|
for (int i=0; i<window->width; i++) _columns[i]->loop(ms);
|
2019-05-29 22:49:54 +00:00
|
|
|
}
|