Compare commits

11 Commits

Author SHA1 Message Date
5549c417ca Blink implementacia 2022-01-22 19:12:36 +01:00
18b13fe506 Blink begin 2022-01-06 17:48:10 +01:00
512be25b9e Reversed funcionality 2022-01-06 17:39:58 +01:00
4b8bc6b0ff Fix bugs 2021-12-24 21:20:32 +01:00
2ef3284a10 Do not reset all settings with esc reset_all 2021-12-20 18:47:18 +01:00
116193b940 IsOff functon, 10ms timer 2021-12-14 14:33:15 +01:00
762ed25669 Cleaned added max loops esc, clear esc 2021-11-30 21:23:33 +01:00
e88638c146 cleaned 2021-11-30 16:03:06 +01:00
a9e6126e8f Added, tested 2021-11-29 21:12:35 +01:00
d5bd755754 Escape color codes rework 2021-11-28 22:30:20 +01:00
d078d7a699 Mqtt + bme280 2020-11-24 20:44:08 +01:00
5 changed files with 363 additions and 133 deletions

View File

@@ -1,5 +1,6 @@
{
"files.associations": {
"random": "cpp"
"random": "cpp",
"fastled.h": "c"
}
}

View File

@@ -8,11 +8,11 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:ttgo-t1]
[env:esp32doit-devkit-v1]
platform = espressif32
board = ttgo-t1
board = esp32doit-devkit-v1
framework = arduino
monitor_port = COM[1367]
monitor_port = COM[456789]
monitor_speed = 115200
lib_deps =
knolleary/PubSubClient@^2.8
@@ -27,3 +27,5 @@ lib_deps =
aharshac/StringSplitter@^1.0.0
sstaub/NTP@^1.4
aster94/Utilities@^0.4.6
adafruit/Adafruit BME280 Library@^2.1.2
spacehuhn/SimpleMap@^1.0.0

View File

