Wikia

CHDK Wiki

Changes: Lua/PTP Scripting

Edit

Back to page

(Some clarifications)
Line 1: Line 1:
The following information applies to the client/PC side scripting to invoke actions on your camera from your PC using ptp client applications (see http://chdk.wikia.com/wiki/PTP_Extension) which also use LUA for PC side scripting (not to be confused with using LUA for scripts that run on your camera). (I believe that ->) This information doesn't apply to the (non-GUI) ptpcam client application but does apply to the (GUI) ptpcamgui and chdkptp client applications.
+
=Introduction=
=General information about lua execution with PTP=
+
This page describes CHDK Lua functions available on the camera to support control using the [[PTP Extension]].
* To execute a Lua script already loaded on your camera (e.g. ''testprgm.lua)'' from chdkptp interactive mode, use
+
con 1 > = loadfile('A/CHDK/SCRIPTS/testprgm.lua')()
+
PTP client applications use the PTP_CHDK_ExecuteScript API to execute Lua code. In chdkptp and ptpcam, this is available through the <tt>lua</tt> and <tt>luar</tt> commands.
  +
  +
The functions listed below are available to scripts invoked from the script menu, but are unlikely to be useful.
  +
  +
=General information about executing Lua code over PTP=
  +
* To execute a Lua script already loaded on your camera (e.g. ''testprgm.lua)'', send the following Lua code from your client
  +
  +
loadfile('A/CHDK/SCRIPTS/testprgm.lua')()
  +
  +
: This loads the file and executes it. This is generally preferable to using "require", because it avoids the problems with sleep etc mentioned below.
 
* Executing a script while another script is already running can cause crashes and memory leaks.
 
* Executing a script while another script is already running can cause crashes and memory leaks.
 
** To check whether your script has terminated, you can use the script status command (script-status in ptpcam.)
 
** To check whether your script has terminated, you can use the script status command (script-status in ptpcam.)
  +
** by default, chdkptp checks scrip status and prevents executing a new script while a script is running.
 
* The function switch_mode_usb() should be used instead of set_record to switch between record and play mode when PTP is connected.
 
* The function switch_mode_usb() should be used instead of set_record to switch between record and play mode when PTP is connected.
 
* Lua return values and errors messages are sent using the message interface.
 
* Lua return values and errors messages are sent using the message interface.
** Simple tables are returned in a limited human readable string format by default, see [http://chdk.setepontos.com/index.php?topic=4338.msg62606#msg62606] for a description. This can be changed by providing your own usb_msg_table_to_string. A replacement that properly serializes lua tables can be found in chdkptp.
+
** Simple tables are returned in a limited human readable string format by default, see [http://chdk.setepontos.com/index.php?topic=4338.msg62606#msg62606] for a description. This can be changed by providing your own usb_msg_table_to_string. A replacement that properly serializes Lua tables can be found in chdkptp.
 
* Lua script execution currently leaves CHDK alt mode in a confused state. You may need to press the alt button a couple times to get the alt menu and buttons to respond as expected.
 
* Lua script execution currently leaves CHDK alt mode in a confused state. You may need to press the alt button a couple times to get the alt menu and buttons to respond as expected.
 
* While scripts in files already on the camera may be used, functions that yield (e.g. sleep(), wait_click() etc.) cannot be used inside require (or other pcall).
 
* While scripts in files already on the camera may be used, functions that yield (e.g. sleep(), wait_click() etc.) cannot be used inside require (or other pcall).
Line 24: Line 25:
 
msg = read_usb_msg([timeout])
 
msg = read_usb_msg([timeout])
   
Read a message from the CHDK ptp interface.
+
Read a message sent from the client program using the PTP_CHDK_WriteScriptMsg API from the CHDK PTP interface (<tt>putm</tt> command in ptpcam or chckdptp).
   
 
Returns the next available message as a string, or nil if no messages are available
 
Returns the next available message as a string, or nil if no messages are available
   
If timeout is given and not zero, wait until a message is available or timeout expires
+
If timeout is given and not zero, wait until a message is available or timeout expires.
   
 
==write_usb_msg==
 
==write_usb_msg==
Line 34: Line 35:
 
status = write_usb_msg(msg,[timeout])
 
status = write_usb_msg(msg,[timeout])
   
Writes a message to the CHDK ptp interface.
+
Writes a message to the CHDK PTP interface. Client applications can read the message using the PTP_CHDK_ReadScriptMsg API (<tt>getm</tt> command in ptpcam or chdkptp)
   
 
msg may be nil, boolean, number, string or table. Tables will be converted to a string by usb_msg_table_to_string
 
msg may be nil, boolean, number, string or table. Tables will be converted to a string by usb_msg_table_to_string
Line 47: Line 48:
 
s=usb_msg_table_to_string(table)
 
s=usb_msg_table_to_string(table)
   
This function is called when a table is returned from a lua script, or sent as a message. Lua code may override this function to provide different serialization formats. The return value should be a string on success. On failure, a lua error should be thrown.
+
This function is called when a table is returned from a Lua script, or sent as a message. Lua code may override this function to provide different serialization formats. The return value should be a string on success. On failure, a Lua error should be thrown.
 
[[Category:Scripting]]
 
[[Category:Scripting]]
 
[[Category:PTP Extension]]
 
[[Category:PTP Extension]]

Revision as of 20:06, July 8, 2012

Introduction

This page describes CHDK Lua functions available on the camera to support control using the PTP Extension.

PTP client applications use the PTP_CHDK_ExecuteScript API to execute Lua code. In chdkptp and ptpcam, this is available through the lua and luar commands.

The functions listed below are available to scripts invoked from the script menu, but are unlikely to be useful.

General information about executing Lua code over PTP

  • To execute a Lua script already loaded on your camera (e.g. testprgm.lua), send the following Lua code from your client
 loadfile('A/CHDK/SCRIPTS/testprgm.lua')()
This loads the file and executes it. This is generally preferable to using "require", because it avoids the problems with sleep etc mentioned below.
  • Executing a script while another script is already running can cause crashes and memory leaks.
    • To check whether your script has terminated, you can use the script status command (script-status in ptpcam.)
    • by default, chdkptp checks scrip status and prevents executing a new script while a script is running.
  • The function switch_mode_usb() should be used instead of set_record to switch between record and play mode when PTP is connected.
  • Lua return values and errors messages are sent using the message interface.
    • Simple tables are returned in a limited human readable string format by default, see [1] for a description. This can be changed by providing your own usb_msg_table_to_string. A replacement that properly serializes Lua tables can be found in chdkptp.
  • Lua script execution currently leaves CHDK alt mode in a confused state. You may need to press the alt button a couple times to get the alt menu and buttons to respond as expected.
  • While scripts in files already on the camera may be used, functions that yield (e.g. sleep(), wait_click() etc.) cannot be used inside require (or other pcall).

PTP Functions

switch_mode_usb

switch_mode_usb(mode)

Set the camera to play or record mode.

mode is 1 for record or 0 for play.

read_usb_msg

msg = read_usb_msg([timeout])

Read a message sent from the client program using the PTP_CHDK_WriteScriptMsg API from the CHDK PTP interface (putm command in ptpcam or chckdptp).

Returns the next available message as a string, or nil if no messages are available

If timeout is given and not zero, wait until a message is available or timeout expires.

write_usb_msg

status = write_usb_msg(msg,[timeout])

Writes a message to the CHDK PTP interface. Client applications can read the message using the PTP_CHDK_ReadScriptMsg API (getm command in ptpcam or chdkptp)

msg may be nil, boolean, number, string or table. Tables will be converted to a string by usb_msg_table_to_string

returns true if the message was queued successfully, otherwise false

If timeout is set and not zero, wait until message is written or timeout expires.

NOTE strings will not include a terminating NULL.

usb_msg_table_to_string

s=usb_msg_table_to_string(table)

This function is called when a table is returned from a Lua script, or sent as a message. Lua code may override this function to provide different serialization formats. The return value should be a string on success. On failure, a Lua error should be thrown.

Around Wikia's network

Random Wiki