Ejemplo n.º 1
0
    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 main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    for a in range(1, 65536):
        iopi.set_interrupt_on_bus(a)
        x = iopi.get_interrupt_on_bus()
        if x != a:
            passed = False
            break
        iopi.set_interrupt_on_bus(a)
        x = iopi.get_interrupt_on_bus()
        if x != a:
            passed = False
            break

    if passed is False:
        print("Test Failed")
    else:
        print("Test Passed")
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
 def hardware_initialise(self):
     # Initialise hardware
     for n in range(0, self.numstopio):
         self.sbuses.append(IOPi(self.stopaddr[n]))
     for n in range(0, self.numstopio):
         self.hardware_initialise_stops(self.sbuses[n], self.stopwport,
                                        self.stoprport)
Ejemplo n.º 8
0
    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 main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Logic Analyser Check
    print("Logic output Started")

    iopi.reset_interrupts()

    print("Logic output Ended")

    if passed is False:
        print("Test Failed")
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)
Ejemplo n.º 11
0
 def hardware_initialise(self):
     for n in range(0, len(self.noteaddr)):
         self.nbuses.append(IOPi(self.noteaddr[n]))
     self.notecheck = "0" * 8 * len(self.nbuses)
     for n in range(0, self.numstopio):
         self.sbuses.append(IOPi(self.stopaddr[n]))
     if self.numpresetio > 0:
         for n in range(0, self.numpresetio):
             self.pbuses.append(IOPi(self.presetaddr[n]))
     for n in range(0, len(self.noteaddr)):
         if self.switchtype == "matrix":
             self.hardware_initialise_matrix(self.nbuses[n], self.notewport,
                                             self.noterport)
         if self.switchtype == "linear":
             self.hardware_initialise_linear(self.nbuses[n])
     for n in range(0, self.numstopio):
         self.hardware_initialise_stops(self.sbuses[n], self.stopwport,
                                        self.stoprport)
     if self.numpresetio > 0:
         for n in range(0, self.numpresetio):
             self.hardware_initialise_presets(self.pbuses[n])
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Check write_bus value for low out of bounds
    try:
        iopi.write_bus(-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_bus value for high out of bounds
    try:
        iopi.write_bus(65536)
        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, 65536):
        iopi.write_bus(x)

    print("Logic output Ended")

    if passed is False:
        print("Test Failed")
