Moved settings from preprocessor directives to variables. Also added a way to (for now only) display them via HTTP server.

This commit is contained in:
Fabian Schlenz 2019-10-02 06:13:55 +02:00
parent 382631d7d7
commit 3f6d4cb0be
16 changed files with 154 additions and 79 deletions

View File

@ -3,6 +3,7 @@
#include <Arduino.h> #include <Arduino.h>
#define FASTLED_INTERNAL #define FASTLED_INTERNAL
#include <FastLED.h> #include <FastLED.h>
#include "prototypes.h"
//#define DEBUG // Uncomment this to enable Debug messages via Serial and, if enabled, MQTT. //#define DEBUG // Uncomment this to enable Debug messages via Serial and, if enabled, MQTT.
//#define CONFIG_USABLE // Uncomment this by removing the // at the beginning! //#define CONFIG_USABLE // Uncomment this by removing the // at the beginning!
@ -49,39 +50,35 @@
#define MONITOR_LOOP_TIME_THRESHOLD 500 #define MONITOR_LOOP_TIME_THRESHOLD 500
#define MONITOR_LOOP_TIME_COUNT_MAX 10 #define MONITOR_LOOP_TIME_COUNT_MAX 10
#define EFFECT_CYCLE_TIME 300 // Time in seconds between cycling effects. // settings.effects.cycle.time = 300; // Time in seconds between cycling effects.
#define EFFECT_CYCLE_RANDOM true // settings.effects.cycle.random = true;
#define EFFECT_MATRIX_LENGTH_MIN 4 // settings.effects.matrix.length_min = 4;
#define EFFECT_MATRIX_LENGTH_MAX 20 // settings.effects.matrix.length_max = 20;
#define EFFECT_MATRIX_SPEED_MIN 50 // settings.effects.matrix.speed_min = 1;
#define EFFECT_MATRIX_SPEED_MAX 135 // settings.effects.matrix.speed_max = 10;
#define EFFECT_SINGLE_DYNAMIC_LOOP_TIME 40 // .dynamic.single_loop_time = 40;
#define EFFECT_MULTI_DYNAMIC_LOOP_TIME 1400 // .dynamic.multi_loop_time = 1400;
#define EFFECT_BIG_DYNAMIC_LOOP_TIME 50 // .dynamic.big_loop_time = 50;
#define EFFECT_BIG_DYNAMIC_SIZE 3 // .dynamic.big_size = 3;
#define EFFECT_CONFETTI_PIXELS_PER_LOOP 2 // .fire.cooldown = 192;
// .fire.spark_chance = 5;
#define EFFECT_SNAKE_DIRECTION_CHANGE 10 // .firework.drag = 255;
#define EFFECT_SNAKE_SLOWDOWN 2 // .firework.bounce = 200;
// .firework.gravity = 10;
// .firework.sparks = 12;
#define EFFECT_FIRE_COOLDOWN 192 // .gol.start_percentage = 90;
#define EFFECT_FIRE_SPARK_CHANCE 5 // .gol.blend_speed = 10;
// .gol.restart_after_steps = 100;
#define EFFECT_FIREWORK_SHOT_CHANCE 200 // .sines.count = 5;
#define EFFECT_FIREWORK_BLUR 200
#define EFFECT_FIREWORK_FADEOUT_SPEED 5
#define EFFECT_GOL_START_PERCENTAGE 90 // .snake.direction_change = 5;
#define EFFECT_GOL_BLEND_SPEED 10 // .snake.slowdown = 2;
#define EFFECT_GOL_RESTART_AFTER_STEPS 100
#define EFFECT_DVD_WIDTH 3
#define EFFECT_DVD_HEIGHT 2
#define EFFECT_SINES_COUNT 5
// Stop editing here // Stop editing here

View File

