From 2715d5aa6aa191c2e9920d83db5dc1bcfbc47b0c Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 8 Jan 2021 20:33:58 +0100 Subject: [PATCH] Nodes now calculate their coordinates. But the code now only wirks with triangles. --- include/node.h | 15 ++++++++++++++- include/prototypes.h | 1 + src/main.cpp | 19 +------------------ src/node.cpp | 29 +++++++++++++---------------- src/wifi.cpp | 1 + 5 files changed, 30 insertions(+), 35 deletions(-) diff --git a/include/node.h b/include/node.h index 09fa75e..5807c62 100644 --- a/include/node.h +++ b/include/node.h @@ -3,15 +3,28 @@ #include "config.h" #include "edge.h" #include "corner.h" +#include "prototypes.h" + +// Delta-X data. Per direction of a triangle is a triple with Delta-X values for each direction. +static const int8_t dx[][3] = {{0, 1, -1}, {1, 0, -1}, {1, -1, 0}, {0, -1, 1}, {-1, 0, 1}, {-1, 1, 0}}; +static const int8_t dy[][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, -1}, {1, 0, 0}, {0, -1, 0}, {0, 0, 1}}; + +typedef struct { + int8_t x; + int8_t y; +} Coords; class Node { private: uint16_t _number; + Coords coords_at_direction(uint8_t edge); public: + Coords coords; + uint8_t direction; Node* neighbours[CORNERS_PER_PART]; Edge* edges[CORNERS_PER_PART]; Corner* _corners[CORNERS_PER_PART]; - Node(uint16_t number); + Node(uint16_t number, Coords c, uint8_t direction); Node* create_neighbour(uint8_t edge); void blend_to(CRGB color, uint16_t effect_id=0, uint8_t effect_speed=0); diff --git a/include/prototypes.h b/include/prototypes.h index 49be9d8..4f36f4d 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -7,6 +7,7 @@ #include "edge.h" #include "corner.h" +class Node; extern std::vector nodes; extern std::list edges; diff --git a/src/main.cpp b/src/main.cpp index 9a0be10..c77172f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,7 +33,7 @@ bool looping; void setup_layout() { LOGln("Setting up layout..."); uint8_t layout[] = LAYOUT; - Node* current_node = new Node(0); + Node* current_node = new Node(0, {0, 0}, 0); nodes.push_back(current_node); for(uint16_t i=0; i_long_neighbours.push_back(_corners[0]); } +Coords Node::coords_at_direction(uint8_t edge) { + return {(int8_t)(coords.x + dx[direction][edge]), (int8_t)(coords.y + dy[direction][edge])}; +} + Node* Node::create_neighbour(uint8_t edge) { - Node* node = new Node(_number + 1); + Coords new_c = coords_at_direction(edge); + int8_t new_dir = (edge==1) ? ((direction - 1) % 6) : ((direction + 1) % 6); + if (new_dir < 0) new_dir+=6; + Node* node = new Node(_number + 1, new_c, new_dir); node->neighbours[0] = this; neighbours[edge] = node; @@ -39,20 +50,6 @@ Node* Node::create_neighbour(uint8_t edge) { _corners[(edge-1) % CORNERS_PER_PART]->_short_neighbours.push_back(node->_corners[0]); _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] = 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] = c; - */ return node; } diff --git a/src/wifi.cpp b/src/wifi.cpp index 52b6012..5f50473 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -10,4 +10,5 @@ void wifi_setup() { delay(300); } LOGln(" Connected as %s", WiFi.localIP().toString().c_str()); + random16_add_entropy(micros()); } \ No newline at end of file