コード例 #1
0
using the read_interrupt_capture or read_port methods will
reset the interrupts


Initialise the IOPi device using the default addresses and set the
output of bank 1 on IC1 to the input of bank 1 on IC2
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
newbus = i2c_helper.get_smbus()

bus1 = IoPi(newbus, 0x20)
bus2 = IoPi(newbus, 0x21)

# Set all pins on bus 2 to be inputs with internal pull-ups disabled.

bus2.set_port_pullups(0, 0x00)
bus2.set_port_pullups(1, 0x00)
bus2.set_port_direction(0, 0xFF)
bus2.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
bus2.set_interrupt_polarity(1)
bus2.mirror_interrupts(0)

# Set the interrupts default value to trigger when 5V is applied to pins 1
# and 16
コード例 #2
0
def checkbit(byte, bit):
    # method for reading the value of a single bit within a byte
    if byte & (1 << bit):
        return 1
    else:
        return 0

# Create an instance of the ABEHelpers class

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

# Create an instance of the IoPi class called bus and set the I2C address to be 0x20 or Bus 1. 

bus = IoPi(i2c_bus, 0x21)

# Set all pins on the bus to be inputs with internal pull-ups enabled.

bus.set_port_pullups(0, 0xFF)
bus.set_port_pullups(1, 0xFF)
bus.set_port_direction(0, 0xFF)
bus.set_port_direction(1, 0xFF)

# Inverting the ports will allow a button connected to ground to register as 1 or on.

bus.invert_port(0, 0xFF)  # invert port 0 so a button press will register as 1
bus.invert_port(1, 0xFF)  # invert port 1 so a button press will register as 1

