Python DSL for interacting with SMBus-compatible i2c devices
Project description
i2cdevice
i2cdevice is a python domain-specific language aimed at dealing with common SMBus/i2c device interaction patterns.
This project aims to make group-up implementations of Python libraries for i2c devices easier, simpler and inherently self-documenting.
It does this by separating a detailed description of the hardware registers and how they should be manipulated into a stuctured definition language.
This project does not aim to help you make a public API for Python devices- that should be built on top of the fundamentals presented here.
Using This Library
You should generally aim for a 1:1 representation of the hardware registers in the device you’re implementing, even if you don’t plan to use all the functionality. Having the full register set implemented allows for the easy addition of new features in future.
Checkout the libraries listed below for real-world examples.
Features
Classes for describing devices, registers and individual bit fields within registers in a fashion which maps closely with the datasheet
Value translation from real world numbers (such as 512ms) to register values (such as 0b111) and back again
Automatic generation of accessors for every BitField- add a mode field and you’ll get get_mode and set_mode methods on your Register.
Support for treating multiple-bytes as a single value, or single register with multiple values
Built With i2cdevice
Examples
The below example defines the ALS_CONTROL register on an ltr559, with register address 0x80.
It has 3 fields; gain - which is mapped to real world values - and sw_reset/mode which are single bit flags.
ALS_CONTROL = Register('ALS_CONTROL', 0x80, fields=(
BitField('gain', 0b00011100, values_map={1: 0b000, 2: 0b001, 4: 0b011, 8:0b011, 48:0b110, 96:0b111}),
BitField('sw_reset', 0b00000010),
BitField('mode', 0b00000001)
))
A lookup table is not required for values, however, a function can be used to translate values from and to a format that the device understands.
The below example uses i2cdevice._byte_swap to change the endianness of two 16bit values before they are stored/retrieved.
# This will address 0x88, 0x89, 0x8A and 0x8B as a continuous 32bit register
ALS_DATA = Register('ALS_DATA', 0x88, fields=(
BitField('ch1', 0xFFFF0000, bitwidth=16, values_in=_byte_swap, values_out=_byte_swap),
BitField('ch0', 0x0000FFFF, bitwidth=16, values_in=_byte_swap, values_out=_byte_swap)
), read_only=True, bitwidth=32)
A “Register” and its “BitField”s define a set of rules and logic for detailing with the hardware register which is intepreted by the device class. Registers are declared on a device using the registers=() keyword argument:
I2C_ADDR = 0x23
ltr559 = Device(I2C_ADDR, bit_width=8, registers=(
ALS_CONTROL,
ALS_DATA
))
0.0.5
Bump to stable release
0.0.4
Bugfixes
0.0.3
Added License
0.0.2
Major Refactor
0.0.1
Initial Release
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file i2cdevice-0.0.5.tar.gz
.
File metadata
- Download URL: i2cdevice-0.0.5.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27b130a2e3187118cc69636352bd497f7bab7d0d9ae214d17cc4ca1c493e88ac |
|
MD5 | b744d40ad3ffdb7c7d1294f3eea5fcfc |
|
BLAKE2b-256 | b08672986b1d769365664592dedf5dbc2af11e7df8c80f9603dec079952531c3 |
File details
Details for the file i2cdevice-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: i2cdevice-0.0.5-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 507543c744e88e60dbfb9e13740350ac2f95962949ee961aac57f2a1e7718651 |
|
MD5 | e0701af8a302ccc5650c3dfc185fc1e4 |
|
BLAKE2b-256 | 544ec15240b95c8d6e27a4a701195c5574b96b72abad244ee2e198f6788b5d08 |
File details
Details for the file i2cdevice-0.0.5-py2-none-any.whl
.
File metadata
- Download URL: i2cdevice-0.0.5-py2-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8df1fb3bebe0344b160809d1515cc63ad3ffaabd7d5a5e4b1d124316ab758ad1 |
|
MD5 | f6604434fe057cc03de08f29b3af1ff5 |
|
BLAKE2b-256 | 66c497312e4798b9c39fdb0952e177b890c634fcad946701f529b56dbb991b28 |