Skip to main content

A python wrapper for libft4222 library

Project description

PyFT4222

A libft4222 library python wrapper.

Description

Building

Installation

The pyft4222 package can be installed using pip:

pip install pyft4222

Use virtual environment prefferably.

udev rule

The FT4222 device is not accesible by all users by default. You can create a rule in /etc/udev/rules.d/99-ftdi.conf to make the device available to all users.

# FTDI's ft4222 USB-I2C Adapter
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="601c", GROUP="plugdev", MODE="0666"

Documentation

Use mypy or other language server that supports Python types. Library functions are easier to use with type hints.

WIP

Examples

Open an SPI master stream:

import pyft4222 as ft
from pyft4222.stream import InterfaceType
from pyft4222.wrapper import ResType
from pyft4222.wrapper.spi import ClkPhase, ClkPolarity
from pyft4222.wrapper.spi.master import ClkDiv, SsoMap

# Print out list of connected devices
for dev in ft.get_device_info_list():
    print(dev)

# Open device using 'device index'
dev = ft.open_by_idx(0)

# Check if it was opened successfully
if dev.tag == ResType.OK:
    handle = dev.result

    # Check if the FT4222 mode is as expected
    if handle.tag == InterfaceType.DATA_STREAM:
        # Initialize FT4222 in spi master mode using a single-bit
        # full-duplex transfer
        spi_master = handle.init_single_spi_master(
            ClkDiv.CLK_DIV_2,
            ClkPolarity.CLK_IDLE_LOW,
            ClkPhase.CLK_TRAILING,
            SsoMap.SS_0,
        )

        # Write and read back data simultaneously
        read_data = spi_master.single_read_write(bytes([0x01, 0x02, 0x03, 0x04]))

        print("Data read:")
        print(read_data)

        # Close the device handle
        spi_master.close()
    else:
        print("FT4222 is in invalid mode!")
else:
    print("Couldn't open the handle")
    print(dev.err)

Open an I2C slave stream:

import pyft4222 as ft
from pyft4222.stream import InterfaceType
from pyft4222.wrapper import ResType

# Print out list of connected devices
for dev in ft.get_device_info_list():
    print(dev)

# Open device using 'device index'
dev = ft.open_by_idx(0)

# Check if it was opened successfully
if dev.tag == ResType.OK:
    handle = dev.result

    # Check if the FT4222 mode is as expected
    if handle.tag == InterfaceType.DATA_STREAM:
        # Initialize FT4222 in I2C slave mode
        i2c_slave = handle.init_i2c_slave()
        # Set an I2C device address
        i2c_slave.set_address(0x0340)

        # Write data into the device buffer
        i2c_slave.write(bytes([0xFF, 0x01, 0x02, 0x03]))

        # Finally, close the device handle
        i2c_slave.close()
    else:
        print("FT4222 is in invalid mode!")
else:
    print("Couldn't open the handle")
    print(dev.err)

Open GPIO stream:

import pyft4222 as ft
from pyft4222.stream import InterfaceType
from pyft4222.wrapper import ResType
from pyft4222.wrapper.gpio import Direction, PortId

# Print out list of connected devices
for dev in ft.get_device_info_list():
    print(dev)

# Open device using 'device index'
dev = ft.open_by_idx(0)

# Check if it was opened successfully
if dev.tag == ResType.OK:
    handle = dev.result

    # Check if the FT4222 mode is as expected
    if handle.tag == InterfaceType.GPIO:
        gpio = handle.init_gpio(
            (
                Direction.OUTPUT,
                Direction.INPUT,
                Direction.OUTPUT,
                Direction.OUTPUT,
            )
        )
        # Disable suspend out, else we cannot control GPIO2
        gpio.set_suspend_out(False)
        # Disable wakeup interrupt, else we cannot control GPIO3
        gpio.set_wakeup_interrupt(False)

        # Set GPIO port 0 into a logical 1 state
        gpio.write(PortId.PORT_0, True)

        # Read GPIO1 input state
        port1_state = gpio.read(PortId.PORT_1)
        print(port1_state)

        # Close the device handle
        gpio.close()
    else:
        print("FT4222 is in invalid mode!")
else:
    print("Couldn't open the handle")
    print(dev.err)

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

pyft4222-0.0.3.tar.gz (422.6 kB view details)

Uploaded Source

Built Distribution

pyft4222-0.0.3-py3-none-any.whl (433.2 kB view details)

Uploaded Python 3

File details

Details for the file pyft4222-0.0.3.tar.gz.

File metadata

  • Download URL: pyft4222-0.0.3.tar.gz
  • Upload date:
  • Size: 422.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for pyft4222-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2423e9f9488b59860e2240ae945b3c3e281c5eef99a6c222d864cb19974cea00
MD5 793a27ceb484a1f85b81cde37f31d12c
BLAKE2b-256 6f2deb7a419c695a6edaaf9ab8585f2d161012832de6e036560474259d00fcec

See more details on using hashes here.

Provenance

File details

Details for the file pyft4222-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyft4222-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 433.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for pyft4222-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6d5e26cc3ec1d8b004da4d6b09c906aafe5cbb76aecc1646a654f5d6d6b3e0ec
MD5 df05445c78e06396e69392a67830a5ea
BLAKE2b-256 f611c7d94e5f6f08f9a1b9152b254ef859c2ee423cb3d37e9422d0cda7207ef4

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page