# Set the interrupt polarity to be active low and mirroring enabled, so
# INT A and INT B go low when an interrupt is triggered
コード例 #3
0
ファイル: tutorial1a.py プロジェクト: abelectronicsuk/Archive
"""
================================================
ABElectronics IO Pi 32-Channel Port Expander - Tutorial 1a
Version 1.1 Created 10/05/2014
Version 1.1 16/11/2014 updated code and functions to PEP8 format

Requires python smbus to be installed: sudo apt-get install python-smbus
run with: sudo python tutorial1a.py
================================================

This example uses the write_port method to count in binary using 8 LEDs
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 0x20)

bus.set_port_direction(0, 0x00)
bus.write_port(0, 0x00)

while True:
    for x in range(0, 255):
        bus.write_port(0, x)
        time.sleep(0.5)

        bus.write_port(0, 0x00)
コード例 #4
0
ファイル: LDR-pullup.py プロジェクト: zigel/piplay
Requires python smbus to be installed: sudo apt-get install python-smbus
run with: sudo python tutorial1.py
================================================

This example uses the write_pin and writePort methods to switch pin 1 on
and off on the IO Pi.
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

sensorbus = IoPi(i2c_bus, 0x20)
# ledbus = IoPi(i2c_bus, 0x20)

# sensorbus.set_pin_direction(1, 1)  # set pin 1 as an input

# ledbus.set_pin_direction(8, 0)  # set pin 8 as an ou tput

# ledbus.write_pin(8, 0)  # turn off pin 8

# print "turn off pin 8"

# sensorbus.set_pin_pullup(1, 1)  # enable the internal pull-up resistor on pin 1

# sensorbus.invert_pin(1, 1)  # invert pin 1 so a button press will register as 1

# sensorbus.set_port_direction(0, 0xFF)
コード例 #5
0
pins 1 and 16 will trigger INT A and B respectively.
using the read_interrupt_capture or read_port methods will
reset the interrupts


Initialise the IOPi device using the default addresses and set the
output of bank 1 on IC1 to the input of bank 1 on IC2
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
newbus = i2c_helper.get_smbus()

bus1 = IoPi(newbus, 0x20)
bus2 = IoPi(newbus, 0x21)

# Set all pins on bus 2 to be inputs with internal pull-ups disabled.

bus2.set_port_pullups(0, 0x00)
bus2.set_port_pullups(1, 0x00)
bus2.set_port_direction(0, 0xFF)
bus2.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
bus2.set_interrupt_polarity(1)
bus2.mirror_interrupts(0)

# Set the interrupts default value to trigger when 5V is applied to pins 1
コード例 #6
0
ファイル: LDR-pulldown.py プロジェクト: zigel/piplay
Requires python smbus to be installed: sudo apt-get install python-smbus
run with: sudo python tutorial1.py
================================================

This example uses the write_pin and writePort methods to switch pin 1 on
and off on the IO Pi.
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

sensorbus = IoPi(i2c_bus, 0x20)
# ledbus = IoPi(i2c_bus, 0x20)

# sensorbus.set_pin_direction(1, 1)  # set pin 1 as an input

# ledbus.set_pin_direction(8, 0)  # set pin 8 as an ou tput

# ledbus.write_pin(8, 0)  # turn off pin 8

# print "turn off pin 8"

# sensorbus.set_pin_pullup(1, 1)  # enable the internal pull-up resistor on pin 1

# sensorbus.invert_pin(1, 1)  # invert pin 1 so a button press will register as 1

# sensorbus.set_port_direction(0, 0xFF)
コード例 #7
0
Requires python 3 smbus to be installed
run with: python3 demo-iopiread.py
================================================

This example reads the first 8 pins of bus 1 on the IO Pi board.  The
internal pull-up resistors are enabled so each pin will read as 1 unless
the pin is connected to ground.

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 0x20)

# We will read the inputs 1 to 8 from bus 2 so set port 0 to be inputs and
# enable the internal pull-up resistors
bus.set_port_direction(0, 0xFF)
bus.set_port_pullups(0, 0xFF)

while True:
    # clear the console
    os.system('clear')

    # read the pins 1 to 8 and print the results
    print ('Pin 1: ' + str(bus.read_pin(1)))
    print ('Pin 2: ' + str(bus.read_pin(2)))
    print ('Pin 3: ' + str(bus.read_pin(3)))
    print ('Pin 4: ' + str(bus.read_pin(4)))
コード例 #8
0
ファイル: Start1.py プロジェクト: zigel/piplay
def Init_IOPi(): 
    global sensorbus    
    global buttonbus
    i2c_helper = ABEHelpers()
    i2c_bus = i2c_helper.get_smbus()
    sensorbus = IoPi(i2c_bus, 0x20) 
    # set both rows of pins to input mode
    sensorbus.set_port_direction(0, 0xFF)
    sensorbus.set_port_direction(1, 0xFF)
    #setup interrupts on bus 2, port 1
    buttonbus = IoPi(i2c_bus, 0x21)
    # Set all pins on the bus to be inputs with internal pull-ups enabled.
    buttonbus.set_port_pullups(1, 0xFF)
    buttonbus.set_port_direction(1, 0xFF)
    # Inverting the ports will allow a button connected to ground to register as 1 or on.
    buttonbus.invert_port(1, 0xFF)  # invert port 1 so a button press will register as 1
    # Set the interrupt polarity to be active low and mirroring enabled, so
    # INT A and INT B go low when an interrupt is triggered
    buttonbus.set_interrupt_polarity(0)
    #buttonbus.mirror_interrupts(1)
    # Set the interrupts default value to 0 so it will trigger when any of the pins on the bus change to 1
    buttonbus.set_interrupt_defaults(1, 0x00)
    # Set the interrupt type to be 0xFF for port B so an interrupt is
    # fired when the pin matches the default value
    buttonbus.set_interrupt_type(1, 0xFF)
    # Enable interrupts for all pins on the port
    buttonbus.set_interrupt_on_port(1, 0xFF)
    # reset the interrups on the IO Pi bus 
    buttonbus.reset_interrupts()

    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 pin 23 the function button_pressed will be run  
    GPIO.add_event_detect(23, GPIO.FALLING, callback=hwbutton_pressed) 
コード例 #9
0
class App:
    global i2c_helper
    global newbus
    global bus2

    def __init__(self, master):
        self.i2c_helper = ABEHelpers()
        self.newbus = self.i2c_helper.get_smbus()
        self.bus2 = IoPi(
            self.newbus, 0x21
        )  # create an instance of Bus 2 which is on I2C address 0x21 by default
        self.bus2.set_port_direction(
            0, 0x00)  # set pins 1 to 8 to be outputs and turn them off
        self.bus2.write_port(0, 0x00)

        self.bus2.set_port_direction(
            1, 0x00)  # set pins 9 to 16 to be outputs and turn them off
        self.bus2.write_port(1, 0x00)

        frame = Frame(master)  # create a frame for the GUI
        frame.pack()

        # create 16 buttons which run the togglepin function when pressed
        self.button = Button(frame,
                             text="Pin 1",
                             command=lambda: self.togglepin(1))
        self.button.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 2",
                             command=lambda: self.togglepin(2))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 3",
                             command=lambda: self.togglepin(3))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 4",
                             command=lambda: self.togglepin(4))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 5",
                             command=lambda: self.togglepin(5))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 6",
                             command=lambda: self.togglepin(6))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 7",
                             command=lambda: self.togglepin(7))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 8",
                             command=lambda: self.togglepin(8))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 9",
                             command=lambda: self.togglepin(9))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 10",
                             command=lambda: self.togglepin(10))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 11",
                             command=lambda: self.togglepin(11))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 12",
                             command=lambda: self.togglepin(12))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 13",
                             command=lambda: self.togglepin(13))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 14",
                             command=lambda: self.togglepin(14))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 15",
                             command=lambda: self.togglepin(15))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 16",
                             command=lambda: self.togglepin(16))
        self.slogan.pack(side=LEFT)

    def togglepin(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)
コード例 #10
0
    def __init__(self, master):
        self.i2c_helper = ABEHelpers()
        self.newbus = self.i2c_helper.get_smbus()
        self.bus2 = IoPi(
            self.newbus, 0x21
        )  # create an instance of Bus 2 which is on I2C address 0x21 by default
        self.bus2.set_port_direction(
            0, 0x00)  # set pins 1 to 8 to be outputs and turn them off
        self.bus2.write_port(0, 0x00)

        self.bus2.set_port_direction(
            1, 0x00)  # set pins 9 to 16 to be outputs and turn them off
        self.bus2.write_port(1, 0x00)

        frame = Frame(master)  # create a frame for the GUI
        frame.pack()

        # create 16 buttons which run the togglepin function when pressed
        self.button = Button(frame,
                             text="Pin 1",
                             command=lambda: self.togglepin(1))
        self.button.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 2",
                             command=lambda: self.togglepin(2))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 3",
                             command=lambda: self.togglepin(3))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 4",
                             command=lambda: self.togglepin(4))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 5",
                             command=lambda: self.togglepin(5))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 6",
                             command=lambda: self.togglepin(6))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 7",
                             command=lambda: self.togglepin(7))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 8",
                             command=lambda: self.togglepin(8))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 9",
                             command=lambda: self.togglepin(9))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 10",
                             command=lambda: self.togglepin(10))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 11",
                             command=lambda: self.togglepin(11))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 12",
                             command=lambda: self.togglepin(12))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 13",
                             command=lambda: self.togglepin(13))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 14",
                             command=lambda: self.togglepin(14))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 15",
                             command=lambda: self.togglepin(15))
        self.slogan.pack(side=LEFT)

        self.slogan = Button(frame,
                             text="Pin 16",
                             command=lambda: self.togglepin(16))
        self.slogan.pack(side=LEFT)
コード例 #11
0
Version 1.2 changes to format source code to PEP8 rules 12/11/2014
Requires python smbus to be installed with: sudo apt-get install python-smbus
run with: sudo python demo-iopiread.py
================================================

This example reads the first 8 pins of bus 1 on the IO Pi board.  The
internal pull-up resistors are enabled so each pin will read as 1 unless
the pin is connected to ground.

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()

bus1 = IoPi(bus, 0x22)

# We will read the inputs 1 to 8 from bus 2 so set port 0 to be inputs and
# enable the internal pull-up resistors
bus1.set_port_direction(0, 0xFF)
bus1.set_port_pullups(0, 0xFF)

while True:
    # clear the console
    os.system('clear')

    # read the pins 1 to 8 and print the results
    print 'Pin 1: ' + str(bus1.read_pin(1))
    print 'Pin 2: ' + str(bus1.read_pin(2))
    print 'Pin 3: ' + str(bus1.read_pin(3))
    print 'Pin 4: ' + str(bus1.read_pin(4))
コード例 #12
0
Requires python smbus to be installed: sudo apt-get install python-smbus
run with: sudo python tutorial1.py
================================================

This example uses the write_pin and writePort methods to switch pin 1 on
and off on the IO Pi.
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 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
コード例 #13
0
run with: sudo python demo-iopiwrite.py
================================================

This example uses the write_pin and writeBank methods to switch the pins
on and off on the IO Pi.

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time
i2c_helper = ABEHelpers()
newbus = i2c_helper.get_smbus()

bus1 = IoPi(newbus, 0x20)

# We will write to the pins 9 to 16 from bus 1 so set port 1 to be outputs
# turn off the pins
bus1.set_port_direction(1, 0x00)
bus1.write_port(1, 0x00)

while True:

    # count to 255 and display the value on pins 9 to 16 in binary format
    for x in range(0, 255):
        time.sleep(0.05)
        bus1.write_port(1, x)

    # turn off all of the pins on bank 1
    bus1.writePort(1, 0x00)
コード例 #14
0
    if len(sys.argv) > 3:
        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)

    # 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
コード例 #15
0
Requires python 3 smbus to be installed
run with: python3 tutorial2.py
================================================

This example uses the write_pin and writePort methods to switch pin 1 on
and off on the IO Pi.
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 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
コード例 #16
0
run with: python3 demo-iopireadwrite.py
================================================

This example reads pin 1 of bus 1 on the IO Pi board and sets pin 1 of bus 2 to match.  
The internal pull-up resistors are enabled so the input pin will read as 1 unless
the pin is connected to ground. 

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

# create two instances of the IoPi class called bus1 and bus2 and set the default i2c addresses

bus1 = IoPi(i2c_bus, 0x20)  # bus 1 will be inputs
bus2 = IoPi(i2c_bus, 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
bus1.set_port_direction(0, 0xFF)
bus1.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
bus2.set_port_direction(0, 0x00)
bus2.write_port(0, 0x00)

while True:
コード例 #17
0
    def run(self):

        bus = IoPi(i2c_bus, 0x20)
コード例 #18
0
Version 1.2 changes to format source code to PEP8 rules 12/11/2014
Requires python smbus to be installed with: sudo apt-get install python-smbus
run with: sudo python demo-iopiread.py
================================================

This example reads the first 8 pins of bus 1 on the IO Pi board.  The
internal pull-up resistors are enabled so each pin will read as 1 unless
the pin is connected to ground.

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 0x20)

# We will read the inputs 1 to 8 from bus 2 so set port 0 to be inputs and
# enable the internal pull-up resistors
bus.set_port_direction(0, 0xFF)
bus.set_port_pullups(0, 0xFF)

while True:
    # clear the console
    os.system("clear")

    # read the pins 1 to 8 and print the results
    print "Pin 1: " + str(bus.read_pin(1))
    print "Pin 2: " + str(bus.read_pin(2))
    print "Pin 3: " + str(bus.read_pin(3))
    print "Pin 4: " + str(bus.read_pin(4))
コード例 #19
0
"""
================================================
ABElectronics IO Pi 32-Channel Port Expander - Tutorial 1
Version 1.1 Created 10/05/2014
Version 1.1 16/11/2014 updated code and functions to PEP8 format

