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 the ports so pulling a pin to ground will show as 1 instead of 0 iobus.invert_port(0, 0xFF) iobus.invert_port(1, 0xFF) # Set the interrupt polarity to be active high and mirroring enabled, so # pin 1 will trigger both INT A and INT B when a pin is grounded iobus.set_interrupt_polarity(1) iobus.mirror_interrupts(1) # Set the interrupts default value to 0 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 a state change occurs iobus.set_interrupt_type(0, 0x00) iobus.set_interrupt_type(1, 0x00) # Enable interrupts for pin 1 iobus.set_interrupt_on_port(0, 0x01) iobus.set_interrupt_on_port(1, 0x00) timer = threading.Thread(target=background_thread(iobus)) timer.daemon = True # set thread to daemon ('ok' won't be printed) timer.start() while 1: """ Do something in the main program loop while the interrupt checking is carried out in the background """ # wait 1 seconds time.sleep(1)
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)
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 """ passed = True iopi = IOPi(0x20, False) # new iopi object without initialisation iopi.invert_bus(0x0000) # Check invert_port port for low out of bounds try: iopi.invert_port(-1, 0) 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 invert_port port for high out of bounds try: iopi.invert_port(2, 0) 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 # Check invert_port value for low out of bounds try: iopi.invert_port(0, -1) pass except ValueError: print("value low boundary check: PASSED") pass except IOError: passed = False print("I2C IOError") else: passed = False print("value low boundary check: FAILED") pass # Check invert_port value for high out of bounds try: iopi.invert_port(0, 256) pass except ValueError: print("value high boundary check: PASSED") pass except IOError: passed = False print("I2C IOError") else: passed = False print("value high boundary check: FAILED") pass # Logic Analyser Check print("Logic output Started") for x in range(0, 256): iopi.invert_port(0, x) iopi.invert_port(1, x) print("Logic output Ended") if passed is False: print("Test Failed")
def main(): """ Main program function """ global bus # Create an instance of the IOPi class called bus and # set the I2C address to be 0x20 or Bus 1. bus = IOPi(0x20) # Set port 0 on the bus to be inputs with internal pull-ups enabled. bus.set_port_pullups(0, 0xFF) bus.set_port_direction(0, 0xFF) # Inverting the port will allow a button connected to ground to # register as 1 or on. bus.invert_port(0, 0xFF) # Set the interrupt polarity to be active low so Int A and IntB go low # when an interrupt is triggered and mirroring disabled, so # Int A is mapped to port 0 and Int B is mapped to port 1 bus.set_interrupt_polarity(0) bus.mirror_interrupts(0) # Set the interrupts default value to 0 so it will trigger when any of # the pins on the port 0 change to 1 bus.set_interrupt_defaults(0, 0x00) # Set the interrupt type to be 0xFF so an interrupt is # fired when the pin matches the default value bus.set_interrupt_type(0, 0xFF) # Enable interrupts for all pins on port 0 bus.set_interrupt_on_port(0, 0xFF) # reset the interrups on the IO Pi bus bus.reset_interrupts() # set the Raspberry Pi GPIO mode to be BCM GPIO.setmode(GPIO.BCM) # Set up GPIO 23 as an input. The pull-up resistor is disabled as the # level shifter will act as a pull-up. GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # when a falling edge is detected on GPIO 23 the function # button_pressed will be run GPIO.add_event_detect(23, GPIO.FALLING, callback=button_pressed) # print out a message and wait for keyboard input before # exiting the program input("press enter to exit ")