Interrupt port 1A on IO Pi Plus Bus 1 in connected to GPIO pin 23 on RPi.
I hope this all made since! I'm still fairly new to this.
import time
import RPi.GPIO as GPIO
from IOPi import IOPi
bus1 = None
def button_pressed(interrupt_pin):
global bus1
"""
this function will be called when GPIO 23 falls low
"""
# read the interrupt capture for port 0 and store it in variable intval
intval = bus1.read_interrupt_capture(0)
# compare the value of intval with the IO Pi port 0
# using read_port(). wait until the port changes which will indicate
# the button has been released.
# without this while loop the function will keep repeating.
while (intval == bus1.read_port(0)):
time.sleep(0.2)
# loop through each bit in the intval variable and check if the bit is 1
# which will indicate a button has been pressed
for num in range(0, 8):
if (intval & (1 << num)):
# print("Pin " + str(num + 1) + " pressed")
if str(num + 1) == '1':
return state0()
elif str(num + 1) == '2':
return state1()
elif str(num + 1) == '3':
return state2()
elif str(num + 1) == '4':
return state3()
def state0():
print ("Pyano: Main Menu")
bus1.write_pin(9, 1)
bus1.write_pin(11, 0)
bus1.write_pin(13, 0)
bus1.write_pin(15, 0)
def state1():
print ("Mode 1: midi_player")
bus1.write_pin(9, 0)
bus1.write_pin(11, 1)
bus1.write_pin(13, 0)
bus1.write_pin(15, 0)
test()
def state2():
print ("Mode 2: midi_maker")
bus1.write_pin(9, 0)
bus1.write_pin(11, 0)
bus1.write_pin(13, 1)
bus1.write_pin(15, 0)
def state3():
print ("Mode 3: midi_live")
bus1.write_pin(9, 0)
bus1.write_pin(11, 0)
bus1.write_pin(13, 0)
bus1.write_pin(15, 1)
def test():
i = 0
while i < 3:
print(i)
time.sleep(2)
i = i + 1
def main():
"""
Main program function
"""
global bus1
# Create an instance of the IOPi class called bus1 and
# set the I2C address to be 0x20 or Bus 1.
bus1 = IOPi(0x20)
# State LED indicator setup
bus1.set_port_direction(1, 0x00)
bus1.write_port(1, 0x00)
# Set port 0 on the bus1 to be inputs with internal pull-ups enabled.
bus1.set_port_pullups(0, 0xFF)
bus1.set_port_direction(0, 0xFF)
# Inverting the port will allow a button connected to ground to
# register as 1 or on.
bus1.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
bus1.set_interrupt_polarity(0)
bus1.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
bus1.set_interrupt_defaults(0, 0x00)
# Set the interrupt type to be 0xFF so an interrupt is
# fired when the pin matches the default value
bus1.set_interrupt_type(0, 0xFF)
# Enable interrupts for all pins on port 0
bus1.set_interrupt_on_port(0, 0xFF)
# reset the interrups on the IO Pi bus1
bus1.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, bouncetime=300)
# print out a message and wait for keyboard input before
# exiting the program
state = state0 # initial state
while state:
state = state() # launch state machine
input("Press enter to exit program \n")
#make sure all LEDs are turned off
bus1.write_port(1, 0x00)
if __name__ == "__main__":
main()