#include "effect_matrix.h" #include "my_color_palettes.h" #include "functions.h" MatrixEffectColumn::MatrixEffectColumn(Window* win, uint8_t dir, bool rand) { window = win; _direction = dir; _random_direction = rand; restart(true); } void MatrixEffectColumn::restart(bool completely_random) { if (_random_direction) { _direction = random8(4); } if (completely_random) { x = random8(window->width); y = random8(window->height); } else { switch(_direction) { case DIR_NORTH: x = random8(window->width); y = window->height - 1; break; case DIR_EAST: x = 0; y = random8(window->height); break; case DIR_SOUTH: x = random8(window->width); y = 0; break; case DIR_WEST: x = window->width - 1; y = random8(window->height); break; } } length = random8(EFFECT_MATRIX_LENGTH_MIN, EFFECT_MATRIX_LENGTH_MAX); running = true; speed = random8(EFFECT_MATRIX_SPEED_MIN, EFFECT_MATRIX_SPEED_MAX); } void MatrixEffectColumn::advance() { switch(_direction) { case DIR_NORTH: y--; if (y > window->height && y + length > window->height) running=false; break; case DIR_EAST: x++; if (x - length > window->width) running=false; break; case DIR_SOUTH: y++; if (y - length > window->height) running=false; break; case DIR_WEST: x--; if (x > window->width && y + length > window->width) running=false; break; } } void MatrixEffectColumn::draw() { int8_t xdir = 0; int8_t ydir = 0; switch (_direction) { case DIR_NORTH: ydir = 1; break; case DIR_EAST: xdir = -1; break; case DIR_SOUTH: ydir = -1; break; case DIR_WEST: xdir = 1; break; } for(int i=0; iraisePixel(x+(xdir*i), y+(ydir*i), &color); } } void MatrixEffectColumn::loop() { if (!running) { if (random8() < 20) { // Start the column again. restart(false); } } else { if (millis() - last_move > speed) { advance(); last_move = millis(); } draw(); } } 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; } void RandomMatrixEffectColumn::restart(bool completely_random) { MatrixEffectColumn::restart(completely_random); _hue = random8(); } boolean MatrixEffect::can_be_shown_with_clock() { return true; }; MatrixEffect::MatrixEffect() { _columns = new MatrixEffectColumn* [window->width]; for (int i=0; iwidth; i++) _columns[i] = new MatrixEffectColumn(window, MatrixEffectColumn::DIR_SOUTH); } RandomMatrixEffect::RandomMatrixEffect() { _columns = new MatrixEffectColumn* [window->width]; for (int i=0; iwidth; i++) _columns[i] = new RandomMatrixEffectColumn(window, random8(4), true); } RainbowMatrixEffect::RainbowMatrixEffect() { _columns = new MatrixEffectColumn* [window->width]; for (int i=0; iwidth; i++) _columns[i] = new RainbowMatrixEffectColumn(window, MatrixEffectColumn::DIR_SOUTH); } MatrixEffect::~MatrixEffect() { for (int i=0; iwidth; i++) { delete _columns[i]; } delete[] _columns; } void MatrixEffect::loop() { window->clear(); for (int i=0; iwidth; i++) _columns[i]->loop(); }