CHDK Wiki
m (→‎uBASIC Syntax short manual: +range for unsigned int variables: -2147483648 to +2147483647)
 
(7 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
The camera is now completely scriptable with uBASIC programs that simulate pressing keys
 
The camera is now completely scriptable with uBASIC programs that simulate pressing keys
   
  +
===uBASIC Syntax short manual===
+
===uBASIC Syntax Short Manual===
   
 
'''Keywords:''' ''let'', ''if'', ''then'', ''else'', ''for'', ''to'', ''next'', ''goto'', ''gosub'', ''return'', ''end''.
 
'''Keywords:''' ''let'', ''if'', ''then'', ''else'', ''for'', ''to'', ''next'', ''goto'', ''gosub'', ''return'', ''end''.
Line 13: Line 14:
 
'''Special keywords:''' ''click'', ''press'', ''release'', ''shoot'', ''sleep''.
 
'''Special keywords:''' ''click'', ''press'', ''release'', ''shoot'', ''sleep''.
   
'''Variables:''' lower latin letter from ''a'' to ''z''. 32 bit signed integer (-2147483648 to +2147483647).
+
'''Variables:''' Upper and lower case Latin letters from ''A-Z'' and ''a''-''z'' as 32-bit signed integers (-2147483648 to +2147483647).
 
   
'''Labels:''' This version of uBasic doesn't support line numbers. Instead, labels are used. A label must be the only statement
+
'''Labels: ''' This version of uBasic does not support line numbers. Instead, labels are used. A label must be the only statement in a line and start with a colon (''':''').
in a line and start with a colon(''':''').
 
   
 
'''Special keywords syntax:'''
 
'''Special keywords syntax:'''
Line 25: Line 24:
   
 
* shoot
 
* shoot
:Take a picture. Procedure will return when first raw data will be ready for processing '''and''' specified time in menu had elapsed.
+
:Take a picture. Procedure will return when first raw data are ready for processing '''and''' specified time in menu has elapsed.
   
 
* sleep ''<number>''
 
* sleep ''<number>''
:Delay for ''<number>'' ms. Timer resolution is around 10-30 ms.
+
:Delay for ''<number>'' in ms. Timer resolution is around 10-30 ms.
   
 
See the [[uBASIC/TutorialScratchpad|Scripting Tutorial]] for more complete explanation and use of all commands.
 
See the [[uBASIC/TutorialScratchpad|Scripting Tutorial]] for more complete explanation and use of all commands.
  +
  +
{{tagb|→}} See also the '''[[CHDK_Scripting_Cross_Reference_Page|Scripting Cross Reference Page]]''' for the complete list of CHDK scripting commands for Lua and uBasic.
   
 
(Note: Newer versions of CHDK allow for saving scripts in a /CHDK/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: Newer versions of CHDK allow for saving scripts in a /CHDK/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.)
Line 49: Line 50:
 
Note that values of variables ''a'', ''b'' and ''c'' are set to values you can choose from the Alt. Conf. menu.
 
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 8K. A maximum of 4 nested for loops is allowed. 10 nested gosub calls are possible. (Clarification needed here -- maximum nested DEPTH of for loops is 4? and specific to GOSUBs, maximum nested *DEPTH* is 10 ?)
+
Maximum script size is 8K. A maximum depth of 4 nested ''for ''loops is allowed. Ten nested gosub calls are possible. (Clarification needed here -- maximum nested DEPTH of for loops is 4? and specific to GOSUBs, maximum nested *DEPTH* is 10 ?)
   
If execution/parse error occurs message with line number is shown in the upper left corner of the display.
+
If execution/parse error occurs, an error message with line number in which the error occured is shown in the upper left corner of the display.
   
===Using labels and gosub===
+
===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.
+
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.<br />
 
A simple program that captures 3 images with increased ISO settings might look like this:
<s>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.</s> -> Should be fixed in build #66. <br>
 
A simple program that captures 3 images with increased ISO settings would look like this:
 
 
<pre>
 
<pre>
 
shoot
 
shoot
Line 79: Line 79:
 
return
 
return
 
</pre>
 
</pre>
 
 
 
[[Category:uBASIC|Syntax]]
 
[[Category:uBASIC|Syntax]]

Latest revision as of 11:43, 8 December 2012

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

Camera scripting using uBASIC

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

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


uBASIC Syntax Short Manual

Keywords: let, if, then, else, for, to, next, goto, gosub, return, end.

Special keywords: click, press, release, shoot, sleep.

Variables: Upper and lower case Latin letters from A-Z and a-z as 32-bit signed integers (-2147483648 to +2147483647).

Labels: This version of uBasic does not 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", press "button_name", release "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).
  • S-series buttons: iso, flash, mf, macro, video, timer
  • shoot
Take a picture. Procedure will return when first raw data are ready for processing and specified time in menu has elapsed.
  • sleep <number>
Delay for <number> in ms. Timer resolution is around 10-30 ms.

See the Scripting Tutorial for more complete explanation and use of all commands.

See also the Scripting Cross Reference Page for the complete list of CHDK scripting commands for Lua and uBasic.

(Note: Newer versions of CHDK allow for saving scripts in a /CHDK/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.)

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 8K. A maximum depth of 4 nested for loops is allowed. Ten nested gosub calls are possible. (Clarification needed here -- maximum nested DEPTH of for loops is 4? and specific to GOSUBs, maximum nested *DEPTH* is 10 ?)

If execution/parse error occurs, an error message with line number in which the error occured is shown in the upper left corner of the 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.
A simple program that captures 3 images with increased ISO settings might look like this:

 shoot
 for i=1 to 3
   gosub "incISO"
   shoot
 next i
 for i=1 to 3
   gosub "decISO"
 next i
 end

 :incISO
   click "menu"
   [some more clicks]
   return

 :decISO
   click "menu"
   [some more clicks]
   return