pylibftdi / doc / advanced_usage.rst

Advanced Usage

Three attributes of Device instances are documented which allow direct access to the underlying libftdi functionality.

  1. fdll - this is a reference to the loaded libftdi library, loaded via ctypes. This should be used with the normal ctypes protocols.

  2. ctx - this is a reference to the context of the current device context. It is managed as a raw ctypes byte-string, so can be modified if required at the byte-level using appropriate ctypes methods.

  3. ftdi_fn - a convenience function wrapper, this is the preferred method for accessing library functions for a specific device instance. This is a function forwarder to the local fdll attribute, but also wraps the device context and passes it as the first argument. In this way, using device.ftdi_fn.ft_xyz is more like the D2XX driver provided by FTDI, in which the device context is passed in at initialisation time and then the client no longer needs to care about it. A call to:

    >>> device.ftdi_fn.ft_xyz(1, 2, 3)
    

    is equivalent to the following:

    >>> device.fdll.ft_xyz(ctypes.byref(device.ctx), 1, 2, 3)
    

    but has the advantages of being shorter and not requiring ctypes to be in scope.

    incorrect operations using any of these attributes of devices are liable to crash the Python interpreter

Examples

The following example shows opening a device in serial mode, switching temporarily to bit-bang mode, then back to serial and writing a string. Why this would be wanted is anyone's guess ;-)

>>> from pylibftdi import Device
>>>
>>> with Device() as dev:
>>>    dev.ftdi_fn.ftdi_set_bitmode(1, 0x01)
>>>    dev.write('\x00\x01\x00')
>>>    dev.ftdi_fn.ftdi_set_bitmode(0, 0x00)
>>>    dev.write('Hello World!!!')

The libftdi documentation should be consulted in conjunction with the ctypes reference for guidance on using these features.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.