Treehopper Python API
TreehopperUsb Class Reference

The core class for communicating with Treehopper USB boards. More...

Main components


Connect to the board.

Calling this method will connect to the board and start the pin listener update thread. Repeated calls to this method are ignored.

Warning
You must connect to the board before performing any operations (other than querying the name() or serial_number() of the board).
Returns
None
 pins = property
 Gets a list of pins that belong to this board. More...
 
 spi = property
 Gets the SPI peripheral that belongs to this board. More...
 
 i2c = property
 Gets the I2C peripheral that belongs to this board. More...
 
 uart = property
 Gets the UART peripheral that belongs to this board. More...
 
 pwm1 = property
 Gets the PWM1 module that belongs to this board. More...
 
 pwm2 = property
 Gets the PWM2 module that belongs to this board. More...
 
 pwm3 = property
 Gets the PWM3 module that belongs to this board. More...
 
 led = property
 Gets or sets the state of the LED. More...
 
 connected = property
 Gets whether the board is connected. More...
 
def connect ()
 
def disconnect ()
 Disconnect from the board. More...
 

Board identity & firmware management

Gets the serial number of the board.

This property is available to read even without connecting to the board. If you wish to change the serial

number,

use update_serial_number().

Note
While you do not need to connect() to the board before querying its serial number, you will not be able to retrieve the serial number of a board to which another application is connected.

Treehopper's Python API doesn't currently support directly querying the OS for device info, so while executing find_boards(), the API implicitly connects to the board, queries the string descriptor, and disconnects. Since concurrent operation isn't supported, this operation will error if the board is already open.

Returns
str The serial number.
 serial_number = property
 
 name = property
 Gets the device name of the board. More...
 
def update_serial_number (serial_number)
 Update the serial number on the device. More...
 
def update_device_name (device_name)
 Update the device name on the device. More...
 

Other components

Reboots the board.

Calling this method will automatically call the disconnect() method, and no further communication will be possible until the board is reopened.

Returns
None
 hardware_pwm_manager = property
 Gets the hardware PWM manager for this board. More...
 
def reboot ()
 
def reboot_into_bootloader ()
 Reboots the board into bootloader mode. More...
 
def await_pin_update ()
 Returns when the board has received a new pin update. More...
 

Detailed Description

The core class for communicating with Treehopper USB boards.

treehopper-hardware.svg
Treehopper pinout

Core hardware

Treehopper is a USB 2.0 Full Speed device with 20 Pins — each of which can be used as an analog input, digital input, digital output, or soft-PWM output. Many of these pins also have dedicated peripheral functions for SPI, I2C,UART, and PWM.

You'll access all the pins, peripherals, and board functions through this class, which will automatically create all peripheral instances for you.

Examples

>>> board = find_boards()[0] # Get the first board.
>>> board.connect() # Be sure to connect before doing anything else.
>>> board.led = True # Turn on the board's LED.
>>> imu = Mpu9250.probe(board.i2c) # Connect an MPU9250 9-DoF IMU to the I2C interface on the board.
>>> print(imu.accelerometer) # Print the acceleration reading from the sensor.
'[0.95, 0.01, 0.03]'
>>> board.disconnect() # Disconnect from the board when finished.

Getting a board reference

To obtain a reference to the board, use the find_boards() method:

>>> board = find_boards()[0] # Get the first board.
Warning
While you're free to create TreehopperUsb variables that reference boards, do not attempt to create a TreehopperUsb instance manually; always obtain references to boards from the find_boards() function.

Connect to the board

Before you use the board, you must explicitly connect to it by calling the connect() method

>>> board = find_boards()[0] # Get the first board.
>>> board.connect() # Be sure to connect before

doing anything else.

Note
Once a board is connected, other applications won't be able to use it.

On-board LED

The only peripheral directly exposed by this class is the led property, which will control the LED's state once the board is connected. This demo will blink the LED until the board is unplugged:

>>> board = find_boards()[0]
>>> board.connect()
>>> while board.connected:
>>> board.led = not board.led # toggle the LED
>>> sleep(0.1)

