diff --git a/platformio.ini b/platformio.ini index 020d0c3..5fd0b7b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ platform = espressif32 board = esp32doit-devkit-v1 framework = arduino -monitor_port = COM[1367] +monitor_port = COM[136789] monitor_speed = 115200 lib_deps = knolleary/PubSubClient@^2.8 diff --git a/src/Scroller.cpp b/src/Scroller.cpp index 21bf55f..afef185 100644 --- a/src/Scroller.cpp +++ b/src/Scroller.cpp @@ -1,5 +1,6 @@ #include "Scroller.h" #include +//#define DEBUG eDict_t Escapes[] = { {"\u001b[30m","black",eDict_t::COLOR, CRGB::Black }, @@ -33,19 +34,33 @@ Scroller::Scroller(int max_loops) { _max_loops = max_loops; _act_setting = _setting; + +} +void Scroller::init() +{ + Serial.println("INIT ESCAPES"); + escMap = new SimpleMap([](String &a, String &b) -> int { + if (a == b) return 0; // a and b are equal + else if (a > b) return 1; // a is bigger than b + else return -1; // a is smaller than b + }); for (int i=0; i< (sizeof(Escapes)/sizeof(Escapes[0])) ; i++) { String s = String(Escapes[i].code); - + String escape; + + escape = String(s); if (Escapes[i].tt == eDict_t::CONTOL_WITH_PARAM) { int n = s.indexOf('%'); if (n !=-1) - s = s.substring(0,n); + escape = String(s.substring(0,n)); } - escMap->put(s,&Escapes[i]); - Serial.printf("%s = %s\n",s.c_str(),Escapes[i].name); + Serial.printf("%s = %s\n",escape,Escapes[i].name); + escMap->put(escape,&Escapes[i]); + } + DumpSetting(_act_setting); } bool Scroller::decodeEscape2Setting(String es) @@ -60,6 +75,7 @@ bool Scroller::decodeEscape2Setting(String es) s = es; } + //Serial.printf("ESC GET = %s\n",s); e = escMap->get(s); if (!e) return false; @@ -83,7 +99,7 @@ bool Scroller::decodeEscape2Setting(String es) if (e->tt == eDict_t::CONTOL_WITH_PARAM) { int param; - sscanf(es.c_str(),e->code,param); + sscanf(es.c_str(),e->code,¶m); switch (e->control) { case SPEED: _act_setting.speed = param; @@ -95,10 +111,17 @@ bool Scroller::decodeEscape2Setting(String es) } if (e->tt == eDict_t::COLOR) _act_setting.color = e->color; if (e->tt == eDict_t::BCOLOR) _act_setting.bcolor = e->color; - +#ifdef DEBUG + DumpSetting(_act_setting); +#endif return true; } +void Scroller::DumpSetting(Setting & s) +{ + Serial.printf("[COLOR=%x Brightness=%d Speed=%d Timeout=%d]\n",s.color,s.brightness,s.speed,s.timeout); +} + void Scroller::SetMatrix(FastLED_NeoMatrix *matrix) { _matrix = matrix; @@ -112,7 +135,7 @@ int Scroller::GetNumberOfLines() int Scroller::AddLine(String line) { _Lines[_nlines] = line; - SetParameters(_nlines); + //SetParameters(_nlines); return _nlines++; } @@ -120,7 +143,7 @@ int Scroller::AddLines(String* lines,int nlines) { for (int i=0; i < nlines; i++) { _Lines[i] = lines[i]; - SetParameters(i); + //SetParameters(i); } _nlines = nlines; @@ -145,27 +168,53 @@ void Scroller::Show(int _x,int _y,String line) for (int l=0;l < line.length();l++) { if (line.charAt(l) == '\u001b') { - int ee = line.indexOf("m",l); - decodeEscape2Setting(line.substring(l,ee)); + int ee = line.indexOf('m',l); + decodeEscape2Setting(line.substring(l,ee+1)); l = ee; + continue; } - _matrix->setTextColor(_act_setting.color); + _matrix->setTextColor(Framebuffer_GFX::Color24to16(_act_setting.color)); _matrix->setBrightness(_act_setting.brightness); - if (l != line.length()) _matrix->print(line.charAt(l)); + if (l != line.length()) { +#ifdef DEBUG + Serial.print(line.charAt(l)); +#endif + _matrix->print(line.charAt(l)); + } + } +#ifdef DEBUG + Serial.print("\n"); +#endif _matrix->show(); } +String Scroller::LineUnescaped(String line) +{ + String rawLine = ""; + for (int l=0;l < line.length();l++) { + if (line.charAt(l) == '\u001b') { + int ee = line.indexOf('m',l); + l = ee; + continue; + } + if (l != line.length()) { + rawLine.concat(line.charAt(l)); + } + } + return rawLine; +} + int Scroller::Scroll() { int mx = 32; String line; line = _Lines[_lp]; - - if (line.length() * 6 > mx) + int ll = LineUnescaped(line).length(); + if (ll * 6 > mx) { - if (_x < line.length() * 6 - mx) + if (_x < ll * 6 - mx) { Show(-_x,_y,line); if (_x++ == 0) { @@ -182,6 +231,7 @@ int Scroller::Scroll() else { Show(_x,_y,line); + _lp++; return _act_setting.timeout/SCROLLER_TICK_TIME; } } @@ -234,6 +284,7 @@ void Scroller::loop(ulong tick) } if (TimesScrolled() == _max_loops) { + Serial.println("MAX LOOP"); TurnOff(); return; } diff --git a/src/Scroller.h b/src/Scroller.h index e480fdc..413063c 100644 --- a/src/Scroller.h +++ b/src/Scroller.h @@ -39,7 +39,7 @@ private: /* data */ const static int MAX_LINES = 100; String _Lines[MAX_LINES]; - Setting _setting = { CRGB::Red, CRGB::Black, 10, 2000, false, false, false}; // default setting + Setting _setting = { CRGB::Red, CRGB::Black, 10, 3000, 100, false, false, false}; // default setting Setting _act_setting; // actual setting int _nlines = 0; int _lp = 0; @@ -57,6 +57,9 @@ public: Scroller(int max_loops); bool TurnOn(); + void init(); + String LineUnescaped(String); + void DumpSetting(Setting &); bool decodeEscape2Setting(String es); void SetColors(uint16_t *colors); void SetMatrix(FastLED_NeoMatrix *matrix); diff --git a/src/main.cpp b/src/main.cpp index 8bcecd4..7d7d2bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -164,6 +164,7 @@ void setup() setup_matrix(); + scroller.init(); scroller.SetColors(colors); scroller.SetMatrix(matrix); @@ -242,7 +243,7 @@ boolean reconnect() { void decode_payload(String msg) { - StringSplitter *splitter = new StringSplitter(msg, ';', 2); + /*StringSplitter *splitter = new StringSplitter(msg, ';', 2); int n = 0; int i = 0; String s,m; @@ -266,16 +267,16 @@ void decode_payload(String msg) if (cmd == "on") on = param.toInt(); } } - - i = scroller.AddLine(m); - if (n == 2) { + */ + scroller.AddLine(msg); + /*if (n == 2) { Serial.printf("color=%d,brightness=%d,off=%d,on=%d,speed=%d\n",color,brightness,off,on,speed); scroller.SetParameters(i,color,brightness,off,on,speed); - } + }*/ } void callback(char* topic, byte* payload, unsigned int length) { - char msg[128]; + char msg[256]; Serial.print("Message arrived ["); Serial.print(topic); @@ -292,8 +293,8 @@ void callback(char* topic, byte* payload, unsigned int length) { msg[length] = '\0'; // Switch on the LED if an 1 was received as first character - scroller.TurnOff(); - scroller.DeleteLines(); + //scroller.TurnOff(); + //scroller.DeleteLines(); decode_payload(msg); showTextMqtt = true; } else if (String("get/home/sensor/decka/bme280") == String(topic)) {