CHDK Wiki
(Beginning some tutorial stuff.)
No edit summary
Line 3: Line 3:
   
 
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 '''some'''thing started in the way of a tutorial that others with no programming knowledge could follow to write their own scripts.
 
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 '''some'''thing 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'''.
   
 
----
 
----
Line 25: Line 27:
 
@param b Interval (Minutes)
 
@param b Interval (Minutes)
 
@default b 0
 
@default b 0
@param c Interval (Seconds)
 
   
 
Let's break down what each of those lines mean and how they are used by CHDK.
 
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 z. 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.).
  +
:
  +
:As far as I know so far, only 4 @param statments, user-controllable variables, may be used in any one script. Correct this if this is wrong, but I recall reading this somewhere in one of the many lengthy forum discussiosn online.
  +
  +
:'''@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.
  +
  +
  +
  +
----
  +
  +
== 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.
  +
;

Revision as of 14:03, 27 April 2007

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" 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 z. 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.).
As far as I know so far, only 4 @param statments, user-controllable variables, may be used in any one script. Correct this if this is wrong, but I recall reading this somewhere in one of the many lengthy forum discussiosn online.
@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.



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.