color_blend_state extended from 0-255 to 0-1023 to allow slower / smoother effects.

This commit is contained in:
Fabian Schlenz 2021-01-08 11:30:10 +01:00
parent 77eaea81d1
commit fa888bcfab
7 changed files with 58 additions and 51 deletions

View File

@ -20,4 +20,6 @@
#define SPEEDUP 1
#define MAX_MILLIAMPS 1000
//#define TEST_MODE

View File

@ -16,19 +16,19 @@ class Corner {
CRGB source_color;
CRGB target_color;
bool effect_running = false;
uint8_t color_blend_state = 255;
uint16_t color_blend_state = 1023;
uint16_t effect_id;
uint8_t effect_speed;
uint16_t effect_speed;
//bool loop();
void draw();
void blend_to(CRGB color, uint16_t effect_id=0, uint8_t effect_speed=0);
void add_led(uint16_t led_id);
void merge_leds(Corner* c);
void step();
void infect(uint8_t short_level, uint8_t long_level);
void infect(uint16_t short_level, uint16_t long_level);
bool is_finished();
void set_color(CRGB color);
bool reached_level(uint8_t level);
bool reached_level(uint16_t level);
private:
void _infect(std::vector<Corner*>* neighbours);

View File

@ -16,7 +16,7 @@ class Node {
void blend_to(CRGB color, uint16_t effect_id=0, uint8_t effect_speed=0);
void set_color(CRGB color);
void infect(uint8_t level);
void infect(uint16_t level);
void step();
void draw();
};

View File

@ -16,5 +16,8 @@ extern CRGB leds[LED_COUNT];
enum AnimationMode {
AM_CORNERS,
AM_NODES
AM_FIRST_CORNER,
AM_NODES,
AM_FIRST_NODE,
AM_FLASH
};

View File

@ -20,16 +20,16 @@ void Corner::_infect(std::vector<Corner*>* neighbours) {
}
void Corner::step() {
if (this->color_blend_state < 255) {
this->color = blend(this->source_color, this->target_color, color_blend_state);
this->color_blend_state = (this->color_blend_state + effect_speed > 255) ? 255 : this->color_blend_state + effect_speed;
if (this->color_blend_state==255) {
if (this->color_blend_state < 1024) {
this->color = blend(this->source_color, this->target_color, color_blend_state>>2);
this->color_blend_state = (this->color_blend_state + effect_speed > 1024) ? 1024 : this->color_blend_state + effect_speed;
if (this->color_blend_state==1024) {
this->color = this->target_color;
}
}
}
void Corner::infect(uint8_t infect_short_level, uint8_t infect_long_level) {
void Corner::infect(uint16_t infect_short_level, uint16_t infect_long_level) {
if (reached_level(infect_short_level)) {
_infect(&_short_neighbours);
}
@ -38,38 +38,11 @@ void Corner::infect(uint8_t infect_short_level, uint8_t infect_long_level) {
}
}
bool Corner::reached_level(uint8_t level) {
uint8_t old_cbs = color_blend_state >= effect_speed ? color_blend_state - effect_speed : 0;
bool Corner::reached_level(uint16_t level) {
uint16_t old_cbs = color_blend_state >= effect_speed ? color_blend_state - effect_speed : 0;
return (old_cbs < level && color_blend_state >= level);
}
/*bool Corner::loop() {
if (this->color_blend_state < 255) {
this->color = blend(this->source_color, this->target_color, color_blend_state);
if (255 - color_blend_state >= effect_speed) {
color_blend_state += effect_speed;
} else {
color_blend_state = 255;
}
if (color_blend_state >= 200 && color_blend_state-effect_speed<200) {
infect(&_long_neighbours);
}
if (color_blend_state>=75 && color_blend_state-effect_speed<75) {
infect(&_short_neighbours);
}
if (color_blend_state==255) {
this->color = this->target_color;
}
}
this->draw();
return color_blend_state < 255;
}*/
void Corner::blend_to(CRGB target, uint16_t eid, uint8_t effect_speed) {
LOGln("blendTo called. Corner: %p, target: %d,%d,%d, eid: %d, 'old' effect_id: %d, speed: %d", this, target.r, target.g, target.b, eid, effect_id, effect_speed);
if (eid==0) {
@ -84,7 +57,9 @@ void Corner::blend_to(CRGB target, uint16_t eid, uint8_t effect_speed) {
}
if (effect_speed==0) {
effect_speed = random8(3) + 1;
// Effect speed 2 - 7.
// Speed 7 @ 50fps => 3 seconds
effect_speed = random8(6) + 2;
}
this->effect_speed = effect_speed;
@ -104,10 +79,10 @@ void Corner::merge_leds(Corner* c) {
}
bool Corner::is_finished() {
return color_blend_state < 255;
return color_blend_state < 1024;
}
void Corner::set_color(CRGB color) {
this->color = color;
this->color_blend_state = 255;
this->color_blend_state = 1024;
}

View File

@ -16,6 +16,8 @@ CRGB leds[LED_COUNT];
AnimationMode mode = AM_CORNERS;
unsigned long last_loop = 0;
#ifdef TEST_MODE
uint8_t base_hue = 0;
#endif
@ -70,6 +72,7 @@ void setup_fastled() {
LOGln("LEDs: %3d", LED_COUNT);
FastLED.setBrightness(255);
FastLED.setDither(DISABLE_DITHER);
FastLED.setMaxPowerInVoltsAndMilliamps(5, MAX_MILLIAMPS);
set_all_leds(CRGB::Black);
}
@ -116,31 +119,55 @@ void loop() {
}
#else
// Normal mode
EVERY_N_MILLISECONDS(40 / SPEEDUP) {
EVERY_N_MILLISECONDS(20 / SPEEDUP) {
looping = false;
if (mode == AM_CORNERS) {
if (mode == AM_CORNERS || mode == AM_FIRST_CORNER) {
for(Corner* corner: corners) {
corner->step();
corner->infect(75, 200);
if (mode == AM_FIRST_CORNER) {
corner->infect(512, 512);
} else {
corner->infect(300, 600);
}
looping |= !corner->is_finished();
corner->draw();
}
if (random8(128)==0) {
corners[random16(corners.size())]->blend_to(CHSV(random8(), 255, 255));
if (mode == AM_FIRST_CORNER) {
corners[0]->blend_to(CHSV(random8(), 255, 255));
} else {
corners[random16(corners.size())]->blend_to(CHSV(random8(), 255, 255));
}
}
} else if (mode == AM_NODES) {
} else if (mode == AM_NODES || mode == AM_FIRST_NODE) {
for(Node* node : nodes) {
node->step();
node->infect(128);
node->infect(512);
node->draw();
}
if (random8(128)==0) {
nodes[random8(nodes.size())]->blend_to(CHSV(random8(), 255, 255));
if (mode == AM_FIRST_NODE) {
nodes[0]->blend_to(CHSV(random8(), 255, 255));
} else {
nodes[random8(nodes.size())]->blend_to(CHSV(random8(), 255, 255));
}
}
} else if (mode == AM_FLASH) {
for (Node* node : nodes) {
node->step();
node->infect(512);
node->draw();
}
if (millis() / 1000 > last_loop / 1000) {
nodes[0]->blend_to(((millis() / 1000) % 2 == 0) ? CRGB::Black : CRGB::Orange, 0, 64);
}
}
last_loop = millis();
}
#endif

View File

@ -83,7 +83,7 @@ void Node::set_color(CRGB color) {
}
}
void Node::infect(uint8_t level) {
void Node::infect(uint16_t level) {
if (this->_corners[0]->reached_level(level)) {
for (Node* neighbour : neighbours) {
if (neighbour == nullptr) continue;