@@ -1,8 +1,138 @@
#include "Scroller.h"
#include <SimpleMap.h>
//#define DEBUG
Scroller::Scroller(int max_loops)
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[5m","blink",eDict_t::CONTROL, BLINK},
{"\u001b[7m","reversed",eDict_t::CONTROL, REVERSED},
{"\u001b[2J","clear",eDict_t::CONTROL, CLEAR_ALL},
{"\u001b[97;%dm","loops",eDict_t::CONTOL_WITH_PARAM, LOOPS},
{"\u001b[98;%dm","speed",eDict_t::CONTOL_WITH_PARAM, SPEED},
{"\u001b[99;%dm","timeout",eDict_t::CONTOL_WITH_PARAM, TIMEOUT},
};
SimpleMap<String, eDict_t*>* escMap;
Scroller::Scroller()
{
_max_loops = max_loops;
_act_setting = _setting;
}
void Scroller::init()
{
Serial.println("INIT ESCAPES");
escMap = new SimpleMap<String, eDict_t*>([](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)
escape = String(s.substring(0,n));
}
Serial.printf("%s = %s\n",escape,Escapes[i].name);
escMap->put(escape,&Escapes[i]);
}
DumpSetting(_act_setting);
}
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;
}
//Serial.printf("ESC GET = %s\n",s);
e = escMap->get(s);
if (!e) return false;
if (e->tt == eDict_t::CONTROL) {
switch (e->control) {
case CLEAR_ALL:
DeleteLines();
case RESET_ALL:
_act_setting.font = _setting.font;
break;
case BOLD:
_act_setting.font.bold = true;
break;
case UNDERLINE:
_act_setting.font.underline = true;
break;
case BLINK:
_act_setting.font.blink = true;
_blink = true;
if (_blinkTimeout == 0) _blinkTimeout = BLINK_TICKS;
break;
case REVERSED:
_act_setting.font.reversed = true;
uint32_t tcolor = _act_setting.font.bcolor;
_act_setting.font.bcolor = _act_setting.font.color;
_act_setting.font.color = tcolor;
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.control.speed = param;
break;
case TIMEOUT:
_act_setting.control.timeout = param;
break;
case LOOPS:
_act_setting.control.loops = param;
break;
}
}
if (e->tt == eDict_t::COLOR) _act_setting.font.color = e->color;
if (e->tt == eDict_t::BCOLOR) _act_setting.font.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.font.color,s.font.brightness,s.control.speed,s.control.timeout);
}
void Scroller::SetMatrix(FastLED_NeoMatrix *matrix)
@@ -17,8 +147,8 @@ int Scroller::GetNumberOfLines()
int Scroller::AddLine(String line)
{
if (_nlines > MAX_LINES) return _nlines;
_Lines[_nlines] = line;
SetParameters(_nlines);
return _nlines++;
}
@@ -26,7 +156,6 @@ int Scroller::AddLines(String* lines,int nlines)
{
for (int i=0; i < nlines; i++) {
_Lines[i] = lines[i];
SetParameters(i);
}
_nlines = nlines;
@@ -42,45 +171,102 @@ bool Scroller::ReplaceLine(int number,String line)
void Scroller::DeleteLines()
{
_nlines = 0;
_lp = 0;
}
int Scroller::Scroll()
void Scroller::NextLine()
{
_lp++;
_blink = false;
}
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;
for (ee = l+2; (line.charAt(ee) >= '0' && line.charAt(ee) <= '9') || line.charAt(ee) == ';'; ee++ )
;
decodeEscape2Setting(line.substring(l,ee+1));
l = ee;
continue;
}
uint32_t fcolor;
if (_blink && _act_setting.font.blink == true && _blinkState == BLINK_OFF)
fcolor = _act_setting.font.bcolor;
else
fcolor = _act_setting.font.color;
_matrix->setTextColor(Framebuffer_GFX::Color24to16(fcolor),Framebuffer_GFX::Color24to16(_act_setting.font.bcolor));
_matrix->setBrightness(_act_setting.font.brightness);
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;
for (ee = l+2; (line.charAt(ee) >= '0' && line.charAt(ee) <= '9') || line.charAt(ee) == ';'; ee++ )
;
l = ee;
continue;
}
if (l != line.length()) {
rawLine.concat(line.charAt(l));
}
}
return rawLine;
}
void 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)
{
_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;
_redrawTicks = _tick + _act_setting.control.timeout/SCROLLER_TICK_TIME;
return;
}
else {
return _Settings[_lp].speed/SCROLLER_TICK_TIME;
_redrawTicks = _tick + _act_setting.control.speed/SCROLLER_TICK_TIME;
return;
}
}
_lp++;
NextLine();
_x = 0;
return _Settings[_lp].timeout_off/SCROLLER_TICK_TIME;
_redrawTicks = _tick + _act_setting.control.timeout/SCROLLER_TICK_TIME;
return;
}
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);
NextLine();
_redrawTicks = _tick + _act_setting.control.timeout/SCROLLER_TICK_TIME;
return;
}
}
@@ -92,6 +278,11 @@ void Scroller::PrintSerialLines()
}
}
bool Scroller::IsOff()
{
return _off;
}
bool Scroller::TurnOn()
{
Serial.println("Turn on Scroller");
@@ -100,49 +291,53 @@ bool Scroller::TurnOn()
_x = 0;
_y = 0;
_nscrolled = 0;
_delayTicks = 0;
_delayTicks = 100;
return true;
}
void Scroller::TurnOff()
{
DeleteLines();
Serial.println("TURN OFF");
_matrix->fillScreen(0);
_matrix->print("");
_matrix->show();
_off = true;
}
void Scroller::SetColors(uint16_t *colors)
{
_colors = colors;
_blink = false;
}
void Scroller::loop(ulong tick)
{
if (_off) return;
if (_off || _nlines == 0) return;
int delta = tick - _tick;
if (_lp == _nlines) {
_lp =0;
_nscrolled++;
_delayTicks = 50;
}
if (_blink) _blinkTimeout -= delta;
_tick = tick;
if (TimesScrolled() == _max_loops) {
if (TimesScrolled() > _act_setting.control.loops) {
Serial.println("MAX LOOP");
TurnOff();
return;
}
if (_delayTicks - delta <= 0) _delayTicks = Scroll();
else _delayTicks -= delta;
if (_delayTicks < 0) _delayTicks = 0;
if (_redrawTicks <= _tick || (_blink && _blinkTimeout <= 0)) {
if (_blink && _blinkTimeout <=0) {
_blinkState = _blinkState == BLINK_OFF ? BLINK_ON : BLINK_OFF;
_blinkTimeout = BLINK_TICKS;
}
Scroll();
}
if (_lp > _nlines) {
_lp =0;
_nscrolled++;
}
_tick = tick;
}
int Scroller::TimesScrolled()
@@ -152,11 +347,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.font.color = color;
_act_setting.font.brightness = brightness;
_act_setting.control.timeout = timeout_on;
_act_setting.control.timeout = timeout_off;
_act_setting.control.speed = speed;
}
Scroller::~Scroller()

