Nodes now calculate their coordinates. But the code now only wirks with triangles.

This commit is contained in:
Fabian Schlenz 2021-01-08 20:33:58 +01:00
parent d966d2ef8e
commit 2715d5aa6a
5 changed files with 30 additions and 35 deletions

View File

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

View File

@ -7,6 +7,7 @@
#include "edge.h"
#include "corner.h"
class Node;
extern std::vector<Node*> nodes;
extern std::list<Edge*> edges;

View File

@ -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<NODE_COUNT-1; i++) {
@ -82,21 +82,6 @@ void setup_fastled() {
set_all_leds(CRGB::Black);
}
void setup_rng() {
LOGln("Starting WiFi scan for RNG initialization...");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
uint16_t seed = 0;
int n = WiFi.scanNetworks();
LOGln("%d WiFi networks found", n);
for(int i=0; i<n; i++) {
LOGln(" %s, %d dB", WiFi.SSID(i).c_str(), WiFi.RSSI(i));
seed = (seed << 2) | (WiFi.RSSI(i) & 0x03);
}
LOGln("WiFi scan done. Generated seed is 0x%04x", seed);
random16_set_seed(seed);
}
void setup() {
Serial.begin(74880);
LOGln("ESPleaf starting.");
@ -106,8 +91,6 @@ void setup() {
#ifdef TEST_MODE
LOGln("TEST_MODE is active!");
#else
setup_rng();
wifi_setup();
mqtt_setup();
ArduinoOTA.setHostname(OTA_HOSTNAME);

View File

@ -2,8 +2,12 @@
#include "prototypes.h"
#include "tools.h"
Node::Node(uint16_t number) {
Node::Node(uint16_t number, Coords c, uint8_t _dir) {
_number = number;
coords = c;
direction = _dir;
LOGln("Created Node #%d at coordinates %d,%d with direction %d.", _number, coords.x, coords.y, direction);
for(int i=0; i<CORNERS_PER_PART; i++) {
edges[i] = new Edge();
}
@ -26,8 +30,15 @@ Node::Node(uint16_t number) {
last_corner->_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;
}

View File

@ -10,4 +10,5 @@ void wifi_setup() {
delay(300);
}
LOGln(" Connected as %s", WiFi.localIP().toString().c_str());
random16_add_entropy(micros());
}