BRT Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

Welcome to the Bridgetek Community!

Please read our Welcome Note

Technical Support enquires
please contact the team
@ Bridgetek Support

Please refer to our website for detailed information on all our products - Bridgetek - Bridging Technology

Author Topic: Multiple Buttons Question  (Read 9230 times)

LaZorraBRT

  • Newbie
  • *
  • Posts: 16
    • View Profile
Multiple Buttons Question
« on: August 03, 2021, 06:05:48 PM »

Hello,
I am trying to set up multiple buttons on a display.  I have two buttons and the first one is working fine to stop and start a counter.  However the second button is able to start the counter but then becomes inoperable when I try to stop the counter.  I am using the same code for both but I am new to this and can't figure out the issue.  I hope this is an easy fix, I've tried searching the forum but can't find an example. This is on a 7" EVE4 display.  Thank you for any help!

Todd

Code: [Select]
/* check for touch events and setup vars for TFT_display() */
void TFT_touch(void)
{
uint8_t tag;
uint8_t tag2;
static uint8_t toggle_lock = 0;
static uint8_t toggle_lock2 = 0;

if(EVE_busy()) /* is EVE still processing the last display list? */
{
return;
}

display_list_size = EVE_memRead16(REG_CMD_DL); /* debug-information, get the size of the last generated display-list */
tag = EVE_memRead8(REG_TOUCH_TAG); /* read the value for the first touch point */
    tag2 = EVE_memRead8(REG_TOUCH_TAG); /* necessary? or can just one tag be used? */

switch(tag)
{
case 0:
toggle_lock = 0;
break;

case 10: /* use button on top as on/off radio-switch */
if(toggle_lock == 0)
{
toggle_lock = 42;
if(toggle_state == 0)
{
toggle_state = EVE_OPT_FLAT;
}
else
{
toggle_state = 0;
}
}
break;


}

switch(tag2)
{
case 1:
toggle_lock2 = 0;
break;

case 11: /* use button on top as on/off radio-switch */
if(toggle_lock2 == 0)
{
toggle_lock2 = 42;
if(toggle_state2 == 0)
{
toggle_state2 = EVE_OPT_FLAT ;
}
else
{
toggle_state2 = 0;
}
}
break;
}
}

void TFT_display(void)
{
//static int32_t rotate = 0;


if(tft_active != 0)
{
#if defined (EVE_DMA)
uint16_t cmd_fifo_size;
cmd_fifo_size = EVE_dma_buffer_index*4; /* without DMA there is no way to tell how many bytes are written to the cmd-fifo */
#endif

EVE_start_cmd_burst(); /* start writing to the cmd-fifo as one stream of bytes, only sending the address once */

EVE_cmd_dl_burst(CMD_DLSTART); /* start the display list */
EVE_cmd_dl_burst(DL_CLEAR_RGB | BLACK); /* set the default clear color to white */
EVE_cmd_dl_burst(DL_CLEAR | CLR_COL | CLR_STN | CLR_TAG); /* clear the screen - this and the previous prevent artifacts between lists, Attributes are the color, stencil and tag buffers */
EVE_cmd_dl_burst(TAG(0));

EVE_cmd_append_burst(MEM_DL_STATIC, num_dl_static); /* insert static part of display-list from copy in gfx-mem */
/* display a button */
EVE_cmd_dl_burst(DL_COLOR_RGB | GREEN);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(10)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(20,120,120,70, 28, toggle_state,"Touch!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */


if(toggle_state != 0)
{
basicnumber++;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | RED);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(11));
EVE_cmd_button_burst(220,320,120,70, 28, toggle_state2,"Touch2!");
EVE_cmd_dl_burst(TAG(1)); /* no touch */

if(toggle_state2 != 0)
{
basicnumber2++;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | PURPLE);
EVE_cmd_number_burst(120, EVE_VSIZE - 35, 26, EVE_OPT_RIGHTX|5, basicnumber);
EVE_cmd_number_burst(120, EVE_VSIZE - 20, 26, EVE_OPT_RIGHTX|6, basicnumber2);

EVE_cmd_dl_burst(DL_DISPLAY); /* instruct the graphics processor to show the list */
EVE_cmd_dl_burst(CMD_SWAP); /* make this list active */

EVE_end_cmd_burst(); /* stop writing to the cmd-fifo, the cmd-FIFO will be executed automatically after this or when DMA is done */
}
}

