WIP: Strange exceptions in Corner: Crashing when accessing _short_neighbours after a corner is finished animating.

This commit is contained in:
Fabian Schlenz 2021-01-04 11:39:46 +01:00
parent 3290c7e794
commit 4092306eb1
7 changed files with 158 additions and 13 deletions

View File

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

View File

@ -1,3 +1,6 @@
#pragma once
class Edge {};
class Edge {
public:
Edge* neighbour = nullptr;
};

View File

@ -5,10 +5,12 @@
#include "corner.h"
class Node {
private:
uint16_t _number;
public:
Node* neighbours[CORNERS_PER_PART];
Edge* edges[CORNERS_PER_PART];
Corner* corners[CORNERS_PER_PART];
Node();
Node(uint16_t number);
Node* create_neighbour(uint8_t edge);
};

View File

@ -16,5 +16,5 @@ upload_port = /dev/cu.wchusbserial*
upload_speed = 921600
monitor_port = /dev/cu.wchusbserial*
monitor_speed = 74880
monitor_filters = default, colorize, time, send_on_enter, esp8266_exception_decoder
monitor_filters = default, time, send_on_enter, esp8266_exception_decoder
lib_deps = FastLED

64
src/corner.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "corner.h"
#include "prototypes.h"
#include "tools.h"
Corner::Corner(Edge* new_e1, Edge* new_e2): e1(new_e1), e2(new_e2) {
}
void Corner::draw() {
for(uint16_t led: this->_leds) {
leds[led] = this->color;
}
}
void Corner::loop() {
if (color_blend_state < 255) {
this->color = blend(this->source_color, this->target_color, color_blend_state);
color_blend_state++;
if (color_blend_state == 255) {
LOGln("I am %p and have %d long neighbours:", this, _long_neighbours.size());
for(Corner* c1 : _long_neighbours) {
c1->blend_to(this->target_color, this->effect_id);
}
LOGln("long neighbours done.");
//LOGln("Address of my short neighbours: %p", &_short_neighbours);
//delay(10);
LOGln("I am %p and have %d short neighbours:", this, _short_neighbours.size());
for(Corner* c2 : _short_neighbours) {
if (c2==nullptr) continue;
LOGln("c2: %p", c2);
c2->blend_to(this->target_color, this->effect_id);
}
LOGln("short neighbours done.");
}
}
this->draw();
}
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);
if (eid==0) {
this->effect_id = random16();
LOGln("Set effect_id to %d", this->effect_id);
} else {
if (this->effect_id == eid) {
return;
}
}
this->source_color = this->color;
this->target_color = target;
this->color_blend_state = 0;
}
void Corner::add_led(uint16_t led) {
_leds.push_back(led);
}
void Corner::merge_leds(Corner* c) {
for(uint16_t led : c->_leds) {
_leds.push_back(led);
}
}

View File

@ -16,7 +16,7 @@ CRGB leds[LED_COUNT];
void setup_layout() {
LOGln("Setting up layout...");
uint8_t layout[] = LAYOUT;
Node* current_node = new Node();
Node* current_node = new Node(0);
nodes.push_back(current_node);
for(uint16_t i=0; i<NODE_COUNT-1; i++) {
@ -26,14 +26,12 @@ void setup_layout() {
for(Node* node: nodes) {
for(Edge* edge: node->edges) {
//LOGln("Edge #%d: %p", i, edge);
auto e = std::find(edges.begin(), edges.end(), edge);
if (e == edges.end()) {
edges.push_back(edge);
}
}
for(Corner* corner: node->corners) {
//LOGln("Corner #%d: %p", i, corner);
auto c = std::find(corners.begin(), corners.end(), corner);
if (c == corners.end()) {
corners.push_back(corner);
@ -41,6 +39,16 @@ void setup_layout() {
}
}
for(Corner* corner: corners) {
LOGln("Corner %p:", corner);
for(auto c: corner->_long_neighbours) {
LOGln(" Long: %p", c);
}
for(auto c: corner->_short_neighbours) {
LOGln(" Short: %p", c);
}
}
LOGln("Counts:");
LOGln("Nodes: %3d", nodes.size());
LOGln("Edges: %3d", edges.size());
@ -54,7 +62,7 @@ void setup_fastled() {
FastLED.addLeds<WS2812B, 5, GRB>(leds, LEDS_PER_CORNER * CORNERS_PER_PART * NODE_COUNT).setCorrection(TypicalLEDStrip);
LOGln("LEDs: %3d", LED_COUNT);
FastLED.setBrightness(255);
set_all_leds(CRGB(0xFF));
set_all_leds(CRGB::Black);
}
void setup() {
@ -63,8 +71,25 @@ void setup() {
setup_layout();
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() {
// put your main code here, to run repeatedly:
EVERY_N_MILLISECONDS(20) {
for(Corner* corner: corners) {
corner->loop();
}
FastLED.show();
}
}

View File

@ -1,22 +1,51 @@
#include "node.h"
Node::Node() {
Node::Node(uint16_t number) {
_number = number;
for(int i=0; i<CORNERS_PER_PART; i++) {
edges[i] = new Edge();
corners[i] = new Corner();
}
for(int i=0; i<CORNERS_PER_PART; i++) {
Corner* c = new Corner(edges[i], edges[(i+1) % CORNERS_PER_PART]);
for(int j=0; j<LEDS_PER_CORNER; j++) {
c->add_led(number * CORNERS_PER_PART * LEDS_PER_CORNER + i * LEDS_PER_CORNER + j);
}
corners[i] = c;
}
for(int i=0; i<CORNERS_PER_PART; i++) {
corners[i]->_long_neighbours.push_back(corners[(i+1) % CORNERS_PER_PART]);
corners[i]->_long_neighbours.push_back(corners[(i-1) % CORNERS_PER_PART]);
}
}
Node* Node::create_neighbour(uint8_t edge) {
Node* node = new Node();
Node* node = new Node(_number + 1);
node->neighbours[0] = this;
neighbours[edge] = node;
node->edges[0]->neighbour = this->edges[edge];
this->edges[edge]->neighbour = node->edges[0];
node->corners[0]->_short_neighbours.push_back(this->corners[(edge-1) % CORNERS_PER_PART]);
node->corners[CORNERS_PER_PART - 1]->_short_neighbours.push_back(this->corners[edge]);
this->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]);
/*
delete node->edges[0];
node->edges[0] = this->edges[edge];
Corner* c = this->corners[(edge-1) % CORNERS_PER_PART];
c->merge_leds(node->corners[0]);
delete node->corners[0];
node->corners[0] = this->corners[(edge-1) % CORNERS_PER_PART];
node->corners[0] = c;
c = this->corners[edge];
c->merge_leds(node->corners[CORNERS_PER_PART-1]);
delete node->corners[CORNERS_PER_PART-1];
node->corners[CORNERS_PER_PART-1] = this->corners[edge];
node->corners[CORNERS_PER_PART-1] = c;
*/
return node;
}