en:documentation:script_example
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:documentation:script_example [2014/06/15 15:49] – created pch | en:documentation:script_example [2016/01/20 15:55] (current) – [Script example] pch | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Script example ====== | ====== Script example ====== | ||
| - | This page contain | + | This page give tips and example of scripting functions.\\ |
| + | You can also look at the three [[toolbox|standard tool box]] code from within the program.\\ | ||
| + | For more details about a specific function see the [[script_reference|script reference]] page. | ||
| + | ===== Generality ===== | ||
| + | |||
| + | We first look in detail at the code of the Goto button of the Observer tools standard tool box.\\ | ||
| + | This cover many programming basis. | ||
| + | |||
| + | The full script code look as following: | ||
| + | < | ||
| + | // Slew telescope | ||
| + | var ra,de: double; | ||
| + | a,b,r: string; | ||
| + | c: Tstringlist; | ||
| + | begin | ||
| + | memo_1.clear; | ||
| + | if MenuTelescopeConnect.checked then begin | ||
| + | if not StrToAR(Edit_1.text, | ||
| + | if not StrToDE(Edit_2.text, | ||
| + | a: | ||
| + | b: | ||
| + | GetSL(' | ||
| + | c.clear; | ||
| + | c.add(' | ||
| + | c.add(a); | ||
| + | c.add(b); | ||
| + | r: | ||
| + | c.clear; | ||
| + | memo_1.lines.add(r); | ||
| + | end | ||
| + | else memo_1.lines.add(rsTelescopeNot); | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | Take a look at each part in detail: | ||
| + | < | ||
| + | // Slew telescope | ||
| + | </ | ||
| + | Is a comment, you can use < | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | var ra,de: double; | ||
| + | a,b,r: string; | ||
| + | c: Tstringlist; | ||
| + | </ | ||
| + | Define the variable we use later in the script.\\ | ||
| + | Important variable type are: integer, double, string.\\ | ||
| + | The Tstringlist type is use here to send a command to Skychart. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | begin | ||
| + | </ | ||
| + | The start of our program. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | memo_1.clear; | ||
| + | </ | ||
| + | Clear the text box we use to show the messages. This ensure the text box is not filled by previous messages. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | if MenuTelescopeConnect.checked then begin | ||
| + | </ | ||
| + | We test the Checked property of the menu item MenuTelescopeConnect. This indicate we have connected the telescope to the program.\\ | ||
| + | If the result is true we execute the code block starting at " | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | if not StrToAR(Edit_1.text, | ||
| + | </ | ||
| + | We try to convert the RA in HMS format from the text in Edit_1 text box to a numeric value. If the conversion fail (because we type some junk in the text box) we show an error message and exit. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | | ||
| + | </ | ||
| + | Convert the RA back to string representation but with the decimal format required by the command. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | GetSL(' | ||
| + | c.clear; | ||
| + | </ | ||
| + | Request a TStringList object identified by STRL1. We clear any data that may stay in the object. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | c.add(' | ||
| + | c.add(a); | ||
| + | c.add(b); | ||
| + | </ | ||
| + | Add the command and the required parameters (in this case RA and DEC) to the stringlist. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | | ||
| + | </ | ||
| + | Execute the command and store the result in the variable r. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | memo_1.lines.add(r); | ||
| + | </ | ||
| + | Show the result of the command to the text box. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | else memo_1.lines.add(rsTelescopeNot); | ||
| + | </ | ||
| + | The case the test MenuTelescopeConnect.checked is false we execute this line.\\ | ||
| + | It show in the text box a translation in the local language of ' | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | | ||
| + | </ | ||
| + | The end of the program. | ||
| + | |||
| + | |||
| + | ===== Call an external library ==== | ||
| + | |||
| + | You can define a function in an external library for use within your script as another local function. | ||
| + | |||
| + | This example implement a simple chronometer by using the GetTickCount function of the Windows API.\\ | ||
| + | There is two button Start and Stop and two text box. A global integer variable is used to store the start time. | ||
| + | |||
| + | Script for the Start button: | ||
| + | < | ||
| + | | ||
| + | var | ||
| + | tick: Longint; | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | Script for the Stop button: | ||
| + | < | ||
| + | function GetTickCount: | ||
| + | var | ||
| + | t: double; | ||
| + | t1,tick: Longint; | ||
| + | begin | ||
| + | tick: | ||
| + | getI(' | ||
| + | t: | ||
| + | edit_1.text: | ||
| + | edit_2.text: | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | You can call any library function this way but beware this is system dependent, the kernel32.dll library is not available on Mac or Linux. | ||
| + | |||
| + | Another limitation is that many library function expect a pointer to a parameter structure. As the script language use byte code internally (as Java) it cannot use a pointer to give the parameters. A solution is to write a C library wrapper that export the function with a flat parameter list. | ||
| + | |||
| + | ===== Using ASCOM directly ==== | ||
| + | |||
| + | This describe how to use an ASCOM device directly from your script without any use of the Skychart internal ASCOM telescope. | ||
| + | |||
| + | This can be use to access another class of device, the example here connect to a dome, or to access additional properties for your telescope.\\ | ||
| + | In the later case you must be careful that your script work as a concurrent to Skychart main program for the device access. | ||
| + | |||
| + | ==== Use the ASCOM chooser === | ||
| + | |||
| + | The following code assigned to a button allow to select the ASCOM Dome driver we want to use. The driver name is saved in the text field Edit_1. | ||
| + | |||
| + | < | ||
| + | var | ||
| + | V: variant; | ||
| + | w,s: widestring; | ||
| + | | ||
| + | V := CreateOleObject(' | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | Replace w: | ||
| + | |||
| + | ==== Connect to the ASCOM driver === | ||
| + | |||
| + | The following code is for the " | ||
| + | < | ||
| + | var | ||
| + | D: variant; | ||
| + | s: widestring; | ||
| + | begin | ||
| + | s: | ||
| + | getV(' | ||
| + | if VarIsEmpty(D) then | ||
| + | D := CreateOleObject(s); | ||
| + | D.connected: | ||
| + | setV(' | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | ==== Use the ASCOM driver === | ||
| + | |||
| + | Now we want to add a button to open the dome shutter. This is just an example, at this point any ASCOM property can be use.\\ | ||
| + | The first test protect again a program crash if we try to use an initialized variant.\\ | ||
| + | The second test protect again an ASCOM error if the dome is not connected. | ||
| + | |||
| + | < | ||
| + | var | ||
| + | D: variant; | ||
| + | begin | ||
| + | getV(' | ||
| + | if (not VarIsEmpty(D)) then | ||
| + | if D.Connected then | ||
| + | | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | ==== Disconnect the ASCOM driver === | ||
| + | |||
| + | Add a button to disconnect the driver and release the resources. | ||
| + | |||
| + | < | ||
| + | var | ||
| + | D: variant; | ||
| + | begin | ||
| + | getV(' | ||
| + | if (not VarIsEmpty(D)) then | ||
| + | if D.Connected then | ||
| + | | ||
| + | D: | ||
| + | setV(' | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | ===== Open a document ===== | ||
| + | |||
| + | The following code open the Skychart documentation page in the default web browser.\\ | ||
| + | You can use any document type with this function, the document open with the default application the same way as if you double click the document in the file explorer. | ||
| + | |||
| + | < | ||
| + | begin | ||
| + | OpenFile(' | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | ===== Run a command ===== | ||
| + | |||
| + | There is two different way to run an external command or program, depending if you want to wait for a result or not. | ||
| + | |||
| + | ==== Wait for a result ==== | ||
| + | |||
| + | The following command run the DIR command in the current directory. The result is stored in a stringlist and later show in a text memo. It contain the list of files in the directory. | ||
| + | |||
| + | < | ||
| + | var r: | ||
| + | begin | ||
| + | GetSL(' | ||
| + | r.clear; | ||
| + | RunOutput(' | ||
| + | Memo_1.lines.assign(r); | ||
| + | end. | ||
| + | </ | ||
| + | |||
| + | ==== No wait ==== | ||
| + | |||
| + | If the command can run for an undetermined time or do not produce an output you need to use the following form.\\ | ||
| + | This example run the Variable star observer program and exit immediately. | ||
| + | |||
| + | < | ||
| + | begin | ||
| + | Run(' | ||
| + | end. | ||
| + | </ | ||
en/documentation/script_example.1402840158.txt.gz · Last modified: 2015/11/06 20:33 (external edit)
