Ejemplo n.º 1
0
def main():
    '''
    Main program function
    '''
    # Create an instance of the IOPi class with an I2C address of 0x20
    iobus = IOPi(0x20)

    # Set all pins on the IO bus to be inputs with internal pull-ups enabled.

    iobus.set_port_pullups(0, 0xFF)
    iobus.set_port_pullups(1, 0xFF)
    iobus.set_port_direction(0, 0xFF)
    iobus.set_port_direction(1, 0xFF)

    # Invert both ports so pins will show 1 when grounded
    iobus.invert_port(0, 0xFF)
    iobus.invert_port(1, 0xFF)

    # Set the interrupt polarity to be active high and mirroring disabled, so
    # pins 1 to 8 trigger INT A and pins 9 to 16 trigger INT B
    iobus.set_interrupt_polarity(1)
    iobus.mirror_interrupts(0)

    # Set the interrupts default value to 0x00 so the interrupt will trigger when any pin registers as true
    iobus.set_interrupt_defaults(0, 0x00)
    iobus.set_interrupt_defaults(1, 0x00)

    # Set the interrupt type to be 1 for ports A and B so an interrupt is
    # fired when the pin matches the default value
    iobus.set_interrupt_type(0, 0xFF)
    iobus.set_interrupt_type(1, 0xFF)

    # Enable interrupts for all pins
    iobus.set_interrupt_on_port(0, 0xFF)
    iobus.set_interrupt_on_port(1, 0xFF)

    while True:

        # read the interrupt status for each port.  
        # If the status is not 0 then an interrupt has occured on one of the pins 
        # so read the value from the interrupt capture.

        if (iobus.read_interrupt_status(0) != 0):
            print("Port 0: " + str(iobus.read_interrupt_capture(0)))
        if (iobus.read_interrupt_status(1) != 0):
            print("Port 1: " + str(iobus.read_interrupt_capture(1)))

        time.sleep(2)
def main():
    '''
    Main program function
    '''
    # Create two instances of the IOPi class with
    # I2C addresses of 0x20 and 0x21
    busin = IOPi(0x20)
    busout = IOPi(0x21)

    # Set port 0 on the busin bus to be inputs with internal pull-ups enabled.

    busin.set_port_pullups(0, 0xFF)
    busin.set_port_direction(0, 0xFF)

    # Invert the port so pins will show 1 when grounded
    busin.invert_port(0, 0xFF)

    # Set port 0 on busout to be outputs and set the port to be off
    busout.set_port_direction(0, 0x00)
    busout.write_port(0, 0x00)

    # Set the interrupts default value for port 0 to 0x00 so the interrupt
    # will trigger when any pin registers as true
    busin.set_interrupt_defaults(0, 0x00)

    # Set the interrupt type to be 1 on each pin for port 0 so an interrupt is
    # fired when the pin matches the default value
    busin.set_interrupt_type(0, 0xFF)

    # Enable interrupts for all pins on port 0
    busin.set_interrupt_on_port(0, 0xFF)

    # Reset the interrupts
    busin.reset_interrupts()

    while True:

        # read the interrupt status for each port.

        if (busin.read_interrupt_status(0) != 0):
            # If the status is not 0 then an interrupt has occured
            # on one of the pins so read the value from the interrupt capture
            value = busin.read_interrupt_capture(0)

            # write the value to port 0 on the busout bus
            busout.write_port(0, value)

        # sleep 200ms before checking the pin again
        time.sleep(0.2)
def main():
    '''
    Main program function
    '''
    # Create two instances of the IOPi class with
    # I2C addresses of 0x20 and 0x21
    ioin = IOPi(0x20)
    ioout = IOPi(0x21)

    # Set port 0 on the ioin bus to be inputs with internal pull-ups enabled.

    ioin.set_port_pullups(0, 0xFF)
    ioin.set_port_direction(0, 0xFF)

    # Invert the port so pins will show 1 when grounded
    ioin.invert_port(0, 0xFF)

    # Set port 0 on ioout to be outputs and set the port to be off
    ioout.set_port_direction(0, 0x00)
    ioout.write_port(0, 0x00)

    # Set the interrupts default value for port 0 to 0x00 so the interrupt
    # will trigger when any pin registers as true
    ioin.set_interrupt_defaults(0, 0x00)

    # Set the interrupt type to be 1 on each pin for port 0 so an interrupt is
    # fired when the pin matches the default value
    ioin.set_interrupt_type(0, 0xFF)

    # Enable interrupts for all pins on port 0
    ioin.set_interrupt_on_port(0, 0xFF)

    # Reset the interrupts
    ioin.reset_interrupts()

    while True:

        # read the interrupt status for each port.

        if (ioin.read_interrupt_status(0) != 0):
            # If the status is not 0 then an interrupt has occured
            # on one of the pins so read the value from the interrupt capture
            value = ioin.read_interrupt_capture(0)

            # write the value to port 0 on the ioout bus
            ioout.write_port(0, value)

        # sleep 200ms before checking the pin again
        time.sleep(0.2)
Ejemplo n.º 4
0
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Check read_interrupt_capture port for low out of bounds
    try:
        iopi.read_interrupt_capture(-1)
        pass
    except ValueError:
        print("port low boundary check: PASSED")
        pass
    except IOError:
        passed = False
        print("I2C IOError")
    else:
        passed = False
        print("port low boundary check: FAILED")
        pass

    # Check read_interrupt_capture port for high out of bounds
    try:
        iopi.read_interrupt_capture(2)
        pass
    except ValueError:
        print("port high boundary check: PASSED")
        pass
    except IOError:
        passed = False
        print("I2C IOError")
    else:
        passed = False
        print("port high boundary check: FAILED")
        pass

    # Logic Analyser Check
    print("Logic output Started")

    iopi.read_interrupt_capture(0)
    iopi.read_interrupt_capture(1)

    print("Logic output Ended")

    if passed is False:
        print("Test Failed")
Ejemplo n.º 5
0
def main():
    '''
    Main program function
    '''
    # Create an instance of the IOPi class with an I2C address of 0x20
    iobus = IOPi(0x20)

    # Set all pins on the IO bus to be inputs with internal pull-ups disabled.

    iobus.set_port_pullups(0, 0x00)
    iobus.set_port_pullups(1, 0x00)
    iobus.set_port_direction(0, 0xFF)
    iobus.set_port_direction(1, 0xFF)

    # Set the interrupt polarity to be active high and mirroring disabled, so
    # pins 1 to 8 trigger INT A and pins 9 to 16 trigger INT B
    iobus.set_interrupt_polarity(1)
    iobus.mirror_interrupts(0)

    # Set the interrupts default value to trigger when 5V is applied to pins 1
    # and 16
    iobus.set_interrupt_defaults(0, 0x01)
    iobus.set_interrupt_defaults(0, 0x80)

    # Set the interrupt type to be 1 for ports A and B so an interrupt is
    # fired when the pin matches the default value
    iobus.set_interrupt_type(0, 1)
    iobus.set_interrupt_type(1, 1)

    # Enable interrupts for pins 1 and 16
    iobus.set_interrupt_on_pin(1, 1)
    iobus.set_interrupt_on_pin(16, 1)

    while True:

        # read the port value from the last capture for ports 0 and 1.
        # This will reset the interrupts
        print(iobus.read_interrupt_capture(0))
        print(iobus.read_interrupt_capture(1))
        time.sleep(2)