From d5bd755754a8be61eb718408cf1a64c9fd8f7b78 Mon Sep 17 00:00:00 2001 From: Jaro Date: Sun, 28 Nov 2021 22:30:20 +0100 Subject: [PATCH] Escape color codes rework --- .vscode/settings.json | 3 +- platformio.ini | 1 + src/Scroller.cpp | 144 ++++++++++++++++++++++++++++++++++++------ src/Scroller.h | 29 +++++++-- 4 files changed, 150 insertions(+), 27 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1428c38..ec3461a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "random": "cpp" + "random": "cpp", + "fastled.h": "c" } } \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 9e622dc..020d0c3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,3 +28,4 @@ lib_deps = sstaub/NTP@^1.4 aster94/Utilities@^0.4.6 adafruit/Adafruit BME280 Library@^2.1.2 + spacehuhn/SimpleMap@^1.0.0 diff --git a/src/Scroller.cpp b/src/Scroller.cpp index 33b51ba..21bf55f 100644 --- a/src/Scroller.cpp +++ b/src/Scroller.cpp @@ -1,8 +1,102 @@ #include "Scroller.h" +#include + +eDict_t Escapes[] = { + {"\u001b[30m","black",eDict_t::COLOR, CRGB::Black }, + {"\u001b[31m","red",eDict_t::COLOR, CRGB::Red }, + {"\u001b[32m","green",eDict_t::COLOR, CRGB::Green }, + {"\u001b[33m","yellow",eDict_t::COLOR, CRGB::Yellow }, + {"\u001b[34m","blue",eDict_t::COLOR, CRGB::Blue }, + {"\u001b[35m","magenta",eDict_t::COLOR, CRGB::Magenta }, + {"\u001b[36m","cyan",eDict_t::COLOR, CRGB::Cyan }, + {"\u001b[37m","white",eDict_t::COLOR, CRGB::White }, + {"\u001b[40m","black",eDict_t::BCOLOR, CRGB::Black }, + {"\u001b[41m","red",eDict_t::BCOLOR, CRGB::Red }, + {"\u001b[42m","green",eDict_t::BCOLOR, CRGB::Green }, + {"\u001b[43m","yellow",eDict_t::BCOLOR, CRGB::Yellow }, + {"\u001b[44m","blue",eDict_t::BCOLOR, CRGB::Blue }, + {"\u001b[45m","magenta",eDict_t::BCOLOR, CRGB::Magenta }, + {"\u001b[46m","cyan",eDict_t::BCOLOR, CRGB::Cyan }, + {"\u001b[47m","white",eDict_t::BCOLOR, CRGB::White }, + + {"\u001b[0m","reset",eDict_t::CONTROL, RESET_ALL}, + {"\u001b[1m","bold",eDict_t::CONTROL, BOLD}, + {"\u001b[4m","underline",eDict_t::CONTROL, UNDERLINE}, + {"\u001b[7m","reversed",eDict_t::CONTROL, REVERSED}, + {"\u001b[98;%dm","speed",eDict_t::CONTOL_WITH_PARAM, SPEED}, + {"\u001b[99;%dm","timeout",eDict_t::CONTOL_WITH_PARAM, TIMEOUT}, +}; + +SimpleMap* escMap; Scroller::Scroller(int max_loops) { _max_loops = max_loops; + _act_setting = _setting; + + for (int i=0; i< (sizeof(Escapes)/sizeof(Escapes[0])) ; i++) { + String s = String(Escapes[i].code); + + if (Escapes[i].tt == eDict_t::CONTOL_WITH_PARAM) { + int n = s.indexOf('%'); + if (n !=-1) + s = s.substring(0,n); + } + + escMap->put(s,&Escapes[i]); + Serial.printf("%s = %s\n",s.c_str(),Escapes[i].name); + } +} + +bool Scroller::decodeEscape2Setting(String es) +{ + String s; + eDict_t *e; + int p = es.indexOf(';'); + + if (p != -1) { + s = es.substring(0,p+1); + } else { + s = es; + } + + e = escMap->get(s); + if (!e) return false; + + if (e->tt == eDict_t::CONTROL) { + switch (e->control) { + case RESET_ALL: + _act_setting = _setting; + break; + case BOLD: + _act_setting.bold = true; + break; + case UNDERLINE: + _act_setting.underline = true; + break; + case REVERSED: + _act_setting.reversed = true; + break; + + } + } + if (e->tt == eDict_t::CONTOL_WITH_PARAM) + { + int param; + sscanf(es.c_str(),e->code,param); + switch (e->control) { + case SPEED: + _act_setting.speed = param; + break; + case TIMEOUT: + _act_setting.timeout = param; + break; + } + } + if (e->tt == eDict_t::COLOR) _act_setting.color = e->color; + if (e->tt == eDict_t::BCOLOR) _act_setting.bcolor = e->color; + + return true; } void Scroller::SetMatrix(FastLED_NeoMatrix *matrix) @@ -44,6 +138,24 @@ void Scroller::DeleteLines() _nlines = 0; } +void Scroller::Show(int _x,int _y,String line) +{ + _matrix->fillScreen(0); + _matrix->setCursor(_x, _y); + + for (int l=0;l < line.length();l++) { + if (line.charAt(l) == '\u001b') { + int ee = line.indexOf("m",l); + decodeEscape2Setting(line.substring(l,ee)); + l = ee; + } + _matrix->setTextColor(_act_setting.color); + _matrix->setBrightness(_act_setting.brightness); + if (l != line.length()) _matrix->print(line.charAt(l)); + } + _matrix->show(); +} + int Scroller::Scroll() { int mx = 32; @@ -55,32 +167,22 @@ int Scroller::Scroll() { if (_x < line.length() * 6 - mx) { - _matrix->fillScreen(0); - _matrix->setCursor(-_x, _y); - _matrix->setTextColor(_colors[_Settings[_lp].color]); - _matrix->setBrightness(_Settings[_lp].brightness); - _matrix->print(line); - _matrix->show(); + Show(-_x,_y,line); if (_x++ == 0) { - return _Settings[_lp].timeout_on/SCROLLER_TICK_TIME; + return _act_setting.timeout/SCROLLER_TICK_TIME; } else { - return _Settings[_lp].speed/SCROLLER_TICK_TIME; + return _act_setting.speed/SCROLLER_TICK_TIME; } } _lp++; _x = 0; - return _Settings[_lp].timeout_off/SCROLLER_TICK_TIME; + return _act_setting.timeout/SCROLLER_TICK_TIME; } else { - _matrix->fillScreen(0); - _matrix->setCursor(_x, _y); - _matrix->setTextColor(_colors[_Settings[_lp].color]); - _matrix->setBrightness(_Settings[_lp].brightness); - _matrix->print(line); - _matrix->show(); - return _Settings[_lp++].timeout_off/SCROLLER_TICK_TIME; + Show(_x,_y,line); + return _act_setting.timeout/SCROLLER_TICK_TIME; } } @@ -152,11 +254,11 @@ int Scroller::TimesScrolled() void Scroller::SetParameters(int line,uint8_t color,uint8_t brightness,int timeout_off,int timeout_on,int speed) { - _Settings[line].color = color; - _Settings[line].brightness = brightness; - _Settings[line].timeout_on = timeout_on; - _Settings[line].timeout_off = timeout_off; - _Settings[line].speed = speed; + _act_setting.color = color; + _act_setting.brightness = brightness; + _act_setting.timeout = timeout_on; + _act_setting.timeout = timeout_off; + _act_setting.speed = speed; } Scroller::~Scroller() diff --git a/src/Scroller.h b/src/Scroller.h index 584aa9a..e480fdc 100644 --- a/src/Scroller.h +++ b/src/Scroller.h @@ -9,22 +9,38 @@ #define SCROLLER_TICK_TIME 100 //ms +typedef struct { + char const *code; + char const *name; + enum{COLOR, BCOLOR, CONTROL, CONTOL_WITH_PARAM} tt; //Tag Type + union { + uint32_t color; + uint16_t control; + } ; +} eDict_t; + +enum {RESET_ALL,BOLD, UNDERLINE=4,REVERSED=7, SPEED, TIMEOUT}; + struct Setting { - uint8_t color; + uint32_t color; + uint32_t bcolor; uint8_t brightness; - int timeout_off; - int timeout_on; + int timeout; int speed; + bool reversed; + bool bold; + bool underline; }; class Scroller { private: /* data */ - const static int MAX_LINES = 10; + const static int MAX_LINES = 100; String _Lines[MAX_LINES]; - Setting _Settings[MAX_LINES]; + Setting _setting = { CRGB::Red, CRGB::Black, 10, 2000, false, false, false}; // default setting + Setting _act_setting; // actual setting int _nlines = 0; int _lp = 0; int _tick=0; @@ -39,7 +55,9 @@ private: public: Scroller(int max_loops); + bool TurnOn(); + bool decodeEscape2Setting(String es); void SetColors(uint16_t *colors); void SetMatrix(FastLED_NeoMatrix *matrix); int AddLine(String line); @@ -53,6 +71,7 @@ public: void PrintSerialLines(); void TurnOff(); void loop(ulong tick); + void Show(int _x,int _y, String line); ~Scroller(); }; #endif \ No newline at end of file