-
Notifications
You must be signed in to change notification settings - Fork 331
/
Start.shtml
248 lines (197 loc) · 8.55 KB
/
Start.shtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15.17), see www.w3.org">
<title>JMRI: Getting Started with Scripting</title><!-- Style -->
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii">
<link rel="stylesheet" type="text/css" href="/css/default.css"
media="screen">
<link rel="stylesheet" type="text/css" href="/css/print.css"
media="print">
<link rel="icon" href="/images/jmri.ico" type="image/png">
<link rel="home" title="Home" href="/"><!-- /Style -->
</head>
<body>
<!--#include virtual="/Header" -->
<div id="mBody">
<!--#include virtual="Sidebar" -->
<div id="mainContent">
<!-- Page Body -->
<h1>JMRI: Getting Started with Scripting</h1>
<h2>Running "Hello World"</h2>There are several ways to use
Python scripts with JMRI. The easiest is to use the built-in
support in the standard JMRI applications: PanelPro,
DecoderPro, etc.<br>
To do that:
<ul>
<li><a href=
"../../../package/jmri/jmrit/beantable/images/jmriScriptWindow.png">
<img src=
"../../../package/jmri/jmrit/beantable/images/jmriScriptWindow.png"
width="250" height="136" align="right"></a>From the
"Panels" menu, select "Script Entry". This will give you a
window where you can type one or more commands to execute.
(Note that it might take a little while the first time you
do this as the program finds its libraries; it will be
faster the next time) The commands stay in the window, so
you can edit them and rerun them until you like them.</li>
<li>From the "Panels" menu, select "Script Output". This
creates a window that shows the output from the script
commands you issue.</li>
<li>To see this in operation, type
<pre>
print "Hello World"
</pre>in the input window, and click "Execute". You should see the
output in the Script Output window immediately:
<pre>
>>> print "Hello World"
Hello World
</pre>
</li>
<li>Python also evaluates expressions, etc. Remove the
contents of the input window (select it and hit delete),
and enter
<pre>
print 1+2
</pre>then click execute. The output window should then show
something like:
<pre>
>>> print 1+2
3
</pre>
</li>
</ul>
<h2>Sample layout commands</h2>
<pre>
>>> lt1 = turnouts.provideTurnout("1")
>>> lt1.setCommandedState(CLOSED)
>>> print lt1.commandedState
2
>>> lt1.commandedState = THROWN
>>> print lt1.commandedState
4
>>> turnouts.provideTurnout("1").getCommandedState()
1
</pre>Note that this is running a complete version of the JMRI
application; all of the windows and menus are presented the same
way, configuration is done by the preferences panel, etc. What the
Jython connection adds is a command line from which you can
directly manipulate things.
<p>This also shows some of the simplifications that Jython
and the Python language brings to using JMRI code. The Java
member function:</p>
<pre>
turnout.SetCommandedState(jmri.Turnout.CLOSED);
</pre>can also be expressed in Jython as:
<pre>
turnout.commandedState = CLOSED
</pre>
<p>This results in much easier-to-read code.</p>
<p>There are a lot of useful Python books and online
tutorials. For more information on the Jython language and
it's relations with Java, the best reference is the <a href=
"http://www.oreilly.com/catalog/jythoness/">Jython
Essentials</a> book published by O'Reilly. The <a href=
"http://www.jython.org/">jython.org web site</a> is also very
useful.</p>
<h3>Access to JMRI</h3>JMRI uses the factory-pattern
extensively to get access to objects. In Java this results in
verbose code like
<pre>
Turnout t2 = InstanceManager.getDefault(TurnoutManager.class).newTurnout("LT2", "turnout 2");
t2.SetCommandedState(Turnout.THROWN)
</pre>Jython simplifies that by allowing us to provide useful
variables, and by shortening certain method calls.
<p>To get access to the SignalHead, Sensor and Turnout
managers and the CommandStation object, several shortcut
variables are defined:</p>
<ul>
<li>sensors</li>
<li>turnouts</li>
<li>lights</li>
<li>signals (SignalHeads)</li>
<li>masts (SignalMasts)</li>
<li>routes</li>
<li>blocks</li>
<li>reporters</li>
<li>memories</li>
<li>powermanager</li>
<li>programmers</li>
<li>dcc (current command station)</li>
<li>shutdown (current shutdown manager)</li>
<li>audio</li>
</ul>These can then be referenced directly in Jython as
<pre>
t2 = turnouts.provideTurnout("12");
dcc.sendPacket(new byte[]{0x12, 0x32, 0x4E}, 5)
</pre>Note that the variable t2 did not need to be declared.
<p>Jython provides a shortcut for parameters that have been
defined with Java-Bean-like get and set methods:</p>
<pre>
t2.SetCommandedState(Turnout.THROWN)
</pre>can be written as
<pre>
t2.commandedState = THROWN
</pre>where the assignment is actually invoking the set method.
Also note that THROWN was defined when running the Python script at
startup; CLOSED, ACTIVE, INACTIVE, RED, YELLOW and GREEN are also
defined. (The shortcuts are all defined in a file called
"jmri_defaults.py" that you can find in the "jython" directory of
the distribution)
<p>A similar mechanism can be used to check the state of
something:</p>
<pre>
>>> print sensors.provideSensor("3").knownState == ACTIVE
1
>>> print sensors.provideSensor("3").knownState == INACTIVE
0
</pre>Note that Jython uses "1" to indicate true, and "0" to
indicate false, so sensor 3 is currently active in this example.
You can also use the symbols "True" and "False" respectively.
<p>You can directly invoke more complicated methods, e.g. to
send a DCC packet to the rails you type:</p>
<pre>
dcc.sendPacket([0x01, 0x03, 0xbb], 4)
</pre>This sends that three-byte packet four times, and then
returns to the command line.
<h3>Script files, listeners, and classes</h3>Scripting would
not be very interesting if you had to type the commands each
time. So you can put scripts in a text file and execute them
by selecting the "Run Script..." menu item, or by using the
"Advanced Preferences" to run the script file when the
program starts.
<p>Although the statements we showed above were so fast you
couldn't see it, the rest of the program was waiting while
you run these samples. This is not a problem for a couple
statements, or for a script file that just does a bunch of
things (perhaps setting a couple turnouts, etc) and quits.
But you might want things to happen over a longer period, or
perhaps even wait until something happens on the layout
before some part of your script runs. For example, you might
want to reverse a locomotive when some sensor indicates it's
reached the end of the track.</p>
<p>There are a couple of ways to do this. First, your script
could define a "listener", and attach it to a particular
sensor, turnout, etc. A listener is a small subroutine which
gets called when whatever it's attached to has it's state
change. For example, a listener subroutine attached to a
particular turnout gets called when the turnout goes from
thrown to closed, or from closed to thrown. The subroutine
can then look around, decide what to do, and execute the
necessary commands. When the subroutine returns, the rest of
the program then continues until the listened-to object has
it's state change again, when the process repeats.</p>
<p>For more complicated things, where you really want your
script code to be free-running within the program, you define
a "class" that does what you want. In short form, this gives
you a way to have independent code to run inside the program.
But don't worry about this until you've got some more
experience with scripting.</p>
<!--#include virtual="/Footer" -->
</div><!-- closes #mainContent-->
</div><!-- closes #mBody-->
</body>
</html>