Added effect "snake".

This commit is contained in:
Fabian Schlenz 2019-05-31 05:45:07 +02:00
parent 36663cf067
commit a2fe1461ad
5 changed files with 73 additions and 0 deletions

View File

@ -53,6 +53,9 @@
#define EFFECT_CONFETTI_PIXELS_PER_LOOP 2 #define EFFECT_CONFETTI_PIXELS_PER_LOOP 2
#define EFFECT_SNAKE_DIRECTION_CHANGE 10
#define EFFECT_SNAKE_SLOWDOWN 2
#ifdef DEBUG #ifdef DEBUG
#define LOG(x) Serial.print(x); #define LOG(x) Serial.print(x);
#define LOGln(x) Serial.println(x); #define LOGln(x) Serial.println(x);

21
include/effect_snake.h Normal file
View File

@ -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

View File

@ -34,6 +34,11 @@ typedef struct Matrix {
double a22; double a22;
} Matrix; } Matrix;
typedef struct {
uint16_t x;
uint16_t y;
} Coords;
extern uint8_t baseHue; extern uint8_t baseHue;
#endif #endif

40
src/effect_snake.cpp Normal file
View File

@ -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<window.w && c.y<window.h;
}
boolean SnakeEffect::can_be_shown_with_clock() { return true; }

View File

@ -14,6 +14,7 @@
#include "effect_twirl.h" #include "effect_twirl.h"
#include "effect_cycle.h" #include "effect_cycle.h"
#include "effect_confetti.h" #include "effect_confetti.h"
#include "effect_snake.h"
SimpleList<EffectEntry>* effects; SimpleList<EffectEntry>* effects;
SimpleList<Effect*>* cycle_effects; SimpleList<Effect*>* cycle_effects;
@ -33,6 +34,7 @@ MatrixEffect effect_matrix;
CycleEffect effect_cycle; CycleEffect effect_cycle;
TwirlEffect effect_twirl; TwirlEffect effect_twirl;
ConfettiEffect effect_confetti; ConfettiEffect effect_confetti;
SnakeEffect effect_snake;
Effect* current_effect; Effect* current_effect;
@ -54,12 +56,14 @@ void setup_effects() {
effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl}); effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl});
effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart}); effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart});
effects->add((EffectEntry){"confetti", (Effect *)&effect_confetti}); effects->add((EffectEntry){"confetti", (Effect *)&effect_confetti});
effects->add((EffectEntry){"snake", (Effect *)&effect_snake});
cycle_effects->add(&effect_sinematrix3); cycle_effects->add(&effect_sinematrix3);
cycle_effects->add(&effect_single_dynamic); cycle_effects->add(&effect_single_dynamic);
cycle_effects->add(&effect_multi_dynamic); cycle_effects->add(&effect_multi_dynamic);
cycle_effects->add(&effect_matrix); cycle_effects->add(&effect_matrix);
cycle_effects->add(&effect_confetti); cycle_effects->add(&effect_confetti);
cycle_effects->add(&effect_snake);
current_effect = &effect_cycle; current_effect = &effect_cycle;
} }