Compare commits

..

No commits in common. "d9708d91593b3baed5b0f8c8d4c7b4c04ed8218f" and "db76255a2c42932bbe6cd728558e1d3b85f335a5" have entirely different histories.

10 changed files with 62 additions and 75 deletions

View File

@ -1,17 +1,14 @@
#pragma once
class Corner;
#include "my_fastled.h"
#include <vector>
#include <list>
#include "node.h"
#include "edge.h"
class Corner {
public:
Node* node;
uint8_t number;
Corner(Node* node, uint8_t number);
Edge* e1;
Edge* e2;
Corner(Edge* e1, Edge* e2);
std::list<uint16_t> _leds;
std::vector<Corner*> _long_neighbours {};
std::vector<Corner*> _short_neighbours {};

6
include/edge.h Normal file
View File

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

View File

@ -1,9 +1,7 @@
#pragma once
class Node;
#include <Arduino.h>
#include "config.h"
#include "edge.h"
#include "corner.h"
#include "prototypes.h"
@ -22,6 +20,7 @@ class Node {
Coords coords;
uint8_t direction;
Node* neighbours[CORNERS_PER_PART];
Edge* edges[CORNERS_PER_PART];
Corner* _corners[CORNERS_PER_PART];
Node(uint16_t number, Coords c, uint8_t direction);
Node* create_neighbour(uint8_t edge);

View File

@ -2,14 +2,15 @@
#include "my_fastled.h"
#include <list>
#include <vector>
#include <map>
#include "config.h"
#include "node.h"
#include "edge.h"
#include "corner.h"
class Node;
extern std::vector<Node*> nodes;
extern std::list<Edge*> edges;
extern std::vector<Corner*> corners;
extern CRGB leds[LED_COUNT];
@ -27,8 +28,6 @@ enum AnimationMode {
AM_STATIC
};
extern std::map<AnimationMode, const char*> animation_mode_names;
extern AnimationMode mode;
extern AnimationMode temp_mode;
extern unsigned long temp_mode_until;

View File

@ -15,7 +15,6 @@ class State {
void set(String key, String value);
void commit();
static void publish_current_state();
static AnimationMode get_active_mode();
void parse_state(String state);
void parse_mode(String mode);

View File

@ -2,7 +2,7 @@
#include "prototypes.h"
#include "tools.h"
Corner::Corner(Node* no, uint8_t nu): node(no), number(nu) {
Corner::Corner(Edge* new_e1, Edge* new_e2): e1(new_e1), e2(new_e2) {
}

View File

@ -5,6 +5,7 @@
#include "config.h"
#include "tools.h"
#include "node.h"
#include "edge.h"
#include "corner.h"
#include "prototypes.h"
#include "mqtt.h"
@ -12,6 +13,7 @@
#include "syslog.h"
std::vector<Node*> nodes;
std::list<Edge*> edges;
std::vector<Corner*> corners;
CRGB leds[LED_COUNT];
@ -42,12 +44,24 @@ void setup_layout() {
nodes.push_back(current_node);
}
bool verbose = false;
for(Node* node: nodes) {
for(Edge* edge: node->edges) {
auto e = std::find(edges.begin(), edges.end(), edge);
if (e == edges.end()) {
edges.push_back(edge);
}
}
LOGln("Node %p:", node);
for(Node* n : node->neighbours) {
LOGln(" %p", n);
}
}
for (Node* n1 : nodes) {
if (verbose) LOGln("Looking for neighbours of node #%d @ %d,%d", n1->_number, n1->coords.x, n1->coords.y);
LOGln("Looking for neighbours of node #%d @ %d,%d", n1->_number, n1->coords.x, n1->coords.y);
for(int edge=0; edge<CORNERS_PER_PART; edge++) {
Coords c = n1->coords_at_direction(edge);
if (verbose) LOGln(" Chcking edge %d @ %d,%d...", edge, c.x, c.y);
LOGln(" Chcking edge %d @ %d,%d...", edge, c.x, c.y);
Node* found = nullptr;
for(Node* n2 : nodes) {
@ -60,7 +74,7 @@ void setup_layout() {
}
}
if (found != nullptr) {
if (verbose) LOGln(" Found node #%d", found->_number);
LOGln(" Found node #%d", found->_number);
uint8_t inverse_dir = (n1->direction + 2*edge + 3) % 6;
int8_t e = (inverse_dir - found->direction) % 6;
if (e < 0) e+=6;
@ -71,39 +85,30 @@ void setup_layout() {
int8_t e2 = (e - 1) % CORNERS_PER_PART;
if (e2<0) e2+=CORNERS_PER_PART;
if (verbose) LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, edge, found->_number, e2);
LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, edge, found->_number, e2);
n1->_corners[edge]->_short_neighbours.push_back(found->_corners[e2]);
if (verbose) LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, e1, found->_number, e);
LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, e1, found->_number, e);
n1->_corners[e1]->_short_neighbours.push_back(found->_corners[e]);
} else {
if (verbose) LOGln(" No match.");
LOGln(" No match.");
}
}
}
for (Node* node : nodes) {
LOGln("Node #%d:", node->_number);
for(Corner* corner : node->_corners) {
LOGln(" Corner #%d,%d:", node->_number, corner->number);
for (auto c: corner->_long_neighbours) {
LOGln(" Long neighbour: #%d,%d", c->node->_number, c->number);
}
for (auto c: corner->_short_neighbours) {
LOGln(" Short neighbour: #%d,%d", c->node->_number, c->number);
}
for(Corner* corner: corners) {
LOGln("Corner %p:", corner);
for(auto c: corner->_long_neighbours) {
LOGln(" Long: %p", c);
}
for (int i=0; i<CORNERS_PER_PART; i++) {
if (node->neighbours[i]==nullptr) {
LOGln(" Neighbour %d: NULL", i);
} else {
LOGln(" Neighbour %d: #%d", i, node->neighbours[i]->_number);
}
for(auto c: corner->_short_neighbours) {
LOGln(" Short: %p", c);
}
}
LOGln("Counts:");
LOGln("Nodes: %3d", nodes.size());
LOGln("Edges: %3d", edges.size());
LOGln("Corners: %3d", corners.size());
}
@ -208,8 +213,6 @@ void setup() {
for(Corner* corner : corners) {
corner->set_color(CRGB::Black);
}
State::publish_current_state();
}
void loop() {
@ -233,7 +236,19 @@ void loop() {
if (speedup > 0 && (millis() - last_loop > (20 / speedup) || last_loop > millis())) {
looping = false;
AnimationMode active_mode = State::get_active_mode();
AnimationMode active_mode = mode;
if (temp_mode_until > 0) {
if (temp_mode_until>millis()) {
active_mode = temp_mode;
} else {
temp_mode_until = 0;
if (return_to_brightness != -1) {
FastLED.setBrightness(return_to_brightness);
return_to_brightness = -1;
}
State::publish_current_state();
}
}
if (active_mode == AM_CORNERS || active_mode == AM_FIRST_CORNER) {
for(Corner* corner: corners) {

View File

@ -9,12 +9,12 @@ Node::Node(uint16_t number, Coords c, uint8_t _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++) {
neighbours[i] = nullptr;
edges[i] = new Edge();
}
Corner* last_corner = nullptr;
for(int i=0; i<CORNERS_PER_PART; i++) {
Corner* c = new Corner(this, 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);
}
@ -42,6 +42,9 @@ Node* Node::create_neighbour(uint8_t edge) {
node->neighbours[0] = this;
neighbours[edge] = node;
node->edges[0]->neighbour = this->edges[edge];
this->edges[edge]->neighbour = node->edges[0];
return node;
}

View File

@ -1,13 +0,0 @@
#include "prototypes.h"
std::map<AnimationMode, const char*> animation_mode_names {
{AM_CORNERS, "corners"},
{AM_FLASH, "flash"},
{AM_FIRST_CORNER, "first_corner"},
{AM_NODES, "nodes"},
{AM_FIRST_NODE, "first_node"},
{AM_OFF, "off"},
{AM_ERROR, "error"},
{AM_NONE, "NONE"},
{AM_STATIC, "static"}
};

View File

@ -106,30 +106,12 @@ void State::publish_current_state() {
StaticJsonDocument<512> json;
json["state"] = (mode==AM_OFF) ? "OFF" : "ON";
json["brightness"] = FastLED.getBrightness();
json["effect"] = animation_mode_names[get_active_mode()];
json["effect"] = mode;
JsonObject rgb = json.createNestedObject("rgb");
rgb["r"] = color.r;
rgb["g"] = color.g;
rgb["b"] = color.b;
String result = "";
serializeJson(json, result);
LOGln("Reporting current state: %s", result.c_str());
mqtt_publish_current_state(result);
}
AnimationMode State::get_active_mode() {
AnimationMode active_mode = mode;
if (temp_mode_until > 0) {
if (temp_mode_until>millis()) {
active_mode = temp_mode;
} else {
temp_mode_until = 0;
if (return_to_brightness != -1) {
FastLED.setBrightness(return_to_brightness);
return_to_brightness = -1;
}
State::publish_current_state();
}
}
return active_mode;
}