Skip to main content

Declarative HID protocol framework for Linux

Project description

hidproto

PyPI Python License Tests

Declarative HID protocol framework for Linux. Define device protocols as pure data, the framework handles transport, discovery, effects, caching, and state tracking.

Why

Every HID device project (OpenRGB, keyRGB, rivalcfg, openrazer) rewrites the same boilerplate: open hidraw, build reports, send ioctl, manage state. hidproto lets you define a protocol in ~50 lines of pure data and get a working device driver.

Install

pip install hidproto

Or from source:

git clone https://github.com/Chocapikk/hidproto
cd hidproto
pip install -e .

Quick start

from hidproto import HIDDevice, HIDProtocol, command, effect

class MyKeyboard(HIDProtocol):
    vendor_id    = 0x048D
    product_id   = 0x8910
    report_id    = 0xCC
    report_size  = 6
    rows         = 6
    cols         = 20
    preset_base  = 0x71
    custom_base  = 0xA1
    color_custom = 0xAA

    animation_mode   = command(0x00, args=1, doc="Animation mode")
    set_led          = command(0x01, args=4, doc="Per-key color")
    brightness_speed = command(0x09, args=4, doc="Brightness + speed")
    wave_slot        = command(0x15, args=4, doc="Wave slot")

    effects = {
        "off":  effect("off",  animation=0x0C, needs_clear=True),
        "wave": effect("wave", animation=0x04, slot_cmd="wave_slot",
                        directions=("up", "down", "left", "right")),
    }

Keyboard = HIDDevice.for_protocol(MyKeyboard)

with Keyboard() as kb:
    kb.brightness(8)
    kb.speed(5)
    kb.effect("wave", direction="right", color=(255, 0, 0))

That's it. No boilerplate, no subclassing, no transport code.

Features

Protocol definition - command() descriptor auto-generates report builders from opcode + arg count. No methods to write.

Effect dispatch - effect() declaratively maps names to animation modes, color slots, directional slots. One kb.effect("wave", direction="right") call handles everything.

Device discovery - auto-finds devices via /sys/class/hidraw by VID/PID.

Transport - hidraw ioctl for feature reports, os.write for output reports, select for reads with timeout.

State caching - send_if_changed() skips redundant HID sends. invalidate() forces resync (suspend/resume).

Checksums - xor_checksum(), sum_checksum(), and _with_checksum() for protocols that need them (Razer, Corsair).

Sequence numbers - _next_seq() for transactional protocols (Logitech, Corsair).

Architecture

hidproto/
  command.py     CommandSpec + @command descriptor
  effect.py      EffectSpec + declarative effect dispatch
  protocol.py    HIDProtocol base class (report building, transport)
  device.py      HIDDevice wrapper (effects, caching, brightness/speed)
  transport.py   HidrawTransport (ioctl, read, write)
  discovery.py   sysfs device discovery
  checksum.py    xor/sum checksum helpers

Examples

See examples/ite8910.py for a complete ITE 8910 keyboard RGB implementation: 9 commands, 11 effects, 8 wave directions, 4 snake diagonals, per-key color. All in ~70 lines of data.

License

MIT

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

hidproto-0.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

hidproto-0.1.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hidproto-0.1.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hidproto-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f116de2905f29a308cf70e3ee79b620fcffb6a667e71625955147ed000fbbea1
MD5 14360240baeae70cd5f5f49606951877
BLAKE2b-256 abcc6ced9fb7e1592eb7001df5571a8f64b02cdaaec88099cacef0a1999392f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for hidproto-0.1.0.tar.gz:

Publisher: publish.yml on Chocapikk/hidproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: hidproto-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hidproto-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37156afffd9228499f6fad09d186273d0eb7f15961c723db22fbdcb8336b5658
MD5 de232bbcc2c949873276ff165ae93aed
BLAKE2b-256 bddec90798245d3061a4b43abe76302007bdfcdb83e97b3c0461a6341a7f4429

See more details on using hashes here.

Provenance

The following attestation bundles were made for hidproto-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Chocapikk/hidproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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