Logged

BRT Community

  • Administrator
  • Hero Member
  • *****
  • Posts: 747
    • View Profile
Re: Multiple Buttons Question
« Reply #1 on: August 04, 2021, 03:36:29 PM »

Hello,

Thank you for your post.

I recreated your display list in EVE Screen Editor and it appears as everything is tagged correctly, the first button = 10, second button = 11, the numbers printed on the screen are tag =1. Im currently setting up an MCU to test your code. but I will not it is not necessary to have two separate tag variables, you could perform one read "EVE_memRead8(REG_TOUCH_TAG);" and use this value in one switch() statement.

Can I ask if the EVE_OPT_FLAT state changes for the second button after it has been pressed? by your description it appears as if the toggle_state2 variable is not getting reset to 0.
It may be worth stepping this code in the debugger if to see what's happening with these variables.

Best Regards,
BRT Community
Logged

LaZorraBRT

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Multiple Buttons Question
« Reply #2 on: August 04, 2021, 04:34:19 PM »

Yes, the second button will go FLAT when I press it the first time.  However, I cannot press it again and stop the (int basicnumber2 = 0;) from counting up.  The first button works correctly to start and stop the basicnumber++ from counting up.  I thought I could just replicate the code from the first button and have it work but I am scratching my head on this. 

I've cleaned up my code but I am still having the same behavior.  Thank you for responding!

Code: [Select]
uint16_t toggle_state = 0;
uint16_t toggle_state2 = 0;
uint16_t display_list_size = 0;



/* check for touch events and setup vars for TFT_display() */
void TFT_touch(void)
{
uint8_t tag;
static uint8_t toggle_lock = 0;
static uint8_t toggle_lock2 = 0;

if(EVE_busy()) /* is EVE still processing the last display list? */
{
return;
}

display_list_size = EVE_memRead16(REG_CMD_DL); /* debug-information, get the size of the last generated display-list */
tag = EVE_memRead8(REG_TOUCH_TAG); /* read the value for the first touch point */
   
switch(tag)
{
case 0:
        toggle_lock = 0;
      break;

case 10: /* use button on top as on/off radio-switch */
if(toggle_lock == 0)
{
toggle_lock = 42;
if(toggle_state == 0)
{
toggle_state = EVE_OPT_FLAT;
}
else
{
toggle_state = 0;
}
}
break;

case 1:
        toggle_lock2 = 0;
      break;

case 11: /* use button on top as on/off radio-switch */
if(toggle_lock2 == 0)
{
toggle_lock2 = 42;
if(toggle_state2 == 0)
{
toggle_state2 = EVE_OPT_FLAT ;
}
else
{
toggle_state2 = 0;
}
}
break;
}
}



