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 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 ''' # Create an instance of the IOPi class with an I2C address of 0x20 iobus = IOPi(0x20) # We will write to the pins 9 to 16 so set port 1 to be outputs turn off # the pins iobus.set_port_direction(1, 0x00) iobus.write_port(1, 0x00) while True: # count to 255 and display the value on pins 9 to 16 in binary format for val in range(0, 255): time.sleep(0.05) iobus.write_port(1, val) # turn off all of the pins on bank 1 iobus.write_port(1, 0x00) # now turn on all of the leds in turn by writing to one pin at a time iobus.write_pin(9, 1) time.sleep(0.1) iobus.write_pin(10, 1) time.sleep(0.1) iobus.write_pin(11, 1) time.sleep(0.1) iobus.write_pin(12, 1) time.sleep(0.1) iobus.write_pin(13, 1) time.sleep(0.1) iobus.write_pin(14, 1) time.sleep(0.1) iobus.write_pin(15, 1) time.sleep(0.1) iobus.write_pin(16, 1) # and turn off all of the leds in turn by writing to one pin at a time iobus.write_pin(9, 0) time.sleep(0.1) iobus.write_pin(10, 0) time.sleep(0.1) iobus.write_pin(11, 0) time.sleep(0.1) iobus.write_pin(12, 0) time.sleep(0.1) iobus.write_pin(13, 0) time.sleep(0.1) iobus.write_pin(14, 0) time.sleep(0.1) iobus.write_pin(15, 0) time.sleep(0.1) iobus.write_pin(16, 0)
def main(): """ Main program function """ passed = True iopi = IOPi(0x20, False) # new iopi object without initialisation # Check get_port_direction for low out of bounds try: iopi.get_port_direction(-1) pass except ValueError: print("get_port_direction() low boundary check: PASSED") pass except IOError: passed = False print("I2C IOError") else: passed = False print("get_port_direction() low boundary check: FAILED") pass # Check get_port_direction for low out of bounds try: iopi.get_port_direction(2) pass except ValueError: print("get_port_direction() high boundary check: PASSED") pass except IOError: passed = False print("I2C IOError") else: passed = False print("get_port_direction() high boundary check: FAILED") pass for a in range(1, 256): iopi.set_port_direction(0, a) x = iopi.get_port_direction(0) if x != a: passed = False break iopi.set_port_direction(1, a) x = iopi.get_port_direction(1) if x != a: passed = False break if passed is False: print("Test Failed") else: print("Test Passed")
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 """ bus = IOPi(0x21) bus.set_port_direction(0, 0x00) bus.write_port(0, 0x00) while True: bus.write_pin(1, 1) time.sleep(1) bus.write_pin(1, 0) 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 """ bus = IOPi(0x20) bus.set_port_direction(0, 0x00) bus.write_port(0, 0x00) while True: for count in range(0, 255): bus.write_port(0, count) time.sleep(0.5) bus.write_port(0, 0x00)
def main(): """ Main program function """ bus = IOPi(0x21) bus.set_port_direction(0, 0x00) bus.write_port(0, 0x00) while True: bus.write_pin(1, 1) print('Motherfucking blink') time.sleep(1) bus.write_pin(1, 0) time.sleep(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 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)
from IOPi import IOPi import time bus = IOPi(0x20) bus.set_port_direction(0, 0x00) bus.write_port(0, 0x00) bus.write_pin(1, 1) bus.write_pin(1, 1) time.sleep(1) bus.write_pin(1, 0) time.sleep(1) bus.write_pin(1, 1) time.sleep(1) bus.write_pin(1, 1) bus.write_pin(1, 0) bus.write_pin(1, 1) bus.write_pin(1, 0) bus.write_pin(1, 1) bus.write_pin(1, 0) bus.write_pin(1, 0) bus.write_pin(1, 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
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
print("Failed to import IOPi from python system path") 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 main(): """ Main program function """ passed = True iopi = IOPi(0x20, False) # new iopi object without initialisation iopi.set_bus_direction(0x0000) # Check set_port_direction port for low out of bounds try: iopi.set_port_direction(-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_direction port for high out of bounds try: iopi.set_port_direction(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_direction value for low out of bounds try: iopi.set_port_direction(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_direction value for high out of bounds try: iopi.set_port_direction(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_direction(0, x) iopi.set_port_direction(1, x) print("Logic output Ended") if passed is False: print("Test Failed")
import sys from IOPi import IOPi pin = sys.argv[2] port = int((pin - 1) / 8) bus = IOPi(sys.argv[1]) bus.set_port_direction(port, 0x01) print bus.read(pin)
from tkinter import * import csv import itertools from collections import defaultdict from tkinter import messagebox try: from IOPi import IOPi # checks to see if hardware connected or not except ModuleNotFoundError: from test import Bus as IOPi # initializing port directions for LED illumination bus1 = IOPi(0x20) # I2C address (bus 1, board 1) bus1.set_port_direction( 0, 0x00) # 0: pins 1-8, 0x00: set port direction as output bus1.set_port_direction( 1, 0x00) # 1: pins 9-16, 0x00: set port direction as output bus1.write_port(0, 0x00) # initially turn off all pins bus2 = IOPi(0x21) # I2C address (bus 2, board 1) bus2.set_port_direction( 0, 0x00) # 0: pins 1-8, 0x00: set port direction as output bus2.set_port_direction( 1, 0x00) # 1: pins 9-16, 0x00: set port direction as output bus2.write_port(0, 0x00) # initially turn off all pins bus3 = IOPi(0x22) # I2C address (bus 1, board 2) bus3.set_port_direction( 0, 0x00) # 0: pins 1-8, 0x00: set port direction as output bus3.set_port_direction( 1, 0x00) # 1: pins 9-16, 0x00: set port direction as output bus3.write_port(0, 0x00) # initially turn off all pins
def main(): """ Main program function """ ser = serial.Serial('/dev/ttyUSB0', baudrate=9600) ## ## bus2 = IOPi(0x21) ## bus1 = IOPi(0x20) ## ## bus2.set_port_direction(0, 0x00) ## bus2.write_port(0, 0x00) ## ## bus2.set_port_direction(1, 0x00) ## bus2.write_port(1, 0x00) ## ## ## bus1.set_port_direction(0, 0x00) ## bus1.write_port(0, 0x00) ## ## bus1.set_port_direction(1, 0x00) ## bus1.write_port(1, 0x00) ## ## bus1.set_port_direction(0, 0xFF) ## bus1.set_port_pullups(0, 0xFF) ## ## bus1.set_port_direction(1, 0xFF) ## bus1.set_port_pullups(1, 0xFF) timeMS = 0.02 ponovitev = 5 test = 0 count = 0 while True: ArduinoSay = ser.readline() ArduinoSayASCII = ArduinoSay.decode("ascii").rstrip() Number, Command = ArduinoSayASCII.split(".") print(ArduinoSayASCII) print(Number) print(Command) noro = 123 if ArduinoSayASCII == "init.start": print("Zaganjam") if ArduinoSayASCII == "init.end": print("Zagon koncan") if Command == "cleartempPassword": count = count + 1 print("PW clear") if Command == "NewStatus": print("test20") NewSt = list(str(Number)) NewStVhod = (NewSt[0]) NewStGaraza = (NewSt[1]) NewStKlet = (NewSt[2]) NewStKuhinja = (NewSt[3]) print(NewStVhod) print(NewStGaraza) print(NewStKlet) print(NewStKuhinja) ser.write(bytes(b'ChangeOK')) time.sleep(4) bus_BoltLock = IOPi(0x21) bus_BoltLock.set_port_direction(0, 0x00) bus_BoltLock.write_port(0, 0x00) bus_BoltLock.set_port_direction(1, 0x00) bus_BoltLock.write_port(1, 0x00) ## ## bus_BoltLock.set_port_direction(0, 0xFF) ## bus_BoltLock.set_port_pullups(0, 0xFF) ## ## bus_BoltLock.set_port_direction(1, 0xFF) ## bus_BoltLock.set_port_pullups(1, 0xFF) if NewStVhod == "1": bus_BoltLock.write_pin(7, 0) if NewStVhod == "0": bus_BoltLock.write_pin(7, 1) if NewStGaraza == "1": bus_BoltLock.write_pin(5, 0) if NewStGaraza == "0": bus_BoltLock.write_pin(5, 1) if NewStKlet == "1": bus_BoltLock.write_pin(3, 0) if NewStKlet == "0": bus_BoltLock.write_pin(3, 1) if NewStKuhinja == "1": bus_BoltLock.write_pin(1, 0) if NewStKuhinja == "0": bus_BoltLock.write_pin(1, 1) ser.write(bytes(b'OK.1111')) if Command == ("CheckPW") and len(Number) == 4: print('neki dela') if Number in open("/home/pi/Desktop/gesla.txt").read(): print("pravilono") Number = "" Command = "" time.sleep(0.5) ser.write("PWok".encode()) ## preveri stanje zaklepa csv_file = open("/home/pi/Desktop/BoltLockCSV.csv") csv_reader = csv.reader(csv_file) data = [] for row in csv_reader: vrata = str(row[0]) TrueOrFalse = str(row[1]) data.append([TrueOrFalse]) test1 = ''.join(data[1]) test2 = ''.join(data[2]) test3 = ''.join(data[3]) test4 = ''.join(data[4]) if test1 == "FALSE": vhod = False StatusLockUnlock1 = ("0") else: vhod = True StatusLockUnlock1 = ("1") if test2 == "FALSE": garaza = False StatusLockUnlock2 = ("0") else: garaza = True StatusLockUnlock2 = ("1") if test3 == "FALSE": klet = False StatusLockUnlock3 = ("0") else: klet = True StatusLockUnlock3 = ("1") if test4 == "FALSE": kuhinja = False StatusLockUnlock4 = ("0") else: kuhinja = True StatusLockUnlock4 = ("1") csv_file.close() StatusLockUnlock = StatusLockUnlock1, StatusLockUnlock2, StatusLockUnlock3, StatusLockUnlock4 s = "" SendFirstStatus = (s.join(StatusLockUnlock)) print(SendFirstStatus) Number = "" Command = "" ## time.sleep(2) ## bus2.write_pin(7, 1) time.sleep(2) ser.write(SendFirstStatus.encode()) main() else: print("napacno") ser.write(bytes(b'WrongPW')) main() elif Command == ("CheckPW") and len(Number) < 4: print("napacnosamo za") Number = "" Command = "" ser.write(bytes(b'WrongPW')) main()
except ImportError: print("Failed to import IOPi from python system path") 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 iobus = IOPi(0x20) iobusb = IOPi(0x21) # Set all pins as outputs and set to off iobus.set_port_direction(0, 0x00) iobus.write_port(0, 0x00) iobus.set_port_direction(1, 0x00) iobus.write_port(1, 0x00) iobusb.set_port_direction(0, 0x00) iobusb.write_port(0, 0x00) iobusb.set_port_direction(1, 0x00) iobusb.write_port(1, 0x00) # This is the Subscriber Client def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc))
from IOPi import IOPi except ImportError: print("Failed to import IOPi from python system path") 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 iobus = IOPi(0x20) iobusb = IOPi(0x21) # Set all pins as outputs and set to off iobus.set_port_direction(0, 0x00) iobus.write_port(0, 0x00) iobus.set_port_direction(1, 0x00) iobus.write_port(1, 0x00) iobusb.set_port_direction(0, 0x00) iobusb.write_port(0, 0x00) iobusb.set_port_direction(1, 0x00) iobusb.write_port(1, 0x00) # This is the Subscriber Client def on_connect(client, userdata, flags, rc):
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 ")
from IOPi import IOPi import time import math ''''setting up the busses of the IO-extenssion board to control the relay module board'''' bus1 = IOPi(0x20) bus1.set_port_direction(0, 0x00) bus1.set_port_direction(1, 0x00) bus1.write_port(0, 0xFF) bus1.write_port(1, 0xFF) bus2 = IOPi(0x21) bus2.set_port_direction(0, 0x00) bus2.set_port_direction(1, 0x00) bus2.write_port(0, 0xFF) bus2.write_port(1, 0xFF) #with these variables you can decide how many boards there are and how many Relays there are on each board numOffRelays = 16 numOffBoards = 2 '''''with this class a relayboard can be made to choose the relay'''' class Relayboard: def __init__ (self, bus): self.bus = bus ''''Function where the user can turn on the choosen relay there are 16 to choose frombut goes from 0-15.'''' def sellectRelayOn(self, relaynumber): if 0 >= relaynumber =< 15: relaynumber = relaynumber + 1 self.bus.write_pin(relaynumber, 0)
from IOPi import IOPi import time bus = IOPi(0x20) bus.set_port_direction(0, 0xFF) bus.set_port_direction(1, 0x00) bus.write_pin(16, 1) bus.write_pin(16, 0) bus.write_pin(10, 0) bus.write_pin(10, 1) bus.write_pin(10, 0) bus.set_pin_pullup(8, 1) bus.set_pin_pullup(9, 1) bus.invert_pin(9, 0) bus.invert_pin(8, 0) count = 0 while True: if bus.read_pin(7) == 1: count += 1 print('button checked ' + str(count) + ' time') bus.write_pin(10, 1) bus.write_pin(16, 1) time.sleep(0.5) else: print('button pressed ') bus.write_pin(10, 0) bus.write_pin(16, 0)
class App: """ Application Class """ bus2 = None def __init__(self, master): """ __init__ is called at startup """ # create an instance of Bus 2 which is on I2C address 0x21 by default self.bus2 = IOPi(0x21) # set pins 1 to 8 to be outputs and turn them off self.bus2.set_port_direction(0, 0x00) self.bus2.write_port(0, 0x00) # set pins 9 to 16 to be outputs and turn them off self.bus2.set_port_direction(1, 0x00) self.bus2.write_port(1, 0x00) self.build_ui(master) def build_ui(self, master): """ Build the UI using tkinter components """ frame = tk.Frame(master) # create a frame for the GUI frame.pack() # create 16 buttons which run the toggle function when pressed self.button = tk.Button(frame, text="Pin 1", command=lambda: self.toggle(1)) self.button.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 2", command=lambda: self.toggle(2)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 3", command=lambda: self.toggle(3)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 4", command=lambda: self.toggle(4)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 5", command=lambda: self.toggle(5)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 6", command=lambda: self.toggle(6)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 7", command=lambda: self.toggle(7)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 8", command=lambda: self.toggle(8)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 9", command=lambda: self.toggle(9)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 10", command=lambda: self.toggle(10)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 11", command=lambda: self.toggle(11)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 12", command=lambda: self.toggle(12)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 13", command=lambda: self.toggle(13)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 14", command=lambda: self.toggle(14)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 15", command=lambda: self.toggle(15)) self.slogan.pack(side=tk.LEFT) self.slogan = tk.Button(frame, text="Pin 16", command=lambda: self.toggle(16)) self.slogan.pack(side=tk.LEFT) def toggle(self, pin): """ read the status from the selected pin, invert it and write it back to the pin """ pinstatus = self.bus2.read_pin(pin) if pinstatus == 1: pinstatus = 0 else: pinstatus = 1 self.bus2.write_pin(pin, pinstatus)