452 lines
22 KiB
Plaintext
452 lines
22 KiB
Plaintext
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy for HTML5 for Apple macOS version 5.8.0">
|
|
<!-- Copyright Bob Jacobsen 2008 -->
|
|
|
|
<title>JMRI: Scripting Examples</title>
|
|
<meta name="author" content="Bob Jacobsen">
|
|
<!--#include virtual="/help/en/parts/Style.shtml" -->
|
|
</head>
|
|
<body>
|
|
<!--#include virtual="/help/en/parts/Header.shtml" -->
|
|
|
|
<div id="mBody">
|
|
<!--#include virtual="Sidebar.shtml" -->
|
|
|
|
<div id="mainContent">
|
|
<!-- Page Body -->
|
|
|
|
<h1>JMRI: Scripting Examples</h1>
|
|
|
|
<p>The JMRI distributions come with a jython directory that contains a few example scripts.
|
|
This page provides a short description of each, copied from the comment in the scripts
|
|
themselves.</p>
|
|
|
|
<p>To run one of these scripts, start DecoderPro or PanelPro (set preferences to "LocoNet
|
|
Simulator" to run without connecting to a layout), then under the <strong>Scripting</strong>
|
|
{<em>Old: <strong>Panels</strong></em>} menu, select <strong>Run Script</strong>. In
|
|
DecoderPro, Run Script is in the <strong>Actions</strong> menu. Find and select the desired
|
|
script file in the "jython" folder of the JMRI program directory and click the Run
|
|
button.</p>
|
|
|
|
<p>To browse the most current set of scripts, please see the <a href=
|
|
"https://www.jmri.org/jython/">jython directory</a> on the JMRI website.</p>
|
|
|
|
<p><a href="../../../../../jython/AD4OnOff.py">AD4OnOff.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to show put a button on the screen that will enable or disable local control
|
|
of AD4 accessory decoders.<br>
|
|
This script has been superseded by the "Lock" capability in JMRI 1.9.3 and later. You can
|
|
directly request that JMRI lock/unlock a Turnout via the Turnout Table, Routes and Logix.</p>
|
|
|
|
<p><a href="../../../../../jython/AddButton.py">AddButton.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to add a button to the main JMRI application window that loads a script
|
|
file</p>
|
|
|
|
<p><a href="../../../../../jython/AlarmClock.py">AlarmClock.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script to pulse an output based on a Fast Clock.</p>
|
|
|
|
<p><a href="../../../../../jython/AppleScript.py">AppleScript.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for invoking an AppleScript from JMRI on Mac OS X.</p>
|
|
|
|
<p><a href="../../../../../jython/AssignIdTagToRollingStock.py">AssignIdTagToRollingStock.py</a>
|
|
</p>
|
|
|
|
<p>A more advanced example of scripting, this script "listens" for new or changed ID Tag Table entries (RFID Tags)
|
|
and opens a window to allow the user to enter a user name (typically locomotive or car/wagon number) and comment.
|
|
It does this by creating a property change listener on the ID Tag Table.
|
|
It will also update the OperationsPro Locomotive Table or Car Table if requested (check box on
|
|
the popup window). It will flag locomotive/car numbers that do not exist, as well as move an ID
|
|
Tag from one loco or car to another and eliminate dupicate assignment of RFID Tag to cars or locomotives.</p>
|
|
|
|
<p>This script illustrates the use of the JMRI property change listener, writing to the
|
|
Script Output window, writing information to the System Console, updating the ID Tag Table,
|
|
updating the OperationsPro Locomotive and Car Tables, and using Java swing to create a popup
|
|
window with entry fields and radio buttons.</p>
|
|
|
|
<p>For more information about the use of RFID and the Id Tags Table, see <a href=
|
|
"../../hardware/rfid/GettingStartedWithRFID.shtml">Getting Started with RFID</a>.</p>
|
|
|
|
<p><a href=
|
|
"../../../../../jython/AutoActiveTrains_Simulator.py">AutoActiveTrains_Simulator.py</a>
|
|
</p>
|
|
|
|
<p>Simulator for <a href=
|
|
"../../../package/jmri/jmrit/dispatcher/Dispatcher.shtml">Dispatcher</a> AutoActiveTrains.
|
|
While auto train(s) are "moving", repeatedly activates "next" allocated block, and deactivate
|
|
"last" occupied Block. Waits for debounce time plus a bit to allow signals, etc. to
|
|
respond.</p>
|
|
|
|
<p><a href="../../../../../jython/AutoDispatcher2.py">AutoDispatcher2.py</a>
|
|
</p>
|
|
|
|
<p>This script provides full layout automation, using connectivity info provided by Layout
|
|
Editor panels.</p>
|
|
|
|
<p><a href="../../../../../jython/AutoLayoutPowerOff.py">AutoLayoutPowerOff.py</a>
|
|
</p>
|
|
|
|
<p>This script turns off power to the layout automatically after there has been no activity
|
|
on the layout for a specified amount of time. (Activity is detected by monitoring block
|
|
occupancy detectors.) It is intended to be run from the Start-up Preferences.</p>
|
|
|
|
<p><a href="../../../../../jython/AutomatonExample.py">AutomatonExample.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for a JMRI "Automat" in Python. It listens to two sensors,
|
|
running a locomotive back and forth between them by changing its direction when a sensor
|
|
detects the engine. You need to set the speed of the engine using a throttle.</p>
|
|
|
|
<p><a href="../../../../../jython/BackAndForth.py">BackAndForth.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for a JMRI "Automat" in Python. It is based on the
|
|
AutomatonExample. It listens to two sensors, running a locomotive back and forth between them
|
|
by changing its direction when a Sensor detects the engine.</p>
|
|
|
|
<p><a href="../../../../../jython/BackAndForthTimed.py">BackAndForthTimed.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for a JMRI "Automat" in Python. It is based on the
|
|
AutomatonExample. It runs a locomotive back and forth using time delays.</p>
|
|
|
|
<p><a href="../../../../../jython/CanExample.py">CanExample.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to show how to send and receive CAN Frames.</p>
|
|
|
|
<p><a href="../../../../../jython/ControlPanel.py">ControlPanel.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to show a set of JButtons that show/hide panel windows when clicked.<br>
|
|
When this script is run, it finds all open Panels (either Panel Editor or Layout Editor) and
|
|
creates a small window with a button for each. The buttons are labeled with the names of the
|
|
Panels, and it's required that those names be unique.</p>
|
|
|
|
<p><a href="../../../../../jython/DebounceSensor.py">DebounceSensor.py</a>
|
|
</p>
|
|
|
|
<p>This script manages an internal Sensor as a debounced version of another Sensor. Both the
|
|
On and Off delays may be specified.</p>
|
|
|
|
<p><a href="../../../../../jython/DecoderPro.py">DecoderPro.py</a>
|
|
</p>
|
|
|
|
<p>Python code to start a DecoderPro app, complete with menu bar</p>
|
|
|
|
<p><a href="../../../../../jython/DisableOpsMode.py">DisableOpsMode.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to navigate through the GUI and disable the Ops Mode button on the main
|
|
DecoderPro window.</p>
|
|
|
|
<p><a href="../../../../../jython/GrandReset.py">GrandReset.py</a>
|
|
</p>
|
|
|
|
<p>A script to remove <strong>All</strong> Transits, Sections, SignalMastLogics and
|
|
remove all saved paths from the Block Table. Requires confirmation when you run it.</p>
|
|
|
|
<p><a href="../../../../../jython/InitTurnouts.py">InitTurnouts.py</a>
|
|
</p>
|
|
|
|
<p>Sample script showing how to initialize turnouts based on the state of their input sensors
|
|
(e.g. feedback)<br>
|
|
This is particularly useful for a C/MRI system, where the turnouts need to be set to a
|
|
particular state quickly.</p>
|
|
|
|
<p><a href="../../../../../jython/JButtonComplexExample.py">JButtonComplexExample.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to show a JFrame for data entry. The frame contains two JTextFields, and a
|
|
button which is inactive until data has been entered. Once activated, the button prints a
|
|
little diagnostic message when clicked. The print statement can be changed to include
|
|
whatever desired, e.g. throw a turnout, program a CV, etc.<br>
|
|
This script creates a panel called "Data entry" with two fields that data is to be entered
|
|
in. The script waits for something in both fields to be entered and then enables the "Enter
|
|
values" button. When "Enter values" button is clicked, it causes the data to be displayed on
|
|
the Java console and the "Data entry" panel to disappear. To see the output on the Java
|
|
console you need to open the Java console or select <strong>Script Output</strong> from the
|
|
PanelPro <strong>Scripting</strong> {<em>Old: <strong>Panels</strong></em>} menu.</p>
|
|
|
|
<p><a href="../../../../../jython/JButtonExample.py">JButtonExample.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to show a JButton which prints a little message when clicked. The print
|
|
statement can be changed to include whatever desired, e.g. throw a turnout, program a CV,
|
|
etc.</p>
|
|
|
|
<p><a href="../../../../../jython/jmri_defaults.py">jmri_defaults.py</a>
|
|
</p>
|
|
|
|
<p>Python code to define common JMRI defaults. Assumes JMRI has already been initialized, so
|
|
this can reference various managers, etc. This is only read once, when the JMRI library first
|
|
executes a script, so changes will not take effect until after restarting the program.</p>
|
|
|
|
<p><a href="../../../../../jython/PanelPro.py">PanelPro.py</a>
|
|
</p>
|
|
|
|
<p>Python code to start a PanelPro app, complete with menu bar. (probably obsolete at this
|
|
point, the usual method now is to start DecoderPro or PanelPro and then run Python scripts
|
|
from within it; left as an example of the approach)</p>
|
|
|
|
<p><a href="../../../../../jython/ListenerExample.py">ListenerExample.py</a>
|
|
</p>
|
|
|
|
<p>Example of an event listener for a JMRI class, in this case a Turnout.</p>
|
|
|
|
<p><a href="../../../../../jython/LnBushbyForwarder.py">LnBushbyForwarder.py</a>
|
|
</p>
|
|
|
|
<p><span class="since">Since <a href="https://www.jmri.org/releasenotes/jmri4.25.2.shtml" target=
|
|
"_blank">JMRI 4.25.2</a></span>Provides a mechanism to forward "normal" LocoNet switch
|
|
(Turnout) control messages to the DCC track signal when a Digitrax command station's Bushby
|
|
feature is enabled. (Note that this script can be <em>cause</em> congestion on LocoNet when
|
|
the Digitrax Bushby feature is in its default (disabled) condition.) See <a href=
|
|
"../../scripthelp/LnBushbyForwarder/LnBushbyForwarder.shtml">this page</a> for additional
|
|
information about this script.</p>
|
|
|
|
<p><a href="../../../../../jython/LnPowerButton.py">LnPowerButton.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to show a JButton which sends a LocoNet message when clicked. In this case,
|
|
the LocoNet message alternates between "On" and "Off" for a PM4 relay. The particular relay
|
|
is addressed by the board and zone variables set in the code. Note that the message sent to
|
|
the PM4 has fixed contents for the other three (of four) channels on the card. Will this
|
|
cause a problem in normal operation?</p>
|
|
|
|
<p><a href="../../../../../jython/LnSendTool.py">LnSendTool.py</a>
|
|
</p>
|
|
|
|
<p>This script will send several of the most command LocoNet type messages. These include
|
|
switch, feedback and sensor type messages. DCC signal packets can also be sent via LocoNet.
|
|
Messages are configured using radio buttons and combo boxes. Portions of this script are
|
|
taken from LnPowerButton.py by Bob Jacobsen.</p>
|
|
|
|
<p><a href="../../../../../jython/LocoTest.py">LocoTest.py</a>
|
|
</p>
|
|
|
|
<p>Script to test a locomotive and decoder after installation. It pops a pane for entering
|
|
the locomotive address, and a "Go" button. Upon clicking the button, it sequences through a
|
|
series of operations: F0 on, F0 off, F1 on, F1 off.</p>
|
|
|
|
<p><a href="../../../../../jython/MemorySiglet.py">MemorySiglet.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for a JMRI "Siglet" in Python. It listens to a Memory object,
|
|
and prints the new state to stdout.</p>
|
|
|
|
<p><a href="../../../../../jython/MemoryValuesPersistence.py">MemoryValuesPersistence.py</a>
|
|
</p>
|
|
|
|
<p>Persist the values of selected memory variables between sessions. Before closing JMRI, this
|
|
script stores to a .csv file the value of each selected memory variable, and reads back in
|
|
the last known value after a restart. The script needs to run after the layout xml data file
|
|
has been loaded. The script has to be copied to the user files location and modified.
|
|
The <code>memoryNames</code> list has the system or user names of the selected memory
|
|
variables.</p>
|
|
|
|
<p><a href="../../../../../jython/NSelectorExample.py">NSelectorExample.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for putting an "N-way" selector on a Panel. The panel contains N
|
|
Sensor icons (in this example numbered 101, 102, 103). When one is clicked on the panel, the
|
|
others Sensors are forced to Inactive and the program sets outputs appropriately (in this
|
|
case setting Turnouts 101 and 102). For N large, this would be better done with arrays and
|
|
closures, but for N = 3 a direct approach is better.</p>
|
|
|
|
<p><a href="../../../../../jython/NumberInput.py">NumberInput.py</a>
|
|
</p>
|
|
|
|
<p>Fill a memory with a two-digit number, using 10 Sensors as digit inputs.</p>
|
|
|
|
<p><a href="../../../../../jython/OpsProgExample.py">OpsProgExample.py</a>
|
|
</p>
|
|
|
|
<p>This sample Automaton watches a Sensor, and adjusts the momentum of a locomotive using
|
|
ops-mode programming when the Sensor state changes. The Sensor and decoder address are
|
|
hardcoded, as this is an example of just the Automaton function. Adding a GUI to configure
|
|
these would be straight-forward. The values could be passed via the constructor, or the
|
|
constructor (which can run in any required thread) could invoke a dialog.</p>
|
|
|
|
<p><a href="../../../../../jython/ParallelPortTest.py">ParallelPortTest.py</a>
|
|
</p>
|
|
|
|
<p>Try to send some bytes to a parallel port.</p>
|
|
|
|
<p><a href="../../../../../jython/ParallelPortTurnouts.py">ParallelPortTurnouts.py</a>
|
|
</p>
|
|
|
|
<p>Connect JMRI Turnouts to parallel port logic. Maps internal Turnouts to specific values to
|
|
be send to the parallel port. Note that JMRI must be set up to have a valid turnout table; if
|
|
you're not using some other DCC connection, configure JMRI to use LocoNet Simulator.</p>
|
|
|
|
<p><a href="../../../../../jython/PM4Announce.py">PM4Announce.py</a>
|
|
</p>
|
|
|
|
<p>Sample script to make an announcement when a PM4 section changes state. This assumes that
|
|
the PM4 sections are doing short protection. If they are doing auto-reversing, you might want
|
|
to change the announcement message in the script. Also, it assumes that only one PM4 is going
|
|
to trip at a time.<br>
|
|
Note that the previous state (oldStateN variables) is not kept per-board, but just as one
|
|
single copy. If you want to track changes in multiple boards, this needs to become a more
|
|
complicated data structure that's e.g. indexed by board.<br>
|
|
Also note that if multiple sections change at once and you're using the "speak" command, the
|
|
announcements may overlap.</p>
|
|
|
|
<p><a href="../../../../../jython/PocketTesterExample.py">PocketTesterExample.py</a>
|
|
</p>
|
|
|
|
<p>Example of an event listener for a PRICOM PocketTester.</p>
|
|
|
|
<p><a href="../../../../../jython/PowerSensor.py">PowerSensor.py</a>
|
|
</p>
|
|
|
|
<p>Provides an example of listening to the PowerManager, and operating a Sensor to indicate
|
|
changes. The Sensor address used to indicate the power status is hardcoded as "100". Change
|
|
this if you want to use some other sensor.</p>
|
|
|
|
<p><a href="../../../../../jython/RailDriver.py">RailDriver.py</a>
|
|
</p>
|
|
|
|
<p>Connect a RailDriver Modern Desktop (USB device) to a throttle.</p>
|
|
|
|
<p><a href="../../../../../jython/ReporterFontControl.py">ReporterFontControl.py</a>
|
|
</p>
|
|
|
|
<p>Script to go through each open Panel, walk down the object hierarchy and search for all
|
|
ReporterIcon objects. Every ReporterIcon will have its font size and color set as specified
|
|
at the top of the script. In addition to being a useful tool in itself, this is a good
|
|
example of stepping through the object structure of a Panel.</p>
|
|
|
|
<p><a href="../../../../../jython/ReporterFormatter.py">ReporterFormatter.py</a>
|
|
</p>
|
|
|
|
<p>Provides an example of listening to a LocoNet Reporter, and putting the changes to a Memory in a
|
|
nicer format. [As of JMRI Version 5.0, there are a dozen different types of Reporters and this example
|
|
specifically refers to the types of messages coming from LocoNet transponding.] The Reporter and Memory names are hardcoded in the example near the bottom, but you can easily change these. The example first defines the listener class.
|
|
This gets messages from the Reporter, uses them to keep track of the decoders in a Block, and
|
|
writes that list to a Memory for display.</p>
|
|
|
|
<p><a href="../../../../../jython/RobotThrottle.py">RobotThrottle.py</a>
|
|
</p>
|
|
|
|
<p>This script runs a loco around the track, controlling the speed according to signals and
|
|
following the Blocks.</p>
|
|
|
|
<p><a href="../../../../../jython/RosterLoop.py">RosterLoop.py</a>
|
|
</p>
|
|
|
|
<p>Sample script showing how to loop through the Roster entries. This example just prints the
|
|
name, but you can extract or use other info too.</p>
|
|
|
|
<p><a href="../../../../../jython/RpsRaceTrack.py">RpsRaceTrack.py</a>
|
|
</p>
|
|
|
|
<p>RaceTrack using RPS position measurement; loco1 is faster, loco2 is slower. When loco1
|
|
comes within dmin=24 inches of loco2, it will: slow to 1/4 of its speed, wait 6 seconds,
|
|
return to its original speed and check again. This is time-based (checks every 200 msec),
|
|
rather than measurement based (listeners) for ease of debugging. Changing it over later on
|
|
will be good.</p>
|
|
|
|
<p><a href="../../../../../jython/SampleLnStats.py">SampleLnStats.py</a>
|
|
</p>
|
|
|
|
<p>This is script for samples the LocoNet statistics once per minute and prints them out.</p>
|
|
|
|
<p><a href="../../../../../jython/SampleSound.py">SampleSound.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for playing a sound in a JMRI script.</p>
|
|
|
|
<p><a href="../../../../../jython/SensorLog.py">SensorLog.py</a>
|
|
</p>
|
|
|
|
<p>Listen to all Sensors, printing a line when they change state.</p>
|
|
|
|
<p><a href="../../../../../jython/Sensor-sound.py">Sensor-sound.py</a>
|
|
</p>
|
|
|
|
<p>Listens for specific Sensor (Sensor #4), plays crossing gate sound on train entering
|
|
Block.</p>
|
|
|
|
<p><a href="../../../../../jython/SetTurnouts.py">SetTurnouts.py</a>
|
|
</p>
|
|
|
|
<p>Sample script showing how to set multiple turnouts to specific positions, with a time
|
|
delay between them, separately from other things that the program is doing. By putting the
|
|
turnout commands in a separate class, they'll run independently after the "start"
|
|
operation.</p>
|
|
|
|
<p><a href="../../../../../jython/ShutDownExample.py">ShutDownExample.py</a>
|
|
</p>
|
|
|
|
<p>Schedule something to happen when a JMRI application ends.</p>
|
|
|
|
<p><a href="../../../../../jython/SigletExample.py">SigletExample.py</a>
|
|
</p>
|
|
|
|
<p>This is an example script for a JMRI "Siglet" in Python. It listens for changes to two
|
|
Sensors and a Turnout, and then recalculates a Signal Mast Aspect based on the values.</p>
|
|
|
|
<p><a href="../../../../../jython/ThrottleSound.py">ThrottleSound.py</a>
|
|
</p>
|
|
|
|
<p>Play a sound when the F3 key is pressed on the handheld and loco 1001 selected.</p>
|
|
|
|
<p><a href="../../../../../jython/TurnoutReset.py">TurnoutReset.py</a>
|
|
</p>
|
|
|
|
<p>Reset a Turnout to Closed every time it's clicked Thrown. This might be used so that a
|
|
Turnout icon on a panel sits in one position, ready to be clicked and fire a Route. The top
|
|
of the file defines the needed code. There are some lines near the bottom you should edit to
|
|
adapt it to your particular layout.</p>
|
|
|
|
<p><a href="../../../../../jython/TurnoutStatePersistence.py">TurnoutStatePersistence.py</a>
|
|
</p>
|
|
|
|
<p>Persist the state of turnouts between sessions. Before closing JMRI, this script stores to a .csv file the
|
|
state of (each) turnout on the layout, and reads back in the last known status after a restart.<br>
|
|
Add it to your preferences to load at startup.
|
|
You may want to edit the script so that only actual turnouts are stored, skipping e.g. turnouts used on
|
|
signals.</p>
|
|
|
|
<p><a href="../../../../../jython/TwoOutputTurnouts.py">TwoOutputTurnouts.py</a>
|
|
</p>
|
|
|
|
<p>Drive a turnout from two C/MRI outputs. Some C/MRI users connect slow-motion switch
|
|
machines between two C/MRI outputs, and then set the outputs to complementary states (one
|
|
active, one inactive) to drive the machine in the two directions. This script slaves a 2nd
|
|
turnout to a first one, so that when the first one is commanded to a particular state, the
|
|
second one goes to the other state. To use this, edit the script to include your Turnouts at
|
|
the bottom (see the existing example; you should remove that). Then select this file as a
|
|
script to be run at startup from the "Advanced Preferences" on the Preferences Start Up
|
|
pane.</p>
|
|
|
|
<p><a href="../../../../../jython/USBThrottle.py">USBThrottle.py</a>
|
|
</p>
|
|
|
|
<p><a>Use a USB device as a throttle</a>
|
|
</p>
|
|
|
|
<p><a href="../../../../../jython/javaone/">javaone</a>
|
|
</p>
|
|
|
|
<p>The jython/javaone directory contains scripts that were first created for the JavaOne test
|
|
layout.</p>
|
|
<!--#include virtual="/help/en/parts/Footer.shtml" -->
|
|
</div>
|
|
<!-- closes #mainContent-->
|
|
</div>
|
|
<!-- closes #mBody-->
|
|
<script src="/js/help.js"></script>
|
|
</body>
|
|
</html>
|