String manipulation functions

New Lua functions

peek

Return the value in memory at the given address, or nil if the address alignment or size is invalid.

size is one of 1, 2 or 4, and specifies to whether to read a byte, half word, or word. Default is 4. (since changeset #864)

WARNING: Reading invalid addresses will crash your camera!

poke

(since changeset #520)

status = poke(address,value[,size])

Set the memory at address to value. Returns true, or nil if the address allignment or size is invalid.

size is one of 1, 2 or 4, and specifies to whether to write a byte, half word, or word. Default is 4. (since changeset #864)

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.

get_buildinfo

(since changeset #520)

get information about the running platform and chdk version. Returns a table with the members
platform, platsub, version, build_number, build_date and build_time.
As of changeset #606 os ("dryos" or "vxworks") and platformid (a number) are also returned.

This can be used to make scripts work around camera differences without requiring users to set anything.

Note: version in is currently just the string "CHDK". build_number contains the entire build number as a string. This isn't very convenient, and may change in the future.

get_prop

(available since build 0.6.5 / changeset #541)

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.

Usage:

props=require("propcase")
tv=get_prop(props.TV)

get_prop_str / set_prop_str / binstr

(available since changeset #1306)

The Lua commands get_prop_str, set_prop_str and binstr can be used to access arbitrary sized propcases from Lua.

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.

RAW development

(available since CHDK version 0.8.4 / changeset #594)

There are the following four new functions for merging RAW images directly in the camera:

set_raw_develop("filename")

Develop RAW on next shot. If filename is nil (or omitted), any pending raw develop is canceled.

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.

Sample script

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.

Flash parameters

Canon cameras store some parameters in onboard flash memory. These functions allow you to query them.

num=get_flash_params_count()

num is the number of parameters. Parameters are numbered starting form zero.

str,num=get_parameter_data(id)

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.

Notes

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.

Sample script

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.

Notes:

The parameter IDs may vary between cameras, so scripts using them will not be portable !

This script is included in the 'complete' download packages from the Autobuild server

set_curve_state

get_meminfo

meminfo=get_meminfo([heapname])
get camera memory information
heapname="system" or "exmem" if not given, meminfo is returned for heap used by CHDK for malloc
meminfo is false if the requested heapname isn't valid ("exmem" when exmem is not enabled, or unknown)
otherwise, a table of the form

meminfo ={
name -- string "system" or "exmem"
chdk_malloc -- bool, this is the heap used by CHDK for malloc
chdk_start -- number, load address of CHDK
chdk_size -- number, size of CHDK image-- all the following are numbers, will not be set if not available
start_address
end_address
total_size
allocated_size
allocated_peak
allocated_count
free_size
free_block_max_size
free_block_count
}

notes

under vxworks and cameras without GetMemInfo only the only valid fields for the system heap will be those defined by chdk and free_block_max_size

the meaning of fields may not correspond exactly between exmem and system

Event procedure and native function API

Changed Lua commands

This section lists commands which are available both in Lua and in uBasic.It describes differences or improvements in the syntax / behaviour of these commands

print_screen

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).

get_mode

(available since CHDK version 0.6.7 / changeset #545)

Returns the active mode of the camera (3 values): bool is_record, bool is_video, number mode. The mode number is a bit field with several different values, exactly as would be returned by mode_get() in the CHDK C code. See the CHDK source for more information.

Drawings

Since changeset #1479 you are able to put simple graphical elements on the screen. You can draw pixels, straight lines, ellipses, rectangles and even text strings. The basic drawing commands are described below. For more complicated and better-controlled drawing try to use commands from drawings.lua module. This module is described here: Lua Drawings Module.

Commands available are:

draw_pixel( x, y, cl )

Puts pixel at (x,y) coordinates with a color cl;

draw_line( x1, y1, x2, y2, cl)

Draws a line that begins at (x1,y1) and ends at (x2,y2) with a color cl;

draw_rect( x1, y1, x2, y2, cl, th)

Draws a rectangle border which top-left corner is at (x1,y1), bottom-right at (x2,y2), with color cl and thickness of th pixels (thickness is an optional parameter, defaults to 1);

draw_rect_filled( x1, y1, x2, y2, cl1, cl2, th)

Similar to above, but the border will be filled with color cl2;

draw_ellipse( x, y, a, b, cl)

Draws ellipse shape which center is at (x,y), half-height is a, half-width is b and color cl [note: you can not specify thickness for an ellipse (yet?)];

draw_ellipse_filled( x, y, a, b, cl1, cl2)

Similar to above, but the shape will be filled with color cl2;

draw_string( x, y, t, clt, clb)

Draws (writes?) a string t, at (x,y) where letters have color clt and background has color clb. You can not specify font and font-size (yet?).

draw_clear()

Deletes all drawing objects.

About Colors

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.

CHDK script palette are numbers between 256-273 and they mean:

256 transparent (no color)

257 black

258 white

259 red

260 dark red

261 light red

262 green

263 dark green

264 light green

265 blue

266 dark blue

267 light blue

268 grey

269 dark grey

270 light grey

271 yellow

272 dark yellow

273 light yellow

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.