Running Lua scripts under CHDKPTP? - Script Writing - CHDK Forum

Running Lua scripts under CHDKPTP?

  • 19 Replies
  • 12302 Views
*

Offline Davo

  • ***
  • 188
Running Lua scripts under CHDKPTP?
« on: 27 / February / 2013, 04:44:13 »
Advertisements
I am trying to run Lua scripts remotely from my PC using CHDKPTP. This will enable me to run my A810 on my RC astro mount.

Presently, I can enter ALT mode while connected by pressing the physical ALT key a few times. However, when I try to run a script remotely by clicking the ptp Shoot button the camera just takes a picture - even though it is in ALT mode. I need to physically press the Shoot button to start the script. The menu button behaves similarly.

Is there a trick to access the ALT behaviour of the buttons under chdkptp?

Re: Running Lua scripts under CHDKPTP?
« Reply #1 on: 27 / February / 2013, 09:58:09 »
Is there a trick to access the ALT behaviour of the buttons under chdkptp?
As you have noticed,  when chdkptp runs,  button clicks work as if <ALT> mode is not enabled so that you can control the camera remotely.

You need to issue a command to chdkptp to have it run a script.  I have that sequence written down somewhere but I'm not home right now.   Probably reyalp will comment at some point here - if he hasn't I'll post later when I did up my "cheat sheet".
Ported :   A1200    SD940   G10    Powershot N    G16

*

Offline nafraf

  • *****
  • 1308
Re: Running Lua scripts under CHDKPTP?
« Reply #2 on: 27 / February / 2013, 11:09:50 »
I think that loadfile() is the command that you need.
You can find examles posted by Waterwingz and reyalp here:
http://chdk.setepontos.com/index.php?topic=6981.msg75065#msg75065
http://chdk.setepontos.com/index.php?topic=9444.msg96836#msg96836

*

Offline reyalp

  • ******
  • 14082
Re: Running Lua scripts under CHDKPTP?
« Reply #3 on: 27 / February / 2013, 23:15:04 »
Is there a trick to access the ALT behaviour of the buttons under chdkptp?
As you have noticed,  when chdkptp runs,  button clicks work as if <ALT> mode is not enabled so that you can control the camera remotely.
To expand on this slightly:
chdkptp works by sending lua script to the camera. The buttons in the gui just send a click("key name") call. Scripted key presses in CHDK (whether from PTP or a script you run from the menu) always go to the stock firmware, not CHDK keyboard handling code. It would be useful to be able to send key presses to chdk (so you could navigate the alt menu remote, for example) but we don't currently have this capability.

Quote
You need to issue a command to chdkptp to have it run a script.  I have that sequence written down somewhere but I'm not home right now.   Probably reyalp will comment at some point here - if he hasn't I'll post later when I did up my "cheat sheet".
To run a script on the camera, you generally want to use something like this in the chdkptp console
Code: [Select]
lua loadfile('myfile.lua')()
loadfile loads the file and returns a function. The following () executes the returned function. The reason to use this instead of the more obvious dofile() or require() is that code executed in these functions cannot yield, which means you can't use sleep or keyboard functions.

Note that while the script is running you will not be able to control the camera from chdkptp using the key buttons or any other script commands. If you want to have a script that runs for a long time on the camera, but still be able to control it over ptp, you will probably need to write your own script using the ptp message interface. Some examples of this in the chdkptp lua code lua/rlibs.lua msg_shell and  lua/multicam.lua
Don't forget what the H stands for.


*

Offline Davo

  • ***
  • 188
Re: Running Lua scripts under CHDKPTP?
« Reply #4 on: 28 / February / 2013, 06:56:59 »
Many thanks for your help and patience folks. I am getting there I think. Here is what I did to run the stock hdr.lua script.

Plug in usb cable
Turn on camera with power button
Start CHDKPTP and click Connect
Click Live and check all boxes under stream
Click rec button
Type
Code: [Select]
lua loadfile('A/CHDK/SCRIPTS/hdr.lua')() in the bottom text box (my A810 gives an error if I omit the full path)
Click the Execute button

I then get an error saying 'Attempting to perform arithmetic on global 'f' (a nil value). I guess the default value of f is not being read? I get the error whether or not I am in ALT mode.

btw, chdkptp is a really cool app. I finally worked out how to download a folder - right click on it and select 'Download contents'. This will save swapping cards all the time.

*

Offline blackhole

  • *****
  • 940
  • A590IS 101b
    • Planetary astrophotography
Re: Running Lua scripts under CHDKPTP?
« Reply #5 on: 28 / February / 2013, 08:09:41 »
Replace gui.lua with this from attachment and HDR script will be added as a button.
I hope this helps


Re: Running Lua scripts under CHDKPTP?
« Reply #6 on: 28 / February / 2013, 08:10:25 »
I then get an error saying 'Attempting to perform arithmetic on global 'f' (a nil value). I guess the default value of f is not being read?
Probably nothing to do with that.   

One the first link nafraf posted above, I had posted the syntax to do this from the CLI part of chdkptp.   That's what I use all the time with small script files for loading CHDK to the camera and for loading Lua/uBASIC scripts to the camera.

I think that you need and "=" sign in front of the loadfile(foo.lua)().   I haven't used the gui to run commands but it might be something like that.  Otherwise its trying to run your script on the PC rather than the camera.


Quote
btw, chdkptp is a really cool app. I finally worked out how to download a folder - right click on it and select 'Download contents'. This will save swapping cards all the time.
You can then imagine how useful it is during camera porting,  CHDK software developement,  and scripting testing.
« Last Edit: 28 / February / 2013, 08:12:08 by waterwingz »
Ported :   A1200    SD940   G10    Powershot N    G16

*

Offline blackhole

  • *****
  • 940
  • A590IS 101b
    • Planetary astrophotography
Re: Running Lua scripts under CHDKPTP?
« Reply #7 on: 28 / February / 2013, 10:13:44 »
Quote
Otherwise its trying to run your script on the PC rather than the camera.
You are right, if we replace this part:
Code: [Select]
expand="HORIZONTAL",
iup.button{
title='HDR',
size='31x15',
action=function(self)
add_status(con:execlua('shoot() set_aflock(1) p=get_av96() s=get_sv96() t=get_tv96()  set_tv96_direct(t-192) set_sv96(s) set_av96(p) shoot() set_tv96_direct(t+192) set_sv96(s) set_av96(p) shoot() set_aflock(0)'))
            end,
},
with this, the script runs on the camera.
Code: [Select]
expand="HORIZONTAL",
iup.button{
title='HDR',
size='31x15',
action=function(self)
gui.execquick([[
loadfile('A/CHDK/SCRIPTS/hdr.lua')()
]])
            end,
},



*

Offline Davo

  • ***
  • 188
Re: Running Lua scripts under CHDKPTP?
« Reply #8 on: 28 / February / 2013, 14:49:37 »
This is great stuff - I can customise the ptp GUI using lua  :)

I am going to try modding the HDR button so that it loads the latest version of my test.lua script onto the camera and then executes it. My workflow will then be

Edit script in Notepad > Save it to disk > Test it via my modded HDR button.

Re: Running Lua scripts under CHDKPTP?
« Reply #9 on: 28 / February / 2013, 15:30:14 »
My workflow will then be
Edit script in Notepad > Save it to disk > Test it via my modded HDR button.
If you are comfortable with the command line,  it even faster to do with a batch file and the chdkptp CLI.
Ported :   A1200    SD940   G10    Powershot N    G16

 

Related Topics