Compare commits
7 Commits
47cd48d572
...
master
Author | SHA1 | Date | |
---|---|---|---|
8949041812 | |||
d9708d9159 | |||
5676b5d63c | |||
e9143b6ca8 | |||
db76255a2c | |||
f4864dec05 | |||
11dbea0302 |
@ -35,7 +35,9 @@ Valid keys are:
|
|||||||
## Startup sequence
|
## Startup sequence
|
||||||
|
|
||||||
During startup:
|
During startup:
|
||||||
* 1 green corner: FastLED is initialized. Layout is now being analyzed.
|
* 1 green corner: FastLED is initialized.
|
||||||
* 2 green corners: Layout is done. WiFi is being connected.
|
* 2 green corners: WiFi is connecting...
|
||||||
* 3 green corners: WiFi connection established. Connecting to MQTT server.
|
* 3 green corners: Layout is analyzed...
|
||||||
|
* 4 green corners: Connecting to MQTT server...
|
||||||
|
* 5 green corners (only if OTA_DELAY is set): Waiting for an OTA connection...
|
||||||
* Everything green (quarter of a second): Initialization done.
|
* Everything green (quarter of a second): Initialization done.
|
@ -24,6 +24,10 @@
|
|||||||
// Maximum color difference for the random effects.
|
// Maximum color difference for the random effects.
|
||||||
// This changes the hue value +/- this value. Use a maximum value of 127, otherwise strange things might happen.
|
// This changes the hue value +/- this value. Use a maximum value of 127, otherwise strange things might happen.
|
||||||
#define COLOR_DIFFERENCE 25
|
#define COLOR_DIFFERENCE 25
|
||||||
|
// If this is enabled, the node layout will be visualized at startup.
|
||||||
|
#define SHOW_LAYOUT_AT_BOOT
|
||||||
|
// Wait this many seconds for OTA requests during startup.
|
||||||
|
#define WAIT_FOR_OTA 5
|
||||||
|
|
||||||
#define WIFI_SSID "..."
|
#define WIFI_SSID "..."
|
||||||
#define WIFI_PASS "..."
|
#define WIFI_PASS "..."
|
||||||
@ -39,4 +43,7 @@
|
|||||||
#define MQTT_TOPIC_STATE_LONG "espleaf/state_long"
|
#define MQTT_TOPIC_STATE_LONG "espleaf/state_long"
|
||||||
#define MQTT_TOPIC_COMMANDS "esplead/cmnd"
|
#define MQTT_TOPIC_COMMANDS "esplead/cmnd"
|
||||||
|
|
||||||
|
#define SYSLOG_HOST "..."
|
||||||
|
#define SYSLOG_PORT 514
|
||||||
|
|
||||||
//#define TEST_MODE
|
//#define TEST_MODE
|
@ -1,14 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
class Corner;
|
||||||
|
|
||||||
#include "my_fastled.h"
|
#include "my_fastled.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include "edge.h"
|
#include "node.h"
|
||||||
|
|
||||||
class Corner {
|
class Corner {
|
||||||
public:
|
public:
|
||||||
Edge* e1;
|
Node* node;
|
||||||
Edge* e2;
|
uint8_t number;
|
||||||
Corner(Edge* e1, Edge* e2);
|
Corner(Node* node, uint8_t number);
|
||||||
std::list<uint16_t> _leds;
|
std::list<uint16_t> _leds;
|
||||||
std::vector<Corner*> _long_neighbours {};
|
std::vector<Corner*> _long_neighbours {};
|
||||||
std::vector<Corner*> _short_neighbours {};
|
std::vector<Corner*> _short_neighbours {};
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class Edge {
|
|
||||||
public:
|
|
||||||
Edge* neighbour = nullptr;
|
|
||||||
};
|
|
@ -1,7 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
class Node;
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "edge.h"
|
|
||||||
#include "corner.h"
|
#include "corner.h"
|
||||||
#include "prototypes.h"
|
#include "prototypes.h"
|
||||||
|
|
||||||
@ -20,11 +22,11 @@ class Node {
|
|||||||
Coords coords;
|
Coords coords;
|
||||||
uint8_t direction;
|
uint8_t direction;
|
||||||
Node* neighbours[CORNERS_PER_PART];
|
Node* neighbours[CORNERS_PER_PART];
|
||||||
Edge* edges[CORNERS_PER_PART];
|
|
||||||
Corner* _corners[CORNERS_PER_PART];
|
Corner* _corners[CORNERS_PER_PART];
|
||||||
Node(uint16_t number, Coords c, uint8_t direction);
|
Node(uint16_t number, Coords c, uint8_t direction);
|
||||||
Node* create_neighbour(uint8_t edge);
|
Node* create_neighbour(uint8_t edge);
|
||||||
Coords coords_at_direction(uint8_t edge);
|
Coords coords_at_direction(uint8_t edge);
|
||||||
|
uint16_t distance_from_start = 0;
|
||||||
|
|
||||||
void blend_to(CRGB color, uint16_t effect_id=0, uint8_t effect_speed=0);
|
void blend_to(CRGB color, uint16_t effect_id=0, uint8_t effect_speed=0);
|
||||||
void set_color(CRGB color);
|
void set_color(CRGB color);
|
||||||
|
@ -2,15 +2,14 @@
|
|||||||
#include "my_fastled.h"
|
#include "my_fastled.h"
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "edge.h"
|
|
||||||
#include "corner.h"
|
#include "corner.h"
|
||||||
|
|
||||||
class Node;
|
class Node;
|
||||||
|
|
||||||
extern std::vector<Node*> nodes;
|
extern std::vector<Node*> nodes;
|
||||||
extern std::list<Edge*> edges;
|
|
||||||
extern std::vector<Corner*> corners;
|
extern std::vector<Corner*> corners;
|
||||||
|
|
||||||
extern CRGB leds[LED_COUNT];
|
extern CRGB leds[LED_COUNT];
|
||||||
@ -25,9 +24,12 @@ enum AnimationMode {
|
|||||||
AM_OFF,
|
AM_OFF,
|
||||||
AM_ERROR,
|
AM_ERROR,
|
||||||
AM_NONE,
|
AM_NONE,
|
||||||
AM_STATIC
|
AM_STATIC,
|
||||||
|
AM_RAINBOW
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern std::map<AnimationMode, const char*> animation_mode_names;
|
||||||
|
|
||||||
extern AnimationMode mode;
|
extern AnimationMode mode;
|
||||||
extern AnimationMode temp_mode;
|
extern AnimationMode temp_mode;
|
||||||
extern unsigned long temp_mode_until;
|
extern unsigned long temp_mode_until;
|
||||||
|
@ -15,6 +15,7 @@ class State {
|
|||||||
void set(String key, String value);
|
void set(String key, String value);
|
||||||
void commit();
|
void commit();
|
||||||
static void publish_current_state();
|
static void publish_current_state();
|
||||||
|
static AnimationMode get_active_mode();
|
||||||
|
|
||||||
void parse_state(String state);
|
void parse_state(String state);
|
||||||
void parse_mode(String mode);
|
void parse_mode(String mode);
|
||||||
|
8
include/syslog.h
Normal file
8
include/syslog.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
|
||||||
|
#ifdef SYSLOG_HOST
|
||||||
|
void syslog(String msg, uint8_t severity=7);
|
||||||
|
#endif
|
@ -1,8 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "my_fastled.h"
|
#include "my_fastled.h"
|
||||||
|
#include "syslog.h"
|
||||||
|
|
||||||
#define LOG(...) Serial.printf(__VA_ARGS__)
|
//#define LOG(...) Serial.printf(__VA_ARGS__)
|
||||||
|
//#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println()
|
||||||
|
|
||||||
|
#ifdef SYSLOG_HOST
|
||||||
|
#define LOGln(...) { char buffer[512]; snprintf(buffer, 512, __VA_ARGS__); syslog(buffer); Serial.println(buffer);}
|
||||||
|
#else
|
||||||
#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println()
|
#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void clear_leds();
|
void clear_leds();
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include "prototypes.h"
|
#include "prototypes.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
|
|
||||||
Corner::Corner(Edge* new_e1, Edge* new_e2): e1(new_e1), e2(new_e2) {
|
Corner::Corner(Node* no, uint8_t nu): node(no), number(nu) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,6 +39,7 @@ void Corner::infect(uint16_t infect_short_level, uint16_t infect_long_level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Corner::reached_level(uint16_t level) {
|
bool Corner::reached_level(uint16_t level) {
|
||||||
|
if (color_blend_state >= 1024) return false;
|
||||||
uint16_t old_cbs = color_blend_state >= effect_speed ? color_blend_state - effect_speed : 0;
|
uint16_t old_cbs = color_blend_state >= effect_speed ? color_blend_state - effect_speed : 0;
|
||||||
return (old_cbs < level && color_blend_state >= level);
|
return (old_cbs < level && color_blend_state >= level);
|
||||||
}
|
}
|
||||||
|
144
src/main.cpp
144
src/main.cpp
@ -5,14 +5,13 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "edge.h"
|
|
||||||
#include "corner.h"
|
#include "corner.h"
|
||||||
#include "prototypes.h"
|
#include "prototypes.h"
|
||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "state.h"
|
#include "state.h"
|
||||||
|
#include "syslog.h"
|
||||||
|
|
||||||
std::vector<Node*> nodes;
|
std::vector<Node*> nodes;
|
||||||
std::list<Edge*> edges;
|
|
||||||
std::vector<Corner*> corners;
|
std::vector<Corner*> corners;
|
||||||
|
|
||||||
CRGB leds[LED_COUNT];
|
CRGB leds[LED_COUNT];
|
||||||
@ -36,6 +35,7 @@ void setup_layout() {
|
|||||||
LOGln("Setting up layout...");
|
LOGln("Setting up layout...");
|
||||||
uint8_t layout[] = LAYOUT;
|
uint8_t layout[] = LAYOUT;
|
||||||
Node* current_node = new Node(0, {0, 0}, 0);
|
Node* current_node = new Node(0, {0, 0}, 0);
|
||||||
|
current_node->distance_from_start = 1;
|
||||||
nodes.push_back(current_node);
|
nodes.push_back(current_node);
|
||||||
|
|
||||||
for(uint16_t i=0; i<NODE_COUNT-1; i++) {
|
for(uint16_t i=0; i<NODE_COUNT-1; i++) {
|
||||||
@ -43,24 +43,12 @@ void setup_layout() {
|
|||||||
nodes.push_back(current_node);
|
nodes.push_back(current_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Node* node: nodes) {
|
bool verbose = false;
|
||||||
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) {
|
for (Node* n1 : nodes) {
|
||||||
LOGln("Looking for neighbours of node #%d @ %d,%d", n1->_number, n1->coords.x, n1->coords.y);
|
if (verbose) 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++) {
|
for(int edge=0; edge<CORNERS_PER_PART; edge++) {
|
||||||
Coords c = n1->coords_at_direction(edge);
|
Coords c = n1->coords_at_direction(edge);
|
||||||
LOGln(" Chcking edge %d @ %d,%d...", edge, c.x, c.y);
|
if (verbose) LOGln(" Checking edge %d @ %d,%d...", edge, c.x, c.y);
|
||||||
|
|
||||||
Node* found = nullptr;
|
Node* found = nullptr;
|
||||||
for(Node* n2 : nodes) {
|
for(Node* n2 : nodes) {
|
||||||
@ -73,7 +61,7 @@ void setup_layout() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (found != nullptr) {
|
if (found != nullptr) {
|
||||||
LOGln(" Found node #%d", found->_number);
|
if (verbose) LOGln(" Found node #%d", found->_number);
|
||||||
uint8_t inverse_dir = (n1->direction + 2*edge + 3) % 6;
|
uint8_t inverse_dir = (n1->direction + 2*edge + 3) % 6;
|
||||||
int8_t e = (inverse_dir - found->direction) % 6;
|
int8_t e = (inverse_dir - found->direction) % 6;
|
||||||
if (e < 0) e+=6;
|
if (e < 0) e+=6;
|
||||||
@ -84,30 +72,56 @@ void setup_layout() {
|
|||||||
int8_t e2 = (e - 1) % CORNERS_PER_PART;
|
int8_t e2 = (e - 1) % CORNERS_PER_PART;
|
||||||
if (e2<0) e2+=CORNERS_PER_PART;
|
if (e2<0) e2+=CORNERS_PER_PART;
|
||||||
|
|
||||||
LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, edge, found->_number, e2);
|
if (verbose) LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, edge, found->_number, e2);
|
||||||
n1->_corners[edge]->_short_neighbours.push_back(found->_corners[e2]);
|
n1->_corners[edge]->_short_neighbours.push_back(found->_corners[e2]);
|
||||||
|
|
||||||
LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, e1, found->_number, e);
|
if (verbose) LOGln(" Mapping Corner #%d,%d with #%d,%d", n1->_number, e1, found->_number, e);
|
||||||
n1->_corners[e1]->_short_neighbours.push_back(found->_corners[e]);
|
n1->_corners[e1]->_short_neighbours.push_back(found->_corners[e]);
|
||||||
} else {
|
} else {
|
||||||
LOGln(" No match.");
|
if (verbose) LOGln(" No match.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Corner* corner: corners) {
|
bool checked_all = false;
|
||||||
LOGln("Corner %p:", corner);
|
while (!checked_all) {
|
||||||
|
checked_all = true;
|
||||||
|
for(Node* n : nodes) {
|
||||||
|
if (n->distance_from_start == 0) {
|
||||||
|
checked_all = false;
|
||||||
|
} else {
|
||||||
|
for(int i=0; i<CORNERS_PER_PART; i++) {
|
||||||
|
if (n->neighbours[i] != nullptr && (n->neighbours[i]->distance_from_start==0 || n->neighbours[i]->distance_from_start > n->distance_from_start)) {
|
||||||
|
n->neighbours[i]->distance_from_start = n->distance_from_start+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Node* node : nodes) {
|
||||||
|
LOGln("Node #%d:", node->_number);
|
||||||
|
LOGln(" Distance from start: %d", node->distance_from_start);
|
||||||
|
for(Corner* corner : node->_corners) {
|
||||||
|
LOGln(" Corner #%d,%d:", node->_number, corner->number);
|
||||||
for (auto c: corner->_long_neighbours) {
|
for (auto c: corner->_long_neighbours) {
|
||||||
LOGln(" Long: %p", c);
|
LOGln(" Long neighbour: #%d,%d", c->node->_number, c->number);
|
||||||
}
|
}
|
||||||
for (auto c: corner->_short_neighbours) {
|
for (auto c: corner->_short_neighbours) {
|
||||||
LOGln(" Short: %p", c);
|
LOGln(" Short neighbour: #%d,%d", c->node->_number, c->number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i=0; i<CORNERS_PER_PART; i++) {
|
||||||
|
if (node->neighbours[i]==nullptr) {
|
||||||
|
LOGln(" Neighbour %d: -", i);
|
||||||
|
} else {
|
||||||
|
LOGln(" Neighbour %d: #%d", i, node->neighbours[i]->_number);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGln("Counts:");
|
LOGln("Counts:");
|
||||||
LOGln("Nodes: %3d", nodes.size());
|
LOGln("Nodes: %3d", nodes.size());
|
||||||
LOGln("Edges: %3d", edges.size());
|
|
||||||
LOGln("Corners: %3d", corners.size());
|
LOGln("Corners: %3d", corners.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,15 +145,31 @@ void show_all() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void show_status(uint8_t status, CRGB color=CRGB::Green) {
|
void show_status(uint8_t status, CRGB color=CRGB::Green) {
|
||||||
for (int i=0; i<status; i++) {
|
for (int i=0; i<status*LEDS_PER_CORNER && i<LED_COUNT; i++) {
|
||||||
if (i<corners.size()) {
|
leds[i] = color;
|
||||||
corners[i]->set_color(color);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i=status*LEDS_PER_CORNER; i<LED_COUNT; i++) {
|
||||||
|
leds[i] = CRGB::Black;
|
||||||
}
|
}
|
||||||
for (int i=status; i<corners.size(); i++) {
|
FastLED.show();
|
||||||
corners[i]->set_color(CRGB::Black);
|
}
|
||||||
|
|
||||||
|
void display_layout() {
|
||||||
|
for(Corner* corner : corners) {
|
||||||
|
corner->set_color(CRGB::Blue);
|
||||||
|
for(Corner* c : corner->_short_neighbours) {
|
||||||
|
c->set_color(CRGB::Red);
|
||||||
|
}
|
||||||
|
for(Corner* c : corner->_long_neighbours) {
|
||||||
|
c->set_color(CRGB::Green);
|
||||||
}
|
}
|
||||||
show_all();
|
show_all();
|
||||||
|
delay(1500);
|
||||||
|
for(Corner* c : corners) {
|
||||||
|
c->set_color(CRGB::Black);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@ -150,16 +180,16 @@ void setup() {
|
|||||||
setup_fastled();
|
setup_fastled();
|
||||||
show_status(1);
|
show_status(1);
|
||||||
|
|
||||||
setup_layout();
|
|
||||||
show_status(2);
|
|
||||||
|
|
||||||
#ifdef TEST_MODE
|
#ifdef TEST_MODE
|
||||||
LOGln("TEST_MODE is active!");
|
LOGln("TEST_MODE is active!");
|
||||||
#else
|
#else
|
||||||
|
show_status(2);
|
||||||
wifi_setup();
|
wifi_setup();
|
||||||
|
|
||||||
show_status(3);
|
show_status(3);
|
||||||
|
setup_layout();
|
||||||
|
|
||||||
|
show_status(4);
|
||||||
mqtt_setup();
|
mqtt_setup();
|
||||||
ArduinoOTA.setHostname(OTA_HOSTNAME);
|
ArduinoOTA.setHostname(OTA_HOSTNAME);
|
||||||
ArduinoOTA.onProgress([&](unsigned int progress, unsigned int total){
|
ArduinoOTA.onProgress([&](unsigned int progress, unsigned int total){
|
||||||
@ -169,10 +199,35 @@ void setup() {
|
|||||||
ArduinoOTA.onEnd([](){ show_status(0); });
|
ArduinoOTA.onEnd([](){ show_status(0); });
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
|
|
||||||
|
#ifdef WAIT_FOR_OTA
|
||||||
|
show_status(5);
|
||||||
|
LOGln("Waiting %d seconds for OTA requests...", WAIT_FOR_OTA);
|
||||||
|
unsigned long ota_target_time = millis() + WAIT_FOR_OTA*1000;
|
||||||
|
while (millis() < ota_target_time) {
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
LOGln("Done waiting for OTA requests.");
|
||||||
|
#endif
|
||||||
|
|
||||||
show_status(255);
|
show_status(255);
|
||||||
delay(250);
|
delay(250);
|
||||||
show_status(0);
|
show_status(0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
LOGln("Setup done.");
|
||||||
|
|
||||||
|
#ifdef SHOW_LAYOUT_AT_BOOT
|
||||||
|
LOGln("SHOW_LAYOUT_AT_BOOT is active - showing layout...");
|
||||||
|
display_layout();
|
||||||
|
LOGln("Showing layout is done.");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for(Corner* corner : corners) {
|
||||||
|
corner->set_color(CRGB::Black);
|
||||||
|
}
|
||||||
|
|
||||||
|
State::publish_current_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -196,19 +251,7 @@ void loop() {
|
|||||||
if (speedup > 0 && (millis() - last_loop > (20 / speedup) || last_loop > millis())) {
|
if (speedup > 0 && (millis() - last_loop > (20 / speedup) || last_loop > millis())) {
|
||||||
looping = false;
|
looping = false;
|
||||||
|
|
||||||
AnimationMode active_mode = mode;
|
AnimationMode active_mode = State::get_active_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) {
|
if (active_mode == AM_CORNERS || active_mode == AM_FIRST_CORNER) {
|
||||||
for(Corner* corner: corners) {
|
for(Corner* corner: corners) {
|
||||||
@ -251,6 +294,13 @@ void loop() {
|
|||||||
for(Node* node : nodes) {
|
for(Node* node : nodes) {
|
||||||
node->set_color(active_mode == AM_OFF ? CRGB::Black : color);
|
node->set_color(active_mode == AM_OFF ? CRGB::Black : color);
|
||||||
}
|
}
|
||||||
|
} else if (active_mode == AM_RAINBOW) {
|
||||||
|
const uint8_t speed = 5; // seconds (approx.) for full color sweep
|
||||||
|
const uint8_t diff_per_node = 5;
|
||||||
|
uint8_t base_hue = (millis() * 256 / speed) / 1000;
|
||||||
|
for(Node* node : nodes) {
|
||||||
|
node->set_color(CHSV(base_hue + diff_per_node * node->distance_from_start, 255, 255));
|
||||||
|
}
|
||||||
} else { // This includes AM_ERROR
|
} else { // This includes AM_ERROR
|
||||||
for(Node* node : nodes) {
|
for(Node* node : nodes) {
|
||||||
node->set_color(CRGB::Black);
|
node->set_color(CRGB::Black);
|
||||||
|
@ -5,15 +5,15 @@
|
|||||||
|
|
||||||
void connect() {
|
void connect() {
|
||||||
LOGln("Connecting to MQTT broker...");
|
LOGln("Connecting to MQTT broker...");
|
||||||
if (mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_TOPIC "state", 0, true, "OFFLINE")) {
|
if (mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_TOPIC "available", 0, true, "offline")) {
|
||||||
LOGln("Connected.");
|
LOGln("Connected.");
|
||||||
mqtt.publish(MQTT_TOPIC "available", "online", true);
|
mqtt.publish(MQTT_TOPIC "available", "online", true);
|
||||||
char buffer[40];
|
char buffer[40];
|
||||||
snprintf(buffer, 40, "online %s", wifi.localIP().toString().c_str());
|
snprintf(buffer, 40, "online %s", wifi.localIP().toString().c_str());
|
||||||
mqtt.publish(MQTT_TOPIC "available_long", buffer, true);
|
mqtt.publish(MQTT_TOPIC "available_long", buffer, true);
|
||||||
mqtt.subscribe(MQTT_TOPIC "cmnd");
|
mqtt.subscribe(MQTT_TOPIC "cmnd");
|
||||||
String discovery_msg = "{\""
|
String discovery_msg = "{"
|
||||||
"\"~\":\"" MQTT_TOPIC ",\"avty_t\":\"~available\",\"cmd_t\":\"~cmnd\",\"stat_t\":\"~state\","
|
"\"~\":\"" MQTT_TOPIC "\",\"avty_t\":\"~available\",\"cmd_t\":\"~cmnd\",\"stat_t\":\"~state\","
|
||||||
"\"name\":\"ESPleaf\",\"schema\":\"json\","
|
"\"name\":\"ESPleaf\",\"schema\":\"json\","
|
||||||
"\"brightness\":true,\"bri_scl\":255,"
|
"\"brightness\":true,\"bri_scl\":255,"
|
||||||
"\"effect\":true,\"effect_list\":[\"off\",\"corners\",\"nodes\",\"flash\",\"static\"],"
|
"\"effect\":true,\"effect_list\":[\"off\",\"corners\",\"nodes\",\"flash\",\"static\"],"
|
||||||
@ -62,11 +62,13 @@ void mqtt_setup() {
|
|||||||
mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
|
mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
|
||||||
mqtt.setCallback(callback);
|
mqtt.setCallback(callback);
|
||||||
mqtt.setSocketTimeout(1);
|
mqtt.setSocketTimeout(1);
|
||||||
|
mqtt.setBufferSize(400);
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_loop() {
|
void mqtt_loop() {
|
||||||
if (!mqtt.connected()) {
|
if (!mqtt.connected()) {
|
||||||
|
LOGln("MQTT disconnected. Reason: %d", mqtt.state());
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
mqtt.loop();
|
mqtt.loop();
|
||||||
|
@ -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);
|
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++) {
|
for(int i=0; i<CORNERS_PER_PART; i++) {
|
||||||
edges[i] = new Edge();
|
neighbours[i] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Corner* last_corner = nullptr;
|
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(this, i);
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -42,9 +42,6 @@ Node* Node::create_neighbour(uint8_t edge) {
|
|||||||
node->neighbours[0] = this;
|
node->neighbours[0] = this;
|
||||||
neighbours[edge] = node;
|
neighbours[edge] = node;
|
||||||
|
|
||||||
node->edges[0]->neighbour = this->edges[edge];
|
|
||||||
this->edges[edge]->neighbour = node->edges[0];
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
src/prototypes.cpp
Normal file
14
src/prototypes.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#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"},
|
||||||
|
{AM_RAINBOW, "rainbow"}
|
||||||
|
};
|
@ -27,14 +27,18 @@ void State::parse_state(String state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void State::parse_mode(String mode) {
|
void State::parse_mode(String mode) {
|
||||||
if (mode.equals("nodes")) { set_mode(AM_NODES); }
|
AnimationMode m = AM_NONE;
|
||||||
else if (mode.equals("first_node")) { set_mode(AM_FIRST_NODE); }
|
for (std::pair<AnimationMode, const char*> pair : animation_mode_names) {
|
||||||
else if (mode.equals("corners")) { set_mode(AM_CORNERS); }
|
if (mode.equals(pair.second)) {
|
||||||
else if (mode.equals("first_corner")) { set_mode(AM_FIRST_CORNER); }
|
m = pair.first;
|
||||||
else if (mode.equals("off")) { set_mode(AM_OFF); }
|
break;
|
||||||
else if (mode.equals("flash")) { set_mode(AM_FLASH); }
|
}
|
||||||
else if (mode.equals("static")) { set_mode(AM_STATIC); }
|
}
|
||||||
else { LOGln("parse_mode: Unknown mode '%s'.", mode.c_str()); }
|
if (m == AM_NONE) {
|
||||||
|
LOGln("parse_mode: Unknown mode '%s'.", mode.c_str());
|
||||||
|
} else {
|
||||||
|
set_mode(m);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void State::set_mode(AnimationMode m) {
|
void State::set_mode(AnimationMode m) {
|
||||||
@ -106,12 +110,30 @@ void State::publish_current_state() {
|
|||||||
StaticJsonDocument<512> json;
|
StaticJsonDocument<512> json;
|
||||||
json["state"] = (mode==AM_OFF) ? "OFF" : "ON";
|
json["state"] = (mode==AM_OFF) ? "OFF" : "ON";
|
||||||
json["brightness"] = FastLED.getBrightness();
|
json["brightness"] = FastLED.getBrightness();
|
||||||
json["effect"] = mode;
|
json["effect"] = animation_mode_names[get_active_mode()];
|
||||||
JsonObject rgb = json.createNestedObject("rgb");
|
JsonObject rgb = json.createNestedObject("rgb");
|
||||||
rgb["r"] = color.r;
|
rgb["r"] = color.r;
|
||||||
rgb["g"] = color.g;
|
rgb["g"] = color.g;
|
||||||
rgb["b"] = color.b;
|
rgb["b"] = color.b;
|
||||||
String result = "";
|
String result = "";
|
||||||
serializeJson(json, result);
|
serializeJson(json, result);
|
||||||
|
LOGln("Reporting current state: %s", result.c_str());
|
||||||
mqtt_publish_current_state(result);
|
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;
|
||||||
|
}
|
21
src/syslog.cpp
Normal file
21
src/syslog.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "syslog.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#ifdef SYSLOG_HOST
|
||||||
|
static uint16_t syslog_msg_id = 1;
|
||||||
|
|
||||||
|
void syslog(String msg, uint8_t severity) {
|
||||||
|
uint8_t facility = 16; // local0
|
||||||
|
if (severity > 7) severity = 7;
|
||||||
|
WiFiUDP udp;
|
||||||
|
|
||||||
|
udp.beginPacket(SYSLOG_HOST, SYSLOG_PORT);
|
||||||
|
udp.write("<");
|
||||||
|
udp.write(String(facility * 8 + severity).c_str());
|
||||||
|
udp.write(">1 - " OTA_HOSTNAME " core 1 ");
|
||||||
|
udp.write(String(syslog_msg_id++).c_str());
|
||||||
|
udp.write(" - ");
|
||||||
|
udp.write(msg.c_str());
|
||||||
|
udp.endPacket();
|
||||||
|
}
|
||||||
|
#endif
|
@ -2,13 +2,14 @@
|
|||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
|
|
||||||
void wifi_setup() {
|
void wifi_setup() {
|
||||||
LOG("Connecting to WiFi %s...", WIFI_SSID);
|
Serial.printf("Connecting to WiFi %s...", WIFI_SSID);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
LOG(".");
|
Serial.print(".");
|
||||||
delay(300);
|
delay(300);
|
||||||
}
|
}
|
||||||
|
Serial.println();
|
||||||
LOGln("Connected as %s", WiFi.localIP().toString().c_str());
|
LOGln("Connected as %s", WiFi.localIP().toString().c_str());
|
||||||
random16_add_entropy(micros());
|
random16_add_entropy(micros());
|
||||||
}
|
}
|
Reference in New Issue
Block a user