def pinMux_dtOverlays(gpio_pin, mode, preserve_mode_on_exit=False):
  gpio_pin = gpio_pin.lower()
  mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin))
  if len(mux_file_glob) == 0:
    try:
      cape_manager.load('PyBBIO-%s' % gpio_pin, not preserve_mode_on_exit)
      bbio.delay(250) # Give driver time to load
      mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin))
    except IOError:
      print "*Could not load %s overlay, resource busy" % gpio_pin
      return False

  if len(mux_file_glob) == 0:
    print "*Could not load overlay for pin: %s" % gpio_pin
    return False
    
  mux_file = mux_file_glob[0]
  # Convert mode to ocp mux name:
  mode = 'mode_%s' % format(mode, '#010b') 
  # Possible modes:
  #  mode_0b00100111  # rx active | pull down
  #  mode_0b00110111  # rx active | pull up
  #  mode_0b00101111  # rx active | no pull
  #  mode_0b00000111  # pull down
  #  mode_0b00010111  # pull up
  #  mode_0b00001111  # no pull
  # See /lib/firmware/PyBBIO-src/*.dts for more info 
  for i in range(3):
    # If the pin's overlay was just loaded there may not have been enough 
    # time for the driver to get fully initialized, which causes an IOError
    # when trying to write the mode; try up to 3 times to avoid this:
    try:
      with open(mux_file, 'wb') as f:
        f.write(mode)
      return True
    except IOError:
      # Wait a bit between attempts
      bbio.delay(10)
  # If we get here then it didn't work 3 times in a row; raise the IOError:
  raise
dataset/ETHPy150Open graycatlabs/PyBBIO/bbio/platform/beaglebone/pinmux.py/pinMux_dtOverlays/original