Ejemplo n.º 13
0
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Check read_interrupt_capture port for low out of bounds
    try:
        iopi.read_interrupt_capture(-1)
        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 read_interrupt_capture port for high out of bounds
    try:
        iopi.read_interrupt_capture(2)
        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

    # Logic Analyser Check
    print("Logic output Started")

    iopi.read_interrupt_capture(0)
    iopi.read_interrupt_capture(1)

    print("Logic output Ended")

    if passed is False:
        print("Test Failed")
    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 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)

    # Logic Analyser Check
    print("Logic output Started")

    iopi.read_bus()

    print("Logic output Ended")

    if passed is False:
        print("Test Failed")
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    iopi.set_interrupt_polarity(0)
    x = iopi.get_interrupt_polarity()
    if x != 0:
        passed = False
    iopi.set_interrupt_polarity(1)
    x = iopi.get_interrupt_polarity()
    if x != 1:
        passed = False

    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)
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)
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)
Ejemplo n.º 20
0
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
import time
import paho.mqtt.client as mqtt
try:
    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)
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Check get_interrupt_defaults for low out of bounds
    try:
        iopi.get_interrupt_defaults(-1)
        pass
    except ValueError:
        print("get_interrupt_defaults() low boundary check: PASSED")
        pass
    except IOError:
        passed = False
        print("I2C IOError")
    else:
        passed = False
        print("get_interrupt_defaults() low boundary check: FAILED")
        pass

    # Check get_interrupt_defaults for low out of bounds
    try:
        iopi.get_interrupt_defaults(2)
        pass
    except ValueError:
        print("get_interrupt_defaults() high boundary check: PASSED")
        pass
    except IOError:
        passed = False
        print("I2C IOError")
    else:
        passed = False
        print("get_interrupt_defaults() high boundary check: FAILED")
        pass

    for a in range(1, 256):
        iopi.set_interrupt_defaults(0, a)
        x = iopi.get_interrupt_defaults(0)
        if x != a:
            passed = False
            break
        iopi.set_interrupt_defaults(1, a)
        x = iopi.get_interrupt_defaults(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
    """

    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")
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)
Ejemplo n.º 26
0
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)
Ejemplo n.º 27
0
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)
Ejemplo n.º 28
0
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
    """

    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_port port for low out of bounds
    try:
        iopi.read_port(-1)
        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 read_port port for high out of bounds
    try:
        iopi.read_port(2)
        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

    # Logic Analyser Check
    print("Logic output Started")

    iopi.read_port(0)
    iopi.read_port(1)

    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
    """
    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)
Ejemplo n.º 32
0
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)
Ejemplo n.º 33
0
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)
 
Ejemplo n.º 34
0
import paho.mqtt.client as mqtt

try:
    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
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)
Ejemplo n.º 35
0
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)
Ejemplo n.º 36
0
		JeedomIP = sys.argv[3]
	if len(sys.argv) > 2:
		eqLogic = int(sys.argv[2])
	if len(sys.argv) > 1:
		port = int(sys.argv[1])

	# On va demander la valeur des compteurs avec un peu de retard expres
	timeCPT=time.time()+11
	NextRefresh=time.time()+40
	sendCPT=0

	# set up IOPi Plus
	#i2c_helper = ABEHelpers()
	#i2c_bus = i2c_helper.get_smbus()
	#bus = IoPi(i2c_bus, boardId)
	bus = IOPi(boardId)

	# Toutes les entrees en impulsion
	# Init du Compteur  d'Impulsion
	CounterPinValue={}
	PinNextSend={}
	TempoPinHIGH={}
	TempoPinLOW={}
	Status_pins={}
	Status_INPUTS={}
	swtch={}
	exit=0
	SetAllLOW=0
	SetAllHIGH=0
	SetAllSWITCH=0
	SetAllPulseLOW=0
Ejemplo n.º 37
0
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Reset to 0x00
    iopi.set_interrupt_on_bus(0x0000)

    # Check set_interrupt_on_pin pin for low out of bounds
    try:
        iopi.set_interrupt_on_pin(0, 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 set_interrupt_on_pin pin for high out of bounds
    try:
        iopi.set_interrupt_on_pin(17, 0)
        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

    # Check set_interrupt_on_pin value for low out of bounds
    try:
        iopi.set_interrupt_on_pin(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_interrupt_on_pin value for high out of bounds
    try:
        iopi.set_interrupt_on_pin(17, 2)
        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(1, 17):
        iopi.set_interrupt_on_pin(x, 1)

    print("Logic output Ended")

    if passed is False:
        print("Test Failed")
Ejemplo n.º 38
0
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)
Ejemplo n.º 39
0
def main():
    """
    Main program function
    """

    passed = True

    iopi = IOPi(0x20, False)  # new iopi object without initialisation

    # Check get_pin_direction for low out of bounds
    try:
        iopi.get_pin_direction(-1)
        pass
    except ValueError:
        print("get_pin_direction() low boundary check: PASSED")
        pass
    except IOError:
        passed = False
        print("I2C IOError")
    else:
        passed = False
        print("get_pin_direction() low boundary check: FAILED")
        pass

    # Check get_pin_direction for low out of bounds
    try:
        iopi.get_pin_direction(17)
        pass
    except ValueError:
        print("get_pin_direction() high boundary check: PASSED")
        pass
    except IOError:
        passed = False
        print("I2C IOError")
    else:
        passed = False
        print("get_pin_direction() high boundary check: FAILED")
        pass

    for a in range(1, 17):
        iopi.set_pin_direction(a, 0)
        x = iopi.get_pin_direction(a)
        if x != 0:
            passed = False
            break
        iopi.set_pin_direction(a, 1)
        x = iopi.get_pin_direction(a)
        if x != 1:
            passed = False
            break

    if passed is False:
        print("Test Failed")
    else:
        print("Test Passed")
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)