Treehopper C# API
TreehopperUsb Class Reference

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

Inheritance diagram for TreehopperUsb:

Main components

ObservableCollection< PinPins [get]
 This list contains all the pins that belong to the board. More...
 
HardwareSpi Spi [get]
 SPI module More...
 
HardwareI2C I2c [get]
 I2C module More...
 
HardwareUart Uart [get]
 Hardware UART supporting RS-232 and OneWire-style communication. More...
 
HardwarePwm Pwm1 [get]
 Hardware PWM #1 More...
 
HardwarePwm Pwm2 [get]
 Hardware PWM #2 More...
 
HardwarePwm Pwm3 [get]
 Hardware PWM #3 More...
 
bool Led [get, set]
 Gets or sets the LED state More...
 
bool IsConnected [get, set]
 Get whether or not the board is connected More...
 
Pin this[int index] [get]
 Shorthand pin accessor property More...
 
PropertyChangedEventHandler PropertyChanged
 Occurs whenever a property changes. More...
 
async Task< bool > ConnectAsync ()
 Open the TreehopperBoard. More...
 
void Disconnect ()
 Close device More...
 

Board identity & firmware management

string Name [get]
 The name of the board More...
 
string SerialNumber [get]
 The unique serial identification string for the board. More...
 
string VersionString [get]
 Get a string representation of the firmware version number More...
 
int Version [get]
 Get the firmware version number More...
 
Task UpdateDeviceNameAsync (string deviceName)
 Update the name of the device. More...
 
Task UpdateSerialNumberAsync (string serialNumber)
 Update the serial number on the device. More...
 
void Reboot ()
 Reboots the board. More...
 
void RebootIntoBootloader ()
 Reboot the board into bootloader mode More...
 

Other components

static Settings Settings [get]
 Controls the global settings used by all Treehopper libraries for this session More...
 
ParallelInterface ParallelInterface [get]
 8080-style parallel interface More...
 
HardwarePwmManager HardwarePwmManager [get]
 Hardware PWM manager More...
 
int NumberOfPins = 20 [get]
 Get the number of pins of this board More...
 
IConnection Connection [get]
 Get the Connection used by this board More...
 
PinValuesUpdatedHandler OnPinValuesUpdated
 Event fires whenever a new pin data report comes in (i.e., when all input pins are updated) More...
 
Task AwaitPinUpdateAsync ()
 Awaits a pin update report from the board More...
 
void Reinitialize ()
 Reinitialize the board, setting all pins as high-impedance More...
 
void Dispose ()
 Close device connection and free memory. More...
 
int CompareTo (object obj)
 Compares another Treehopper to this one. More...
 
bool Equals (TreehopperUsb x, TreehopperUsb y)
 Determines if two Treehopper boards are the same More...
 
int GetHashCode (TreehopperUsb board)
 Hash, obtained from the device path. More...
 
bool Equals (TreehopperUsb other)
 Determines if another Treehopper board is the same as this one More...
 
override string ToString ()
 A human-readable string describing this instance. More...
 
override int GetHashCode ()
 Get the hash code of this board More...
 
override bool Equals (object obj)
 Determines if another object is the same as this Treehopper board 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.

Getting a board reference

To obtain a reference to the board, use the ConnectionService.Instance.GetFirstDeviceAsync() method:

// Get a reference to the connected board, or
// hang out on this line until a board is connected.
var board = await ConnectionService.Instance.GetFirstDeviceAsync();
// now do something with the newly-attached board

Or the ConnectionService.Instance.Boards ObservableCollection, which fires events useful for hot-plug reactions:

ConnectionService.Instance.Boards.CollectionChanged += async (o, e) =>
{
var board = (TreehopperUsb)e.NewItems[0];
// do something with the new board
}
Warning
While you're free to create TreehopperUsb variables that reference boards, never call TreehopperUsb's constructor yourself.

Connect to the board

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

var board = await ConnectionService.Instance.GetFirstDeviceAsync();
await board.ConnectAsync();
Note
Once a board is connected, other applications won't be able to use it. When your app is running in Windows or macOS, you can investigate the Name or SerialNumber properties before calling ConnectAsync(). This will let you determine which board to connect to if you have multiple boards used by multiple applications.

Next steps

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

Member Function Documentation

◆ ConnectAsync()

async Task<bool> ConnectAsync ( )

Open the TreehopperBoard.

The board must be opened before any other methods are called.

◆ Disconnect()

void Disconnect ( )

Close device

This will reset all pins to inputs and then disconnect from the device. It will not free memory associated with this device.

◆ UpdateDeviceNameAsync()

Task UpdateDeviceNameAsync ( string  deviceName)

Update the name of the device.

Parameters
deviceNameA 60-character (or fewer) string containing the new name of the device.

While the name is immediately written to the device and the Name property is updated immediately, the changes will not take effect to other applications until the device is reset. This can be done by calling Reboot()

◆ UpdateSerialNumberAsync()

Task UpdateSerialNumberAsync ( string  serialNumber)