Requires python smbus to be installed: sudo apt-get install python-smbus
run with: sudo python tutorial1.py
================================================

This example uses the write_pin and write_port methods to switch pin 1 on
and off on the IO Pi.
"""
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import time

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 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)
コード例 #20
0
run with: python3 demo-iopireadwrite.py
================================================

This example reads pin 1 of bus 1 on the IO Pi board and sets pin 1 of bus 2 to match.  
The internal pull-up resistors are enabled so the input pin will read as 1 unless
the pin is connected to ground. 

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

# create two instances of the IoPi class called bus1 and bus2 and set the default i2c addresses

bus1 = IoPi(i2c_bus, 0x20) # bus 1 will be inputs
bus2 = IoPi(i2c_bus, 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
bus1.set_port_direction(0, 0xFF)
bus1.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
bus2.set_port_direction(0, 0x00)
bus2.write_port(0, 0x00)

while True:
コード例 #21
0
Requires python 3 smbus to be installed
run with: python3 demo-iopiread.py
================================================

This example reads the first 8 pins of bus 1 on the IO Pi board.  The
internal pull-up resistors are enabled so each pin will read as 1 unless
the pin is connected to ground.

Initialise the IOPi device using the default addresses, you will need to
change the addresses if you have changed the jumpers on the IO Pi
"""
i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

bus = IoPi(i2c_bus, 0x20)

# We will read the inputs 1 to 8 from bus 2 so set port 0 to be inputs and
# enable the internal pull-up resistors
bus.set_port_direction(0, 0xFF)
bus.set_port_pullups(0, 0xFF)

while True:
    # clear the console
    os.system('clear')

    # read the pins 1 to 8 and print the results
    print('Pin 1: ' + str(bus.read_pin(1)))
    print('Pin 2: ' + str(bus.read_pin(2)))
    print('Pin 3: ' + str(bus.read_pin(3)))
    print('Pin 4: ' + str(bus.read_pin(4)))