# Path: bbio/common.py
# def addToCleanup(routine):
#   """ Takes a callable object to be called during the cleanup once a 
#       program has stopped, e.g. a function to close a log file, kill 
#       a thread, etc. """
#   ADDITIONAL_CLEANUP.append(routine)


from config import OCP_PATH, GPIO, GPIO_FILE_BASE, EXPORT_FILE, UNEXPORT_FILE,\
                   SLOTS_FILE
from bbio.common import addToCleanup
import glob, os, cape_manager, bbio
  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:

# Path: bbio/common.py
# def addToCleanup(routine):
#   """ Takes a callable object to be called during the cleanup once a 
#       program has stopped, e.g. a function to close a log file, kill 
#       a thread, etc. """
#   ADDITIONAL_CLEANUP.append(routine)


# Path: bbio/platform/beaglebone/pinmux.py
from config import OCP_PATH, GPIO, GPIO_FILE_BASE, EXPORT_FILE, UNEXPORT_FILE,\
                   SLOTS_FILE
from bbio.common import addToCleanup
import glob, os, cape_manager, bbio

  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)