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(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) # 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 """ # 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 """ 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(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 """ 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()
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
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))
#CC # GPIO.output(10, GPIO.LOW) # GPIO.output(27, GPIO.LOW) # # GPIO.output(10, GPIO.HIGH) # GPIO.output(27, GPIO.HIGH) #GPIO.setup(port_or_pin, GPIO.OUT, initial=1) bus = IOPi(0x20)# Tilføj flere busser her bus2 = IOPi(0x21) bus.set_port_direction(0, 0x00) time.sleep(1.5) bus.write_port(0, 0xFF) time.sleep(1.5) bus.set_port_direction(1, 0x00) time.sleep(1.5) bus.write_port(1, 0xFF) bus2.set_port_direction(0, 0x00) time.sleep(1.5) bus2.write_port(0, 0xFF) time.sleep(1.5) bus2.set_port_direction(1, 0x00) time.sleep(1.5) bus2.write_port(1, 0xFF) #bus.write_pin(1, 1) pin 1-16 and 1 for off or 0 for on
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)
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)) client.subscribe("topic/iopi")
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
adc1 = ADCPi(0x6C, 0x6D, 12) adc2 = ADCPi(0x6A, 0x6B, 12) # IO PI PLUS shield setup iobus1 = IOPi(0x20) # bus 1 will be inputs iobus2 = IOPi(0x21) # bus 2 will be outputs # inputs op bus 1 iobus1.set_port_direction(0, 0xFF) iobus1.set_port_pullups(0, 0xFF) iobus1.set_port_direction(1, 0xFF) iobus1.set_port_pullups(1, 0xFF) # Outputs op bus 2 iobus2.set_port_direction(0, 0x00) iobus2.write_port(0, 0x00) iobus2.set_port_direction(1, 0x00) iobus2.write_port(1, 0x00) # ---- test ---- game_status = multiprocessing.Value('i', 0) # ------------------ ASSIGNING THE SHIELD PINS --------------- ''' DIGITAL INPUTS ON BUS 1 (ALL PULLUP) ''' # top buttons top_button1 = 1 top_button2 = 3 top_button3 = 5 top_button4 = 7
def main(): """ Main program function """ passed = True iopi = IOPi(0x20, False) # new iopi object without initialisation iopi.write_bus(0x0000) # Check write_port port for low out of bounds try: iopi.write_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 write_port port for high out of bounds try: iopi.write_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 write_port value for low out of bounds try: iopi.write_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 write_port value for high out of bounds try: iopi.write_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.write_port(0, x) iopi.write_port(1, x) print("Logic output Ended") if passed is False: print("Test Failed")
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)
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 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)
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 from ADCDACPi import ADCDACPi from time import sleep from ServoPi import PWM pwm = PWM(0x41) pwm.set_pwm_freq(1000) ##output_enable() adcdac = ADCDACPi(1) adcdac.set_adc_refvoltage(3.3) bus = IOPi(0x20) bus.set_port_direction(0, 0x00) bus.set_port_direction(1, 0xF0) bus.write_port(0, 0) bus.write_port(1, 0) sleep(1) bus.write_pin(9, 1) sleep(1) bus.set_pin_pullup(16, 1) bus.set_pin_pullup(15, 1) bus.set_pin_pullup(14, 1) bus.invert_pin(16, 1) bus.invert_pin(15, 1) bus.invert_pin(14, 0) count = 0 dc = 0 while True: if bus.read_pin(14) == 1: