Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move renderer, add touch display slider #10903

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,96 @@ void VButton::xdrawButton(bool inverted) {
wr_redir=0;
}

boolean VButton::didhit(int16_t x, int16_t y) {
return ((x >= spars.xp) && (x < (int16_t) (spars.xp + spars.xs)) &&
(y >= spars.yp) && (y < (int16_t) (spars.yp + spars.ys)));
}

void VButton::SliderInit(Renderer *renderer, uint16_t xp, uint16_t yp, uint16_t xs, uint16_t ys, uint16_t nelem, uint16_t bgcol, uint16_t frcol, uint16_t barcol) {
spars.xp = xp;
spars.yp = yp;
spars.xs = xs;
spars.ys = ys;
spars.nelem = nelem;
spars.bgcol = bgcol;
spars.frcol = frcol;
spars.barcol = barcol;
rend = renderer;

rend->fillRect(spars.xp, spars.yp, spars.xs, spars.ys, spars.bgcol);

if (xs < ys) {
float bxs = spars.xs - 6;
float bys = (float)(spars.ys - 6) / (float)nelem;
float bxp = xp + 3;
float byp = yp + 3;
for (uint32_t count = 0; count < spars.nelem; count++) {
rend->fillRect(bxp, byp, bxs, bys - 3, spars.barcol);
rend->drawRect(bxp, byp, bxs, bys - 3, spars.frcol);
byp += bys;
}
} else {
float bys = spars.ys - 6;
float bxs = (float)(spars.xs - 6) / (float)nelem;
float byp = yp + 3;
float bxp = xp + 3;
for (uint32_t count = 0; count < spars.nelem; count++) {
rend->fillRect(bxp, byp, bxs - 3 , bys, spars.barcol);
rend->drawRect(bxp, byp, bxs - 3, bys, spars.frcol);
bxp += bxs;
}
}
}

uint16_t VButton::UpdateSlider(int16_t x, int16_t y) {
uint16_t elems = spars.nelem + 1;

if (x < 0) {
x = spars.xp + (-x * spars.xs) / 100;
y = spars.yp + (spars.ys - (-y * spars.ys) / 100);
}

if (spars.xs < spars.ys) {
uint16_t dy = spars.ys - (y - spars.yp);
uint16_t limit = elems - ((float)dy /(float)spars.ys * elems);
float bxs = spars.xs - 6;
float bys = (float)(spars.ys - 6) / (float)spars.nelem;
float bxp = spars.xp + 3;
float byp = spars.yp + 3;
uint16_t col;
for (uint32_t count = 0; count < spars.nelem; count++) {
if (count >= limit) {
col = spars.barcol;
} else {
col = spars.bgcol;
}
rend->fillRect(bxp, byp, bxs, bys - 3, col);
rend->drawRect(bxp, byp, bxs, bys - 3, spars.frcol);
byp += bys;
}
return 100 - (float(y - spars.yp) / (float)spars.ys) * 100.0;
} else {
uint16_t limit = (x - spars.xp) * elems / spars.xs;
float bys = spars.ys - 6;
float bxs = (float)(spars.xs - 6) / (float)spars.nelem;
float byp = spars.yp + 3;
float bxp = spars.xp + 3;
uint16_t col;
for (uint32_t count = 0; count < spars.nelem; count++) {
if (count < limit) {
col = spars.barcol;
} else {
col = spars.bgcol;
}
rend->fillRect(bxp, byp, bxs - 3, bys, col);
rend->drawRect(bxp, byp, bxs - 3 , bys, spars.frcol);
bxp += bxs;
}
return (float(x - spars.xp) / (float)spars.xs) * 100.0;
}

}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,35 @@ typedef union {
uint8_t spare0 : 1;
uint8_t spare1 : 1;
uint8_t spare2 : 1;
uint8_t spare3 : 1;
uint8_t slider : 1;
uint8_t disable : 1;
uint8_t on_off : 1;
uint8_t is_pushbutton : 1;
uint8_t is_virtual : 1;
};
} TButton_State;


struct Slider {
uint16_t xp;
uint16_t yp;
uint16_t xs;
uint16_t ys;
uint16_t nelem;
uint16_t bgcol;
uint16_t frcol;
uint16_t barcol;
};

class VButton : public Adafruit_GFX_Button {
public:
TButton_State vpower;
struct Slider spars;
Renderer *rend;
void xdrawButton(bool inverted);
boolean didhit(int16_t x, int16_t y);
uint16_t UpdateSlider(int16_t x, int16_t y);
void SliderInit(Renderer *rend, uint16_t xp, uint16_t yp, uint16_t xs, uint16_t ys, uint16_t nelem, uint16_t bgcol, uint16_t frcol, uint16_t barcol);
};