Update the serial number on the device.

Parameters
serialNumberA 60-character (or fewer) string containing the new serial number

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()

◆ Reboot()

void Reboot ( )

Reboots the board.

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

◆ RebootIntoBootloader()

void RebootIntoBootloader ( )

Reboot the board into bootloader mode

◆ AwaitPinUpdateAsync()

Task AwaitPinUpdateAsync ( )

Awaits a pin update report from the board

Returns
An awaitable Task that completes when a new pin update report has been received.

◆ Reinitialize()

void Reinitialize ( )

Reinitialize the board, setting all pins as high-impedance

◆ Dispose()

void Dispose ( )

Close device connection and free memory.

◆ CompareTo()

int CompareTo ( object  obj)

Compares another Treehopper to this one.

Parameters
objThe other Treehopper
Returns

◆ Equals() [1/3]

bool Equals ( TreehopperUsb  x,
TreehopperUsb  y 
)

Determines if two Treehopper boards are the same

Parameters
xThe first board
yThe second board
Returns

◆ GetHashCode() [1/2]

int GetHashCode ( TreehopperUsb  board)

Hash, obtained from the device path.

Parameters
boardthe Treehopper board to calculate the hash for
Returns
The hash code for the board

Object.GetHashCode

◆ Equals() [2/3]

bool Equals ( TreehopperUsb  other)

Determines if another Treehopper board is the same as this one

Parameters
otherThe other Treehopper board to compare
Returns
True if the two boards are the same

◆ ToString()

override string ToString ( )

A human-readable string describing this instance.

This is formed from the name and serial number of the device.

Returns
The string

◆ GetHashCode() [2/2]

override int GetHashCode ( )

Get the hash code of this board

Returns
The hash code

◆ Equals() [3/3]

override bool Equals ( object  obj)

Determines if another object is the same as this Treehopper board

Parameters
objThe other Treehopper board to compare
Returns
True if the two boards are the same

Property Documentation

◆ Pins

ObservableCollection<Pin> Pins
get

This list contains all the pins that belong to the board.

◆ Spi

SPI module

◆ I2c

HardwareI2C I2c
get

I2C module

◆ Uart

Hardware UART supporting RS-232 and OneWire-style communication.

◆ Pwm1

HardwarePwm Pwm1
get

Hardware PWM #1

◆ Pwm2

HardwarePwm Pwm2
get

Hardware PWM #2

◆ Pwm3

HardwarePwm Pwm3
get

Hardware PWM #3

◆ Led

bool Led
getset

Gets or sets the LED state

◆ IsConnected

bool IsConnected
getset

Get whether or not the board is connected

◆ this[int index]

Pin this[int index]
get

Shorthand pin accessor property

Parameters
indexThe pin index to access
Returns
the Pin with the provided index

This property gives you shorthand access to pins. Instead of typing

newValue = board.Pins[5].DigitalValue;

using this accessor, you can type:

newValue = board[5].DigitalValue;

We provide both accessors, because while this one is briefer, it is less intuitive.

◆ Name

string Name
get

The name of the board

This property is set by a USB string descriptor read when the device is attached to the computer. It is available even if the device is not open.

You can set this string to whichever value you want by using the Treehopper Firmware Manager software. Once set, this property will persist, even if the device is removed from power or connected to a different computer.

This property is useful for identifying a particular Treehopper board when you have multiple boards connected to your computer, or you don't want a program communicating with the incorrect board.

◆ SerialNumber

string SerialNumber
get

The unique serial identification string for the board.


This property is available even if the device is not opened (though on Linux, the device will be implicitly opened and then closed to read this property).

Consequently, you can use this property, or the TreehopperUsb.Name property, to screen the devices your application attempts to connect to, without interfereing with other applications.

You can write a new serial number to the board by calling UpdateSerialNumber().

Warning
Despite the name of this property, most device serial numbers will consist of both numbers and alphabetical characters. Do not attempt to convert this to a numeric type.

◆ VersionString

string VersionString
get

Get a string representation of the firmware version number

◆ Version

int Version
get

Get the firmware version number

◆ Settings

Settings Settings
staticget

Controls the global settings used by all Treehopper libraries for this session

This is a static property. Access it through the TreehopperUsb class. As an example:

TreehopperUsb.Settings.PropertyWritesReturnImmediately = true;

◆ ParallelInterface

8080-style parallel interface

◆ HardwarePwmManager

Hardware PWM manager

◆ NumberOfPins

int NumberOfPins = 20
get

Get the number of pins of this board

◆ Connection

IConnection Connection
get

Get the Connection used by this board

Event Documentation

◆ PropertyChanged

PropertyChangedEventHandler PropertyChanged

Occurs whenever a property changes.

◆ OnPinValuesUpdated

PinValuesUpdatedHandler OnPinValuesUpdated

Event fires whenever a new pin data report comes in (i.e., when all input pins are updated)

While each pin can be configured to fire events when it changes, if you're dealing with a large number of input pins, you may get better performance by subscribing to this event alone.


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