Skip to content

Python API to access a USB or Bluetooth LE attached PFx Brick.

License

Notifications You must be signed in to change notification settings

fx-bricks/pfx-brick-py

Repository files navigation

images/sitelogo.png

PFx Brick Python API

https://travis-ci.org/fx-bricks/pfx-brick-py.svg?branch=master https://img.shields.io/github/issues/fx-bricks/pfx-brick-py.svg?style=flat

This repository contains a python package API for developing python scripts and applications which communicate with the PFx Brick. This package supports both USB and Bluetooth LE connections to the PFx Brick and is supported for Windows, macOS and linux.

Getting Started

OS Requirements

  • Supports Windows 10, version 16299 (Fall Creators Update) or greater
  • Supports Linux distributions with BlueZ >= 5.43
  • OS X/macOS support via Core Bluetooth API, from at least OS X version 10.11

Package Requirements

Installation

Pre-install System requirements for linux

The pfxbrick package will require some packages to be installed suport access to USB and Bluetooth hardware drivers. Use your preferred package manager to install these packages:

  • libhidapi-dev
  • libudev-dev
  • libusb-1.0-0-dev
  • bluez
  • bluetooth
  • libbluetooth-dev

Pre-install System requirements for macOS

It is recommended to use the brew package manager to install the packages for USB hardware access. (Hardware support for Bluetooth will automatically be installed with pfxbrick setup.py install script which installs the bleak package with its dependency to pyobjc-framework-CoreBluetooth).

$ brew install hidapi

Note: for python scripts that use Bluetooth communication with the PFx Brick running under macOS 11.0 (Big Sur) or later, permission must be granted to your terminal shell to use Bluetooth. Go to System Preferences -> Security & Privacy -> Privacy, select Bluetooth from the list, click the (+) button and select your terminal shell, e.g. Terminal.app or iTerm.app, etc.

Installation with pip

The pfxbrick package can be installed with pip:

$ pip install pfxbrick

Install from source

Install directly from the source code with the setup.py script:

$ git clone https://github.com/fx-bricks/pfx-brick-py.git
$ cd pfx-brick-py
$ python setup.py install

Conda Virtual Environment

You can also use the package in a standalone conda virtual environment. To create a conda environment named pfxtest:

$ git clone https://github.com/fx-bricks/pfx-brick-py.git
$ cd pfx-brick-py
$ conda env create -f environment.yml
$ conda activate pfxtest
$ pip install -r requirements.txt
$ python setup.py install

Basic Usage

After installation, the package can imported:

$ python
>>> import pfxbrick
>>> pfxbrick.__version__

An example of the package can be seen below

import time
from pfxbrick import *

# Open a PFx Brick session instance
brick = PFxBrick()
brick.open()

# Get the PFx Brick configuration settings
brick.get_config()
brick.print_config()

# Get the user defined name of the PFx Brick
brick.get_name()
print(brick.name)

# Change the user defined name
brick.set_name('My Cool Brick')

# Turn on some lights for 5 sec
brick.light_on([1, 2, 7, 8])
time.sleep(5)
brick.light_off([1, 2, 7, 8])

# Set motor channel A to 50% speed gradually
for speed in range(50):
    brick.set_motor_speed([1], speed)
    time.sleep(0.1)
brick.stop_motor([1])

# Play an audio file with indefinite repeat
brick.repeat_audio_file("LongBeep1")
time.sleep(5)
brick.stop_audio_file("LongBeep1")

# End the session
brick.close()

Utility Scripts

The pfxbrick package will install some useful command line script applications in your python environment's path. These include:

  • pfxcat - dumps the raw contents of a file on the PFx Brick to your console.
  • pfxdir - shows the directory listing of the PFx Brick file system
  • pfxget - gets a file from the PFx Brick
  • pfxput - copies a local host file on to the PFx Brick
  • pfxrm - delete a file from the PFx Brick
  • pfxrename - renames a file on the PFx Brick
  • pfxdump - dumps raw contents of PFx Brick flash memory
  • pfxinfo - discovers and shows information on all attached PFx Bricks
  • pfxmonitor - shows real time operational status of a PFx Brick
  • pfxtest - PFx Brick self test utility
  • pfxplay - plays an audio file on the PFx Brick
  • pfxrun - runs a script a file on the PFx Brick
  • pfxscan - scans for Bluetooth PFx Bricks and shows each brick's information

After installing the pfxbrick package, you should be able to use any of these utilities directly from the command line as follows:

$ pfxinfo
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ A216 PFx Brick 16 MB                                                     ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Serial Number         : AF716069                                         │
│ ICD Version           : 03.38                                            │
│ Firmware Version      : 01.50 build 0555                                 │
│ USB vendor ID         : 0x04D8                                           │
│ USB product ID        : 0xEF74                                           │
│ Status                : 0x00 Normal                                      │
│ Errors                : 0x00 None                                        │
│ Name                  : My PFx Brick                                     │
└──────────────────────────────────────────────────────────────────────────┘
$ pfxdir
 ID Name                       Size    Attr    User1    User2    CRC32
  0 sin150Hz.wav              132.3 kB 0000 000204CE 0000002C A712A54E
  1 pink6dB.wav               132.3 kB 0000 000204CE 0000002C E6AACE61
2 files, 270.3 kB used, 16490.5 kB remaining

To find out more information on how to use each utility, use without arguments or with the -h argument.

$ pfxinfo -h
usage: pfxinfo [-h] [-c]

Show information for all attached PFx Bricks

optional arguments:
-h, --help    show this help message and exit
-c, --config  Show configuration details

Documentation

If you want to learn more about PFx Brick, check out our website.