Skip to main content

typed-registers

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.0.tar.gz (10.4 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.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typed_registers-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 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.0.tar.gz
Algorithm Hash digest
SHA256 4c2e2cbdcbc43ab0952dd86cdcfc93764c1afb1d05ec6163b8b299dd0bbd7387
MD5 3b9451b603b0be6d7fb9b994787e8b81
BLAKE2b-256 6fe05d9b1084d349900db69ffa9caaa37a243d9cbd163e1cef399eb6eae5d1ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for typed_registers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 935c541c812e2b7829739ed639cea00ccdf75a15c0d84a5f96bc187d7dd5b6df
MD5 33711e205526fbb0fcdb964c888c6dee
BLAKE2b-256 3ad7600addb57dc78287e433f874fbc913a3df9cbe2b1b36f566830d919c6492

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