It's working now! And looks quite nice.

This commit is contained in:
Fabian Schlenz 2021-01-04 12:44:41 +01:00
parent 4092306eb1
commit 43916c3b78
6 changed files with 74 additions and 36 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <FastLED.h> #include <FastLED.h>
#include <vector>
#include <list> #include <list>
#include "edge.h" #include "edge.h"
@ -9,17 +10,20 @@ class Corner {
Edge* e2; Edge* e2;
Corner(Edge* e1, Edge* e2); Corner(Edge* e1, Edge* e2);
std::list<uint16_t> _leds; std::list<uint16_t> _leds;
std::list<Corner*> _long_neighbours {}; std::vector<Corner*> _long_neighbours {};
std::list<Corner*> _short_neighbours {nullptr}; std::vector<Corner*> _short_neighbours {};
CRGB color = CRGB(0x000000); CRGB color = CRGB(0x000000);
CRGB source_color; CRGB source_color;
CRGB target_color; CRGB target_color;
bool effect_running = false; bool effect_running = false;
uint8_t color_blend_state = 255; uint8_t color_blend_state = 255;
uint16_t effect_id; uint16_t effect_id;
void loop(); bool loop();
void draw(); void draw();
void blend_to(CRGB color, uint16_t effect_id=0); void blend_to(CRGB color, uint16_t effect_id=0);
void add_led(uint16_t led_id); void add_led(uint16_t led_id);
void merge_leds(Corner* c); void merge_leds(Corner* c);
private:
void infect(std::vector<Corner*>* neighbours);
}; };

View File

@ -10,7 +10,7 @@ class Node {
public: public:
Node* neighbours[CORNERS_PER_PART]; Node* neighbours[CORNERS_PER_PART];
Edge* edges[CORNERS_PER_PART]; Edge* edges[CORNERS_PER_PART];
Corner* corners[CORNERS_PER_PART]; Corner* _corners[CORNERS_PER_PART];
Node(uint16_t number); Node(uint16_t number);
Node* create_neighbour(uint8_t edge); Node* create_neighbour(uint8_t edge);
}; };

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <FastLED.h> #include <FastLED.h>
#include <list> #include <list>
#include <vector>
#include "config.h" #include "config.h"
#include "node.h" #include "node.h"
#include "edge.h" #include "edge.h"
@ -9,6 +10,6 @@
extern std::list<Node*> nodes; extern std::list<Node*> nodes;
extern std::list<Edge*> edges; extern std::list<Edge*> edges;
extern std::list<Corner*> corners; extern std::vector<Corner*> corners;
extern CRGB leds[LED_COUNT]; extern CRGB leds[LED_COUNT];

View File

