A useful i2c package for Pi
Project description
A useful i2c Python3 package for Pi
It is a simple I2C interface based on smbus2.
1. Installation
Via pip
pip3 install pyi2c
2. API and example
2.1 I2C
2.1.1 declare
from pyi2c import I2C
# Create i2c
BUS_N = 0 # 0 or 1 or 2. Change this to yours
i2c = I2C(BUS_N)
ADDR = 0x38 # Change this to yours
2.1.2 write(ADDR, data)
datacan be a byte or list of bytes.
WRITE0 = 0x00 # Change this to yours
i2c.write(ADDR, WRITE0)
# or write multi bytes, up to 64 bytes
WRITE1 = 0x01 # Change this to yours
i2c.write(ADDR, [WRITE0, WRITE1])
2.1.2 read(ADDR, byte_size=1)
byte_sizecan be empty (default is 1)
read_data = i2c.read(ADDR)
# or set length of reading bytes
byte_size = 2
read_data = i2c.read(ADDR, byte_size)
print( len(read_data) )
# 2
2.1.3 writeread(ADDR, data, byte_size=1)
datacan be a byte or list of bytes.byte_sizecan be empty (default is 1)
# First write and read rapidly one byte
read_data = i2c.writeread(ADDR, WRITE0)
# These also work
read_data = i2c.writeread(ADDR, [WRITE0, WRITE1])
read_data = i2c.writeread(ADDR, [WRITE0, WRITE1], byte_size)
2.2 getBit(byte, bin_n, bin_m=-1)
bin_nshould be>= 0bin_mcan be empty (default is -1 but will overwrote withbin_n)bin_norbin_mcan be larger than byte's size
from pyi2c import getBit
byte = 0x5a # Any byte data
print( bin(byte) )
# '0b1011010'
- Get bit #n of byte
print( getBit(byte, 0) )
# 0
print( getBit(byte, 1) )
# 1
- Get multi bits from #n to #m of byte
print( getBit(byte, 4, 3) )
# 3 = 0b10
print( getBit(byte, 3, 4) )
# 3 = 0b10, the same as previous
- Recommend usage
if getBit(byte, 4) == 0b1:
print('hoge')
# 'hoge'
For developers
Build
python3 -m build
Upload
python3 -m twine upload dist/*
Reference
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
pyi2c-0.0.5.tar.gz
(3.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
pyi2c-0.0.5-py3-none-any.whl
(4.0 kB
view details)
File details
Details for the file pyi2c-0.0.5.tar.gz.
File metadata
- Download URL: pyi2c-0.0.5.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
718c97013e948d1ae1d8829abed80332d16bd229448f9542e76d2ad7d660f62d
|
|
| MD5 |
c25b2071dcfa84518668f156953352a2
|
|
| BLAKE2b-256 |
6cb94b408c8e787907b88ae574b147ed844c0e5f326efae1766dfc53ce7be962
|
File details
Details for the file pyi2c-0.0.5-py3-none-any.whl.
File metadata
- Download URL: pyi2c-0.0.5-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e25f0d038805652a57eb26de10400477ec56d141dbdac9893e3db5d1e8f42752
|
|
| MD5 |
46bac418984e2ffdb6ed5759c0c3b2d3
|
|
| BLAKE2b-256 |
125685eec3211c42bbec69126f1aec4f1986225be4d15121911ad6ef1b089f75
|