Tidy-up tasks before exiting script? - Script Writing - CHDK Forum

Tidy-up tasks before exiting script?

  • 8 Replies
  • 4819 Views
*

Offline Davo

  • ***
  • 189
Tidy-up tasks before exiting script?
« on: 16 / February / 2013, 07:01:04 »
Advertisements
I am writing a script that will record videos. It starts with a call to click("video") which starts recording. and the script keeps on recording clips below the 4GB limit. While it is recording it is using the motion detection call.

However, I can't work out how to stop it properly. When I press the shoot button to stop the script the script stops as expected but the camera keeps on recording.

Is there a way to run some tidy-up script functions after the script is interrupted by the shoot button? This would let me close open files and shut down recording.

Dave

*

Offline philmoz

  • *****
  • 3450
    • Photos
Re: Tidy-up tasks before exiting script?
« Reply #1 on: 16 / February / 2013, 07:52:49 »
I am writing a script that will record videos. It starts with a call to click("video") which starts recording. and the script keeps on recording clips below the 4GB limit. While it is recording it is using the motion detection call.

However, I can't work out how to stop it properly. When I press the shoot button to stop the script the script stops as expected but the camera keeps on recording.

Is there a way to run some tidy-up script functions after the script is interrupted by the shoot button? This would let me close open files and shut down recording.

Dave

If you're using uBasic add this at the end of the script.
Code: [Select]
:restore
  clean up stuff
end

For Lua scripts add a function called 'restore' that does any necessary cleaning up.

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 Davo

  • ***
  • 189
Re: Tidy-up tasks before exiting script?
« Reply #2 on: 16 / February / 2013, 15:50:17 »
Very cool Phil and here is an example lua script that uses it.

Code: [Select]
--[[ @title recordz --]]

function restore()
  if get_movie_status() == 4 then
    click("video")
  end
end

click("video")
restore()

Thanks, Dave

Re: Tidy-up tasks before exiting script?
« Reply #3 on: 18 / February / 2013, 10:25:25 »
As I remember, the restore() is called automagically when script is interrupted, so there's no need to call it by restore() command. Of course - if not needed:)
if (2*b || !2*b) {
    cout<<question
}

Compile error: poor Yorick

*

Offline lapser

  • *****
  • 1093
Re: Tidy-up tasks before exiting script?
« Reply #4 on: 18 / February / 2013, 12:09:52 »
As I remember, the restore() is called automagically when script is interrupted, so there's no need to call it by restore() command. Of course - if not needed:)
I think restore() is only called when you interrupt the script by pressing shoot_full. It's not called with a normal script end, or with a script error (until you press shoot_full).

So you need to call restore() at the end of a script.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline Davo

  • ***
  • 189
Re: Tidy-up tasks before exiting script?
« Reply #5 on: 06 / March / 2013, 10:16:38 »
Looks like I am stuck again. I found that when I add some actual code to my simple example above the restore() function don't work.

The script below will record a 10s video and then turn itself off by calling restore. This turns off recording and prints Restore point 1 and 2 as expected. However, when I terminate the script by pressing the shoot button I only see Restore point 1 and the recording doesn't stop. I have to come out of ALT mode and then press the video button to stop recording.

Any ideas where I am going wrong? I am running 1.2.0-2593 on an A810 version e.

Code: [Select]
--[[ @title restore --]]

function restore()
print("Restore point 1")
click("video")
print("Restore point 2")
end

t_start = get_tick_count()
click("video")
print("Recording started")

repeat
sleep(100)
until (get_tick_count() - t_start) > 10000

print("Script ended itself")

restore()

*

Offline philmoz

  • *****
  • 3450
    • Photos
Re: Tidy-up tasks before exiting script?
« Reply #6 on: 06 / March / 2013, 14:10:09 »
Looks like I am stuck again. I found that when I add some actual code to my simple example above the restore() function don't work.

The script below will record a 10s video and then turn itself off by calling restore. This turns off recording and prints Restore point 1 and 2 as expected. However, when I terminate the script by pressing the shoot button I only see Restore point 1 and the recording doesn't stop. I have to come out of ALT mode and then press the video button to stop recording.

Any ideas where I am going wrong? I am running 1.2.0-2593 on an A810 version e.

Code: [Select]
--[[ @title restore --]]

function restore()
print("Restore point 1")
click("video")
print("Restore point 2")
end

t_start = get_tick_count()
click("video")
print("Recording started")

repeat
sleep(100)
until (get_tick_count() - t_start) > 10000

print("Script ended itself")

restore()

The current implementation doesn't support this.
Calling 'click' suspends the script while the button press and release are done in the the core CHDK code.
However this looks like 'restore' has exited to the script engine so it finishes interrupting the script execution (so click never gets a chance to run).

Not easy to fix, anything used in 'restore' that causes Lua to yield control back to CHDK will have this effect.

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 reyalp

  • ******
  • 14125
Re: Tidy-up tasks before exiting script?
« Reply #7 on: 06 / March / 2013, 17:25:37 »
The current implementation doesn't support this.
Calling 'click' suspends the script while the button press and release are done in the the core CHDK code.
However this looks like 'restore' has exited to the script engine so it finishes interrupting the script execution (so click never gets a chance to run).

Not easy to fix, anything used in 'restore' that causes Lua to yield control back to CHDK will have this effect.
That sounds right. A possible workaround in this instance would be to use the levent system to send the video button press instead, something like

Code: [Select]
post_levent_to_ui('PressMovieButton')
post_levent_to_ui('UnpressMovieButton')
The Unpress may not be needed.
Don't forget what the H stands for.

*

Offline Davo

  • ***
  • 189
Re: Tidy-up tasks before exiting script?
« Reply #8 on: 07 / March / 2013, 08:23:07 »
Code: [Select]
post_levent_to_ui('PressMovieButton')
post_levent_to_ui('UnpressMovieButton')

This worked like a charm and my script now turns off recording and lets me tidy up my files before exiting.

Many, many thanks for your help.

ps I didn't need to call UnpressMovieButton.

 

Related Topics


SimplePortal © 2008-2014, SimplePortal