@ -12,15 +12,35 @@ void Corner::draw() {
} }
} }
void Corner::loop() { void Corner::infect(std::vector<Corner*>* neighbours) {
if (color_blend_state < 255) { for(Corner* c : *neighbours) {
if (c==nullptr) continue;
c->blend_to(this->target_color, this->effect_id);
}
}
bool Corner::loop() {
if (this->color_blend_state < 255) {
this->color = blend(this->source_color, this->target_color, color_blend_state); this->color = blend(this->source_color, this->target_color, color_blend_state);
color_blend_state++; color_blend_state++;
if (color_blend_state == 200) {
infect(&_long_neighbours);
}
if (color_blend_state==75) {
infect(&_short_neighbours);
}
if (color_blend_state==255) { if (color_blend_state==255) {
LOGln("I am %p and have %d long neighbours:", this, _long_neighbours.size()); this->color = this->target_color;
}
/* LOGln("I am %p and have %d long neighbours:", this, _long_neighbours.size());
for(Corner* c1 : _long_neighbours) { for(Corner* c1 : _long_neighbours) {
if (c1==nullptr) continue;
c1->blend_to(this->target_color, this->effect_id); c1->blend_to(this->target_color, this->effect_id);
LOGln("c1: cbs:%d, target_color.r:%d", c1->color_blend_state, c1->target_color.r);
} }
LOGln("long neighbours done."); LOGln("long neighbours done.");
//LOGln("Address of my short neighbours: %p", &_short_neighbours); //LOGln("Address of my short neighbours: %p", &_short_neighbours);
@ -30,24 +50,32 @@ void Corner::loop() {
if (c2==nullptr) continue; if (c2==nullptr) continue;
LOGln("c2: %p", c2); LOGln("c2: %p", c2);
c2->blend_to(this->target_color, this->effect_id); c2->blend_to(this->target_color, this->effect_id);
LOGln("c2: cbs:%d, target_color.r:%d", c2->color_blend_state, c2->target_color.r);
} }
LOGln("short neighbours done."); LOGln("short neighbours done.");
}
}*/
} }
this->draw(); this->draw();
return color_blend_state < 255;
} }
void Corner::blend_to(CRGB target, uint16_t eid) { void Corner::blend_to(CRGB target, uint16_t eid) {
LOGln("blendTo called. Corner: %p, target: %d,%d,%d, eid: %d", this, target.r, target.g, target.b, eid); LOGln("blendTo called. Corner: %p, target: %d,%d,%d, eid: %d, 'old' effect_id: %d", this, target.r, target.g, target.b, eid, effect_id);
if (eid==0) { if (eid==0) {
this->effect_id = random16(); this->effect_id = random16();
LOGln("Set effect_id to %d", this->effect_id); LOGln("Set effect_id to %d", this->effect_id);
} else { } else {
if (this->effect_id == eid) { if (this->effect_id == eid) {
LOGln("'Old' effect. Doing nothing.");
return; return;
} }
this->effect_id = eid;
} }
this->source_color = this->color; this->source_color = this->color;
this->target_color = target; this->target_color = target;
this->color_blend_state = 0; this->color_blend_state = 0;

View File

@ -9,10 +9,12 @@
std::list<Node*> nodes; std::list<Node*> nodes;
std::list<Edge*> edges; std::list<Edge*> edges;
std::list<Corner*> corners; std::vector<Corner*> corners;
CRGB leds[LED_COUNT]; CRGB leds[LED_COUNT];
bool looping;
void setup_layout() { void setup_layout() {
LOGln("Setting up layout..."); LOGln("Setting up layout...");
uint8_t layout[] = LAYOUT; uint8_t layout[] = LAYOUT;
@ -31,12 +33,12 @@ void setup_layout() {
edges.push_back(edge); edges.push_back(edge);
} }
} }
for(Corner* corner: node->corners) { /*for(Corner* corner: node->corners) {
auto c = std::find(corners.begin(), corners.end(), corner); auto c = std::find(corners.begin(), corners.end(), corner);
if (c == corners.end()) { if (c == corners.end()) {
corners.push_back(corner); corners.push_back(corner);
} }
} }*/
} }
for(Corner* corner: corners) { for(Corner* corner: corners) {
@ -71,25 +73,23 @@ void setup() {
setup_layout(); setup_layout();
setup_fastled(); setup_fastled();
LOGln("corners.size(): %d", corners.size());
uint16_t cid = random16(corners.size());
LOGln("cid: %d", cid);
auto i = corners.begin();
for(int j=0; j<cid; j++) {
i++;
}
Corner* start = *i;
LOGln("start: %p", start);
start->blend_to(CRGB(0xFF0000));
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
EVERY_N_MILLISECONDS(20) { EVERY_N_MILLISECONDS(20) {
looping = false;
for(Corner* corner: corners) { for(Corner* corner: corners) {
corner->loop(); looping = corner->loop() || looping;
} }
FastLED.show(); FastLED.show();
if (!looping) {
LOGln("corners.size(): %d", corners.size());
uint16_t cid = random16(corners.size());
LOGln("cid: %d", cid);
Corner* start = corners[cid];
start->blend_to(CHSV(random8(), 255, 255));
}
} }
} }

View File

@ -1,4 +1,5 @@
#include "node.h" #include "node.h"
#include "prototypes.h"
Node::Node(uint16_t number) { Node::Node(uint16_t number) {
_number = number; _number = number;
@ -6,18 +7,22 @@ Node::Node(uint16_t number) {
edges[i] = new Edge(); edges[i] = new Edge();
} }
Corner* last_corner = nullptr;
for(int i=0; i<CORNERS_PER_PART; i++) { for(int i=0; i<CORNERS_PER_PART; i++) {
Corner* c = new Corner(edges[i], edges[(i+1) % CORNERS_PER_PART]); Corner* c = new Corner(edges[i], edges[(i+1) % CORNERS_PER_PART]);
for(int j=0; j<LEDS_PER_CORNER; j++) { for(int j=0; j<LEDS_PER_CORNER; j++) {
c->add_led(number * CORNERS_PER_PART * LEDS_PER_CORNER + i * LEDS_PER_CORNER + j); c->add_led(number * CORNERS_PER_PART * LEDS_PER_CORNER + i * LEDS_PER_CORNER + j);
} }
corners[i] = c; if (last_corner != nullptr) {
c->_long_neighbours.push_back(last_corner);
last_corner->_long_neighbours.push_back(c);
} }
_corners[i] = c;
for(int i=0; i<CORNERS_PER_PART; i++) { corners.push_back(c);
corners[i]->_long_neighbours.push_back(corners[(i+1) % CORNERS_PER_PART]); last_corner = c;
corners[i]->_long_neighbours.push_back(corners[(i-1) % CORNERS_PER_PART]);
} }
_corners[0]->_long_neighbours.push_back(last_corner);
last_corner->_long_neighbours.push_back(_corners[0]);
} }
Node* Node::create_neighbour(uint8_t edge) { Node* Node::create_neighbour(uint8_t edge) {
@ -28,10 +33,10 @@ Node* Node::create_neighbour(uint8_t edge) {
node->edges[0]->neighbour = this->edges[edge]; node->edges[0]->neighbour = this->edges[edge];
this->edges[edge]->neighbour = node->edges[0]; this->edges[edge]->neighbour = node->edges[0];
node->corners[0]->_short_neighbours.push_back(this->corners[(edge-1) % CORNERS_PER_PART]); node->_corners[0]->_short_neighbours.push_back(_corners[(edge-1) % CORNERS_PER_PART]);
node->corners[CORNERS_PER_PART - 1]->_short_neighbours.push_back(this->corners[edge]); node->_corners[CORNERS_PER_PART - 1]->_short_neighbours.push_back(_corners[edge]);
this->corners[(edge-1) % CORNERS_PER_PART]->_short_neighbours.push_back(node->corners[0]); _corners[(edge-1) % CORNERS_PER_PART]->_short_neighbours.push_back(node->_corners[0]);
this->corners[edge]->_short_neighbours.push_back(node->corners[CORNERS_PER_PART - 1]); _corners[edge]->_short_neighbours.push_back(node->_corners[CORNERS_PER_PART - 1]);
/* /*
delete node->edges[0]; delete node->edges[0];