Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
DeBrates

Double Click Issue/Touch Signal Sent Twice?

Question

This is a very hard issue to explain, but hopefully someone knows what I'm talking about. This is either a major bug or a simple flaw in my code.

If you want to try this for yourself, or view the code

pastebin get 7UHGc1tu demo

I have a program that has multiple pages, and a button that is in the exact same position on both pages. The issue is when I press the button on one page, which triggers an event then switches to the second page, it triggers the first event but also part of the second event. I have setup a sample program to demonstrate this issue. The program is supposed to display "Event 1" if the button on Page 1 is pressed, and display "Event 2" if the button on page 2 is pressed. Pressing the button on page 1 will also switch the page to page 2 where an identical button is. The button on page 2 will trigger the second event and switch back to page 1.

What ends up happening is "Event 1" is triggered, but when the program switches to page 2, it detects the press for a second time even though it was only clicked once. The odd part is that it doesn't display "Event 2" but it does switch the page back to page 1.

I feel like I'm not being followed, so here is a gif of the sample program and explanation of the program.

The X represents the amount of times page 1 has been drawn. The program should switch to page 2, instead it acts like it switched to page 2, switched back to page 1 because of the button press, but didn't display "Event 2".

 

What Page 2 would look like after 1 button press:

https://imgur.com/a/wbIMjmF

The basic conclusion is that it toggles the buttons on both pages, but only sets the variable on page 2 and doesn't display "Event 2". Ideally it would only display "Event 1" and switch to page 2 on a single press.

I know this is basically impossible to follow but if you have any insight about anything, I would appreciate the help! If you would like me to explain anything further or clarify anything, let me know! Thanks!

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 1
  • Solution

The bug is really simple. To find it, follow through the code line-by-line, analyzing what happens at each line. (This is something I do quite often when debugging an obscure problem.)

First, skip the first few dozens of lines until we get to the L65 (short for line 65), where the loop is defined. We then encounter the if block at L68, of which the first branch is actually run (page == "page1" as it was set to this value on L63), resulting in the pageOne call.

The next line to examine is L79, where the program stops until it pulls the touch event. Here, I check that the event parameters weren't mixed up. All right, let's go on and press the button.

The first condition, on L82, is satisfied (page == "page1", set on L63), so we get the page variable updated to the new value, "page2". But there's more code to run before we loop back! The second condition, on L89. The coodinate mess after the first and evaluates to true. What about page? Well, we've just set it to "page2", so... it, too, yields true. Therefore, the whole condition is satisfied. And the variable is set back to "page1". We loop back, and redraw the first page.

This is how we've just spotted the bug. In case you wonder why Event 2 isn't written to the screen: well, it is written. At position (4, 45). You've just mistaken x for y (and vice versa): 4 is the column, 45 is the row. This is clearly beyond the viewport 58×18, so you see nothing actually printed.

OK. How should you fix it? It's simple: use elseifs.

if (x >= w / 2 - 5 and
    x <= w / 2 - 5 + 11 and
    y >= h / 2 - 1 and
    y <= h / 2 + 1) then
  if page == "page1" then
    mon.set(4, 10, "Event 1")
    table.insert(a, "X")
    page = "page2"
  elseif page == "page2" then
    mon.set(45, 10, "Event 2")
    page = "page1"
  end
end
Link to post
Share on other sites
  • 0
3 hours ago, Fingercomp said:

The bug is really simple. To find it, follow through the code line-by-line, analyzing what happens at each line. (This is something I do quite often when debugging an obscure problem.)

First, skip the first few dozens of lines until we get to the L65 (short for line 65), where the loop is defined. We then encounter the if block at L68, of which the first branch is actually run (page == "page1" as it was set to this value on L63), resulting in the pageOne call.

The next line to examine is L79, where the program stops until it pulls the touch event. Here, I check that the event parameters weren't mixed up. All right, let's go on and press the button.

The first condition, on L82, is satisfied (page == "page1", set on L63), so we get the page variable updated to the new value, "page2". But there's more code to run before we loop back! The second condition, on L89. The coodinate mess after the first and evaluates to true. What about page? Well, we've just set it to "page2", so... it, too, yields true. Therefore, the whole condition is satisfied. And the variable is set back to "page1". We loop back, and redraw the first page.

This is how we've just spotted the bug. In case you wonder why Event 2 isn't written to the screen: well, it is written. At position (4, 45). You've just mistaken x for y (and vice versa): 4 is the column, 45 is the row. This is clearly beyond the viewport 58×18, so you see nothing actually printed.

OK. How should you fix it? It's simple: use elseifs.


if (x >= w / 2 - 5 and
    x <= w / 2 - 5 + 11 and
    y >= h / 2 - 1 and
    y <= h / 2 + 1) then
  if page == "page1" then
    mon.set(4, 10, "Event 1")
    table.insert(a, "X")
    page = "page2"
  elseif page == "page2" then
    mon.set(45, 10, "Event 2")
    page = "page1"
  end
end

Wow! Great analysis and explanation. This problem has stumped me for days on a larger project, and the solution was that simple. You have certainly helped me here and given me some debugging advice for the future! Thanks!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.