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 preferably.

udev rule

The FT4222 device is not accessible by all users by default. You can create a rule in /etc/udev/rules.d/99-ftdi.rules 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:

from koda import Err, Ok
import pyft4222 as ft
from pyft4222.stream import InterfaceType
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 using Python < 3.10, use "isinstance(dev, Ok)"
match dev:
    case Ok(handle):

        # Check if the FT4222 mode is as expected
        if handle.tag == InterfaceType.DATA_STREAM:

            # Use context manager to close the handle automatically at the end of scope
            with handle:

                # Initialize FT4222 in SPI master mode using a single-bit
                # full-duplex transfer
                with handle.init_single_spi_master(
                    ClkDiv.CLK_DIV_2,
                    ClkPolarity.CLK_IDLE_LOW,
                    ClkPhase.CLK_TRAILING,
                    SsoMap.SS_0,
                ) as spi_master:

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

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

        else:
            print("FT4222 is in invalid mode!")

    case Err(err):
        print("Couldn't open the handle")
        print(err)

Open an I2C slave stream:

import pyft4222 as ft
from pyft4222.stream import InterfaceType

from koda import Err, Ok

# 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 using Python < 3.10, use "isinstance(dev, Ok)"
match dev:
    case Ok(handle):
        # Check if the FT4222 mode is as expected
        if handle.tag == InterfaceType.DATA_STREAM:
            # The handle is closed automatically at the end of scope
            with handle:
                # Initialize the FT4222 in I2C slave mode
                # The handle is uninitialized automatically at the end of scope
                with handle.init_i2c_slave() as iic_slave:
                    # Set an I2C device address
                    iic_slave.set_address(0x0340)

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

        else:
            print("FT4222 is in invalid mode!")

    case Err(err):
        print("Couldn't open the handle")
        print(err)

Open GPIO stream:

from koda import Err, Ok
import pyft4222 as ft
from pyft4222.stream import InterfaceType
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'
result = ft.open_by_idx(0)

# Check if it was opened successfully
# If using Python < 3.10, use "isinstance(dev, Ok)"
match result:
    case Ok(handle):
        # Check if the FT4222 mode is as expected
        if handle.tag == InterfaceType.GPIO:
            # The handle is automatically closed at the end of the scope
            with handle:
                # The handle is automatically uninitialized at the end of the scope
                with handle.init_gpio(
                    (
                        Direction.OUTPUT,
                        Direction.INPUT,
                        Direction.OUTPUT,
                        Direction.OUTPUT,
                    )
                ) as gpio:
                    # 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 GPIO port 1 input state
                    port1_state = gpio.read(PortId.PORT_1)
                    print(port1_state)

        else:
            print("FT4222 is in invalid mode!")

    case Err(err):
        print("Couldn't open the handle")
        print(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.1.0.tar.gz (423.2 kB view details)

Uploaded Source

Built Distribution

pyft4222-0.1.0-py3-none-any.whl (432.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyft4222-0.1.0.tar.gz
  • Upload date:
  • Size: 423.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pyft4222-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f478cb984942da7af2dcf2b55be5c5983dcb32996074e53e5658017361ae009b
MD5 5fccfde5930a794d54ebcc595b620805
BLAKE2b-256 d96f2f215ec8d3f9d16a849b0f02398f2b6637114a8b00a370cf8feae0af1e66

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pyft4222-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 432.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pyft4222-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 723ca7ab479fe15711bb64188cb9a642da7a99da0189f6e86b8b32a59effd14c
MD5 83e851a843d6bb2301a91cb4dc839f50
BLAKE2b-256 e58d2f616228a0f5cf70f38b2a1e2a9ae036c40782f04a7833d035a9595acd08

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