# 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
      exiting.
      *This should generally not be called directly from user code. """
  if not gpio_pin:
    print "*unknown pinmux pin: %s" % gpio_pin
    return

  if SLOTS_FILE:
    status = pinMux_dtOverlays(gpio_pin, mode, preserve_mode_on_exit)
  else:
    status = pinMux_universalIO(gpio_pin, mode, preserve_mode_on_exit)
  if not status:
    print "*could not configure pinmux for pin %s" % gpio_pin


def export(gpio_pin, unexport_on_exit=False):
  """ Reserves a pin for userspace use with sysfs /sys/class/gpio interface. 
      If unexport_on_exit=True unexport(gpio_pin) will be called automatically
      when the program exits. Returns True if pin was exported, False if it was 
      already under userspace control. """
  if ("USR" in gpio_pin):
    # The user LEDs are already under userspace control
    return True
  gpio_num = GPIO[gpio_pin]['gpio_num']
  gpio_file = '%s/gpio%i' % (GPIO_FILE_BASE, gpio_num)
  if (os.path.exists(gpio_file)): 
    # Pin already under userspace control
    return True
  with open(EXPORT_FILE, 'wb') as f:
    f.write(str(gpio_num))
  if unexport_on_exit: 

# 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

      exiting.
      *This should generally not be called directly from user code. """
  if not gpio_pin:
    print "*unknown pinmux pin: %s" % gpio_pin
    return

  if SLOTS_FILE:
    status = pinMux_dtOverlays(gpio_pin, mode, preserve_mode_on_exit)
  else:
    status = pinMux_universalIO(gpio_pin, mode, preserve_mode_on_exit)
  if not status:
    print "*could not configure pinmux for pin %s" % gpio_pin


def export(gpio_pin, unexport_on_exit=False):
  """ Reserves a pin for userspace use with sysfs /sys/class/gpio interface. 
      If unexport_on_exit=True unexport(gpio_pin) will be called automatically
      when the program exits. Returns True if pin was exported, False if it was 
      already under userspace control. """
  if ("USR" in gpio_pin):
    # The user LEDs are already under userspace control
    return True
  gpio_num = GPIO[gpio_pin]['gpio_num']
  gpio_file = '%s/gpio%i' % (GPIO_FILE_BASE, gpio_num)
  if (os.path.exists(gpio_file)): 
    # Pin already under userspace control
    return True
  with open(EXPORT_FILE, 'wb') as f:
    f.write(str(gpio_num))
  if unexport_on_exit: 

    addToCleanup(lambda: unexport(gpio_pin))