This is the reference page for Lua functions which do not have an equivalent command in ubasic, or are significantly modified compared to the ubasic equivalent.
Most of the uBASIC commands are also available in Lua and can be used the same way, so UBASIC/TutorialScratchpad and Script_commands can be used as reference also for Lua.
Return the value in memory at the given address, or nil if the address alignment or size is invalid.
WARNING: Reading invalid addresses will crash your camera!
Set the memory at address to value. Returns true, or nil if the address allignment or size is invalid.
WARNING: Setting random memory locations is risky. If you don't know what you are doing, don't use it. If you do know what you are doing, you know you could crash your camera, or if you try hard enough something even worse.
→ perform the like named bitwise operations.
→ performs an arithmetic (sign extending) right shift.
→ performs a logical (non-sign extending) right shift.
These fields can be used to make scripts work around camera differences without requiring users to set anything.
Note: version currently returns just the string "CHDK". And build_number contains the entire build number as a string.
With this command a Lua script can use PropertyCases without knowing the cameras propset and the propcase number.
get_prop (and set_prop) can be used with propcase.lua library which is included in the 'complete' download packages. This module loads a table which maps camera property case names the correct number for a given camera.
There are several different "property sets" known for CHDK cameras: Propset 1 on most Digic II cameras, propset 2 on most Digic III and some Digic IV, and further propsets on later Digic IV and Digic V. During the CHDK build process a Lua library with a table of the property case names and numbers is generated for each propset. These generated tables are saved in the \CHDK\LUALIB\GEN\ folder. The propcase.lua library automatically loads the correct propset table, allowing you to write scripts which use property cases by name, in a platform independent manner.
The Lua commands get_prop_str, set_prop_str and binstr can be used to access arbitrary sized propcases from Lua.
→ Both of the above use Lua strings, which can contain any sequence of bytes.
Because many of the values you'd want to get/set at are numbers or arrays of numbers, a lua helper module called binstr is available to convert to and from strings for use with the above.
This function allows you to run regular CHDK file browser from the script. When user will select a file, the command will return the string - a path to the selected file.
The 'path' given as an argument is the startup directory for the file browser.
When user will not select any file (in example exit file browser by pressing 'menu' button) command wil return nil value.
Once browser is run by script it behaves exactly as CHDK file browser with all options - user can copy, cut and paste files, do RAW processings and other things that are allowed in browser run from menu. There's no way to control the things user can do.
Without file name the default filename is used. See this forum thread for more details.
Develop RAW on next shot. If filename is nil (or omitted), any pending raw develop is canceled.
Start merging process, operation is a number: 0=sum 1=average. Other=error.
Error checking is minimal. If you pass an invalid filename, things may silently fail. If you call raw_merge_add or raw_merge_end without calling start first, or call raw_merge_start multiple times without an intervening end, the results are undefined.
The RAW merge stuff runs in the same task as the script (the keyhook task), while raw operations from the menu run from the spytask. This appears to work, but it's a bit suspect.
This script demonstrates the usage of the RAW development commands, it performs the requested RAW operation on n files.
You can specify what exposure number to start at, or use 0 to specify the most recent.
The script works by counting down through the files until it find enough RAW files, so they don't need to be sequential.
It can optionally develop the resulting file.
The merged file will be named with the number of the last merged file, which will be the lowest number found.
Note: please update List of Params as you see fit.
Canon cameras store some parameters in onboard flash memory. These functions allow you to query them.
num is the number of parameters. Parameters are numbered starting form zero.
str is the parameter value as a lua string, which may contain embedded NULLs or other non-printable characters. If the size of the flash parameter is 4 bytes or less, a second value is returned, containing the parameter value as a number. If the parameter id is invalid, or the parameter size is 0, then nil is returned for both values.
parameter IDs and meanings vary between cameras. Using them will make your script non-portable, unless your script has code to check what camera it is running on (using get_buildinfo) and select the correct parameter IDs for each camera.
You can find useful parameter IDs in the CHDK source.
Setting parameter values is not supported yet.
This script dumps the parameters from the internal flash memory and write them to a logfile paramdmp.log on the root folder of the memory card.
The parameter IDs may vary between cameras, so scripts using them will not be portable !
name -- string "system", "exmem". CHDK 1.3 also includes "aram" and "combined"
start_address -- pool start, not set for "combined"
end_address  -- pool end, not set for "combined"
If heapname is not valid, false is returned. Which heapnames are valid depends on the port and CHDK version.
For very old vxworks cameras without GetMemInfo the only valid fields for the system heap will be those defined by chdk and free_block_max_size.
if heapname not given, meminfo is returned for heap that CHDK is configured to allocate from at compile time.
The values of the allocated_* fields for exmem are not compatible with the same values for "system", and are generally not meaningful.
CHDK 1.3 adds several changes to memory management which affect this function.
CHDK allocates memory from all available pools, so a new heap name "combined" is available, which gives the total amount of memory available to CHDK. This is now the default if no heapname is specified, and chdk_malloc is now set to true for all heaps.
For "combined" free_block_max_size gives the largest single free block. Other sizes and counters give the total from all available heaps. start_adress and end_address are not returned.
An additional heap called "aram" is available on some cameras.
The meaning of allocated_size, allocated_peak and allocated_count for exmem and aram now match the meaning in dryos.
Get the current image directory as a string.
This section lists commands which are available both in Lua and in uBasic.
Logging can only be disabled by 'print_screen(false)'. Using 'print_screen(true)' enables logging in overwrite mode to LOG_0001.TXT. Using print_screen(n) enables logging in overwrite mode to LOG_nnnn.TXT if n>-10000. ? Using print_screen(n) enables logging in append mode to LOG_dddd.TXT if n< -10000 where d=abs(-10000-n).
Note: you might want to use os.date (human readable) or os.time() (unix timestamp) instead. In both cases, having no parameters will return the current value.
Listed below are several Lua functions used to put simple graphical elements on the screen. You can draw pixels, straight lines, ellipses, rectangles and even text strings.
Note : For better-controlled drawing (and more importantly redrawing) there is a drawings.lua module described here: Lua Drawings Module.
Draws (writes?) a string t, at (x,y) where letters have color clt and background has color clb.
In general you can use two types of colors - Canon palette, which is specific for each camera or more portable CHDK script palette.
Canon palette colors are numbers in a range 0-255. They are different for various cameras and even on one camera palette differs between modes (play, rec and even more). For example number 100 might for one camera mean blue in rec mode and yellow on play mode but for another camera red in rec mode and white in play mode.
Note 1: Not all colors are exactly the same and exist on all cameras. You might expect, that every camera should have transparent, white, black, red, green and blue colors. In some cameras other tones might default to these basic colors. Nevertheless, CHDK script palette is much more portable.
Note 2: Everything, that you draw on a screen will be wiped away, when screen is updated (for example on camera rotation, OSD changes and maybe in other cases. These simple Lua drawings are quite ephemeral.
Since changeset #2453 (CHDK 1.2) are some mathematical functions available. CHDK only knows integers. Decimal numbers are scaled with 3 decimals, 123.456 => 123456.
(') represents a virtual decimal point. Please do not use in a real script!
All values ​​are around 1000 extended to 3 digits represent.
phi=PI/2 or 3*PI/2 -> x<>inf.