11.1.2.1 Invoking Python functions from the UI

The onload python hook allows you to add menu items to the project tree (in the Projects window). When selected, these items will invoke the registered callback function.

To persist user defined objects across multiple sessions, Python code can store and fetch strings in the settings map.

Here´s an example from the SAML11 scripts. In this example, the functions are meant to be called outside debug sessions. As such, when invoked, the code in the python function will ask the debugger to connect to the device and perform memory and register access to manipulate the device specific debug access level:

def onload(ide):
    ide.addCommand("Device actions|Set DAL 0", "set_dal_0")
    ide.addCommand("Device actions|Set DAL 1", "set_dal_1")

def set_dal_0():
  global dal
  special_action([],L11SDAL0)
  msg.msg("DAL is now " + str(dal),"Set DAL 0")

def set_dal_1():
  global dal
  special_action([],L11SDAL1)
  msg.msg("DAL is now " + str(dal),"Set DAL 1")

def special_action(key,cmd):
  if deb.Connected():
    r=msg.msg("Debugger is in use. Stop debugging and retry","Busy")       
    return
  deb.Connect()
  try:
    SpecialOp(key,cmd)  # initiate bootrom cmd or change dal
  finally:
    deb.Disconnect()