Python driver for the PCA9536 GPIO expander
Project description
pca9536-driver
Easy-to-use python driver for the PCA9536 GPIO expander.
Installation
The package is available on PyPI. Installation is can be done with your favourite package manager. For example:
pip install pca9536-driver
Usage
In order to initialise the device we need an open SMBus
object.
Depending on the machine that you are running on you may need to provide another bus number or path:
from pca9536 import PCA9536
from smbus2 import SMBus
with SMBus(1) as bus:
device = PCA9536(bus=bus)
The address of the PCA9536
defaults to 0x41
. This is the (fixed) address of the PCA9536 devices, so you should
never have to change it. If you do want to change it, you can provide it like PCA9536(bus=bus, address=0x42)
.
Controlling a pin
The PCA9536 has four GPIO pins, which are represented by PCA9536Pin
objects that can be accessed by using index brackets:
pin = device[0]
If desired, you can loop over all pins:
for pin in device:
...
Reading the logic level of the pin is done with the read()
method, which returns a boolean:
logic_level = pin.read()
To switch the pin to output mode and write the logic level:
from pca9536 import PinMode
pin.mode = PinMode.output # or use "output"
pin.write(True)
Note that writing only sets the output flip-flops of the GPIO expander, and has therefore no effect when the pin is in input mode.
Simultaneous operations
If you have the need to read from, write to or set the mode of multiple pins simultaneously,
then you can use the respective methods and properties of the PCA9536
object.
Reading returns a tuple of four booleans:
pin_0, pin_1, pin_2, pin_3 = device.read()
Writing can be done to one or more pins at the same time:
device.write(pin_0=True, pin_2=False)
Setting the same mode of all pins to the same value:
device.mode = PinMode.input
or specify a value per pin by providing a tuple of four values:
device.mode = PinMode.input, Pinmode.output, "input", None
Note that also here the values PinMode.input
and PinMode.output
may be used interchangeably with the strings
"input"
and "output"
. Providing None
for any of the values will leave the mode of the respective pin unchanged.
Polarity inversion
The PCA9536 offers functionality to invert the polarity of the input bits. Note that the outputs are not affected by this setting.
Polarity inversion of a single pin can be enabled by:
pin.polarity_inversion = True
Polarity inversion of all pins can be set at the same time in a similar manner to that of the pin mode:
device.polarity_inversion = False
# or
device.polarity_inversion = False, True, True, None
where None
will leave the current polarity inversion of a pin untouched.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pca9536-driver-0.1.2.tar.gz
.
File metadata
- Download URL: pca9536-driver-0.1.2.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 515cd14ca55b5f2d67ca80cd4d67fb2e286b1f42d5589c3f2002cd86524276cb |
|
MD5 | 52877b07cbbd0dd73ca7981a3a1d4fdd |
|
BLAKE2b-256 | 91275129419ed96cc4ff3d52891c80f0f05ca9f678c11f70e5b1a685c9a5e55d |
File details
Details for the file pca9536_driver-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: pca9536_driver-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0b5f9a710ae5f4dbf3d42205d3ebe36fb5b997a25f1a9a89bfc99f28788e863 |
|
MD5 | b1708533385f599f5bc50a2d53554264 |
|
BLAKE2b-256 | 4f444944b22ab9509b9b4f0a3552303d3a721cf6727aea37e5ab12d9a0bc14d5 |