CHDK Wiki
Advertisement

Template:New wiki Since nobody is presently working on making a more elaborate section of how to write scripts for CHDK, this might be a good place to do some brainstorming on the right way to use the uBASIC programming language and available commands. Eventually some of these miight become detailed enough to add to the more official /CHDK/uBASIC/ main page.

I haven't done any programming in BASIC for about 30 years, and I am totally unfamiliar with uBASIC, but I'm going to do my best to try to at least get something started in the way of a tutorial that others with no programming knowledge could follow to write their own scripts.

If anyone sees some errors (there will be many), and can correct, elaborate, or clarify on some section already started please do so. If you can add your own section on an area that has not been covered yet, again, please do so.


The REM Command

The "rem" (which stands for "remark") command is sometimes used to place a comments in a script. It is only used as a reminder for the person writing or viewing the sciprt. Like an internal note. This command is not exectued nor seen when the script is run. However, keep in mind that scripts for CHDK can be only 2k (2 thousand characters) in length. Too many REM statements can slow down your script as well as taking up valuable space.

2K may not seem like much room to write a program in these days, when most of your simple programs are well over 1meg (1,000k) in size, but I once wrote a full-featured space-maze game with space-craft and retro-rockets in under 2k. A creative programmer can do much in 2k.



The Script Header

When viewing scripts you'll often see the opening section look something like this:
@title Interval shooting
@param a Shoot count
@default a 5
@param b Interval (Minutes)
@default b 0

Let's break down what each of those lines mean and how they are used by CHDK.

@title Your Script Title

This is the title that will appear when you have the script loaded in CHDK and go to "Scripting Parameters" menu selection. It will appear under the line "----Current Script----" as well as in the lower-left corner of your viewfinder when you are in <ALT> mode. Keep your title short (24 characters or less). Otherwise the title will cover up the <ALT> label.

@param x (label)
@default x n
This is where you will define the begining values of any variables used in your script. These are often used to set how many exposures you want, how long of a delay you want, how many bracketing steps you want, etc. These variables can be changed by the end-user from the "Scripting Parameters" menu items. In that sub-menu, they will appear under the line "----Script Parameters-----"
@param x (label)
The "x" in that line will be one of any lower-case latin letter from a to j. The (label) is the text string that will appear in your "----Script Parameters----" list, to let the end user know which of the variables they are changing (i.e. number of shots, how many steps, etc.).
Up to 10 @param statments, user-controllable variables, may be used in any one script.
@default x n
This statement sets up the default, or beginning value of your (lower-case letter) variable, where "x" is the variable from the @param statement above, and "n" is the default value to start with. This value is only used when script is loaded first time.
Notes:
If there is no @title command the filename of script is used.
If there are no @param commands HDK assumes that there are three adjustable variables: 'a', 'b' and 'c'.


After your default variable values have been defined here, it is good to add some lines right after this section to ensure those numbers will be used in case the user has input 0 (zero) for some value that needs to be higher (or lower). You will see this in scripts as:

if a<2 then let a=5
If your default value that you wanted the user to start out at for parameter variable a was 5, then if they left that setting at 0 (zero) or 1, then this will automatically increase that variable's value back up to 5.

After you have set up your variable parameters, then comes the brunt of your script, the part that does the actual work and tells the camera what to do, when to do it, and what buttons or commands need to be executed. Since we are working with a very small subset of the larger uBASIC programming language, it might be good to list and explain only those that are availble to the CHDK script writer.



The Basics of BASIC Programming

Logic Commands
All programs are designed to mindlessly repeat some commands. In order to make them work in the proper order, and the correct number of sequences, they have to be contained in some simple recurring loops and counters. Testing for when some condition has been met, before it can go onto the next command, or finally end the program (script).

There are several ways this can be done in BASIC. By using numeric counters, and loops. There are some built-in commands to simplify these tasks.

The LET Command
This one is simple. If you see a command that says "let a = 2" then that's exactly what happens. It defines the value of 2 to the variable a.
The IF / THEN / ELSE Commands
These are used to test for the truth of a certain condition. IF something is true, THEN this takes place, ELSE (otherwise) do this if it is not true.
A simple example:
if a > 2 then goto "subroutine1"
If in your script, the variable a has been assigned to a value greater-than 2, then the script will jump to the labeled subroutine1.
if a >2 then goto "subroutine1" else "subroutine2"
In this case if a is NOT greater than the value of 2, your program will jump to subroutine2.
The conditional expressions allowed in uBASIC are: = (equal to), > (greater than), < (less than), <> (not equal to), <= (less than or equal to), >= (greater than or equal to)


The FOR / TO / NEXT Commands
These are used to set up simple loops. You will often see them in scripts as in this example:
for n=2 to a
    sleep t
    print "Shoot", n, "of", a
    shoot
next n
The first line "for n=2 to a" means that the "for / to / next" loop will run while variable-n equals the sequence of numbers of 2 up to whatever the number variable-a has been assigned to. The commands that take place in the loop are containted between the FOR statement and the NEXT statment. "next n" tells the loop to go back to the beginning "for ..." statement until the the a value has been reached.

For example:

for n=1 to 10
   print "This is line number", n
next n

This will produce the sequence of:

This is line number 1
This is line number 2
This is line number 3
.
.
.
This is line number 9
This is line number 10

And then that loop will end and go onto the next sequence of commands.


Math Expressions allowed in uBASIC

Addition, Subtraction +, -
Multiplication, Division *, /
Integer Division \
Remainder @
Power ^
Note 1. Remainder of the last integer division is in the system variable RES.
Note 2. X^Y^Z is interpreted as (X^Y)^Z.
Note 3. A += B is equivalent to A = A + B, except that A is evaluated only once. A *= B + C stands for A = A * (B + C) etc.


[Added by GrAnd]
I'm afraid, but CHDK uBasic supports only integer type of variables and expressions. Just +, -, *, /, %, |, &, <, > and = operations. And just simple assignment =.

Advertisement