Expand Down
154 changes: 97 additions & 57 deletions tasmota/xdrv_13_display.ino
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ void DisplayText(void)
cp += var;
linebuf[fill] = 0;
break;
#if (defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)) || defined(USE_UFILESYS)
#ifdef USE_UFILESYS
case 'P':
{ char *ep=strchr(cp,':');
if (ep) {
Expand All @@ -508,7 +508,7 @@ void DisplayText(void)
}
}
break;
#endif // USE_SCRIPT_FATFS
#endif // USE_UFILESYS
case 'h':
// hor line to
var = atoiv(cp, &temp);
Expand Down Expand Up @@ -765,33 +765,51 @@ void DisplayText(void)

#ifdef USE_TOUCH_BUTTONS
case 'b':
{ int16_t num,gxp,gyp,gxs,gys,outline,fill,textcolor,textsize; uint8_t dflg=1;
if (*cp=='e' || *cp=='d') {
{ int16_t num, gxp, gyp, gxs, gys, outline, fill, textcolor, textsize; uint8_t dflg = 1, sbt = 0;
if (*cp == 'e' || *cp == 'd') {
// enable disable
uint8_t dis=0;
if (*cp=='d') dis=1;
uint8_t dis = 0;
if (*cp == 'd') dis = 1;
cp++;
var=atoiv(cp,&num);
num=num%MAX_TOUCH_BUTTONS;
cp+=var;
var = atoiv(cp, &num);
num = num % MAX_TOUCH_BUTTONS;
cp += var;
if (buttons[num]) {
buttons[num]->vpower.disable=dis;
buttons[num]->vpower.disable = dis;
if (!dis) {
if (buttons[num]->vpower.is_virtual) buttons[num]->xdrawButton(buttons[num]->vpower.on_off);
else buttons[num]->xdrawButton(bitRead(TasmotaGlobal.power,num));
}
}
break;
}
if (*cp=='-') {
if (*cp == '-') {
cp++;
dflg=0;
dflg = 0;
}
if (*cp == 's') {
cp++;
sbt = 1;
}
var=atoiv(cp,&num);
cp+=var;
cp++;
uint8_t bflags=num>>8;
num=num%MAX_TOUCH_BUTTONS;
if (*cp == 's') {
cp++;
var=atoiv(cp,&gxp);
if (buttons[num]) {
// set slider or button
if (buttons[num]->vpower.slider) {
buttons[num]->UpdateSlider(-gxp, -gxp);
} else {
buttons[num]->vpower.on_off = gxp;
buttons[num]->xdrawButton(buttons[num]->vpower.on_off);
}
}
break;
}
cp++;
var=atoiv(cp,&gxp);
cp+=var;
cp++;
Expand All @@ -818,32 +836,42 @@ void DisplayText(void)
cp++;
// text itself
char bbuff[32];
cp=get_string(bbuff,sizeof(bbuff),cp);

if (!sbt) {
// text itself
cp = get_string(bbuff, sizeof(bbuff), cp);
}
if (buttons[num]) {
delete buttons[num];
}
if (renderer) {
buttons[num]= new VButton();
if (buttons[num]) {
buttons[num]->initButtonUL(renderer,gxp,gyp,gxs,gys,renderer->GetColorFromIndex(outline),\
renderer->GetColorFromIndex(fill),renderer->GetColorFromIndex(textcolor),bbuff,textsize);
if (!bflags) {
// power button
if (dflg) buttons[num]->xdrawButton(bitRead(TasmotaGlobal.power,num));
buttons[num]->vpower.is_virtual=0;
} else {
// virtual button
buttons[num]->vpower.is_virtual=1;
if (bflags==2) {
// push
buttons[num]->vpower.is_pushbutton=1;
if (!sbt) {
buttons[num]->vpower.slider = 0;
buttons[num]->initButtonUL(renderer, gxp, gyp, gxs, gys, renderer->GetColorFromIndex(outline),\
renderer->GetColorFromIndex(fill), renderer->GetColorFromIndex(textcolor), bbuff, textsize);
if (!bflags) {
// power button
if (dflg) buttons[num]->xdrawButton(bitRead(TasmotaGlobal.power, num));
buttons[num]->vpower.is_virtual = 0;
} else {
// toggle
buttons[num]->vpower.is_pushbutton=0;
// virtual button
buttons[num]->vpower.is_virtual = 1;
if (bflags==2) {
// push
buttons[num]->vpower.is_pushbutton = 1;
} else {
// toggle
buttons[num]->vpower.is_pushbutton = 0;
}
if (dflg) buttons[num]->xdrawButton(buttons[num]->vpower.on_off);
buttons[num]->vpower.disable = !dflg;
}
if (dflg) buttons[num]->xdrawButton(buttons[num]->vpower.on_off);
buttons[num]->vpower.disable=!dflg;
} else {
// slider
buttons[num]->vpower.slider = 1;
buttons[num]->SliderInit(renderer, gxp, gyp, gxs, gys, outline, renderer->GetColorFromIndex(fill),\
renderer->GetColorFromIndex(textcolor), renderer->GetColorFromIndex(textsize));
}
}
}
Expand Down Expand Up @@ -1575,7 +1603,7 @@ char get_jpeg_size(unsigned char* data, unsigned int data_size, unsigned short *
#endif // JPEG_PICTS
#endif // ESP32

#if (defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)) || defined(USE_UFILESYS)
#ifdef USE_UFILESYS
extern FS *ufsp;
#define XBUFF_LEN 128
void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp, bool inverted ) {
Expand Down Expand Up @@ -1675,7 +1703,7 @@ void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp, bool inverted ) {
#endif // ESP32
}
}
#endif // USE_SCRIPT_FATFS
#endif // USE_UFILESYS

#ifdef USE_AWATCH
#define MINUTE_REDUCT 4
Expand Down Expand Up @@ -2188,9 +2216,11 @@ uint8_t vbutt=0;

//AddLog(LOG_LEVEL_INFO, PSTR("touch %d - %d"), pLoc.x, pLoc.y);
// now must compare with defined buttons
for (uint8_t count=0; count<MAX_TOUCH_BUTTONS; count++) {
if (buttons[count] && !buttons[count]->vpower.disable) {
if (buttons[count]->contains(pLoc.x, pLoc.y)) {
for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) {
if (buttons[count]) {
if (!buttons[count]->vpower.slider) {
if (!buttons[count]->vpower.disable) {
if (buttons[count]->contains(pLoc.x, pLoc.y)) {
// did hit
buttons[count]->press(true);
if (buttons[count]->justPressed()) {
Expand All @@ -2216,12 +2246,20 @@ uint8_t vbutt=0;
Touch_MQTT(count, cp, buttons[count]->vpower.on_off);
}
}
}
if (!buttons[count]->vpower.is_virtual) {
rbutt++;
} else {
vbutt++;
}
}
if (!buttons[count]->vpower.is_virtual) {
rbutt++;
} else {
vbutt++;
} else {
// slider
if (buttons[count]->didhit(pLoc.x, pLoc.y)) {
uint16_t value = buttons[count]->UpdateSlider(pLoc.x, pLoc.y);
Touch_MQTT(count, "SLD", value);
}
}
}
}
}
Expand All @@ -2237,27 +2275,29 @@ uint8_t vbutt=0;
}
}
#endif
for (uint8_t count=0; count<MAX_TOUCH_BUTTONS; count++) {
for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) {
if (buttons[count]) {
buttons[count]->press(false);
if (buttons[count]->justReleased()) {
if (buttons[count]->vpower.is_virtual) {
if (buttons[count]->vpower.is_pushbutton) {
// push button
buttons[count]->vpower.on_off = 0;
Touch_MQTT(count,"PBT", buttons[count]->vpower.on_off);
buttons[count]->xdrawButton(buttons[count]->vpower.on_off);
if (!buttons[count]->vpower.slider) {
buttons[count]->press(false);
if (buttons[count]->justReleased()) {
if (buttons[count]->vpower.is_virtual) {
if (buttons[count]->vpower.is_pushbutton) {
// push button
buttons[count]->vpower.on_off = 0;
Touch_MQTT(count,"PBT", buttons[count]->vpower.on_off);
buttons[count]->xdrawButton(buttons[count]->vpower.on_off);
}
}
}
}
if (!buttons[count]->vpower.is_virtual) {
// check if power button stage changed
uint8_t pwr = bitRead(TasmotaGlobal.power, rbutt);
uint8_t vpwr = buttons[count]->vpower.on_off;
if (pwr != vpwr) {
Touch_RDW_BUTT(count, pwr);
if (!buttons[count]->vpower.is_virtual) {
// check if power button stage changed
uint8_t pwr = bitRead(TasmotaGlobal.power, rbutt);
uint8_t vpwr = buttons[count]->vpower.on_off;
if (pwr != vpwr) {
Touch_RDW_BUTT(count, pwr);
}
rbutt++;
}
rbutt++;
}
}
}
Expand Down