def Init_IOPi(): global sensorbus global buttonbus i2c_helper = ABEHelpers() i2c_bus = i2c_helper.get_smbus() sensorbus = IoPi(i2c_bus, 0x20) # set both rows of pins to input mode sensorbus.set_port_direction(0, 0xFF) sensorbus.set_port_direction(1, 0xFF) #setup interrupts on bus 2, port 1 buttonbus = IoPi(i2c_bus, 0x21) # Set all pins on the bus to be inputs with internal pull-ups enabled. buttonbus.set_port_pullups(1, 0xFF) buttonbus.set_port_direction(1, 0xFF) # Inverting the ports will allow a button connected to ground to register as 1 or on. buttonbus.invert_port(1, 0xFF) # invert port 1 so a button press will register as 1 # Set the interrupt polarity to be active low and mirroring enabled, so # INT A and INT B go low when an interrupt is triggered buttonbus.set_interrupt_polarity(0) #buttonbus.mirror_interrupts(1) # Set the interrupts default value to 0 so it will trigger when any of the pins on the bus change to 1 buttonbus.set_interrupt_defaults(1, 0x00) # Set the interrupt type to be 0xFF for port B so an interrupt is # fired when the pin matches the default value buttonbus.set_interrupt_type(1, 0xFF) # Enable interrupts for all pins on the port buttonbus.set_interrupt_on_port(1, 0xFF) # reset the interrups on the IO Pi bus buttonbus.reset_interrupts() 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 pin 23 the function button_pressed will be run GPIO.add_event_detect(23, GPIO.FALLING, callback=hwbutton_pressed)
Initialise the IOPi device using the default addresses, you will need to change the addresses if you have changed the jumpers on the IO Pi """ i2c_helper = ABEHelpers() i2c_bus = i2c_helper.get_smbus() # create two instances of the IoPi class called bus1 and bus2 and set the default i2c addresses bus1 = IoPi(i2c_bus, 0x20) # bus 1 will be inputs bus2 = IoPi(i2c_bus, 0x21) # bus 2 will be outputs # Each bus is divided up two 8 bit ports. Port 0 controls pins 1 to 8, Port 1 controls pins 9 to 16. # We will read the inputs on pin 1 of bus 1 so set port 0 to be inputs and # enable the internal pull-up resistors bus1.set_port_direction(0, 0xFF) bus1.set_port_pullups(0, 0xFF) # We will write to the output pin 1 on bus 2 so set port 0 to be outputs and # turn off the pins on port 0 bus2.set_port_direction(0, 0x00) bus2.write_port(0, 0x00) while True: # read pin 1 on bus 1. If pin 1 is high set the output on bus 2 pin 1 to high, otherwise set it to low. # connect pin 1 on bus 1 to ground to see the output on bus 2 pin 1 change state. if (bus1.read_pin(1) == 1): bus2.write_pin(1, 1) else:
Initialise the IOPi device using the default addresses and set the output of bank 1 on IC1 to the input of bank 1 on IC2 """ from ABE_helpers import ABEHelpers from ABE_IoPi import IoPi import time i2c_helper = ABEHelpers() newbus = i2c_helper.get_smbus() bus1 = IoPi(newbus, 0x20) bus2 = IoPi(newbus, 0x21) # Set all pins on bus 2 to be inputs with internal pull-ups disabled. bus2.set_port_pullups(0, 0x00) bus2.set_port_pullups(1, 0x00) bus2.set_port_direction(0, 0xFF) bus2.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 bus2.set_interrupt_polarity(1) bus2.mirror_interrupts(0) # Set the interrupts default value to trigger when 5V is applied to pins 1 # and 16 bus2.set_interrupt_defaults(0, 0x01) bus2.set_interrupt_defaults(0, 0x80) # Set the interrupt type to be 1 for ports A and B so an interrupt is
Initialise the IOPi device using the default addresses, you will need to change the addresses if you have changed the jumpers on the IO Pi """ i2c_helper = ABEHelpers() i2c_bus = i2c_helper.get_smbus() # create two instances of the IoPi class called bus1 and bus2 and set the default i2c addresses bus1 = IoPi(i2c_bus, 0x20) # bus 1 will be inputs bus2 = IoPi(i2c_bus, 0x21) # bus 2 will be outputs # Each bus is divided up two 8 bit ports. Port 0 controls pins 1 to 8, Port 1 controls pins 9 to 16. # We will read the inputs on pin 1 of bus 1 so set port 0 to be inputs and # enable the internal pull-up resistors bus1.set_port_direction(0, 0xFF) bus1.set_port_pullups(0, 0xFF) # We will write to the output pin 1 on bus 2 so set port 0 to be outputs and # turn off the pins on port 0 bus2.set_port_direction(0, 0x00) bus2.write_port(0, 0x00) while True: # read pin 1 on bus 1. If pin 1 is high set the output on bus 2 pin 1 to high, otherwise set it to low. # connect pin 1 on bus 1 to ground to see the output on bus 2 pin 1 change state. if (bus1.read_pin(1) == 1): bus2.write_pin(1, 1) else: bus2.write_pin(1, 0)
# ledbus.set_pin_direction(8, 0) # set pin 8 as an ou tput # ledbus.write_pin(8, 0) # turn off pin 8 # print "turn off pin 8" # sensorbus.set_pin_pullup(1, 1) # enable the internal pull-up resistor on pin 1 # sensorbus.invert_pin(1, 1) # invert pin 1 so a button press will register as 1 # sensorbus.set_port_direction(0, 0xFF) # sensorbus.set_port_pullups(0, 0xFF) # sensorbus.invert_port(0, 0xFF) sensorbus.set_port_direction(1, 0xFF) sensorbus.set_port_pullups(1, 0xFF) sensorbus.invert_port(1, 0xFF) print "start" vold = 99 while True: v = sensorbus.read_port(1) # print v if v != vold: print v # print "CCC" vold = v time.sleep(0.1) # if sensorbus.read_pin(1) == 1: # check to see if the button is pressed # print 'button pressed' # print a message to the screen # # ledbus.write_pin(8, 1) # turn on the led on pin 8