Merge pull request #11018 from gemu2015/displaytext

update displaytext dv
This commit is contained in:
Theo Arends 2021-02-18 12:54:01 +01:00 committed by GitHub
commit 13c35773e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 140 additions and 90 deletions

View File

@ -540,14 +540,10 @@ void Renderer::drawPixel(int16_t x, int16_t y, uint16_t color) {
} }
extern uint16_t index_colors[MAX_INDEXCOLORS];
// this is called for every driver // this is called for every driver
void Renderer::setDrawMode(uint8_t mode) { void Renderer::setDrawMode(uint8_t mode) {
drawmode=mode; drawmode=mode;
for (uint32_t count = 0; count < MAX_INDEXCOLORS; count++) {
index_colors[count] = GetColorFromIndex(count);
}
} }
void Renderer::invertDisplay(boolean i) { void Renderer::invertDisplay(boolean i) {

View File

@ -151,7 +151,8 @@ uint8_t dsp_font;
uint8_t dsp_flag; uint8_t dsp_flag;
uint8_t dsp_on; uint8_t dsp_on;
uint16_t index_colors[MAX_INDEXCOLORS]; #define PREDEF_INDEXCOLORS 19
uint16_t index_colors[MAX_INDEXCOLORS - PREDEF_INDEXCOLORS];
#ifdef USE_DISPLAY_MODES1TO5 #ifdef USE_DISPLAY_MODES1TO5
@ -322,7 +323,12 @@ uint32_t decode_te(char *line) {
uint16_t GetColorFromIndex(uint32_t index) { uint16_t GetColorFromIndex(uint32_t index) {
if (index >= MAX_INDEXCOLORS) index = 0; if (index >= MAX_INDEXCOLORS) index = 0;
return index_colors[index];
if (index < PREDEF_INDEXCOLORS) {
return renderer->GetColorFromIndex(index);
} else {
return index_colors[index - PREDEF_INDEXCOLORS];
}
} }
void DisplayText(void) void DisplayText(void)
@ -610,14 +616,15 @@ void DisplayText(void)
cp++; cp++;
var = fatoiv(cp, &ftemp); var = fatoiv(cp, &ftemp);
cp += var; cp += var;
if (temp >= MAX_INDEXCOLORS) temp = 0; if (temp >= MAX_INDEXCOLORS) temp = PREDEF_INDEXCOLORS;
index_colors[temp] = ftemp; if (temp < PREDEF_INDEXCOLORS) temp = PREDEF_INDEXCOLORS;
index_colors[temp - PREDEF_INDEXCOLORS] = ftemp;
break; break;
} }
#ifdef USE_DT_VARS #ifdef USE_DT_VARS
if (*cp == 'v') { if (*cp == 'v') {
cp++; cp++;
{ int16_t num, gxp, gyp, textbcol, textfcol, font, textsize, txlen, dp; { int16_t num, gxp, gyp, textbcol, textfcol, font, textsize, txlen, dp, time;
var=atoiv(cp,&num); var=atoiv(cp,&num);
cp+=var; cp+=var;
cp++; cp++;
@ -645,12 +652,15 @@ void DisplayText(void)
var=atoiv(cp,&dp); var=atoiv(cp,&dp);
cp+=var; cp+=var;
cp++; cp++;
var=atoiv(cp,&time);
cp+=var;
cp++;
// text itself // text itself
char bbuff[32]; char bbuff[32];
cp = get_string(bbuff, sizeof(bbuff), cp); cp = get_string(bbuff, sizeof(bbuff), cp);
char unit[4]; char unit[4];
cp = get_string(unit, sizeof(unit), cp); cp = get_string(unit, sizeof(unit), cp);
define_dt_var(num, gxp, gyp, textbcol, textfcol, font, textsize, txlen, dp, bbuff, unit); define_dt_var(num, gxp, gyp, textbcol, textfcol, font, textsize, txlen, time, dp, bbuff, unit);
} }
} }
#endif // USE_DT_VARS #endif // USE_DT_VARS
@ -995,6 +1005,8 @@ typedef struct {
int8_t txtlen; int8_t txtlen;
int8_t dp; int8_t dp;
int8_t font; int8_t font;
int8_t time;
int8_t timer;
char unit[6]; char unit[6];
char *jstrbuf; char *jstrbuf;
char rstr[32]; char rstr[32];
@ -1002,7 +1014,7 @@ typedef struct {
DT_VARS *dt_vars[MAX_DT_VARS]; DT_VARS *dt_vars[MAX_DT_VARS];
void define_dt_var(uint32_t num, uint32_t xp, uint32_t yp, uint32_t txtbcol, uint32_t txtfcol, int32_t font, int32_t txtsiz, int32_t txtlen, int32_t dp, char *jstr, char *unit) { void define_dt_var(uint32_t num, uint32_t xp, uint32_t yp, uint32_t txtbcol, uint32_t txtfcol, int32_t font, int32_t txtsiz, int32_t txtlen, int32_t time, int32_t dp, char *jstr, char *unit) {
if (num >= MAX_DT_VARS) return; if (num >= MAX_DT_VARS) return;
if (dt_vars[num]) { if (dt_vars[num]) {
@ -1022,70 +1034,106 @@ void define_dt_var(uint32_t num, uint32_t xp, uint32_t yp, uint32_t txtbcol, u
dtp->txtfcol = txtfcol; dtp->txtfcol = txtfcol;
dtp->font = font; dtp->font = font;
dtp->txtsiz = txtsiz; dtp->txtsiz = txtsiz;
dtp->time = time;
if (txtlen > MAX_DVTSIZE) {txtlen = MAX_DVTSIZE;} if (txtlen > MAX_DVTSIZE) {txtlen = MAX_DVTSIZE;}
dtp->txtlen = txtlen; dtp->txtlen = txtlen;
dtp->dp = dp; dtp->dp = dp;
dtp->jstrbuf = (char*)malloc(strlen(jstr + 1)); uint8_t jlen = strlen(jstr);
dtp->jstrbuf = (char*)calloc(jlen + 2,1);
if (!dtp->jstrbuf) { if (!dtp->jstrbuf) {
free (dtp); free (dtp);
return; return;
} }
dtp->rstr[0] = 0;
strcpy(dtp->unit, unit);
strcpy(dtp->jstrbuf, jstr); strcpy(dtp->jstrbuf, jstr);
strcpy(dtp->unit,unit); if (!time) time = 1;
dtp->timer = time;
} }
void draw_dt_vars(void) { void draw_dt_vars(void) {
if (!renderer) return; if (!renderer) return;
for (uint32_t cnt = 0; cnt < MAX_DT_VARS; cnt++) { for (uint32_t cnt = 0; cnt < MAX_DT_VARS; cnt++) {
if (dt_vars[cnt]) { DT_VARS *dtp = dt_vars[cnt];
if (dt_vars[cnt]->jstrbuf) { if (dtp) {
if (dtp->jstrbuf) {
// draw // draw
dtp->timer--;
if (!dtp->timer) {
dtp->timer = dtp->time;
char vstr[MAX_DVTSIZE + 7]; char vstr[MAX_DVTSIZE + 7];
memset(vstr, ' ', sizeof(vstr)); memset(vstr, ' ', sizeof(vstr));
strcpy(vstr, dt_vars[cnt]->rstr); strcpy(vstr, dtp->rstr);
strcat(vstr, " "); strcat(vstr, " ");
strcat(vstr, dt_vars[cnt]->unit); strcat(vstr, dtp->unit);
uint16_t slen = strlen(vstr); uint16_t slen = strlen(vstr);
vstr[slen] = ' '; vstr[slen] = ' ';
if (!dt_vars[cnt]->txtlen) { if (!dtp->txtlen) {
vstr[slen] = 0; vstr[slen] = 0;
} else { } else {
vstr[abs(int(dt_vars[cnt]->txtlen))] = 0; vstr[abs(int(dtp->txtlen))] = 0;
} }
if (dt_vars[cnt]->txtlen < 0) { if (dtp->txtlen < 0) {
// right align // right align
alignright(vstr); alignright(vstr);
} }
if (dt_vars[cnt]->txtsiz > 0) { if (dtp->txtsiz > 0) {
renderer->setDrawMode(0); renderer->setDrawMode(0);
} else { } else {
renderer->setDrawMode(2); renderer->setDrawMode(2);
} }
renderer->setTextColor(GetColorFromIndex(dt_vars[cnt]->txtfcol),GetColorFromIndex(dt_vars[cnt]->txtbcol)); renderer->setTextColor(GetColorFromIndex(dtp->txtfcol),GetColorFromIndex(dtp->txtbcol));
renderer->setTextFont(dt_vars[cnt]->font); renderer->setTextFont(dtp->font);
renderer->setTextSize(abs(dt_vars[cnt]->txtsiz)); renderer->setTextSize(abs(dtp->txtsiz));
renderer->DrawStringAt(dt_vars[cnt]->xp, dt_vars[cnt]->yp, vstr, GetColorFromIndex(dt_vars[cnt]->txtfcol), 0);
// reset display vars if (dtp->jstrbuf[0]=='[') {
uint16_t s_disp_xpos = disp_xpos;
uint16_t s_disp_ypos = disp_ypos;
uint16_t s_bg_color = bg_color;
uint16_t s_fg_color = fg_color;
disp_xpos = dtp->xp;
disp_ypos = dtp->yp;
bg_color = GetColorFromIndex(dtp->txtbcol);
fg_color = GetColorFromIndex(dtp->txtfcol);
char *savmbd = XdrvMailbox.data;
XdrvMailbox.data = dtp->jstrbuf;
DisplayText();
XdrvMailbox.data = savmbd;
disp_xpos = s_disp_xpos;
disp_ypos = s_disp_ypos;
bg_color = s_bg_color;
fg_color = s_fg_color;
} else {
renderer->DrawStringAt(dtp->xp, dtp->yp, vstr, GetColorFromIndex(dtp->txtfcol), 0);
}
// restore display vars
renderer->setTextColor(fg_color, bg_color); renderer->setTextColor(fg_color, bg_color);
renderer->setDrawMode(auto_draw); renderer->setDrawMode(auto_draw);
} }
} }
} }
}
} }
#define DTV_JSON_SIZE 1024 #define DTV_JSON_SIZE 1024
void DTVarsTeleperiod(void) { void DTVarsTeleperiod(void) {
char *json = (char*)malloc(DTV_JSON_SIZE); if (TasmotaGlobal.mqtt_data && TasmotaGlobal.mqtt_data[0]) {
uint32_t jlen = strlen(TasmotaGlobal.mqtt_data);
if (jlen < DTV_JSON_SIZE) {
char *json = (char*)malloc(jlen + 2);
if (json) { if (json) {
strlcpy(json, TasmotaGlobal.mqtt_data, DTV_JSON_SIZE); strlcpy(json, TasmotaGlobal.mqtt_data, jlen + 1);
get_dt_vars(json); get_dt_vars(json);
free(json); free(json);
} }
}
}
} }
void get_dt_mqtt(void) { void get_dt_mqtt(void) {
@ -1108,7 +1156,7 @@ void get_dt_vars(char *json) {
for (uint32_t cnt = 0; cnt < MAX_DT_VARS; cnt++) { for (uint32_t cnt = 0; cnt < MAX_DT_VARS; cnt++) {
if (dt_vars[cnt]) { if (dt_vars[cnt]) {
if (dt_vars[cnt]->jstrbuf) { if (dt_vars[cnt]->jstrbuf && dt_vars[cnt]->jstrbuf[0]!='[') {
char sbuf[32]; char sbuf[32];
uint32_t res = JsonParsePath(&obj, dt_vars[cnt]->jstrbuf, '#', NULL, sbuf, sizeof(sbuf)); uint32_t res = JsonParsePath(&obj, dt_vars[cnt]->jstrbuf, '#', NULL, sbuf, sizeof(sbuf));
if (res) { if (res) {
@ -1536,15 +1584,21 @@ void DisplayInitDriver(void)
renderer->setTextSize(Settings.display_size); renderer->setTextSize(Settings.display_size);
// force opaque mode // force opaque mode
renderer->setDrawMode(0); renderer->setDrawMode(0);
for (uint32_t cnt = 0; cnt < (MAX_INDEXCOLORS - PREDEF_INDEXCOLORS); cnt++) {
index_colors[cnt] = 0;
} }
}
#ifdef USE_DT_VARS
free_dt_vars();
#endif
#ifdef USE_UFILESYS #ifdef USE_UFILESYS
Display_Text_From_File("/display.ini"); Display_Text_From_File("/display.ini");
#endif #endif
#ifdef USE_DT_VARS
free_dt_vars();
#endif
// AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Display model %d"), Settings.display_model); // AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Display model %d"), Settings.display_model);