View File

@@ -7,52 +7,86 @@
#include "ESP32TimerInterrupt.h"
#define SCROLLER_TICK_TIME 100 //ms
#define SCROLLER_TICK_TIME 10 //ms
#define BLINK_TICKS (250/SCROLLER_TICK_TIME) // 250 ms for change time
typedef struct {
char const *code;
char const *name;
enum{COLOR, BCOLOR, FONT, CONTROL, CONTOL_WITH_PARAM} tt; //Tag Type
union {
uint32_t color;
uint16_t control;
void *font;
} ;
} eDict_t;
enum {RESET_ALL,CLEAR_ALL, BOLD, UNDERLINE=4, BLINK=5,REVERSED=7, SPEED, TIMEOUT, LOOPS};
enum eblinkState {BLINK_ON, BLINK_OFF} ;
struct Setting
{
uint8_t color;
uint8_t brightness;
int timeout_off;
int timeout_on;
int speed;
struct {
uint32_t color;
uint32_t bcolor;
uint8_t brightness;
bool underline;
bool reversed;
bool bold;
bool blink;
} font;
struct {
uint8_t loops;
int timeout;
int speed;
} control;
};
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, false, false, false}, {1,3000,100} }; // default setting
Setting _act_setting; // actual setting
int _nlines = 0;
int _lp = 0;
int _tick=0;
int _delayTicks = 0;
ulong _tick=0;
int _delayTicks = 100;
ulong _redrawTicks;
boolean _blink = false;
int _blinkTimeout = 0;
eblinkState _blinkState;
int _nscrolled = 0;
int _x = 0, _y = 0;
FastLED_NeoMatrix *_matrix;
uint16_t *_colors;
int _max_loops;
bool _off = true;
public:
Scroller(int max_loops);
Scroller();
bool TurnOn();
void SetColors(uint16_t *colors);
void init();
String LineUnescaped(String);
void DumpSetting(Setting &);
bool decodeEscape2Setting(String es);
void SetMatrix(FastLED_NeoMatrix *matrix);
int AddLine(String line);
void SetParameters(int line,uint8_t color=1,uint8_t brightness=10,int timeout_off=2000,int timeout_on=2000,int speed=200);
int AddLines(String* lines,int nlines);
void DeleteLines();
void NextLine();
bool ReplaceLine(int number,String line);
int GetNumberOfLines();
int Scroll();
void Scroll();
int TimesScrolled();
void PrintSerialLines();
bool IsOff();
void TurnOff();
void loop(ulong tick);
void Show(int _x,int _y, String line);
~Scroller();
};
#endif

View File

