def init_ABE(): # Create an instance of the IOPi class with an I2C address of 0x20 iobus = IOPi(0x21) # Create an instance of the Servo class with an I2C address of 0x40 # Servo(Address, Low_Limit, High_Limit) # If servo is not getting full range of motion # Decrease low limit by 0.1 until servo does not move # Increase high limit by 0.1 until servo does not move servo = Servo(0x40, LOW_LIMIT, HIGH_LIMIT) # We will read the inputs 1 to 16 from the I/O bus so set port 0 and # port 1 to be inputs and enable the internal pull-up resistors iobus.set_port_direction(0, 0xFF) iobus.set_port_pullups(0, 0xFF) iobus.set_port_direction(1, 0xFF) iobus.set_port_pullups(1, 0xFF) iobus.invert_port(0, 0xFF) iobus.invert_port(1, 0xFF) # Enable servo outputs servo.output_enable() return iobus, servo
def main(): """ Main program function """ iobus2 = IOPi(0x21) iobus2.set_pin_direction(2, 0) iobus2.set_pin_direction(4, 0) iobus2.set_pin_direction(10, 1) iobus2.set_pin_direction(14, 1) iobus2.set_port_pullups(1, 0xFF) while True: # clear the console if iobus2.read_pin(14) == 0: iobus2.write_pin(2, 1) else: iobus2.write_pin(2, 0) if iobus2.read_pin(10) == 0: iobus2.write_pin(4, 1) else: iobus2.write_pin(4, 0)
def main(): ''' Main program function ''' iobus = IOPi(0x20, False) # We will read the inputs 1 to 16 from the I/O bus so set port 0, # port 1 to be inputs and enable the internal pull-up resistors iobus.set_port_direction(0, 0xA1) print("Port direction 0: " + hex(iobus.get_port_direction(0))) iobus.set_port_direction(1, 0xB1) print("Port direction 1: " + hex(iobus.get_port_direction(1))) iobus.set_port_pullups(0, 0xC1) print("Port pullups 0: " + hex(iobus.get_port_pullups(0))) iobus.set_port_pullups(1, 0xD1) print("Port pullups 1: " + hex(iobus.get_port_pullups(1))) iobus.invert_port(0, 0xE1) print("Invert Port 0: " + hex(iobus.get_port_polarity(0))) iobus.invert_port(1, 0xF1) print("Invert Port 1: " + hex(iobus.get_port_polarity(1)))
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 ''' # 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)
def main(): """ Main program function """ # create two instances of the IoPi class called iobus1 and iobus2 and set # the default i2c addresses iobus1 = IOPi(0x20) # bus 1 will be inputs iobus2 = IOPi(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 iobus1.set_port_direction(0, 0xFF) iobus1.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 iobus2.set_port_direction(0, 0x00) iobus2.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 iobus1.read_pin(1) == 1: iobus2.write_pin(1, 1) else: iobus2.write_pin(1, 0) # wait 0.1 seconds before reading the pins again time.sleep(0.1)
def main(): """ Main program function """ iobus1 = IOPi(0x20) iobus2 = IOPi(0x21) # We will read the inputs 1 to 16 from the I/O bus so set port 0 and # port 1 to be inputs and enable the internal pull-up resistors iobus1.set_port_direction(0, 0xFF) iobus1.set_port_pullups(0, 0xFF) iobus1.set_port_direction(1, 0xFF) iobus1.set_port_pullups(1, 0xFF) # Repeat the steps above for the second bus iobus2.set_port_direction(0, 0xFF) iobus2.set_port_pullups(0, 0xFF) iobus2.set_port_direction(1, 0xFF) iobus2.set_port_pullups(1, 0xFF) while True: # clear the console os.system("clear") # read the pins 1 to 16 on both buses and print the results print("Bus 1 Bus 2") print("Pin 1: " + str(iobus1.read_pin(1)) + " Pin 1: " + str(iobus2.read_pin(1))) print("Pin 2: " + str(iobus1.read_pin(2)) + " Pin 2: " + str(iobus2.read_pin(2))) print("Pin 3: " + str(iobus1.read_pin(3)) + " Pin 3: " + str(iobus2.read_pin(3))) print("Pin 4: " + str(iobus1.read_pin(4)) + " Pin 4: " + str(iobus2.read_pin(4))) print("Pin 5: " + str(iobus1.read_pin(5)) + " Pin 5: " + str(iobus2.read_pin(5))) print("Pin 6: " + str(iobus1.read_pin(6)) + " Pin 6: " + str(iobus2.read_pin(6))) print("Pin 7: " + str(iobus1.read_pin(7)) + " Pin 7: " + str(iobus2.read_pin(7))) print("Pin 8: " + str(iobus1.read_pin(8)) + " Pin 8: " + str(iobus2.read_pin(8))) print("Pin 9: " + str(iobus1.read_pin(9)) + " Pin 9: " + str(iobus2.read_pin(9))) print("Pin 10: " + str(iobus1.read_pin(10)) + " Pin 10: " + str(iobus2.read_pin(10))) print("Pin 11: " + str(iobus1.read_pin(11)) + " Pin 11: " + str(iobus2.read_pin(11))) print("Pin 12: " + str(iobus1.read_pin(12)) + " Pin 12: " + str(iobus2.read_pin(12))) print("Pin 13: " + str(iobus1.read_pin(13)) + " Pin 13: " + str(iobus2.read_pin(13))) print("Pin 14: " + str(iobus1.read_pin(14)) + " Pin 14: " + str(iobus2.read_pin(14))) print("Pin 15: " + str(iobus1.read_pin(15)) + " Pin 15: " + str(iobus2.read_pin(15))) print("Pin 16: " + str(iobus1.read_pin(16)) + " Pin 16: " + str(iobus2.read_pin(16))) # wait 0.5 seconds before reading the pins again time.sleep(0.1)
class Hardware: """ Hardware abstraction Class """ bus = None def __init__(self, simulated): """ __init__ is called at startup """ self.simulatedHw = simulated if self.simulatedHw: self.doorStateUp = False else: # create an instance of Bus 1 which is on I2C address 0x21 by default self.bus = IOPi(0x20) # set pins 1 to 8 to be outputs and turn them off self.bus.set_port_direction(0, 0x00) self.bus.write_port(0, 0x00) # set pins 9 to 16 to be inputs and turn pull-up on self.bus.set_port_direction(1, 1) self.bus.set_port_pullups(1, 1) self.timeToMove = 20 if self.detect_door_down(): self.doorStateUp = False elif self.detect_door_up(): self.doorStateUp = True else: # reboot while undetermined state -> close door self.doorStateUp = True self.motor_down() def motor_stop(self): """ controls the H-bridge to stop the motor. """ if not self.simulatedHw: # set all H-bridge switches to off self.bus.write_pin(1, 0x00) self.bus.write_pin(2, 0x00) self.bus.write_pin(3, 0x00) self.bus.write_pin(4, 0x00) def motor_up(self): """ controls the H-bridge to move the motor in a specific direction. """ if not self.simulatedHw: # avoid conflicts self.motor_stop() # enable up self.bus.write_pin(1, 0x01) self.bus.write_pin(3, 0x01) time.sleep(self.timeToMove) self.motor_stop() self.doorStateUp = True def motor_down(self): """ controls the H-bridge to move the motor in a specific direction. """ if not self.simulatedHw: # avoid conflicts self.motor_stop() # enable downward side of H-bridge self.bus.write_pin(1, 0x01) self.bus.write_pin(2, 0x01) time.sleep(self.timeToMove) self.motor_stop() self.doorStateUp = False def button_pressed(self): """ checks if the button is pressed with debouncing. """ if self.simulatedHw: return False else: pin = 9 if self.bus.read_pin(pin) == 0: time.sleep(0.05) if self.bus.read_pin(pin) == 0: # debounce 50 ms return True return False def detect_door_down(self): """ checks if the door if completely at the down end stop """ if self.simulatedHw: return True else: pin = 10 if self.bus.read_pin(pin) == 0: time.sleep(0.05) if self.bus.read_pin(pin) == 0: # debounce 50 ms return True return False def detect_door_up(self): """ checks if the door if completely at the down end stop """ if self.simulatedHw: return True else: pin = 11 if self.bus.read_pin(pin) == 0: time.sleep(0.05) if self.bus.read_pin(pin) == 0: # debounce 50 ms return True return False def is_door_up(self): """ Returns the state of the door without checking the hardware to save detection time """ return self.doorStateUp
def main(): """ Main program function """ passed = True iopi = IOPi(0x20, False) # new iopi object without initialisation iopi.set_bus_pullups(0x0000) # Check set_port_pullups port for low out of bounds try: iopi.set_port_pullups(-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 set_port_pullups port for high out of bounds try: iopi.set_port_pullups(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 set_port_pullups value for low out of bounds try: iopi.set_port_pullups(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 set_port_pullups value for high out of bounds try: iopi.set_port_pullups(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.set_port_pullups(0, x) iopi.set_port_pullups(1, x) print("Logic output Ended") if passed is False: print("Test Failed")
print("Importing from parent folder instead") try: import sys sys.path.append('..') from IOPi import IOPi except ImportError: raise ImportError("Failed to import library from parent folder") # Setup IOPi I2C addresses iobus1 = IOPi(0x20) iobus2 = IOPi(0x21) # We will read the inputs 1 to 16 from the I/O bus so set port 0 and # port 1 to be inputs and enable the internal pull-up resistors iobus1.set_port_direction(0, 0xFF) iobus1.set_port_pullups(0, 0xFF) iobus1.set_port_direction(1, 0xFF) iobus1.set_port_pullups(1, 0xFF) # Repeat the steps above for the second bus iobus2.set_port_direction(0, 0xFF) iobus2.set_port_pullups(0, 0xFF) iobus2.set_port_direction(1, 0xFF) iobus2.set_port_pullups(1, 0xFF) ############### MQTT section ################## def on_connect(client, userdata, flags, rc):
class Hardware: """ Hardware abstraction Class """ bus = None def __init__(self): """ __init__ is called at startup """ # create an instance of Bus 1 which is on I2C address 0x21 by default self.bus = IOPi(0x20) # set pins 1 to 8 to be outputs and turn them off self.bus.set_port_direction(0, 0x00) self.bus.write_port(0, 0x00) # set pins 9 to 16 to be inputs and turn pullup on self.bus.set_port_direction(1, 1) self.bus.set_port_pullups(1, 1) def motor_stop(self): """ controls the H-bridge to stop the motor. """ # set all H-bridge switches to off self.bus.write_pin(1, 0x00) self.bus.write_pin(2, 0x00) self.bus.write_pin(3, 0x00) self.bus.write_pin(4, 0x00) def motor_up(self): """ controls the H-bridge to move the motor in a specific direction. """ # avoid conflicts self.motor_stop() # enable up self.bus.write_pin(1, 0x01) self.bus.write_pin(3, 0x01) def motor_down(self): """ controls the H-bridge to move the motor in a specific direction. """ # avoid conflicts self.motor_stop() # enable downward sidecof H-bridge self.bus.write_pin(1, 0x01) self.bus.write_pin(2, 0x01) def button_pressed(self): """ controls the H-bridge to move the motor in a specific direction. """ if self.read_pin(8) == 0: time.sleep(0.05) if self.read_pin(8) == 0: # debounce 50 ms return True return False
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 ")