smbus2
A drop-in replacement for smbus-cffi/smbus-python in pure Python
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.1%) to scientific vocabulary
Keywords from Contributors
Repository
A drop-in replacement for smbus-cffi/smbus-python in pure Python
Basic Info
- Host: GitHub
- Owner: kplindegaard
- License: mit
- Language: Python
- Default Branch: master
- Size: 130 KB
Statistics
- Stars: 265
- Watchers: 16
- Forks: 66
- Open Issues: 32
- Releases: 10
Metadata Files
README.md
smbus2
A drop-in replacement for smbus-cffi/smbus-python in pure Python
Introduction
smbus2 is (yet another) pure Python implementation of the python-smbus package.
It was designed from the ground up with two goals in mind:
- It should be a drop-in replacement of smbus. The syntax shall be the same.
- Use the inherent i2c structs and unions to a greater extent than other pure Python implementations like pysmbus does. By doing so, it will be more feature complete and easier to extend.
Currently supported features are:
- Get i2c capabilities (I2C_FUNCS)
- SMBus Packet Error Checking (PEC) support
- read_byte
- write_byte
- readbytedata
- writebytedata
- readworddata
- writeworddata
- readi2cblock_data
- writei2cblock_data
- write_quick
- process_call
- readblockdata
- writeblockdata
- blockprocesscall
- i2c_rdwr - combined write/read transactions with repeated start
It is developed on Python 2.7 but works without any modifications in Python 3.X too.
More information about updates and general changes are recorded in the change log.
SMBus code examples
smbus2 installs next to smbus as the package, so it's not really a 100% replacement. You must change the module name.
Example 1a: Read a byte
```python from smbus2 import SMBus
Open i2c bus 1 and read one byte from address 80, offset 0
bus = SMBus(1) b = bus.readbytedata(80, 0) print(b) bus.close() ```
Example 1b: Read a byte using 'with'
This is the very same example but safer to use since the smbus will be closed automatically when exiting the with block.
```python from smbus2 import SMBus
with SMBus(1) as bus: b = bus.readbytedata(80, 0) print(b) ```
Example 1c: Read a byte with PEC enabled
Same example with Packet Error Checking enabled.
```python from smbus2 import SMBus
with SMBus(1) as bus: bus.pec = 1 # Enable PEC b = bus.readbytedata(80, 0) print(b) ```
Example 2: Read a block of data
You can read up to 32 bytes at once.
```python from smbus2 import SMBus
with SMBus(1) as bus: # Read a block of 16 bytes from address 80, offset 0 block = bus.readi2cblock_data(80, 0, 16) # Returned value is a list of 16 bytes print(block) ```
Example 3: Write a byte
```python from smbus2 import SMBus
with SMBus(1) as bus: # Write a byte to address 80, offset 0 data = 45 bus.writebytedata(80, 0, data) ```
Example 4: Write a block of data
It is possible to write 32 bytes at the time, but I have found that error-prone. Write less and add a delay in between if you run into trouble.
```python from smbus2 import SMBus
with SMBus(1) as bus: # Write a block of 8 bytes to address 80 from offset 0 data = [1, 2, 3, 4, 5, 6, 7, 8] bus.writei2cblock_data(80, 0, data) ```
I2C
Starting with v0.2, the smbus2 library also has support for combined read and write transactions. i2c_rdwr is not really a SMBus feature but comes in handy when the master needs to:
- read or write bulks of data larger than SMBus' 32 bytes limit.
- write some data and then read from the slave with a repeated start and no stop bit between.
Each operation is represented by a i2c_msg message object.
Example 5: Single i2c_rdwr
```python from smbus2 import SMBus, i2c_msg
with SMBus(1) as bus: # Read 64 bytes from address 80 msg = i2cmsg.read(80, 64) bus.i2crdwr(msg)
# Write a single byte to address 80
msg = i2c_msg.write(80, [65])
bus.i2c_rdwr(msg)
# Write some bytes to address 80
msg = i2c_msg.write(80, [65, 66, 67, 68])
bus.i2c_rdwr(msg)
```
Example 6: Dual i2c_rdwr
To perform dual operations just add more i2c_msg instances to the bus call:
```python from smbus2 import SMBus, i2c_msg
Single transaction writing two bytes then read two at address 80
write = i2cmsg.write(80, [40, 50]) read = i2cmsg.read(80, 2) with SMBus(1) as bus: bus.i2c_rdwr(write, read) ```
Example 7: Access i2c_msg data
All data is contained in the i2c_msg instances. Here are some data access alternatives.
```python
1: Convert message content to list
msg = i2c_msg.write(60, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) data = list(msg) # data = [1, 2, 3, ...] print(len(data)) # => 10
2: i2c_msg is iterable
for value in msg: print(value)
3: Through i2c_msg properties
for k in range(msg.len): print(msg.buf[k]) ```
Installation instructions
From PyPi with pip:
pip install smbus2
From conda-forge using conda:
conda install -c conda-forge smbus2
Installation from source code is straight forward:
python setup.py install
Owner
- Name: Karl-Petter Lindegaard
- Login: kplindegaard
- Kind: user
- Location: Oslo, Norway
- Repositories: 2
- Profile: https://github.com/kplindegaard
GitHub Events
Total
- Create event: 2
- Issues event: 2
- Release event: 1
- Watch event: 25
- Issue comment event: 2
- Push event: 1
- Pull request event: 2
Last Year
- Create event: 2
- Issues event: 2
- Release event: 1
- Watch event: 25
- Issue comment event: 2
- Push event: 1
- Pull request event: 2
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Karl-Petter Lindegaard | k****d@g****m | 47 |
| dependabot[bot] | 4****] | 6 |
| Rail Shafigulin | r****n@f****m | 4 |
| Paulo Costa | me@p****r | 3 |
| Thijs Triemstra | i****o@c****l | 3 |
| Arti Zirk | a****k@g****m | 2 |
| Mark Mentovai | m****k@c****g | 2 |
| Blaise Thompson | b****e@u****m | 1 |
| Damien Walsh | me@d****t | 1 |
| Ellis Percival | g****m@f****k | 1 |
| Fabian | f****l@g****m | 1 |
| Gabriel Smith | g****h@g****m | 1 |
| Henrik Nilsson | 4****c | 1 |
| Lars Kruse | d****l@s****e | 1 |
| Matej Urbas | m****s@g****m | 1 |
| Michal Suchánek | h****h@g****m | 1 |
| Moritz | 7****g | 1 |
| Riccardo Gusmeroli | m****s@g****m | 1 |
| Stefan S | k****i@g****m | 1 |
| YuLun Shih | s****h@y****e | 1 |
| Yuri Pieters | M****n | 1 |
| nxet | p****l@g****m | 1 |
| wschmied | s****d@g****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 10 months ago
All Time
- Total issues: 61
- Total pull requests: 56
- Average time to close issues: 3 months
- Average time to close pull requests: 21 days
- Total issue authors: 53
- Total pull request authors: 26
- Average comments per issue: 2.43
- Average comments per pull request: 1.55
- Merged pull requests: 48
- Bot issues: 0
- Bot pull requests: 6
Past Year
- Issues: 1
- Pull requests: 1
- Average time to close issues: 5 days
- Average time to close pull requests: 11 minutes
- Issue authors: 1
- Pull request authors: 1
- Average comments per issue: 1.0
- Average comments per pull request: 1.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- mefistotelis (4)
- daylanKifky (2)
- rossmacarthur (2)
- kplindegaard (2)
- untzag (2)
- jabdoa2 (2)
- scottscott70 (2)
- wildbiotiger (1)
- cyphunk (1)
- SuReinfelder (1)
- kungpfui (1)
- Batucada2 (1)
- g3d12 (1)
- sdrck (1)
- satya-prakash-7 (1)
Pull Request Authors
- kplindegaard (18)
- dependabot[bot] (8)
- thijstriemstra (7)
- eindiran (4)
- paulo-raca (3)
- markmentovai (2)
- henrik-nil-acc (2)
- yodaldevoid (2)
- cyboflash (2)
- wschmied (1)
- artizirk (1)
- untzag (1)
- kungpfui (1)
- nxet (1)
- mschulteg (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 2
-
Total downloads:
- pypi 439,418 last-month
- Total docker downloads: 5,509,546
-
Total dependent packages: 81
(may contain duplicates) -
Total dependent repositories: 628
(may contain duplicates) - Total versions: 20
- Total maintainers: 1
pypi.org: smbus2
smbus2 is a drop-in replacement for smbus-cffi/smbus-python in pure Python
- Homepage: https://github.com/kplindegaard/smbus2
- Documentation: https://smbus2.readthedocs.io/
- License: MIT
-
Latest release: 0.5.0
published over 1 year ago
Rankings
Maintainers (1)
conda-forge.org: smbus2
- Homepage: https://github.com/kplindegaard/smbus2
- License: MIT
-
Latest release: 0.4.2
published about 4 years ago
Rankings
Dependencies
- actions/checkout v2 composite
- github/codeql-action/analyze v1 composite
- github/codeql-action/autobuild v1 composite
- github/codeql-action/init v1 composite
- actions/checkout v3 composite
- actions/setup-python v3 composite
- actions/checkout v2 composite
- actions/setup-python v2 composite