@ -6,11 +6,6 @@
enum FireworkDotType { FIREWORK_DOT_NONE, FIREWORK_DOT_SHELL, FIREWORK_DOT_SPARK }; enum FireworkDotType { FIREWORK_DOT_NONE, FIREWORK_DOT_SHELL, FIREWORK_DOT_SPARK };
#define EFFECT_FIREWORK_DRAG 255
#define EFFECT_FIREWORK_BOUNCE 200
#define EFFECT_FIREWORK_GRAVITY 10
#define EFFECT_FIREWORK_SPARKS 12
class FireworkEffect; class FireworkEffect;
class FireworkEffectDot { class FireworkEffectDot {
@ -49,7 +44,7 @@ private:
CRGB _burst_color; CRGB _burst_color;
FireworkEffectDot* _dot; FireworkEffectDot* _dot;
FireworkEffectDot* _sparks[EFFECT_FIREWORK_SPARKS]; FireworkEffectDot** _sparks;
public: public:
FireworkEffect(); FireworkEffect();
~FireworkEffect(); ~FireworkEffect();

View File

@ -20,7 +20,7 @@ public:
class SinesEffect : public Effect { class SinesEffect : public Effect {
private: private:
SinesEffectSinus* _sinus[EFFECT_SINES_COUNT]; SinesEffectSinus** _sinus;
uint8_t _step = 0; uint8_t _step = 0;
public: public:
SinesEffect(); SinesEffect();

View File

@ -2,6 +2,9 @@
#include <Arduino.h> #include <Arduino.h>
extern uint8_t baseHue;
extern char hostname[30];
typedef struct { typedef struct {
uint8_t width; uint8_t width;
uint8_t height; uint8_t height;
@ -27,5 +30,78 @@ typedef struct {
uint16_t y; uint16_t y;
} Coords; } Coords;
extern uint8_t baseHue; enum SettingType {
extern char hostname[30]; TYPE_UINT8,
TYPE_UINT16,
TYPE_BOOL
};
typedef struct {
const char* name;
uint16_t* value;
SettingType type;
} Setting;
struct /* settings */{
uint16_t fps = 50;
struct /* effects */ {
struct /* cycle */ {
uint16_t time = 300;
uint16_t random = 1;
} cycle ;
struct /* matrix */ {
uint16_t length_min = 4;
uint16_t length_max = 20;
uint16_t speed_min = 1;
uint16_t speed_max = 10;
} matrix;
struct /* confetti */ {
uint16_t pixels_per_loop = 2;
} confetti;
struct /* dvd */ {
uint16_t width = 3;
uint16_t height = 2;
} dvd;
struct /* dynamic */ {
uint16_t single_loop_time = 40;
uint16_t multi_loop_time = 1400;
uint16_t big_loop_time = 50;
uint16_t big_size = 3;
} dynamic;
struct /* fire */ {
uint16_t cooldown = 192;
uint16_t spark_chance = 5;
} fire;
struct /* firework */ {
uint16_t drag = 255;
uint16_t bounce = 200;
uint16_t gravity = 10;
uint16_t sparks = 12;
} firework;
struct /* gol */ {
uint16_t start_percentage = 90;
uint16_t blend_speed = 10;
uint16_t restart_after_steps = 100;
} gol;
struct /* sines */ {
uint16_t count = 5;
} sines;
struct /* snake */ {
uint16_t direction_change = 5;
uint16_t slowdown = 2;
} snake;
} effects;
} settings;
extern Setting all_settings[];
extern const uint8_t all_settings_size;

View File

@ -5,7 +5,7 @@
void ConfettiEffect::loop(uint16_t ms) { void ConfettiEffect::loop(uint16_t ms) {
window->fadeToBlackBy(3); window->fadeToBlackBy(3);
for (int i=0; i<EFFECT_CONFETTI_PIXELS_PER_LOOP; i++) { for (int i=0; i<settings.effects.confetti.pixels_per_loop; i++) {
CRGB color = _getColor(); CRGB color = _getColor();
window->addPixelColor(random16(LED_COUNT), &color); window->addPixelColor(random16(LED_COUNT), &color);
} }

View File

@ -11,8 +11,8 @@ CycleEffect::~CycleEffect() {
} }
void CycleEffect::changeEffect() { void CycleEffect::changeEffect() {
int new_id; uint8_t new_id;
if (EFFECT_CYCLE_RANDOM) { if (settings.effects.cycle.random && _effects_count>1) {
do { do {
new_id = random8(cycle_effects_count); new_id = random8(cycle_effects_count);
} while (new_id == effect_id); } while (new_id == effect_id);

View File

@ -7,11 +7,11 @@ void DvdEffect::loop(uint16_t ms) {
_x += _x_dir; _x += _x_dir;
_y += _y_dir; _y += _y_dir;
if (_x == 0 || _x + EFFECT_DVD_WIDTH >= window->width) { if (_x == 0 || _x + settings.effects.dvd.width >= window->width) {
_x_dir = -_x_dir; _x_dir = -_x_dir;
dir_changed = true; dir_changed = true;
} }
if (_y == 0 || _y + EFFECT_DVD_HEIGHT >= window->height) { if (_y == 0 || _y + settings.effects.dvd.height >= window->height) {
_y_dir = -_y_dir; _y_dir = -_y_dir;
dir_changed = true; dir_changed = true;
} }
@ -19,7 +19,7 @@ void DvdEffect::loop(uint16_t ms) {
window->clear(); window->clear();
for (int x=0; x<EFFECT_DVD_WIDTH; x++) for (int y=0; y<EFFECT_DVD_HEIGHT; y++) { for (int x=0; x<settings.effects.dvd.width; x++) for (int y=0; y<settings.effects.dvd.height; y++) {
window->setPixel(_x + x, _y + y, (CRGB*)&_color); window->setPixel(_x + x, _y + y, (CRGB*)&_color);
} }

View File

@ -11,7 +11,7 @@ void SingleDynamicEffect::init() {
} }
void SingleDynamicEffect::loop(uint16_t ms) { void SingleDynamicEffect::loop(uint16_t ms) {
EVERY_N_MILLISECONDS( EFFECT_SINGLE_DYNAMIC_LOOP_TIME ) { EVERY_N_MILLISECONDS( settings.effects.dynamic.single_loop_time ) {
tiles[random8(tile_count)] = CHSV(baseHue + random8(64), 180, 255); tiles[random8(tile_count)] = CHSV(baseHue + random8(64), 180, 255);
} }
this->draw(); this->draw();
@ -29,7 +29,7 @@ boolean SingleDynamicEffect::can_be_shown_with_clock() {
} }
void MultiDynamicEffect::loop(uint16_t ms) { void MultiDynamicEffect::loop(uint16_t ms) {
EVERY_N_MILLISECONDS( EFFECT_MULTI_DYNAMIC_LOOP_TIME ) { EVERY_N_MILLISECONDS( settings.effects.dynamic.multi_loop_time ) {
for (int i=0; i<tile_count; i++) tiles[i] = CHSV(baseHue + random8(64), 180, 255); for (int i=0; i<tile_count; i++) tiles[i] = CHSV(baseHue + random8(64), 180, 255);
} }
this->draw(); this->draw();
@ -40,23 +40,15 @@ BigDynamicEffect::~BigDynamicEffect() {
} }
void BigDynamicEffect::loop(uint16_t ms) { void BigDynamicEffect::loop(uint16_t ms) {
EVERY_N_MILLISECONDS( EFFECT_BIG_DYNAMIC_LOOP_TIME ) { EVERY_N_MILLISECONDS( settings.effects.dynamic.big_loop_time ) {
uint8_t x = random8(0, window->width - EFFECT_BIG_DYNAMIC_SIZE + 1); uint8_t x = random8(0, window->width - settings.effects.dynamic.big_size + 1);
uint8_t y = random8(0, window->height - EFFECT_BIG_DYNAMIC_SIZE + 1); uint8_t y = random8(0, window->height - settings.effects.dynamic.big_size + 1);
CRGB color = CHSV(random8(), 255, 255); CRGB color = CHSV(random8(), 255, 255);
CRGB black(0x000000); CRGB black(0x000000);
for (uint8_t ix=0; ix<EFFECT_BIG_DYNAMIC_SIZE; ix++) for (uint8_t iy=0; iy<EFFECT_BIG_DYNAMIC_SIZE; iy++) { for (uint8_t ix=0; ix<settings.effects.dynamic.big_size; ix++) for (uint8_t iy=0; iy<settings.effects.dynamic.big_size; iy++) {
window->setPixel(x+ix, y+iy, &color); window->setPixel(x+ix, y+iy, &color);
} }
/*for (uint8_t ix=0; ix<EFFECT_BIG_DYNAMIC_SIZE+2; ix++) {
window->setPixel(x-1+ix, y-1, &black);
window->setPixel(x-1+ix, y+EFFECT_BIG_DYNAMIC_SIZE+1, &black);
}
for (uint8_t iy=0; iy<EFFECT_BIG_DYNAMIC_SIZE+2; iy++) {
window->setPixel(x-1, y-1+iy, &black);
window->setPixel(x+EFFECT_BIG_DYNAMIC_SIZE+1, y-1+iy, &black);
}*/
} }
} }

View File

@ -22,11 +22,11 @@ void FireEffect::loop(uint16_t ms) {
} }
void FireEffect::cooldown() { void FireEffect::cooldown() {
for(int i=0; i<(this->window->width * this->window->height); i++) this->data[i] = scale8(this->data[i], EFFECT_FIRE_COOLDOWN); // 240 or something for(int i=0; i<(this->window->width * this->window->height); i++) this->data[i] = scale8(this->data[i], settings.effects.fire.cooldown);
} }
void FireEffect::spark() { void FireEffect::spark() {
for(int x=0; x<this->window->width; x++) if (random8(EFFECT_FIRE_SPARK_CHANCE)==0) this->data[x] = this->spark_temp(); for(int x=0; x<this->window->width; x++) if (random8(settings.effects.fire.spark_chance)==0) this->data[x] = this->spark_temp();
} }
inline uint8_t FireEffect::spark_temp() { inline uint8_t FireEffect::spark_temp() {

View File

@ -56,13 +56,13 @@ void FireworkEffectDot::draw() {
void FireworkEffectDot::move() { void FireworkEffectDot::move() {
if (!show) return; if (!show) return;
_yv -= EFFECT_FIREWORK_GRAVITY; _yv -= settings.effects.firework.gravity;
_xv = _scale15by8_local(_xv, EFFECT_FIREWORK_DRAG); _xv = _scale15by8_local(_xv, settings.effects.firework.drag);
_yv = _scale15by8_local(_yv, EFFECT_FIREWORK_DRAG); _yv = _scale15by8_local(_yv, settings.effects.firework.drag);
if (type == FIREWORK_DOT_SPARK) { if (type == FIREWORK_DOT_SPARK) {
_xv = _scale15by8_local(_xv, EFFECT_FIREWORK_DRAG); _xv = _scale15by8_local(_xv, settings.effects.firework.drag);
_yv = _scale15by8_local(_yv, EFFECT_FIREWORK_DRAG); _yv = _scale15by8_local(_yv, settings.effects.firework.drag);
_color.nscale8(255); _color.nscale8(255);
if (!_color) { if (!_color) {
show = 0; show = 0;
@ -75,7 +75,7 @@ void FireworkEffectDot::move() {
show = 0; show = 0;
} else { } else {
_yv = -_yv; _yv = -_yv;
_yv = _scale15by8_local(_yv, EFFECT_FIREWORK_BOUNCE); _yv = _scale15by8_local(_yv, settings.effects.firework.bounce);
if (_yv < 500) { if (_yv < 500) {
show = 0; show = 0;
} }
@ -143,7 +143,7 @@ void FireworkEffect::loop(uint16_t ms) {
window->clear(); window->clear();
_dot->move(); _dot->move();
_dot->draw(); _dot->draw();
for (int i=0; i<EFFECT_FIREWORK_SPARKS; i++) { for (int i=0; i<settings.effects.firework.sparks; i++) {
_sparks[i]->move(); _sparks[i]->move();
_sparks[i]->draw(); _sparks[i]->draw();
} }
@ -159,7 +159,7 @@ void FireworkEffect::loop(uint16_t ms) {
} }
if (_skyburst) { if (_skyburst) {
int nsparks = random8(EFFECT_FIREWORK_SPARKS / 2, EFFECT_FIREWORK_SPARKS + 1); int nsparks = random8(settings.effects.firework.sparks / 2, settings.effects.firework.sparks + 1);
for (int i=0; i<nsparks; i++) { for (int i=0; i<nsparks; i++) {
_sparks[i]->sky_burst(_burst_x, _burst_y, _burst_yv, _burst_color); _sparks[i]->sky_burst(_burst_x, _burst_y, _burst_yv, _burst_color);
_skyburst = 0; _skyburst = 0;
@ -169,14 +169,15 @@ void FireworkEffect::loop(uint16_t ms) {
FireworkEffect::FireworkEffect() { FireworkEffect::FireworkEffect() {
_dot = new FireworkEffectDot(window, this); _dot = new FireworkEffectDot(window, this);
for (int i=0; i<EFFECT_FIREWORK_SPARKS; i++) { _sparks = new FireworkEffectDot*[settings.effects.firework.sparks];
for (int i=0; i<settings.effects.firework.sparks; i++) {
_sparks[i] = new FireworkEffectDot(window, this); _sparks[i] = new FireworkEffectDot(window, this);
} }
} }
FireworkEffect::~FireworkEffect() { FireworkEffect::~FireworkEffect() {
delete window; delete window;
for (int i=0; i<EFFECT_FIREWORK_SPARKS; i++) { for (int i=0; i<settings.effects.firework.sparks; i++) {
delete _sparks[i]; delete _sparks[i];
} }
delete _dot; delete _dot;

View File

@ -16,7 +16,7 @@ bool GolEffect::can_be_shown_with_clock() { return true; }
void GolEffect::_initialize() { void GolEffect::_initialize() {
for(uint16_t i=0; i<this->window->count; i++) { for(uint16_t i=0; i<this->window->count; i++) {
_data[i] = random8() < EFFECT_GOL_START_PERCENTAGE ? 1 : 0; _data[i] = random8() < settings.effects.gol.start_percentage ? 1 : 0;
} }
_old_hue = _hue; _old_hue = _hue;
_hue = random8(); _hue = random8();
@ -30,11 +30,11 @@ GolEffect::~GolEffect() {
} }
void GolEffect::loop(uint16_t ms) { void GolEffect::loop(uint16_t ms) {
if (EFFECT_GOL_BLEND_SPEED + _blend > 255) { if (settings.effects.gol.blend_speed + _blend > 255) {
_blend = 0; _blend = 0;
_advance(); _advance();
} else { } else {
_blend += EFFECT_GOL_BLEND_SPEED; _blend += settings.effects.gol.blend_speed;
} }
_draw(); _draw();
@ -43,7 +43,7 @@ void GolEffect::loop(uint16_t ms) {
void GolEffect::_advance() { void GolEffect::_advance() {
_step++; _step++;
_old_hue = _hue; _old_hue = _hue;
if (_step >= EFFECT_GOL_RESTART_AFTER_STEPS) { if (_step >= settings.effects.gol.restart_after_steps) {
_initialize(); _initialize();
} else { } else {
for(uint16_t i=0; i<this->window->count; i++) { for(uint16_t i=0; i<this->window->count; i++) {

View File

@ -38,9 +38,9 @@ void MatrixEffectColumn::restart(bool completely_random) {
} }
} }
length = random8(EFFECT_MATRIX_LENGTH_MIN, EFFECT_MATRIX_LENGTH_MAX); length = random8(settings.effects.matrix.length_min, settings.effects.matrix.length_max);
running = true; running = true;
speed = random8(EFFECT_MATRIX_SPEED_MIN, EFFECT_MATRIX_SPEED_MAX); speed = random8(settings.effects.matrix.speed_min, settings.effects.matrix.speed_max);
} }
void MatrixEffectColumn::advance(uint16_t ms) { void MatrixEffectColumn::advance(uint16_t ms) {

View File

@ -25,13 +25,14 @@ void SinesEffectSinus::loop(uint16_t ms) {
} }
SinesEffect::SinesEffect() { SinesEffect::SinesEffect() {
for (int i=0; i<EFFECT_SINES_COUNT; i++) { _sinus = new SinesEffectSinus*[settings.effects.sines.count];
for (int i=0; i<settings.effects.sines.count; i++) {
_sinus[i] = new SinesEffectSinus(window); _sinus[i] = new SinesEffectSinus(window);
} }
} }
SinesEffect::~SinesEffect() { SinesEffect::~SinesEffect() {
for (int i=0; i<EFFECT_SINES_COUNT; i++) { delete _sinus[i]; } for (int i=0; i<settings.effects.sines.count; i++) { delete _sinus[i]; }
} }
boolean SinesEffect::can_be_shown_with_clock() { boolean SinesEffect::can_be_shown_with_clock() {
@ -42,7 +43,7 @@ void SinesEffect::loop(uint16_t ms) {
// do stuff // do stuff
if (_step++ % 4) return; // Skip 3 out of 4 steps. if (_step++ % 4) return; // Skip 3 out of 4 steps.
window->shift_down_and_blur(); window->shift_down_and_blur();
for (int i=0; i<EFFECT_SINES_COUNT; i++) { for (int i=0; i<settings.effects.sines.count; i++) {
_sinus[i]->loop(ms); _sinus[i]->loop(ms);
} }
} }

View File

@ -11,8 +11,8 @@ SnakeEffect::~SnakeEffect() {
} }
void SnakeEffect::loop(uint16_t ms) { void SnakeEffect::loop(uint16_t ms) {
if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run. if (run++ % settings.effects.snake.slowdown == 0) { // Change the coordinates only on every n-th run.
if (random8(EFFECT_SNAKE_DIRECTION_CHANGE)==0 || is_turn_needed()) turn_random(); if (random8(settings.effects.snake.direction_change)==0 || is_turn_needed()) turn_random();
this->coords = update_position(this->coords, this->direction); this->coords = update_position(this->coords, this->direction);
} }

View File

@ -1,4 +1,5 @@
#include "effects.h" #include "effects.h"
#include "config.h"
#include "my_fastled.h" #include "my_fastled.h"
#include <ErriezCRC32.h> #include <ErriezCRC32.h>
#include "effect_bell.h" #include "effect_bell.h"

View File

@ -42,7 +42,7 @@ void http_server_setup() {
PGM_P text_plain = PSTR("text/plain"); PGM_P text_plain = PSTR("text/plain");
http_server.on("/", HTTP_GET, [&](){ http_server.on("/", HTTP_GET, [&](){
LOGln("HTTP * GET /"); LOGln("HTTP * GET /");
String message = "<html><head><title>Pitrix</title></head><body><h1>Pitrix</h1><p>Known animations:</p>"; String message = "<html><head><title>Pitrix</title></head><body><h1>Pitrix</h1><a href='/settings'>Settings</a><p>Known animations:</p>";
if (!SPIFFS.begin()) { if (!SPIFFS.begin()) {
message += "<strong>No SPIFFS file system found.</strong>"; message += "<strong>No SPIFFS file system found.</strong>";
} else { } else {
@ -57,6 +57,18 @@ void http_server_setup() {
message += "</body></html>"; message += "</body></html>";
http_server.send(200, "text/html", message); http_server.send(200, "text/html", message);
}); });
http_server.on("/settings", HTTP_GET, [&]() {
String message = "<html><head><title>Pitrix settings</title></head><body><h1>Pitrix settings</h1><a href='/'>Back to main page</a><table>";
for (int i=0; i<all_settings_size; i++) {
message += "<tr><td>";
message += all_settings[i].name;
message += "</td><td>";
message += *all_settings[i].value;
message += "</td></tr>";
}
message += "</table></body></html>";
http_server.send(200, "text/html", message);
});
http_server.on("/delete", HTTP_GET, [&]() { http_server.on("/delete", HTTP_GET, [&]() {
LOGln("HTTP * GET /delete"); LOGln("HTTP * GET /delete");
if (http_server.args()==0) { if (http_server.args()==0) {