/*
dynamic portion of display-handling, meant to be called every 20ms or more
*/
void TFT_display(void)
{

if(tft_active != 0)
{
#if defined (EVE_DMA)
uint16_t cmd_fifo_size;
cmd_fifo_size = EVE_dma_buffer_index*4; /* without DMA there is no way to tell how many bytes are written to the cmd-fifo */
#endif

EVE_start_cmd_burst(); /* start writing to the cmd-fifo as one stream of bytes, only sending the address once */

EVE_cmd_dl_burst(CMD_DLSTART); /* start the display list */
EVE_cmd_dl_burst(DL_CLEAR_RGB | BLACK); /* set the default clear color to white */
EVE_cmd_dl_burst(DL_CLEAR | CLR_COL | CLR_STN | CLR_TAG); /* clear the screen - this and the previous prevent artifacts between lists, Attributes are the color, stencil and tag buffers */
EVE_cmd_dl_burst(TAG(0));

EVE_cmd_append_burst(MEM_DL_STATIC, num_dl_static); /* insert static part of display-list from copy in gfx-mem */
/* display a button */
EVE_cmd_dl_burst(DL_COLOR_RGB | GREEN);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(10)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(20,120,120,70, 28, toggle_state,"Touch!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */

if(toggle_state != 0)
{
basicnumber++;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | RED);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(11)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(220,320,120,70, 28, toggle_state2,"Touch2!");
EVE_cmd_dl_burst(TAG(1)); /* no touch */

if(toggle_state2 != 0)
{
basicnumber2++;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | PURPLE);
EVE_cmd_number_burst(120, EVE_VSIZE - 35, 26, EVE_OPT_RIGHTX|5, basicnumber);
EVE_cmd_number_burst(120, EVE_VSIZE - 20, 26, EVE_OPT_RIGHTX|5, basicnumber2);

EVE_cmd_dl_burst(DL_DISPLAY); /* instruct the graphics processor to show the list */
EVE_cmd_dl_burst(CMD_SWAP); /* make this list active */

EVE_end_cmd_burst(); /* stop writing to the cmd-fifo, the cmd-FIFO will be executed automatically after this or when DMA is done */
}
}

Logged

Rudolph

  • Sr. Member
  • ****
  • Posts: 392
    • View Profile
Re: Multiple Buttons Question
« Reply #3 on: August 05, 2021, 10:50:00 AM »

As this is my stupid code I probably should answer this.  :)
The whole idea behind this "toggle_lock" is that continuously generating touch-events only triggers one action.
So it is set when a touch event is detected and cleared when there is no longer a touch detected.
I am calling the TFT_touch() function every 5ms so this requires some sort of mechanism to only have one action.
Unless of course continous actions are required, for example when using a slider.

And in my current projects I added one additional step, reading of REG_TOUCH_RAW_XY to make sure that
the finger is lifted from the display in between.
As it is you could rest your finger on a button, move it outside the area of the button and slide into the next one.
In the simple case of using only two buttons this is not likely to be an issue.
But it is a very real issue when you are using a slider with a button on top of it or below it and get touch events
for these buttons when you are trying to use the slider.

Code: [Select]
void TFT_touch(void)
{
    uint8_t tag;
    static uint8_t toggle_lock = 0;

    if(EVE_busy()) /* is EVE still processing the last display list? */
    {
        return;
    }

    display_list_size = EVE_memRead16(REG_CMD_DL); /* debug-information, get the size of the last generated display-list */
    tag = EVE_memRead8(REG_TOUCH_TAG); /* read the value for the first touch point */
    uint32_t touchtest = EVE_memRead32(REG_TOUCH_RAW_XY);
   
    switch(tag)
    {
        case 0:
            if(touchtest == 0xffffffff) /* display is no longer touched */
            {
                toggle_lock = 0;
            }
            break;

        case 10: /* use button on top as on/off radio-switch */
            if(toggle_lock == 0)
            {
                toggle_lock = 42;
                if(toggle_state == 0)
                {
                    toggle_state = EVE_OPT_FLAT;
                }
                else
                {
                    toggle_state = 0;
                }
            }
            break;

        case 11: /* use button on top as on/off radio-switch */
            if(toggle_lock == 0)
            {
                toggle_lock = 42;
                if(toggle_state2 == 0)
                {
                    toggle_state2 = EVE_OPT_FLAT ;
                }
                else
                {
                    toggle_state2 = 0;
                }
            }
            break;
    }
}
Logged

LaZorraBRT

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Multiple Buttons Question
« Reply #4 on: August 08, 2021, 07:46:10 PM »

I would not call your code stupid at all, quite the opposite!  Thank you for all your time and effort!

I updated my code based on your help and all is working well now.  I added a third button that just increments the number by one instead of counting up continuously.  Thank you!

Code: [Select]

uint8_t tag;
static uint8_t toggle_lock = 0;
uint16_t toggle_state = 0;
uint16_t toggle_state2 = 0;
uint16_t toggle_state3 = 0;
uint16_t display_list_size = 0;