Next steps

To learn about accessing different Treehopper peripherals, visit the doc links to the relevant classes:

Member Function Documentation

◆ await_pin_update()

def await_pin_update ( )

Returns when the board has received a new pin update.

◆ connect()

def connect ( )

◆ disconnect()

def disconnect ( )

Disconnect from the board.

This method disconnects from the board and stops the pin listener update thread. Repeated calls to this method are ignored.

◆ reboot()

def reboot ( )

◆ reboot_into_bootloader()

def reboot_into_bootloader ( )

Reboots the board into bootloader mode.

Calling this method will automatically call the disconnect() method, and no further communication will be possible. You can load new firmware onto the board once in bootloader mode, or if you wish to return to normal operation, replug the board to reset it.

Returns
None

◆ update_device_name()

def update_device_name (   device_name)

Update the device name on the device.

While the new serial number is immediately available from the SerialNumber property, the changes will not take effect in other applications until the device is reset. This can be done by calling reboot().

Note
Microsoft Windows caches the device name in the registry when it is first attached to the board, so even if you change the device name with this function, you won't see the new device name — even after replugging the board. To force Windows to update theregistry with the new name, make sure to change the serial number, too (see update_serial_number()).
Parameters
device_namea 60-character (or fewer) string containing the new device name (str).
Returns
None

Examples

>>> board = find_boards()[0] # Get the first board.
>>> board.connect() # Be sure to connect before doing anything else.
>>> board.update_device_name("Acme Widget") # Update the device name.
>>> board.update_serial_number("a3bY392") # Change the serial number to force Windows refresh.
>>> board.reboot() # Reboot the board so the OS picks up the changes.

◆ update_serial_number()

def update_serial_number (   serial_number)

Update the serial number on the device.

While the new serial number is immediately available from the SerialNumber property, the changes will not take effect in other applications until the device is reset. This can be done by calling reboot().

Parameters
serial_numbera 60-character (or fewer) string containing the new serial number (str).
Returns
None

Examples

>>> board = find_boards()[0] # Get the first board.
>>> board.connect() # Be sure to connect before doing anything else.
>>> board.update_serial_number("a3bY392") # Change the serial number.
>>> board.reboot() # Reboot the board so the OS picks up the changes.

Property Documentation

◆ connected

connected = property
static

Gets whether the board is connected.

Returns
bool Whether the board is connected
Note
The board will automatically disconnect if an unrecoverable error is encountered (which includes a disconnect), so this is a useful property to consult to determine if a board is physically attached to a
Returns
computer.

◆ hardware_pwm_manager

hardware_pwm_manager = property
static

Gets the hardware PWM manager for this board.

◆ i2c

i2c = property
static

Gets the I2C peripheral that belongs to this board.

◆ led

led = property
static

Gets or sets the state of the LED.

Examples

>>> while board.connected:
>>> board.led = not board.led # toggle the LED
>>> sleep(0.1)
Returns
bool The current state of the LED.

◆ name

name = property
static

Gets the device name of the board.

This property is available to read even without connecting to the board. If you wish to change the device name, use update_device_name().

Note
While you do not need to connect() to the board before querying its device name, you will not be able to retrieve the device name of a board to which another application is connected.

Treehopper's Python API doesn't currently support directly querying the OS for device info, so while executing find_boards(), the API implicitly connects to the board, queries the string descriptor, and disconnects. Since concurrent operation isn't supported, this operation will error if the board is already open.

Returns
str The device name.

◆ pins

pins = property
static

Gets a list of pins that belong to this board.

◆ pwm1

pwm1 = property
static

Gets the PWM1 module that belongs to this board.

◆ pwm2

pwm2 = property
static

Gets the PWM2 module that belongs to this board.

◆ pwm3

pwm3 = property
static

Gets the PWM3 module that belongs to this board.

◆ serial_number

serial_number = property
static

◆ spi

spi = property
static

Gets the SPI peripheral that belongs to this board.

◆ uart

uart = property
static

Gets the UART peripheral that belongs to this board.


The documentation for this class was generated from the following file: