hidtools.hidraw module — hid-tools documentation

# hidtools.hidraw module

class `HidrawDevice`(device)

  Bases: `object`

  A device as exposed by the kernel `hidraw` module. `hidraw` allows direct access to the HID device, both for reading and writing.

      with open('/dev/hidraw0', 'r+b') as fd:
          dev = HidrawDevice(fd)
          while True:
              dev.read_events()  # this blocks
              print(f'We received {len(dev.events)} events so far')

  Parameters

    device (File) – a file-like object pointing to `/dev/hidrawX`

  `name`

    The device name

  `bustype`

    The numerical bus type (`0x3` for USB, `0x5` for Bluetooth, see `linux/input.h`)

  `vendor_id`

    16-bit numerical vendor ID

  `product_id`

    16-bit numerical product ID

  `report_descriptor`

    The `hidtools.hid.ReportDescriptor` for this device

  `events`

    All events accumulated so far, a list of `HidrawEvent`

  … attribute:: time_offset

  > The offset to be used for recording events. By default the offset is the timestamp of the first event. When recording multiple devices, the time_offset from the first device to receive an event should be copied to the other device to ensure all recordings are in sync.

  `dump`(file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, from_the_beginning=False)

    Format this device in a file format in the form of

        R: 123 43 5 52 2 ... # the report descriptor size, followed by the integers
        N: the device name
        I: 3 124 abcd # bustype, vendor, product
        # comments are allowed
        E: 00001.000002 AB 12 34 56 # sec, usec, length, data
        ...

    This method is designed to be called repeatedly and only print the new events on each call. To repeat the dump from the beginning, set `from_the_beginning` to True.

    Parameters

      - file (File) – the output file to write to

      - from_the_beginning (bool) – if True, print everything again instead of continuing where we left off

  `read_events`()

    Read events from the device and store them in the device.

    This function simply calls `os.read()`, it is the caller’s task to either make sure the device is set nonblocking or to handle any `KeyboardInterrupt` if this call does end up blocking.

    Returns

      a tuple of `(index, count)` of the `events` added.

class `HidrawEvent`(sec, usec, bytes)

  Bases: `object`

  A single event from a hidraw device. The first event always has a timestamp of 0.0, all other events are offset accordingly.

  `sec`

    Timestamp seconds

  `usec`

    Timestamp microseconds

  `bytes`

    The data bytes read for this event