def init(init_board=True):
    """Initialises the PiFace Digital board"""
    pfcom.init(SPI_BUS, SPI_CHIP_SELECT)

    if init_board:
         # set up each board
        ioconfig = pfcom.BANK_OFF | \
            pfcom.INT_MIRROR_OFF | pfcom.SEQOP_ON | pfcom.DISSLW_OFF | \
            pfcom.HAEN_ON | pfcom.ODR_OFF | pfcom.INTPOL_LOW

        pfd_detected = False

        for board_index in range(MAX_BOARDS):
            pfcom.write(ioconfig, pfcom.IOCON, board_index)  # configure

            if not pfd_detected:
                if pfcom.read(pfcom.IOCON, board_index) == ioconfig:
                    pfd_detected = True

            pfcom.write(0, pfcom.GPIOA, board_index)  # clear port A
            pfcom.write(0, pfcom.IODIRA, board_index)  # set port A as outputs
            pfcom.write(0xff, pfcom.IODIRB, board_index)  # set port B as inputs
            pfcom.write(0xff, pfcom.GPPUB, board_index)  # set port B pullups on

        if not pfd_detected:
            raise NoPiFaceDigitalDetectedError(
                "There was no PiFace Digital board detected!"
            )
def disable_interrupts():
    # neither edge
    with open(GPIO_INTERRUPT_DEVICE_EDGE, 'w') as gpio25edge:
        gpio25edge.write('none')

    # remove the pin from userspace
    with open(GPIO_UNEXPORT_FILE, 'w') as unexport_file:
        unexport_file.write(str(GPIO_INTERRUPT_PIN))

    # disable the interrupt
    for board_index in range(MAX_BOARDS):
        pfcom.write(0, pfcom.GPINTENB, board_index)
def disable_interupts():
    with open(GPIO_INTERUPT_DEVICE+'edge', 'w') as gpio25edge:
        gpio25edge.write('none')

    try:
        subprocess.check_call(["gpio-admin", "unexport", "25"])
    except subprocess.CalledProcessError as e:
        if e.returncode != 4:  # we can ignore 4 (gpio25 is already down)
            raise e

    for board_index in range(MAX_BOARDS):
        pfcom.write(0, pfcom.GPINTENB, board_index)  # disable the interupt
def enable_interrupts():
    # enable interrupts
    for board_index in range(MAX_BOARDS):
        pfcom.write(0xff, pfcom.GPINTENB, board_index)

    try:
        _bring_gpio_interrupt_into_userspace()
        _set_gpio_interrupt_edge()
    except Timeout as e:
        raise InterruptEnableException(
            "There was an error bringing gpio%d into userspace. %s" % \
            (GPIO_INTERRUPT_PIN, e.message)
        )
def enable_interupts():
    for board_index in range(MAX_BOARDS):
        pfcom.write(0xff, pfcom.GPINTENB, board_index)

    # access quick2wire-gpio-admin for gpio pin twiddling
    try:
        subprocess.check_call(["gpio-admin", "export", "25"])
    except subprocess.CalledProcessError as e:
        if e.returncode != 4:  # we can ignore 4 (gpio25 is already up)
            raise e

     # we're only interested in the falling edges of this file (1 -> 0)
    with open(GPIO_INTERUPT_DEVICE+'edge', 'w') as gpio25edge:
        gpio25edge.write('falling')