Skip to main content

A small, type-safe library for working with hardware registers in Python.

Project description

typed-registers

typed-registers is a small, type-safe library for working with hardware registers in Python.

It lets you define registers as Python classes and interact with them through a pluggable bus interface — eliminating manual byte manipulation while keeping full control over hardware behavior.

The goal is to make register access:

  • typed
  • explicit
  • composable
  • testable

instead of opaque byte slicing and bit masking.

Features

  • Typed register definitions using Python classes
  • Clear separation between register logic and transport
  • Works with I²C, SPI, UART, mock buses, or custom transports
  • Minimal API surface
  • Fully typed (Typing :: Typed)
  • Python 3.11+

Built-in Register Types

Class Description
ByteRegister single byte register
Int24Register signed 24-bit integer
Int32Register signed 32-bit integer
BlockRegister structured multi-byte register blocks

Installation

pip install typed-registers

With I²C support:

pip install typed-registers[i2c]

Quick Example

from typed_registers import Int32Register

class TemperatureRegister(Int32Register):
    ADDRESS = 0x10
from smbus2 import SMBus
from typed_registers.bus import SMBusRegisterBus

bus = SMBusRegisterBus(SMBus(1))

temp = TemperatureRegister.read(bus, addr=0x40)
print(temp.value)

Byte Registers

from dataclasses import dataclass
from typed_registers import ByteRegister

@dataclass(slots=True, frozen=True)
class ModeRegister(ByteRegister):
    ADDRESS = 0x01

    enabled: bool

    def to_byte(self) -> int:
        return 0x01 if self.enabled else 0x00

    @classmethod
    def from_byte(cls, value: int):
        return cls(enabled=bool(value & 0x01))

Block Registers

BlockRegister is used for structured multi-byte registers.

You define how raw bytes map into fields:

from dataclasses import dataclass
from typed_registers import BlockRegister

@dataclass(slots=True, frozen=True)
class ExampleBlock(BlockRegister):
    ADDRESS = 0x20
    WIDTH = 3

    high: int
    low: int

    @classmethod
    def _decode(cls, data: bytes):
        return cls(
            high=data[0],
            low=(data[1] << 8) | data[2],
        )

Read it like any other register:

value = ExampleBlock.read(bus, addr=0x40)

By default, BlockRegister is read-only. Override _encode() to support writing.

Bus Abstraction

Registers operate through the RegisterBus protocol:

class RegisterBus(Protocol):
    def read(self, addr: int, reg: int, length: int) -> bytes
    def write(self, addr: int, reg: int, data: bytes) -> None

This allows registers to work with any transport layer.

SMBus / I²C Support

from smbus2 import SMBus
from typed_registers.bus import SMBusRegisterBus

bus = SMBusRegisterBus(SMBus(1))

Supports both smbus and smbus2.

Testing

Because the bus is abstracted, registers are easy to test:

class FakeBus:
    def read(self, addr, reg, length):
        return b"\x00\x00\x00\x2A"

    def write(self, addr, reg, data):
        print("write:", addr, reg, data)

Design Philosophy

This library intentionally keeps things simple:

  • Registers are first-class objects
  • Encoding/decoding is explicit and local
  • No hidden magic or frameworks
  • Hardware behavior remains visible

The register map is the API.

Status

Alpha

The API is stabilizing and already used in real drivers, but may evolve.

License

MIT License

Author

Joshua B. Bussdieker https://github.com/jbussdieker

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

typed_registers-0.2.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typed_registers-0.2.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file typed_registers-0.2.0.tar.gz.

File metadata

  • Download URL: typed_registers-0.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for typed_registers-0.2.0.tar.gz
Algorithm Hash digest
SHA256 80dcf56efc395a0407f8e3011c30a28a8a5622a00b5f0184b60d9aa10dc67ced
MD5 9453a11f51bd87c1bb991362c9a2d856
BLAKE2b-256 918a2b84b2335cb980cc66523e6a726efec44f8d62785715d91254ab3e69305a

See more details on using hashes here.

File details

Details for the file typed_registers-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_registers-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f5a9eb80f2baafc15e550b656957862e5ad7c723e5ad358c184f4c9f57a7185
MD5 37b32e6e6d019509549e19b892f82b61
BLAKE2b-256 5280b4f5ac1080bffce538b1dfa4109cd30f41721d92153f29debda33de21944

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page