CHDK/uBASIC/uBASIC syntax


 * !!! This text has to be updated !!!

=Camera scripting using uBASIC=

The HDK includes a is a slightly modified version of the uBASIC compiler written by Adam Dunkels, http://www.sics.se/~adam/ubasic/

The camera is now completely scriptable with BASIC programs that simulate pressing keys

uBASIC Syntax short manual
Keywords: let, if, then, else, for, to, next, goto, gosub, return, end.

Special keywords: click, shoot, sleep.

Variables: lower latin letter from a to z. 32 bit signed integer.

Labels: This version of ubasic doesn't support line numbers. Instead, labels are used. A label must be the only statement in a line and start with a colon(:).

Special keywords syntax:
 * click "button_name"
 * where button_name can be one of: up, down, left, right, set, shoot_half, shoot_full, zoom_in, zoom_out, menu, display, print, erase. Corresponding button is "clicked" (pressed and released).


 * shoot
 * Take a picture. Procedure will return when first raw data will be ready for processing and specified time in menu had elapsed.
 * Warning: as two "raws" are there when shooting with long exposures this function is partially broken.


 * sleep  
 * Delay for   ms. Timer resolution is around 10-30 ms.

Script must be named SCRIPT.BAS and placed in the root directory on a card.

(Note 1: Newer versions of CHDK allow for saving scripts in a /SCRIPTS/ sub-folder. This makes for easier selection and loading of different scripts. They may have a more descriptive name, but they must end with the .bas extension. Also, some features (button commands and menu selections) between the A-Series and Sx IS cameras are slightly different. Scripts may need to be slightly modified to run on one line of camera or the other. If you write and share scripts with others it would help if you make note somewhere of which camera your particular script was written for, or if it will run on all of them.)

(Note 2: If you use the auto-loading/booting feature of CHDK it will still read the default "script.bas" file in the root folder of your SD card on startup. If you do not wish to have this happen then move, rename, or delete this "script.bas" file from the root folder. You can make use of this feature however, by copying and renaming your most favorite and often-used script to "script.bas" and putting it in the root of your SD card. Then every time you power-up your camera that script will be loaded and ready to go every time that [alt] features are toggled on and you press the shutter-button. This behavior is retained for backward compatibility. Anyway, last used script is loaded upon CHDK is booted.)

Default script follows: sleep 1000 if a<1 then let a=2 if b<1 then let b=3 for s=1 to a  shoot for n=1 to b    click "right" next n next s shoot end

Note that values of variables a, b and c are set to values you can choose from the Alt. Conf. menu.

Maximum script size is 2K. A maximum of 4 nested for loops is allowed. 10 nested gosub calles are possible.

If compile/parse error occurs message with line number is shown in the upper left corner of a display.

Using labels and gosub
For complex programming tasks, it is often helpful to split the program into smaller subroutines that can be called with the gosub command. You can jump to labels using the gosub statements. Due to a known bug, you might need to add a space char right after the label (":label "), otherwise you might get an "Unk Label"-error. A simple program that captures 3 images with increased ISO settings would look like this: shoot for i=1 to 3 gosub "incISO" shoot next i for i=1 to 3 gosub "decISO" next i end

click "menu" [some more clicks] return

click "menu" [some more clicks] return