From a2fe1461adc8eb36fb2f86bad0fe011bc9bb44ef Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 31 May 2019 05:45:07 +0200 Subject: [PATCH] Added effect "snake". --- include/config.sample.h | 3 +++ include/effect_snake.h | 21 +++++++++++++++++++++ include/prototypes.h | 5 +++++ src/effect_snake.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/effects.cpp | 4 ++++ 5 files changed, 73 insertions(+) create mode 100644 include/effect_snake.h create mode 100644 src/effect_snake.cpp diff --git a/include/config.sample.h b/include/config.sample.h index 367d565..9bbbf5c 100644 --- a/include/config.sample.h +++ b/include/config.sample.h @@ -53,6 +53,9 @@ #define EFFECT_CONFETTI_PIXELS_PER_LOOP 2 +#define EFFECT_SNAKE_DIRECTION_CHANGE 10 +#define EFFECT_SNAKE_SLOWDOWN 2 + #ifdef DEBUG #define LOG(x) Serial.print(x); #define LOGln(x) Serial.println(x); diff --git a/include/effect_snake.h b/include/effect_snake.h new file mode 100644 index 0000000..fca8a86 --- /dev/null +++ b/include/effect_snake.h @@ -0,0 +1,21 @@ +#ifndef effect_snake_H +#define effect_snake_H + +#include "Effect.h" +#include "prototypes.h" + +class SnakeEffect : public Effect { +private: + Coords coords; + uint8_t direction; + uint8_t hue; + uint8_t run; + Window window = {0, 0, LED_WIDTH, LED_HEIGHT-6}; +public: + void loop(); + boolean valid_position(Coords c); + Coords update_position(Coords c, uint8_t direction); + boolean can_be_shown_with_clock(); +}; + +#endif diff --git a/include/prototypes.h b/include/prototypes.h index 78ea87c..59590c2 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -34,6 +34,11 @@ typedef struct Matrix { double a22; } Matrix; +typedef struct { + uint16_t x; + uint16_t y; +} Coords; + extern uint8_t baseHue; #endif diff --git a/src/effect_snake.cpp b/src/effect_snake.cpp new file mode 100644 index 0000000..d07f826 --- /dev/null +++ b/src/effect_snake.cpp @@ -0,0 +1,40 @@ +#include "effect_snake.h" +#include "functions.h" + +void SnakeEffect::loop() { + if (run++ % EFFECT_SNAKE_SLOWDOWN) return; + + uint8_t r = random8(EFFECT_SNAKE_DIRECTION_CHANGE); + if (r==0 && valid_position(update_position(this->coords, direction+1))) { + direction++; + } else if (r==1 && valid_position(update_position(this->coords, direction-1))) { + direction--; + } else if (!valid_position(update_position(this->coords, direction))) { + if (valid_position(update_position(this->coords, direction+1))) { + direction++; + } else if (valid_position(update_position(this->coords, direction-1))) { + direction--; + } + } + + this->coords = update_position(this->coords, direction); + + fadeToBlackBy(leds, LED_COUNT, 2); + setPixel(window, this->coords.x, this->coords.y, CHSV(hue, 200, 255)); + hue++; +} + +Coords SnakeEffect::update_position(Coords original, uint8_t direction) { + direction = direction % 4; + if (direction == 0) original.y--; + else if (direction == 1) original.x++; + else if (direction == 2) original.y++; + else if (direction == 3) original.x--; + return original; +} + +boolean SnakeEffect::valid_position(Coords c) { + return c.x* effects; SimpleList* cycle_effects; @@ -33,6 +34,7 @@ MatrixEffect effect_matrix; CycleEffect effect_cycle; TwirlEffect effect_twirl; ConfettiEffect effect_confetti; +SnakeEffect effect_snake; Effect* current_effect; @@ -54,12 +56,14 @@ void setup_effects() { effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl}); effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart}); effects->add((EffectEntry){"confetti", (Effect *)&effect_confetti}); + effects->add((EffectEntry){"snake", (Effect *)&effect_snake}); cycle_effects->add(&effect_sinematrix3); cycle_effects->add(&effect_single_dynamic); cycle_effects->add(&effect_multi_dynamic); cycle_effects->add(&effect_matrix); cycle_effects->add(&effect_confetti); + cycle_effects->add(&effect_snake); current_effect = &effect_cycle; }