Skip to content

Button Functions

Tom Willemsen edited this page Oct 19, 2020 · 3 revisions

Wiki > General Scripts > Utilities > Button Functions

Functions for assigning user python function to buttons that can be access through a PV and user interface.

The buttons are supported using user parameters, which are run from the INSTETC ioc. The maximum number of buttons is set using the NUM_USER_BUTTONS macro which defaults to 4. To add a function to a button you must call add_button_function - a sensible place to do this is in the background IOC (which should be added to your config). The background script should be kept running so it can respond to the button presses; to do this, use the run_buttons function. For example the background_script.py would read:

import sys
import os
from time import sleep

sys.path.insert(0, os.path.abspath(os.path.join(r"C:\\", "Instrument", "scripts")))
sys.path.insert(1, os.path.abspath(os.path.join(os.environ["KIT_ROOT"], "ISIS", "inst_servers", "master")))

from genie_python import genie as g, block_names as b
from server_common.helpers import register_ioc_start
from general.utilities.button_functions import add_button_function, run_buttons
import inst

# let IBEX know that the IOC has started
register_ioc_start("BGRSCRPT_01")

# setup genie
g.set_instrument(None)

def my_function(status):
    """User function"""
    status("starting")
    print("start")
    sleep(1)
    status("... sleeping ...")
    sleep(1)
    print("stop")
    status("stop")


# Setup Buttons
add_button_function(0, "Function", my_function)

# Run buttons loop
run_buttons()

In the function you write, it is good to set the status (must be less than 40 characters) throughout the call - this will be shown on the user interface so that a user can follow the progress of the function being run.

There is also an OPI (User buttons) provided to click on the buttons which can be added to your device screens or a synoptic. You can also access the pvs to add them as blocks: the PVS are of the form IN:<instrument>:PARS:USER:BUTTON0. You can then trigger the buttons programmatically using a cset: g.cset("my_button_block", 1) would trigger the button.

API

add_button_function

Add a function to a given button so it will execute when the button is pressed.

add_button_function(button_index, name, function, block_until_pv_exists=True, retry_delay=5)

where:

  • button_index: index of the button (0 to NUM_USER_BUTTONS)
  • name: name of the action that function performs (will be displayed alongside the button in the OPI)
  • function: function to run when button is clicked. Should have one argument which is a function which can be used to set the button status with a string, e.g. def my_func(set_status): set_status("Starting")
  • block_until_pv_exists: Wait to connect to button until it exists; False throw exception
  • retry_delay: delay between retries if blocking

run_buttons

Loop which runs the code for the given buttons, and cleans up after the loop ends. Should be placed at the end of the background_script.py script. Note that this function blocks indefinitely.

run_buttons()

Clone this wiki locally