smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python

https://github.com/kplindegaard/smbus2

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

interactive serializer cycles packaging network-simulation shellcodes hacking autograding observability genomics
Last synced: 10 months ago · JSON representation

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
Created over 10 years ago · Last pushed over 1 year ago
Metadata Files
Readme Changelog License

README.md

smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python

Build Status Documentation Status CodeQL Quality Gate Status

Python Verions PyPi Version PyPI - Downloads

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:

  1. It should be a drop-in replacement of smbus. The syntax shall be the same.
  2. 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:

  1. read or write bulks of data larger than SMBus' 32 bytes limit.
  2. 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

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

All Time
  • Total Commits: 83
  • Total Committers: 23
  • Avg Commits per committer: 3.609
  • Development Distribution Score (DDS): 0.434
Past Year
  • Commits: 3
  • Committers: 3
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.667
Top Committers
Name Email 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
question (21) duplicate (1)
Pull Request Labels
dependencies (8)

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

  • Versions: 16
  • Dependent Packages: 79
  • Dependent Repositories: 627
  • Downloads: 439,418 Last month
  • Docker Downloads: 5,509,546
Rankings
Dependent packages count: 0.2%
Dependent repos count: 0.5%
Docker downloads count: 0.6%
Downloads: 1.1%
Average: 2.1%
Stargazers count: 4.6%
Forks count: 5.3%
Maintainers (1)
Last synced: 10 months ago
conda-forge.org: smbus2
  • Versions: 4
  • Dependent Packages: 2
  • Dependent Repositories: 1
Rankings
Dependent packages count: 19.6%
Forks count: 23.2%
Average: 23.3%
Dependent repos count: 24.4%
Stargazers count: 26.1%
Last synced: 11 months ago

Dependencies

.github/workflows/codeql-analysis.yml actions
  • actions/checkout v2 composite
  • github/codeql-action/analyze v1 composite
  • github/codeql-action/autobuild v1 composite
  • github/codeql-action/init v1 composite
.github/workflows/python-build-test.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v3 composite
.github/workflows/python-publish.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite