wait_click() with short timeout - page 2 - General Discussion and Assistance - CHDK Forum  

wait_click() with short timeout

  • 40 Replies
  • 14794 Views
*

Offline philmoz

  • *****
  • 3450
    • Photos
Re: wait_click() with short timeout
« Reply #10 on: 26 / February / 2013, 02:40:39 »
Advertisements
In fact, the keyboard buffer eliminates the need for wait_click, since it doesn't miss keystrokes.

What happens to all the existing scripts that use 'wait_click'?

Phil.
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)
  g5x (1.00c, 1.01a, 1.01b)
  g7x2 (1.01a, 1.01b, 1.10b)

*

Offline a1ex

  • *****
  • 671
  • ML dev
Re: wait_click() with short timeout
« Reply #11 on: 26 / February / 2013, 03:05:44 »
I've used the key buffer method in Magic Lantern, and it's robust enough for simple games. Before, I had trouble with missed keys that were sent between two calls to wait_click (e.g. for scrollwheel turns).

Feel free to look at my implementation:
https://bitbucket.org/hudson/magic-lantern/src/tip/picoc/library_ml.c#cl-851
http://www.magiclantern.fm/forum/index.php?topic=4362.0 - section "Key Input"

*

Offline msl

  • *****
  • 1280
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: wait_click() with short timeout
« Reply #12 on: 26 / February / 2013, 05:16:45 »
What happens to all the existing scripts that use 'wait_click'?

Tested with some existing lua scripts. Works all fine.

msl
CHDK-DE:  CHDK-DE links

*

Offline lapser

  • *****
  • 1093
Re: wait_click() with short timeout
« Reply #13 on: 26 / February / 2013, 08:32:01 »
What happens to all the existing scripts that use 'wait_click'?
We'll have to leave wait_click in as it is, modified to read from the keyboard buffer. But reading a key with wait_click(0) is confusing since there's no waiting.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos


*

Offline msl

  • *****
  • 1280
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: wait_click() with short timeout
« Reply #14 on: 26 / February / 2013, 08:39:17 »
Confirmed, wait_click(0) is no more backward compatible.

msl
CHDK-DE:  CHDK-DE links

*

Offline lapser

  • *****
  • 1093
Re: wait_click() with short timeout
« Reply #15 on: 26 / February / 2013, 08:40:42 »
I've used the key buffer method in Magic Lantern, and it's robust enough for simple games. Before, I had trouble with missed keys that were sent between two calls to wait_click (e.g. for scrollwheel turns).
Thanks! I wasn't thinking about using the keyboard buffer for games, but that would be a big improvement. I was only planning to set it up for use in scripts, clearing the keyboard buffer at the start and end of the script.

How do the current CHDK game modules read key input? How hard would it be to modify the code to use the keyboard buffer for all CHDK key input?
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline lapser

  • *****
  • 1093
Re: wait_click() with short timeout
« Reply #16 on: 26 / February / 2013, 08:51:30 »
Confirmed, wait_click(0) is no more backward compatible.
The current wait_click doesn't check the key if it times out because of this statement:

    if (action_process_delay(delay) || (camera_info.state.kbd_last_clicked = kbd_get_clicked_key()))

I changed it to this:

    camera_info.state.kbd_last_clicked = kbd_get_clicked_key();
    if (action_process_delay(delay) || camera_info.state.kbd_last_clicked)

So in the current implementation, wait_click(0) or wait_click(10) wouldn't ever find a key. But wait_click with a short time out doesn't make much sense. I'd rather use a new function:  get_key() when you want to check if a key is ready, but not wait for it. wait_click is needed currently because kbd_get_clicked_key() only lasts for 10 msec (1 keyboard task call). With a keyboard buffer, you can check for a key any time.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline philmoz

  • *****
  • 3450
    • Photos
Re: wait_click() with short timeout
« Reply #17 on: 26 / February / 2013, 16:31:38 »
How do the current CHDK game modules read key input?

kbd_get_autoclicked_key()

Quote
How hard would it be to modify the code to use the keyboard buffer for all CHDK key input?

Two things to consider.

1. kbd_get_autoclicked_key() is used instead of kbd_get_clicked_key() to get auto repeat. Changing wait_click to use kbd_get_autoclicked_key should not cause any problems for scripts and would enable auto repeat in scripts.

2. More complex is the code that uses two buttons being pressed together to trigger an action. The shutter half press + other key combos that trigger state changes would be challenging using buffered input. You would need to store the shutter half press as a modifier for each click in the buffer rather than an actual key 'click' (this is done now using the camera_info.state.is_shutter_half_press variable). This seems like contradicting requirements - scripts want to see the shutter half press as a keyboard click (iskey("shoot_half")); but the core CHDK wants the half press as a modifier to another key press.

Phil.
 
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)
  g5x (1.00c, 1.01a, 1.01b)
  g7x2 (1.01a, 1.01b, 1.10b)


*

Offline lapser

  • *****
  • 1093
Re: wait_click() with short timeout
« Reply #18 on: 26 / February / 2013, 21:41:46 »
I see that Phil already got the wait_click changes into the trunk. Thanks! I'll test them out for awhile and see if a keyboard buffer would be worth it.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline msl

  • *****
  • 1280
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: wait_click() with short timeout
« Reply #19 on: 28 / February / 2013, 11:23:12 »
There is another issue with wait_click().

Try the example script. All keys are fine with the exception of half_shot. I miss the drawings.

Code: (lua) [Select]
--[[
@title test wait_click
]]

function draw()
    draw_clear()
    draw_rect_filled(0,0,360,240,257,257)
    draw_string(10,110,"second frame - klick a key",258,257)
    wait_click()
end

loop = true

while loop do
    draw_clear()
    draw_rect_filled(0,0,360,240,257,257)
    draw_string(10,110,"first frame - klick a key",258,257)
    draw_string(10,140,"klick menu for end"   ,258,257)
    wait_click()
    if is_pressed("down") then draw()
    elseif is_pressed("up") then draw()
    elseif is_pressed("right") then draw()
    elseif is_pressed("left") then draw()
    elseif is_pressed("set") then draw()
    elseif is_pressed("display") then draw()
    elseif is_pressed("shoot_half") then draw()
    elseif is_pressed("menu") then loop = false
    end
end

msl
CHDK-DE:  CHDK-DE links

 

Related Topics