@@ -1,3 +1,7 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <SPI.h>
#include <FastLED.h>
@@ -25,9 +29,9 @@
#define PSTR // Make Arduino Due happy
#endif
#define COMMAND_PARAMETER_LENGTH 30
#define MAX_TIMES_SCROLLED 5
#define PIN 27
Adafruit_BME280 bme; // I2C
#define MATRIX_PIN 13
WiFiClient espClient;
PubSubClient client(espClient);
@@ -55,11 +59,7 @@ IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttSe
#define NUMMATRIX (lcdw*lcdh)
CRGB matrixleds[NUMMATRIX];
FastLED_NeoMatrix *matrix;
uint16_t colors[] = {
Framebuffer_GFX::Color(255, 0, 0), Framebuffer_GFX::Color(0, 255, 0), Framebuffer_GFX::Color(0, 0, 255)};
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
@@ -70,7 +70,7 @@ NTP ntp(ntpUDP);
TimeZone DateTime;
Scroller scroller(MAX_TIMES_SCROLLED);
Scroller scroller;
unsigned long last_millis;
bool showTextMqtt = false;
@@ -103,11 +103,11 @@ void setup_matrix()
matrix = new FastLED_NeoMatrix(matrixleds, lcdw, lcdh, 1, 1,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG );
FastLED.addLeds<NEOPIXEL,PIN>(matrixleds, NUMMATRIX);
FastLED.addLeds<NEOPIXEL,MATRIX_PIN>(matrixleds, NUMMATRIX);
matrix->begin();
matrix->setTextWrap(false);
matrix->setBrightness(20);
matrix->setTextColor(colors[1]);
matrix->setTextColor(Framebuffer_GFX::Color24to16(CRGB::Green));
matrix->print("Start");
matrix->show();
delay(1000);
@@ -125,6 +125,8 @@ void wifiConnected()
needMqttConnect = true;
}
#define SEALEVELPRESSURE_HPA (1027.3)
void setup()
{
Serial.begin(115200);
@@ -156,9 +158,39 @@ void setup()
setup_matrix();
scroller.SetColors(colors);
scroller.init();
scroller.SetMatrix(matrix);
Serial.println(F("BME280 test"));
if (! bme.begin(0x76, &Wire)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
}
Serial.println("-- Default Test --");
Serial.println("normal mode, 16x oversampling for all, filter off,");
bme.takeForcedMeasurement(); // has no effect in normal mode
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
void handleRoot()
@@ -189,6 +221,7 @@ boolean reconnect() {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("home/scroller");
client.subscribe("get/home/sensor/decka/bme280");
return true;
} else {
Serial.print("failed, rc=");
@@ -200,60 +233,30 @@ boolean reconnect() {
}
}
void decode_payload(String msg)
{
StringSplitter *splitter = new StringSplitter(msg, ';', 2);
int n = 0;
int i = 0;
String s,m;
int color=1,brightness=10,speed=200, off=2000,on=2000;
if (splitter->getItemCount() == 2)
s = splitter->getItemAtIndex(n++);
m = splitter->getItemAtIndex(n++);
if (n == 2) {
StringSplitter *splitter = new StringSplitter(s, ':', 10);
String cmd,param;
while (i < splitter->getItemCount()) {
cmd = splitter->getItemAtIndex(i++);
param = splitter->getItemAtIndex(i++);
Serial.printf("cmd=%s,param=%s\n",cmd,param);
if (cmd == "c") color = param.toInt();
if (cmd == "b") brightness = param.toInt();
if (cmd == "s") speed = param.toInt();
if (cmd == "off") off = param.toInt();
if (cmd == "on") on = param.toInt();
}
}
i = scroller.AddLine(m);
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);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
if (String("home/scroller") == String(topic)) {
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
strncpy(msg,(char *)payload,length>255 ? 255 : length);
msg[length>255 ? 255 : length] = '\0';
if (scroller.IsOff()) scroller.TurnOn();
scroller.AddLine(msg);
} else if (String("get/home/sensor/decka/bme280") == String(topic)) {
client.publish("home/sensor/decka/bme280/temperature",(String("N:") + String(bme.readTemperature())).c_str());
client.publish("home/sensor/decka/bme280/humidity",(String("N:") + String(bme.readHumidity())).c_str());
client.publish("home/sensor/decka/bme280/pressure",(String("N:") + String(bme.readPressure() / 100.0F)).c_str());
}
Serial.println();
strncpy(msg,(char *)payload,length);
msg[length] = '\0';
// Switch on the LED if an 1 was received as first character
scroller.TurnOff();
scroller.DeleteLines();
decode_payload(msg);
showTextMqtt = true;
}
void loop()
@@ -297,11 +300,6 @@ void loop()
updateNtpTime = false;
}
if (showTextMqtt) {
scroller.TurnOn();
showTextMqtt = false;
}
scroller.loop(tick);
client.loop();
}