Support for the [http://www.lua.org/manual/5.1/manual.html#5.4 string] library has been added in changeset 606.

Support for the [http://www.lua.org/manual/5.1/manual.html#5.4 string] library has been added in changeset 606.

−

Support for the [http://www.lua.org/manual/5.1/manual.html#5.6 math] library exists in a modified form. In standard Lua, umbers are floats but CHDK Lua is configured to use 32bit ints instead. The portions of the math library that make sense for ints are included.

+

Support for the [http://www.lua.org/manual/5.1/manual.html#5.6 math] library exists in a modified form. In standard Lua, numbers are floats but CHDK Lua is configured to use 32bit ints instead. The portions of the math library that make sense for ints are included.

Disadvantages Versus uBasic

since Lua was late to the party (2008), and as most people know at least a little bit about BASIC programming, most CHDK examples are given in uBasic

language syntax doesn't match BASIC, C, so can be frustrating to learn

CHDK Lua information is limited (however google of "lua for loop example" will often yield several results)

Recommendation

New users should strongly consider using Lua over uBasic if writing new scripts for themselves. Lua is much more powerful and will yield lower frustration in the long run. Review the examples below for syntax examples. If only making simple changes to existing uBasic scripts, stick with uBasic.

Running Lua scripts

A CHDK version with built-in Lua support is needed, e.g. CHDK builds > version 0.5 / changeset 512

The script should be saved with the filename extension .lua in the CHDK scripts folder chdk/scripts

Lua scripts support libraries, these files have to be placed either in chdk/scripts or chdk/lualib, see Lua syntax: Libraries.

Loading, starting, pausing & stopping work exactly in the same way as on UBASIC scripts

UBASIC scipts are also working as usual, the filename extension (.lua or .bas) tells CHDK how to run the script

For advanced Lua development, you may wish to run scripts on your computer. Set up a Lua Dev Environment

A first Lua sample script:

for i =1,10doprint("Picture "..i )
shoot()
sleep(2000)end

Lua standard libraries: io, os, string, and math

CHDK has included support for the lua standard IO and OS libraries since changeset 517. These allow you to manipulate files and directories. See this post for important differences, limitations and warnings.

Support for the math library exists in a modified form. In standard Lua, numbers are floats but CHDK Lua is configured to use 32bit ints instead. The portions of the math library that make sense for ints are included.

→ A test script to check the correct functionality of the library functions (including a list of successfully tested cameras) is also available: Lua IO+OS library test !

Differences in the base library

Missing features

module is not implemented.

dynamic linking binaries and related features are not supported

package.seeall is not supported

Prior to CHDK 1.2 changeset 2732, package.path is not supported.

Differences from standard lua iolib

Missing features

io.popen: CHDK has no other processes, no pipes

standard file handles: stdin/stdout/stderr all start out closed, as do io.input() and io.output()

io.tmpfile: Not implemented yet, but may be added later.

Added features

file:_getfptr()

fptrval=file:_getfptr()

Returns the actual FILE * associated with the file object. This is strictly for development/debugging, but combined with peek makes verifying FILE structure hack much simpler.

Other differences

Error detection and reporting is more limited:

vxworks reports errno, but no text description

dryos has no errno (or not found until now), and simply reports "error"

file:read("*n") will behave slightly differently because it doesn't use scanf. Shouldn't matter for sanely structured files, but corner cases will be different.

On cameras running DryOS 2.3 R50 and later, functions that write to a file do not appear to update the create and modify times.

Differences from standard lua oslib

Missing features

os.clock: not implemented. Note this relates to CPU time used by a process, not time of day (which is handled by os.time and os.date)

os.execute: no processes, revisit for ELF edition

os.exit: not meaningful for CHDK

os.getenv: environment variables exist in vxworks, but usefulness is doubtful

os.setlocale: functions may exist, but it's unlikely the rest of the firmware uses them

os.tmpname: TODO, not implemented yet (a beta implementation is available in the forum). Also trivial to implement entirely in lua script.

Added features

The following are not in the standard lua OS lib, but are added in CHDK

os.mkdir

status[,errstring[,errno]]=os.mkdir("A/path")

Attempts to create a directory at the named path. Returns true on success or nil,"error string",errno.

os.stat(implemented since build 0.6.5 / changeset 541)

stat_table[,errstring[,errno]=os.stat("filename")

Returns a table on success, or nil followed by the values of strerror and errno

The table has the following fields (all numbers)

dev

device number - not present on dryos >= R39

mode

not clear which bits are useful. It does change depending on whether you look at a file or directory (but you should use attrib or is_* below instead) - not present on dryos >= R39

size

size in bytes

atime

time of last access - not present on dryos >= R39

mtime

time of last modification

ctime

time of last change of file status

blksize

block size in bytes. This is NOT the dos sector size. 512 on all I've tested, possibly hardware block size. - not present on dryos >= R39

blocks

Number of blksize blocks in the file. blocks*blocksizes is not the same as "size on disk", per above. - not present on dryos >= R39

attrib

bitmask of dos attributes (see stdlib.h)

The following boolean values are set based on attrib, so you don't have to do bit testing for the most common values of interest:

is_dir

is_file

Notes:

It is possible to stat things that are neither file nor directory, e.g. volume labels. So you should test for exactly what you want, rather than assuming that not is_file means directory.

Files larger than 2gb would appear to have a negative size in lua.

os.utime(implemented since build 0.6.5 / changeset 541)

status[,errstring[,errno]]=os.utime("filename"[,atime[,mtime]])

Set the modification and access time of the named file. If either time is missing or nil, the current time is used. Returns true on success, or nil, "error message", errno. To set one time and leave the other unchanged, you must use stat to get the current value.

os.listdir(implemented since build 0.7.4 / changeset 555)

dir_table[,errstring[,errno]]=os.listdir("dirpath"[,showall])

List the contents of a directory. Returns array of file names, or nil, "error message", errno.

Lua library test

If the libraries have not already been tested on your camera and firmware version, you should run the script before attempting to use them. This script tests the various IO and OS functions, generating /llibtst.log and reporting pass or fail in the console. If not all tests are run, or there are failures, it may leave other files or directories in the root.

Failures could crash your camera. It is also possible, although not likely, that it could corrupt your SD card. Use caution if your camera has not been tested.

The following cameras have been successfully tested as of May 14, 2013:

Since version 0.6.5 / changeset #541 the test script in its latest version is included in the distribution directory CHDK/SCRIPTS/TEST/LLIBTST.LUA in the 'complete' Autobuild download package, it is also available from here.

Some Lua scripts

Additional example scripts are included in the 'complete' CHDK download packages, they are also directly available from CHDK SVN: script examples.

Minimalistic Intervalometer

Often, we want the camera to just take pictures on an interval without being smart about anything. This script was built to fill the need for a very, very simple intervalometer.

This might serve well also as a script for you to read so that you can understand how scripting works. To use: create an empty text file called "mint.lua" in CHDK/SCRIPTS on the flash card. Paste the code below into the file. Start CHDK, press the shortcut key, press Set, select Load Script from File, find mint.lua, go wild.