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 """ passed = True iopi = IOPi(0x20, False) # new iopi object without initialisation # Reset to 0x00 iopi.write_bus(0x0000) iopi.set_bus_direction(0xFFFF) # Check read_pin pin for low out of bounds try: iopi.read_pin(0) pass except ValueError: print("pin low boundary check: PASSED") pass except IOError: passed = False print("I2C IOError") else: passed = False print("pin low boundary check: FAILED") pass # Check read_pin pin for high out of bounds try: iopi.read_pin(17) pass except ValueError: print("pin high boundary check: PASSED") pass except IOError: passed = False print("I2C IOError") else: passed = False print("pin high boundary check: FAILED") pass # Logic Analyser Check print("Logic output Started") for x in range(1, 17): iopi.read_pin(x) print("Logic output Ended") if passed is False: print("Test Failed")
def main(): """ Main program function """ bus = IOPi(0x20) bus.set_pin_direction(1, 1) # set pin 1 as an input bus.set_pin_direction(8, 0) # set pin 8 as an output bus.write_pin(8, 0) # turn off pin 8 bus.set_pin_pullup(1, 1) # enable the internal pull-up resistor on pin 1 bus.invert_pin(1, 1) # invert pin 1 so a button press will register as 1 while True: if bus.read_pin(1) == 1: # check to see if the button is pressed print('button pressed') # print a message to the screen bus.write_pin(8, 1) # turn on the led on pin 8 time.sleep(2) # wait 2 seconds else: bus.write_pin(8, 0) # turn off the led on pin 8
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
PinNextSend={} TempoPinHIGH={} TempoPinLOW={} Status_pins={} Status_INPUTS={} swtch={} exit=0 SetAllLOW=0 SetAllHIGH=0 SetAllSWITCH=0 SetAllPulseLOW=0 SetAllPulseHIGH=0 pinStr = '' for i in range(0,16): bus.set_pin_direction(i+1, 1) input=bus.read_pin(i+1) Status_INPUTS[i]=input pinStr += '&IN_' + str(i) + '=' + str(input) CounterPinValue[i] = 0 PinNextSend[i] = 0 TempoPinHIGH[i] = 0 TempoPinLOW[i] = 0 swtch[i] = 0 Status_pins[i]='.' # Envoi de l'etats des inputs au boot if pinStr!='': SimpleSend(pinStr) threadLock = threading.Lock()
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: count += 1 print('\n\nbutton checked ' + str(count) + ' time') for i in range(1, 6): bus.write_pin(i, 1) #sleep(0.1) print('Read relais 1=' + str(bus.read_pin(16)) + '\nRead relais 2=' + str(bus.read_pin(15))) dc = 0 #sleep(0.5) else: print('\n\nbutton pressed ') print('Volt1=' + str(adcdac.read_adc_voltage(1, 0)) + 'Volt2=' + str(adcdac.read_adc_voltage(2, 0)) + 'ciao ' + str(dc)) if dc >= 4095: dc = 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)
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)