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 provides a simple abstraction for defining registers as Python classes and reading/writing them through a pluggable bus interface.

The goal is to make device register access:

  • typed
  • explicit
  • reusable
  • testable

instead of manually handling raw byte buffers.

This is especially useful when working with:

  • I²C devices
  • SPI devices
  • embedded sensors
  • ADCs / DACs
  • microcontroller peripherals
  • device drivers

Features

  • Typed register definitions using Python classes

  • Clear separation between register definitions and transport buses

  • Works with I²C, SPI, UART, mock buses, or custom transports

  • Built-in register helpers:

    • ByteRegister
    • Int24Register
    • Int32Register
  • Optional SMBus / smbus2 support

  • Fully typed (Typing :: Typed)

  • Python 3.11+

Installation

Basic installation

pip install typed-registers

With I²C support

pip install typed-registers[i2c]

This installs:

smbus2

Quick Example

Define a register:

from typed_registers import Int32Register


class TemperatureRegister(Int32Register):
    ADDRESS = 0x10

Read it from a device:

from smbus2 import SMBus
from typed_registers.bus import SMBusRegisterBus

bus = SMBusRegisterBus(SMBus(1))

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

print(temp.value)

Write a register:

TemperatureRegister(value=42).write(bus, addr=0x40)

Byte Registers

ByteRegister is useful for registers that represent a single byte with custom encoding.

Example:

from typed_registers import ByteRegister


class ModeRegister(ByteRegister):
    ADDRESS = 0x01

    def __init__(self, enabled: bool):
        self.enabled = enabled

    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))

Built-in Register Types

The library provides a few common register types:

Class Description
ByteRegister single byte register
Int24Register signed 24-bit integer
Int32Register signed 32-bit integer

All registers:

  • validate byte width
  • convert between Python values and raw bytes
  • support .read() and .write() operations

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.

For example:

  • I²C
  • SPI
  • UART
  • USB
  • mock testing buses

SMBus / I²C Support

If the i2c extra is installed, an SMBus adapter is available.

from smbus2 import SMBus
from typed_registers.bus import SMBusRegisterBus

bus = SMBusRegisterBus(SMBus(1))

This works with both:

  • smbus
  • smbus2

Testing

Because the bus is abstracted behind a protocol, it is easy to create test buses.

Example:

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)

Now registers can be tested without real hardware.

Design Goals

This library aims to provide:

  • minimal API surface
  • strong typing
  • clear register semantics
  • easy device driver construction

without introducing heavy frameworks or complex abstractions.

Status

This project is currently alpha.

The API may change while the design stabilizes.

License

MIT License

Author

Joshua B. Bussdieker

GitHub: 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.1.1.tar.gz (10.5 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.1.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for typed_registers-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7477c159422d8a29d82c0d9d0e59808773d381165413d060b1ce50556747d54d
MD5 ac449fec2cf378f129c0c060a97f3f91
BLAKE2b-256 b0b758f36c8f3bdddfd042f63bc176bb5506bebd966590d71ba8ecd40f6f6729

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typed_registers-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f10e2c51f315e4cd6a01057cffd72f2ac56c11792924f6b8e6b484137670f6e
MD5 94cf4180775970a896a4dc167ff929b0
BLAKE2b-256 8e28fac7d5dd124c02c75ac9a4753a39c358131ec3ad9a29ad92f4dd93d6e365

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