void TFT_touch(void)
{
    if(EVE_busy()) /* is EVE still processing the last display list? */
    {
        return;
    }

    display_list_size = EVE_memRead16(REG_CMD_DL); /* debug-information, get the size of the last generated display-list */
    tag = EVE_memRead8(REG_TOUCH_TAG); /* read the value for the first touch point */
    uint32_t touchtest = EVE_memRead32(REG_TOUCH_RAW_XY);
   
    switch(tag)
    {
        case 0:
            if(touchtest == 0xffffffff) /* display is no longer touched */
            {
                toggle_lock = 0;
            }
            break;

        case 10: /* use button on top as on/off radio-switch */
            if(toggle_lock == 0)
            {
                toggle_lock = 42;
                if(toggle_state == 0)
                {
                    toggle_state = EVE_OPT_FLAT;
                }
                else
                {
                    toggle_state = 0;
                }
            }
            break;

        case 11: /* use button on top as on/off radio-switch */
            if(toggle_lock == 0)
            {
                toggle_lock = 42;
                if(toggle_state2 == 0)
                {
                    toggle_state2 = EVE_OPT_FLAT ;
                }
                else
                {
                    toggle_state2 = 0;
                }
            }
            break;

case 12: /* use button on top as on/off radio-switch */
            if(toggle_lock == 0)
            {
                toggle_lock = 42;
                if(toggle_state3 == 0)
                {
                    toggle_state3 = EVE_OPT_FLAT ;
                }
                else
                {
                    toggle_state3 = 0;
                }
            }
            break;
    }
}


/*
dynamic portion of display-handling, meant to be called every 20ms or more
*/

int basicnumber = 0;
int basicnumber2 = 0;
int basicnumber3 = 0;

void TFT_display(void)
{

if(tft_active != 0)
{
#if defined (EVE_DMA)
uint16_t cmd_fifo_size;
cmd_fifo_size = EVE_dma_buffer_index*4; /* without DMA there is no way to tell how many bytes are written to the cmd-fifo */
#endif

EVE_start_cmd_burst(); /* start writing to the cmd-fifo as one stream of bytes, only sending the address once */

EVE_cmd_dl_burst(CMD_DLSTART); /* start the display list */
EVE_cmd_dl_burst(DL_CLEAR_RGB | BLACK); /* set the default clear color to white */
EVE_cmd_dl_burst(DL_CLEAR | CLR_COL | CLR_STN | CLR_TAG); /* clear the screen - this and the previous prevent artifacts between lists, Attributes are the color, stencil and tag buffers */
EVE_cmd_dl_burst(TAG(0));

EVE_cmd_append_burst(MEM_DL_STATIC, num_dl_static); /* insert static part of display-list from copy in gfx-mem */
/* display a button */
EVE_cmd_dl_burst(DL_COLOR_RGB | GREEN);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(10)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(50,120,120,70, 28, toggle_state,"Touch!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */

if(toggle_state != 0)
{
basicnumber++;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | RED);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(11)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(250,120,120,70, 28, toggle_state2,"Touch2!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */

if(toggle_state2 != 0)
{
basicnumber2++;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | PURPLE);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(12)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(450,120,120,70, 28, toggle_state3,"Touch3!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */

if(toggle_state3 != 0)
{
basicnumber3++;
toggle_state3 = 0;
}

EVE_cmd_dl_burst(DL_COLOR_RGB | GREEN);
EVE_cmd_number_burst(155, 200, 25, EVE_OPT_RIGHTX|5, basicnumber);
EVE_cmd_dl_burst(DL_COLOR_RGB | RED);
EVE_cmd_number_burst(355, 200, 25, EVE_OPT_RIGHTX|5, basicnumber2);
EVE_cmd_dl_burst(DL_COLOR_RGB | PURPLE);
EVE_cmd_number_burst(555, 200, 25, EVE_OPT_RIGHTX|5, basicnumber3);

EVE_cmd_dl_burst(DL_DISPLAY); /* instruct the graphics processor to show the list */
EVE_cmd_dl_burst(CMD_SWAP); /* make this list active */

EVE_end_cmd_burst(); /* stop writing to the cmd-fifo, the cmd-FIFO will be executed automatically after this or when DMA is done */
}
}

« Last Edit: August 09, 2021, 08:05:10 PM by LaZorraBRT »
Logged